The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Drives one example through the real CLI lifecycle: `alchemy dev` until the |
| 3 | * run converges, then `alchemy deploy` (unless `--dev-only`), and always |
| 4 | * `alchemy destroy` afterwards. |
| 5 | * |
| 6 | * Usage: node scripts/test-example-cli.ts <example-directory> [--dev-only] |
| 7 | */ |
| 8 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 9 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 10 | import * as Cause from "effect/Cause"; |
| 11 | import { Argument, Command, Flag } from "effect/cli"; |
| 12 | import * as Config from "effect/Config"; |
| 13 | import * as Console from "effect/Console"; |
| 14 | import * as Data from "effect/Data"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import * as Exit from "effect/Exit"; |
| 17 | import * as FileSystem from "effect/FileSystem"; |
| 18 | import * as Path from "effect/Path"; |
| 19 | import { ChildProcess } from "effect/process"; |
| 20 | import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; |
| 21 | import * as Runtime from "effect/Runtime"; |
| 22 | import * as Stream from "effect/Stream"; |
| 23 | |
| 24 | /** A lifecycle step failed; `message` carries the CLI output. */ |
| 25 | class StepFailed extends Data.TaggedError("StepFailed")<{ readonly message: string }> { |
| 26 | readonly [Runtime.errorReported] = false; |
| 27 | } |
| 28 | |
| 29 | type StepName = "dev" | "deploy" | "destroy"; |
| 30 | |
| 31 | type CommandResult = { |
| 32 | readonly exitCode: number | null; |
| 33 | readonly output: string; |
| 34 | }; |
| 35 | |
| 36 | const stage = "cli-example-test"; |
| 37 | // The summary line the CLI prints once a run converges. In non-TTY mode every |
| 38 | // line carries a `[time] LEVEL (#fiber): ` prefix, so anchor on the text. |
| 39 | const DONE = /Done: \d+ succeeded/; |
| 40 | const timeoutMs = 4 * 60_000; |
| 41 | |
| 42 | const command = Command.make( |
| 43 | "test-example-cli", |
| 44 | { |
| 45 | example: Argument.String("example-directory").pipe( |
| 46 | Argument.withDescription("Example directory, relative to the repository root"), |
| 47 | ), |
| 48 | devOnly: Flag.Boolean("dev-only").pipe( |
| 49 | Flag.withDefault(false), |
| 50 | Flag.withDescription("Skip `alchemy deploy`; run only dev and destroy"), |
| 51 | ), |
| 52 | }, |
| 53 | Effect.fn(function* ({ example, devOnly }) { |
| 54 | const fs = yield* FileSystem.FileSystem; |
| 55 | const path = yield* Path.Path; |
| 56 | const spawner = yield* ChildProcessSpawner; |
| 57 | |
| 58 | const repositoryRoot = path.resolve(import.meta.dirname, ".."); |
| 59 | const exampleRoot = path.resolve(repositoryRoot, example); |
| 60 | // The real launcher, not `bin/alchemy.js`: it pins bun's tsconfig to |
| 61 | // alchemy's own, so the CLI's .tsx files are not transpiled with the |
| 62 | // example's JSX settings (solid-js examples otherwise crash the CLI). |
| 63 | const alchemyBin = path.join(repositoryRoot, "packages", "alchemy", "bin", "cli.js"); |
| 64 | const debug = (yield* Config.String("DEBUG").pipe(Config.withDefault(""))) !== ""; |
| 65 | |
| 66 | // With a profile (`test:examples --profile testing`) the CLI must see the |
| 67 | // real ALCHEMY_HOME (where the profile's credentials live) and must NOT run |
| 68 | // as CI, which makes auth resolution skip profiles for env credentials. |
| 69 | // Without a profile, run against an empty home as CI so the CLI can only |
| 70 | // authenticate from env vars, the way a fresh CI runner would. |
| 71 | const profile = yield* Config.String("ALCHEMY_PROFILE").pipe(Config.option); |
| 72 | const alchemyHome = |
| 73 | profile._tag === "None" |
| 74 | ? yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-example-cli-" }) |
| 75 | : undefined; |
| 76 | const childEnv = { |
| 77 | ...(alchemyHome === undefined |
| 78 | ? {} |
| 79 | : { ALCHEMY_HOME: alchemyHome, AWS_PROFILE: undefined, CI: "true" }), |
| 80 | NO_COLOR: "1", |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * Spawns `alchemy <name>` in its own process group and collects its |
| 85 | * output. `onOutput` sees the output so far and may stop the CLI early. |
| 86 | * Interrupting (Ctrl-C) sends SIGINT to the group and waits for it. |
| 87 | */ |
| 88 | const runCli = ( |
| 89 | name: StepName, |
| 90 | /** Returns a signal to stop the CLI's process group early. */ |
| 91 | onOutput: (output: string) => "SIGINT" | "SIGKILL" | undefined, |
| 92 | ) => |
| 93 | Effect.scoped( |
| 94 | Effect.gen(function* () { |
| 95 | const child = yield* spawner.spawn( |
| 96 | ChildProcess.make( |
| 97 | "bun", |
| 98 | [ |
| 99 | alchemyBin, |
| 100 | name, |
| 101 | "--stage", |
| 102 | stage, |
| 103 | "--no-input", |
| 104 | ...(name === "dev" ? [] : ["--yes"]), |
| 105 | ], |
| 106 | { |
| 107 | cwd: exampleRoot, |
| 108 | detached: true, |
| 109 | env: childEnv, |
| 110 | extendEnv: true, |
| 111 | stdin: "ignore", |
| 112 | stdout: "pipe", |
| 113 | stderr: "pipe", |
| 114 | killSignal: "SIGINT", |
| 115 | forceKillAfter: "15 seconds", |
| 116 | }, |
| 117 | ), |
| 118 | ); |
| 119 | let output = ""; |
| 120 | const kill = (signal: "SIGINT" | "SIGKILL") => |
| 121 | child |
| 122 | .kill( |
| 123 | signal === "SIGINT" |
| 124 | ? { killSignal: "SIGINT", forceKillAfter: "15 seconds" } |
| 125 | : { killSignal: "SIGKILL" }, |
| 126 | ) |
| 127 | .pipe(Effect.ignore, Effect.forkScoped); |
| 128 | yield* child.all.pipe( |
| 129 | Stream.decodeText, |
| 130 | Stream.runForEach( |
| 131 | Effect.fnUntraced(function* (text) { |
| 132 | output += text; |
| 133 | if (debug) yield* Effect.sync(() => process.stderr.write(text)); |
| 134 | const signal = onOutput(output); |
| 135 | if (signal !== undefined) yield* kill(signal); |
| 136 | }), |
| 137 | ), |
| 138 | Effect.ignore, |
| 139 | Effect.forkScoped, |
| 140 | ); |
| 141 | const exitCode = yield* child.exitCode.pipe( |
| 142 | Effect.map((code): number | null => code), |
| 143 | // Terminated by a signal. |
| 144 | Effect.orElseSucceed(() => null), |
| 145 | Effect.timeoutOption(timeoutMs), |
| 146 | ); |
| 147 | if (exitCode._tag === "Some") return { exitCode: exitCode.value, output }; |
| 148 | output += `\nTimed out after ${timeoutMs / 1000}s waiting for alchemy ${name}${name === "dev" ? " to become ready" : ""}.`; |
| 149 | yield* kill("SIGKILL"); |
| 150 | const code = yield* child.exitCode.pipe(Effect.orElseSucceed(() => null)); |
| 151 | return { exitCode: code, output } satisfies CommandResult; |
| 152 | }), |
| 153 | ); |
| 154 | |
| 155 | const run = (name: "deploy" | "destroy") => runCli(name, () => undefined); |
| 156 | |
| 157 | const runDev = Effect.suspend(() => { |
| 158 | let ready = false; |
| 159 | return runCli("dev", (output) => { |
| 160 | if (ready) return undefined; |
| 161 | if (DONE.test(output) && /https?:\/\//.test(output)) { |
| 162 | ready = true; |
| 163 | return "SIGINT"; |
| 164 | } |
| 165 | // A failed run leaves `dev` waiting for a file change that never |
| 166 | // comes; fail now instead of at the timeout. |
| 167 | if (output.includes("alchemy dev: run failed")) return "SIGKILL"; |
| 168 | return undefined; |
| 169 | }).pipe( |
| 170 | Effect.map((result): CommandResult => |
| 171 | ready ? { exitCode: 0, output: result.output } : result, |
| 172 | ), |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | const assertSuccess = (name: StepName, result: CommandResult) => |
| 177 | result.exitCode === 0 |
| 178 | ? Effect.void |
| 179 | : Effect.fail( |
| 180 | new StepFailed({ |
| 181 | message: `${example}: alchemy ${name} failed (exit ${result.exitCode ?? "signal"})\n${result.output}`, |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | const assertOutput = (name: StepName, result: CommandResult, expected: readonly RegExp[]) => { |
| 186 | const missing = expected.find((pattern) => !pattern.test(result.output)); |
| 187 | return missing === undefined |
| 188 | ? Effect.void |
| 189 | : Effect.fail( |
| 190 | new StepFailed({ |
| 191 | message: `${example}: alchemy ${name} output did not match ${missing}\n${result.output}`, |
| 192 | }), |
| 193 | ); |
| 194 | }; |
| 195 | |
| 196 | // Not a finalizer: on Ctrl-C the CLI shuts down and this script exits |
| 197 | // without starting destroy, like the interrupted run it follows. |
| 198 | const primary = yield* Effect.exit( |
| 199 | Effect.gen(function* () { |
| 200 | const dev = yield* runDev; |
| 201 | yield* assertSuccess("dev", dev); |
| 202 | yield* assertOutput("dev", dev, [new RegExp(`Dev · ${stage}`), DONE, /https?:\/\//]); |
| 203 | |
| 204 | if (!devOnly) { |
| 205 | const deployed = yield* run("deploy"); |
| 206 | yield* assertSuccess("deploy", deployed); |
| 207 | yield* assertOutput("deploy", deployed, [ |
| 208 | new RegExp(`Deploy · ${stage}`), |
| 209 | DONE, |
| 210 | /https?:\/\//, |
| 211 | ]); |
| 212 | } |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | const destroyed = yield* Effect.exit(run("destroy")); |
| 217 | let failure = Exit.isFailure(primary) ? primary.cause : undefined; |
| 218 | if (Exit.isFailure(destroyed)) { |
| 219 | if (failure === undefined) failure = destroyed.cause; |
| 220 | else |
| 221 | yield* Console.error( |
| 222 | `${example}: cleanup destroy failed\n${Cause.pretty(destroyed.cause)}`, |
| 223 | ); |
| 224 | } else if (destroyed.value.exitCode !== 0 && failure === undefined) { |
| 225 | failure = Cause.fail( |
| 226 | new StepFailed({ |
| 227 | message: `${example}: alchemy destroy failed (exit ${destroyed.value.exitCode ?? "signal"})\n${destroyed.value.output}`, |
| 228 | }), |
| 229 | ); |
| 230 | } else if (destroyed.value.exitCode !== 0) { |
| 231 | yield* Console.error(`${example}: cleanup destroy also failed\n${destroyed.value.output}`); |
| 232 | } else if (failure === undefined) { |
| 233 | const matched = yield* Effect.exit( |
| 234 | assertOutput("destroy", destroyed.value, [new RegExp(`Destroy · ${stage}`), DONE]), |
| 235 | ); |
| 236 | if (Exit.isFailure(matched)) failure = matched.cause; |
| 237 | } |
| 238 | |
| 239 | if (failure !== undefined) { |
| 240 | const error = Cause.squash(failure); |
| 241 | const message = error instanceof StepFailed ? error.message : Cause.pretty(failure); |
| 242 | yield* Console.error(message); |
| 243 | return yield* new StepFailed({ message }); |
| 244 | } |
| 245 | }, Effect.scoped), |
| 246 | ).pipe(Command.withDescription("Run an example through alchemy dev, deploy and destroy")); |
| 247 | |
| 248 | Command.run(command, { version: "0.0.0" }).pipe( |
| 249 | Effect.provide(NodeServices.layer), |
| 250 | NodeRuntime.runMain, |
| 251 | ); |