The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import { describe, expect, it } from "@alchemy.run/test"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { PlatformServices } from "@/Util/PlatformServices.ts"; |
| 8 | import { nodePath, nodeSupportsDevMode } from "../nodeProbe.ts"; |
| 9 | |
| 10 | const CLI = fileURLToPath(new URL("../../bin/cli.js", import.meta.url)); |
| 11 | const DESTROY_FIXTURE = fileURLToPath( |
| 12 | new URL("./fixtures/destroy-skips-program.ts", import.meta.url), |
| 13 | ); |
| 14 | |
| 15 | /** |
| 16 | * Run the CLI with no TTY on any stdio and return its exit code. Each |
| 17 | * invocation is a real `bun bin/cli.js` child with `ALCHEMY_HOME` pointed at |
| 18 | * a throwaway directory, so these pin the exit-code contract scripts and |
| 19 | * agents rely on — 0 only when the command completed — without touching the |
| 20 | * real `~/.alchemy`. |
| 21 | */ |
| 22 | const exitCodeOf = ( |
| 23 | args: ReadonlyArray<string>, |
| 24 | env: Record<string, string> = {}, |
| 25 | runtime = "bun", |
| 26 | ) => |
| 27 | Effect.gen(function* () { |
| 28 | const fs = yield* FileSystem.FileSystem; |
| 29 | const home = yield* fs.makeTempDirectoryScoped({ |
| 30 | prefix: "alchemy-exit-codes-", |
| 31 | }); |
| 32 | const handle = yield* ChildProcess.make(runtime, [CLI, ...args], { |
| 33 | env: { ALCHEMY_HOME: home, ...env }, |
| 34 | extendEnv: true, |
| 35 | stdin: "ignore", |
| 36 | stdout: "ignore", |
| 37 | stderr: "ignore", |
| 38 | killSignal: "SIGTERM", |
| 39 | forceKillAfter: "1 second", |
| 40 | }); |
| 41 | return yield* handle.exitCode; |
| 42 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)); |
| 43 | |
| 44 | /** Like {@link exitCodeOf}, but from an empty project directory with output captured. */ |
| 45 | const runInEmptyProject = (args: ReadonlyArray<string>, runtime = "bun") => |
| 46 | Effect.gen(function* () { |
| 47 | const fs = yield* FileSystem.FileSystem; |
| 48 | const home = yield* fs.makeTempDirectoryScoped({ |
| 49 | prefix: "alchemy-exit-codes-", |
| 50 | }); |
| 51 | const project = yield* fs.makeTempDirectoryScoped({ |
| 52 | prefix: "alchemy-empty-project-", |
| 53 | }); |
| 54 | const handle = yield* ChildProcess.make(runtime, [CLI, ...args], { |
| 55 | cwd: project, |
| 56 | env: { ALCHEMY_HOME: home }, |
| 57 | extendEnv: true, |
| 58 | stdin: "ignore", |
| 59 | stdout: "pipe", |
| 60 | stderr: "pipe", |
| 61 | killSignal: "SIGTERM", |
| 62 | forceKillAfter: "1 second", |
| 63 | }); |
| 64 | const [stdout, stderr, exitCode] = yield* Effect.all( |
| 65 | [ |
| 66 | handle.stdout.pipe(Stream.decodeText, Stream.mkString), |
| 67 | handle.stderr.pipe(Stream.decodeText, Stream.mkString), |
| 68 | handle.exitCode, |
| 69 | ], |
| 70 | { concurrency: 3 }, |
| 71 | ); |
| 72 | return { stdout, stderr, exitCode }; |
| 73 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)); |
| 74 | |
| 75 | describe("CLI exit codes", { tags: ["unit", "local"] }, () => { |
| 76 | it.live("dev without a stack entrypoint reports it and exits 1", () => |
| 77 | Effect.gen(function* () { |
| 78 | const { stderr, exitCode } = yield* runInEmptyProject(["dev"]); |
| 79 | expect(exitCode).toBe(1); |
| 80 | expect(stderr).toContain("Stack entrypoint 'alchemy.run.ts' does not exist"); |
| 81 | expect(stderr).not.toContain("PlatformError"); |
| 82 | expect(stderr).not.toContain("at Effect.fn"); |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | it.live.skipIf(!nodeSupportsDevMode)( |
| 87 | "dev without a stack entrypoint reports it and exits 1 under node", |
| 88 | () => |
| 89 | Effect.gen(function* () { |
| 90 | const { stderr, exitCode } = yield* runInEmptyProject(["dev"], nodePath!); |
| 91 | expect(exitCode).toBe(1); |
| 92 | expect(stderr).toContain("Stack entrypoint 'alchemy.run.ts' does not exist"); |
| 93 | expect(stderr).not.toContain("PlatformError"); |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | it.live("plan accepts --adopt like deploy --dry-run", () => |
| 98 | Effect.gen(function* () { |
| 99 | const { stderr, exitCode } = yield* runInEmptyProject(["plan", "--adopt"]); |
| 100 | expect(exitCode).toBe(1); |
| 101 | expect(stderr).not.toContain("Unrecognized flag"); |
| 102 | expect(stderr).toContain("Stack entrypoint 'alchemy.run.ts' does not exist"); |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | // #1976: destroying a stage created by `alchemy dev` must not evaluate the |
| 107 | // program in live mode, where declarations may call the real cloud. |
| 108 | it.live("destroy plans from state without evaluating the stack program", () => |
| 109 | Effect.gen(function* () { |
| 110 | const { stdout, stderr, exitCode } = yield* runInEmptyProject([ |
| 111 | "destroy", |
| 112 | "--config", |
| 113 | DESTROY_FIXTURE, |
| 114 | "--stage", |
| 115 | "cli_destroy", |
| 116 | "--yes", |
| 117 | ]); |
| 118 | expect(stdout + stderr).not.toContain("STACK_PROGRAM_EVALUATED"); |
| 119 | expect(exitCode).toBe(0); |
| 120 | }), |
| 121 | ); |
| 122 | |
| 123 | it.live("deploy evaluates the stack program", () => |
| 124 | Effect.gen(function* () { |
| 125 | const { stdout, stderr, exitCode } = yield* runInEmptyProject([ |
| 126 | "deploy", |
| 127 | "--config", |
| 128 | DESTROY_FIXTURE, |
| 129 | "--stage", |
| 130 | "cli_destroy", |
| 131 | "--yes", |
| 132 | ]); |
| 133 | expect(stdout + stderr).toContain("STACK_PROGRAM_EVALUATED"); |
| 134 | expect(exitCode).toBe(1); |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | it.live("bare `profile` without a terminal prints help and exits 1", () => |
| 139 | Effect.gen(function* () { |
| 140 | expect(yield* exitCodeOf(["profile"])).toBe(1); |
| 141 | }), |
| 142 | ); |
| 143 | |
| 144 | it.live("bare `state` without a terminal prints help and exits 1", () => |
| 145 | Effect.gen(function* () { |
| 146 | expect(yield* exitCodeOf(["state"])).toBe(1); |
| 147 | }), |
| 148 | ); |
| 149 | |
| 150 | it.live("--no-input is accepted and forces a plain, working run", () => |
| 151 | Effect.gen(function* () { |
| 152 | expect(yield* exitCodeOf(["--no-input", "profile", "list"])).toBe(0); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | it.live("--fast-distilled is accepted", () => |
| 157 | Effect.gen(function* () { |
| 158 | expect(yield* exitCodeOf(["--no-input", "--fast-distilled", "profile", "list"])).toBe(0); |
| 159 | }), |
| 160 | ); |
| 161 | |
| 162 | it.live("--help exits 0", () => |
| 163 | Effect.gen(function* () { |
| 164 | expect(yield* exitCodeOf(["--help"])).toBe(0); |
| 165 | }), |
| 166 | ); |
| 167 | |
| 168 | // Pins buildless node dev: the launcher must run the checkout's .ts/.tsx |
| 169 | // source under plain node (type stripping + the register-dev-mode hooks) |
| 170 | // — --help renders the TSX help view through the full terminal runtime. |
| 171 | // Stderr is captured: this is the one test in the file whose failure |
| 172 | // mode is environmental, so "expected 9 to be 0" alone is useless. |
| 173 | it.live.skipIf(!nodeSupportsDevMode)( |
| 174 | "--help exits 0 under node from source, no build required", |
| 175 | () => |
| 176 | Effect.gen(function* () { |
| 177 | const fs = yield* FileSystem.FileSystem; |
| 178 | const home = yield* fs.makeTempDirectoryScoped({ |
| 179 | prefix: "alchemy-exit-codes-", |
| 180 | }); |
| 181 | const handle = yield* ChildProcess.make(nodePath!, [CLI, "--help"], { |
| 182 | // The test runner is bun and marks every child's env as |
| 183 | // bun-invoked; blank the markers so the launcher takes its |
| 184 | // node path like a real `node bin/cli.js` invocation. |
| 185 | env: { |
| 186 | ALCHEMY_HOME: home, |
| 187 | npm_execpath: "", |
| 188 | npm_config_user_agent: "", |
| 189 | }, |
| 190 | extendEnv: true, |
| 191 | stdin: "ignore", |
| 192 | stdout: "ignore", |
| 193 | stderr: "pipe", |
| 194 | killSignal: "SIGTERM", |
| 195 | forceKillAfter: "1 second", |
| 196 | }); |
| 197 | const [stderr, exitCode] = yield* Effect.all( |
| 198 | [ |
| 199 | handle.stderr.pipe( |
| 200 | Stream.decodeText, |
| 201 | Stream.runCollect, |
| 202 | Effect.map((chunks) => chunks.join("")), |
| 203 | ), |
| 204 | handle.exitCode, |
| 205 | ], |
| 206 | { concurrency: 2 }, |
| 207 | ); |
| 208 | expect({ |
| 209 | exitCode, |
| 210 | stderr: exitCode === 0 ? "" : stderr, |
| 211 | }).toEqual({ exitCode: 0, stderr: "" }); |
| 212 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)), |
| 213 | ); |
| 214 | }); |