The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { PassThrough } from "node:stream"; |
| 2 | import { fileURLToPath } from "node:url"; |
| 3 | import { expect, it } from "@alchemy.run/test"; |
| 4 | import { Interaction, layerNonInteractive, select, success } from "alchemy/Interaction"; |
| 5 | import { nodeLoaderArgs } from "alchemy/Util/Node"; |
| 6 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { nodePath, nodeSupportsDevMode } from "./nodeProbe.ts"; |
| 11 | |
| 12 | class CaptureStream extends PassThrough { |
| 13 | readonly columns = 80; |
| 14 | readonly rows = 24; |
| 15 | readonly isTTY = false; |
| 16 | output = ""; |
| 17 | |
| 18 | constructor() { |
| 19 | super(); |
| 20 | this.on("data", (chunk) => { |
| 21 | this.output += chunk.toString(); |
| 22 | }); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | const nonInteractive = (stdout: CaptureStream) => |
| 27 | layerNonInteractive({ stdout: stdout as unknown as NodeJS.WriteStream }); |
| 28 | |
| 29 | it( |
| 30 | "prints plain status lines and fails prompts typed", |
| 31 | () => { |
| 32 | const stdout = new CaptureStream(); |
| 33 | return Effect.gen(function* () { |
| 34 | const interaction = yield* Interaction; |
| 35 | yield* interaction.output.info({ message: "plain info", detail: "extra" }); |
| 36 | yield* success("plain success"); |
| 37 | const failure = yield* Effect.flip(interaction.prompt.confirm({ message: "proceed?" })); |
| 38 | expect(failure._tag).toBe("NonInteractiveTerminal"); |
| 39 | const selection = yield* Effect.flip(select({ message: "pick", options: [] })); |
| 40 | expect(selection._tag).toBe("NonInteractiveTerminal"); |
| 41 | const lines = stdout.output.split("\n"); |
| 42 | expect(lines[0]).toContain("plain info"); |
| 43 | expect(lines[0]).toContain("· extra"); |
| 44 | expect(lines[1]).toContain("plain success"); |
| 45 | }).pipe(Effect.provide(nonInteractive(stdout))); |
| 46 | }, |
| 47 | { tags: ["unit", "local"] }, |
| 48 | ); |
| 49 | |
| 50 | it( |
| 51 | "task prints a start line and settles with a status line", |
| 52 | () => { |
| 53 | const stdout = new CaptureStream(); |
| 54 | return Effect.gen(function* () { |
| 55 | const interaction = yield* Interaction; |
| 56 | yield* interaction.task({ label: "working", detail: "step" }, Effect.void); |
| 57 | const failed = yield* Effect.result( |
| 58 | interaction.task({ label: "breaking" }, Effect.fail("boom")), |
| 59 | ); |
| 60 | expect(failed._tag).toBe("Failure"); |
| 61 | const lines = stdout.output.trimEnd().split("\n"); |
| 62 | expect(lines[0]).toContain("working"); |
| 63 | expect(lines[0]).toContain("· step"); |
| 64 | expect(lines[1]).toContain("working"); |
| 65 | expect(lines[2]).toContain("breaking"); |
| 66 | expect(lines[3]).toContain("breaking"); |
| 67 | }).pipe(Effect.provide(nonInteractive(stdout))); |
| 68 | }, |
| 69 | { tags: ["unit", "local"] }, |
| 70 | ); |
| 71 | |
| 72 | // The regression this pins: spawned dev children carry NO interaction |
| 73 | // services at all, so the vite child runner's entire module graph must load |
| 74 | // under node (the same dev-mode loader `ViteChild.ts` spawns it with) and |
| 75 | // proceed to config resolution — the original bug was an eager terminal-renderer import |
| 76 | // killing every `runtime: "node"` dev child at startup. |
| 77 | it.live.skipIf(!nodeSupportsDevMode)( |
| 78 | "the vite child runner's module graph loads under node", |
| 79 | () => |
| 80 | Effect.gen(function* () { |
| 81 | const runner = fileURLToPath( |
| 82 | new URL("../../cloudflare/src/Workers/ViteChildRunner.ts", import.meta.url), |
| 83 | ); |
| 84 | const handle = yield* ChildProcess.make(nodePath!, [...nodeLoaderArgs(runner), runner], { |
| 85 | cwd: fileURLToPath(new URL("..", import.meta.url)), |
| 86 | env: { NO_COLOR: "1" }, |
| 87 | extendEnv: true, |
| 88 | stdin: "ignore", |
| 89 | stdout: "pipe", |
| 90 | stderr: "pipe", |
| 91 | killSignal: "SIGKILL", |
| 92 | }); |
| 93 | const [stdout, stderr] = yield* Effect.all( |
| 94 | [ |
| 95 | handle.stdout.pipe( |
| 96 | Stream.decodeText, |
| 97 | Stream.runCollect, |
| 98 | Effect.map((chunks) => chunks.join("")), |
| 99 | ), |
| 100 | handle.stderr.pipe( |
| 101 | Stream.decodeText, |
| 102 | Stream.runCollect, |
| 103 | Effect.map((chunks) => chunks.join("")), |
| 104 | ), |
| 105 | // Exit code is irrelevant — with no stdin config the runner is |
| 106 | // EXPECTED to fail; what matters is WHERE it fails. |
| 107 | handle.exitCode, |
| 108 | ], |
| 109 | { concurrency: 3 }, |
| 110 | ); |
| 111 | const combined = `${stdout}\n${stderr}`; |
| 112 | expect(combined).not.toContain("Unknown file extension"); |
| 113 | expect(combined).not.toContain("ERR_MODULE_NOT_FOUND"); |
| 114 | // Reaching environment/config resolution proves the whole module |
| 115 | // graph (auth providers, credentials, vite loader) parsed under node. |
| 116 | expect(combined).toContain("ALCHEMY_RPC_SERVER_ENVIRONMENT"); |
| 117 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)), |
| 118 | { tags: ["unit", "local"], timeout: 60_000 }, |
| 119 | ); |