The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import { expect, it } from "@alchemy.run/test"; |
| 3 | import * as BunServices from "@effect/platform-bun/BunServices"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 6 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | |
| 10 | const selection = Schema.fromJsonString( |
| 11 | Schema.Struct({ |
| 12 | command: Schema.Array(Schema.String), |
| 13 | cwd: Schema.String, |
| 14 | external: Schema.Boolean, |
| 15 | resetShared: Schema.Boolean, |
| 16 | endpoint: Schema.String, |
| 17 | }), |
| 18 | ); |
| 19 | |
| 20 | const invoke = Effect.fn(function* (args: string[], env: Record<string, string> = {}) { |
| 21 | const script = yield* Effect.sync(() => |
| 22 | fileURLToPath(new URL("../../../../scripts/test-aws-floci.ts", import.meta.url)), |
| 23 | ); |
| 24 | // The launcher runs on Node; resolve it here because the child gets an |
| 25 | // empty PATH. |
| 26 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 27 | const executable = (yield* spawner.string( |
| 28 | ChildProcess.make("node", ["-p", "process.execPath"]), |
| 29 | )).trim(); |
| 30 | const child = yield* ChildProcess.make(executable, [script, ...args], { |
| 31 | env: { |
| 32 | PATH: "", |
| 33 | AWS_ENDPOINT_URL: "http://localhost:4566", |
| 34 | ALCHEMY_FLOCI_EXTERNAL: "", |
| 35 | ALCHEMY_FLOCI_NO_RESET: "", |
| 36 | ...env, |
| 37 | }, |
| 38 | extendEnv: false, |
| 39 | stdin: "ignore", |
| 40 | stdout: "pipe", |
| 41 | stderr: "pipe", |
| 42 | killSignal: "SIGTERM", |
| 43 | forceKillAfter: "1 second", |
| 44 | }); |
| 45 | return yield* Effect.all( |
| 46 | { |
| 47 | code: child.exitCode, |
| 48 | stdout: child.stdout.pipe( |
| 49 | Stream.decodeText, |
| 50 | Stream.runCollect, |
| 51 | Effect.map((chunks) => chunks.join("")), |
| 52 | ), |
| 53 | stderr: child.stderr.pipe( |
| 54 | Stream.decodeText, |
| 55 | Stream.runCollect, |
| 56 | Effect.map((chunks) => chunks.join("")), |
| 57 | ), |
| 58 | }, |
| 59 | { concurrency: "unbounded" }, |
| 60 | ); |
| 61 | }); |
| 62 | |
| 63 | const dryRun = Effect.fn(function* (args: string[] = [], env: Record<string, string> = {}) { |
| 64 | const result = yield* invoke(["--dry-run", ...args], env); |
| 65 | expect(result.code).toBe(0); |
| 66 | return yield* Schema.decodeUnknownEffect(selection)(result.stdout); |
| 67 | }); |
| 68 | |
| 69 | it.live( |
| 70 | "Floci dry run preserves state and defaults to four concurrent files without Docker", |
| 71 | () => |
| 72 | Effect.gen(function* () { |
| 73 | const plan = yield* dryRun(); |
| 74 | expect(plan.resetShared).toBe(false); |
| 75 | expect(plan.external).toBe(false); |
| 76 | expect(plan.command.slice(-4)).toEqual(["--concurrency", "4", "--profile", "testing"]); |
| 77 | expect(plan.command).toContain("test/AWS/Organizations/Organization.test.ts"); |
| 78 | // The Organizations suites share the account's one organization |
| 79 | // through OrganizationLease, so the whole folder is selected. |
| 80 | expect(plan.command).toContain("test/AWS/Organizations/Account.test.ts"); |
| 81 | expect(plan.command.some((arg) => arg.endsWith(".local.test.ts"))).toBe(false); |
| 82 | }).pipe(Effect.provide(BunServices.layer)), |
| 83 | { tags: ["unit", "local"] }, |
| 84 | ); |
| 85 | |
| 86 | it.live( |
| 87 | "Floci launcher preserves explicit concurrency and profile flag forms", |
| 88 | () => |
| 89 | Effect.gen(function* () { |
| 90 | for (const flags of [["--concurrency", "2"], ["--concurrency=2"], ["-c", "2"]]) { |
| 91 | const plan = yield* dryRun([...flags, "--profile=testing"]); |
| 92 | expect(plan.command.slice(-flags.length - 1)).toEqual([...flags, "--profile=testing"]); |
| 93 | expect(plan.command).not.toContain("4"); |
| 94 | expect(plan.command).not.toContain("--profile"); |
| 95 | } |
| 96 | }).pipe(Effect.provide(BunServices.layer)), |
| 97 | { tags: ["unit", "local"] }, |
| 98 | ); |
| 99 | |
| 100 | it.live( |
| 101 | "Floci shared reset requires an explicit flag and rejects safety conflicts", |
| 102 | () => |
| 103 | Effect.gen(function* () { |
| 104 | expect((yield* dryRun(["--reset-shared"])).resetShared).toBe(true); |
| 105 | for (const [args, env] of [ |
| 106 | [["--reset-shared", "--external"], {}], |
| 107 | [["--reset-shared"], { ALCHEMY_FLOCI_EXTERNAL: "1" }], |
| 108 | [["--reset-shared"], { ALCHEMY_FLOCI_NO_RESET: "1" }], |
| 109 | ] as const) { |
| 110 | const result = yield* invoke(["--dry-run", ...args], env); |
| 111 | expect(result.code).toBe(1); |
| 112 | expect(result.stderr).toContain("--reset-shared conflicts"); |
| 113 | } |
| 114 | }).pipe(Effect.provide(BunServices.layer)), |
| 115 | { tags: ["unit", "local"] }, |
| 116 | ); |
| 117 | |
| 118 | it.live( |
| 119 | "Floci external mode is selected by flag or environment without resetting", |
| 120 | () => |
| 121 | Effect.gen(function* () { |
| 122 | for (const plan of [ |
| 123 | yield* dryRun(["--external"]), |
| 124 | yield* dryRun([], { ALCHEMY_FLOCI_EXTERNAL: "1" }), |
| 125 | ]) { |
| 126 | expect(plan.external).toBe(true); |
| 127 | expect(plan.resetShared).toBe(false); |
| 128 | expect(plan.command).not.toContain("--external"); |
| 129 | expect(plan.command).not.toContain("--dry-run"); |
| 130 | } |
| 131 | }).pipe(Effect.provide(BunServices.layer)), |
| 132 | { tags: ["unit", "local"] }, |
| 133 | ); |
| 134 | |
| 135 | it.live( |
| 136 | "Floci rejects mismatched gateways and normalizes the loopback alias", |
| 137 | () => |
| 138 | Effect.gen(function* () { |
| 139 | for (const endpoint of [ |
| 140 | "not a URL", |
| 141 | "http://127.0.0.1:1", |
| 142 | "http://localhost:4567", |
| 143 | "https://localhost:4566", |
| 144 | "http://localhost:4566/path", |
| 145 | "http://localhost:4566/?query=1", |
| 146 | "http://localhost:4566/#fragment", |
| 147 | "http://user:password@localhost:4566", |
| 148 | ]) { |
| 149 | const result = yield* invoke(["--dry-run"], { |
| 150 | AWS_ENDPOINT_URL: endpoint, |
| 151 | }); |
| 152 | expect(result.code).toBe(1); |
| 153 | expect(result.stderr).toContain("AWS_ENDPOINT_URL must be http://localhost:4566"); |
| 154 | } |
| 155 | expect((yield* dryRun([], { AWS_ENDPOINT_URL: "http://127.0.0.1:4566/" })).endpoint).toBe( |
| 156 | "http://localhost:4566", |
| 157 | ); |
| 158 | const listed = yield* invoke(["--list", "test/AWS/SSM/Parameter.test.ts"]); |
| 159 | expect(listed.code).toBe(0); |
| 160 | expect(listed.stdout.trim()).toBe("test/AWS/SSM/Parameter.test.ts"); |
| 161 | }).pipe(Effect.provide(BunServices.layer)), |
| 162 | { tags: ["unit", "local"] }, |
| 163 | ); |