The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 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 | // A package whose exports answer `distilled-lazy` the way distilled's |
| 11 | // service modules do: the stub only under the condition, the full module |
| 12 | // otherwise. |
| 13 | const conditional = (name: string) => |
| 14 | JSON.stringify({ |
| 15 | name, |
| 16 | type: "module", |
| 17 | exports: { |
| 18 | "./machines": { "distilled-lazy": "./lazy.js", default: "./full.js" }, |
| 19 | }, |
| 20 | }); |
| 21 | |
| 22 | // The published launcher under Node with the real `bin/register-oxc.js`: |
| 23 | // report which file each package resolves to, through `import` and |
| 24 | // `require`, and the env var a child process would inherit. |
| 25 | const resolved = (args: ReadonlyArray<string>, env: Record<string, string> = {}) => |
| 26 | Effect.gen(function* () { |
| 27 | const fs = yield* FileSystem.FileSystem; |
| 28 | const path = yield* Path.Path; |
| 29 | const packageDir = yield* path.fromFileUrl(new URL("../../", import.meta.url)); |
| 30 | const project = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-distilled-lazy-" }); |
| 31 | const installed = path.join(project, "node_modules", "alchemy"); |
| 32 | const bin = path.join(installed, "bin"); |
| 33 | yield* fs.makeDirectory(bin, { recursive: true }); |
| 34 | for (const file of ["cli.js", "register-oxc.js"]) |
| 35 | yield* fs.copyFile(path.join(packageDir, "bin", file), path.join(bin, file)); |
| 36 | yield* fs.symlink(path.join(packageDir, "node_modules"), path.join(installed, "node_modules")); |
| 37 | yield* fs.writeFileString( |
| 38 | path.join(installed, "package.json"), |
| 39 | JSON.stringify({ type: "module" }), |
| 40 | ); |
| 41 | for (const name of ["@distilled.cloud/fly-io", "other"]) { |
| 42 | const dir = path.join(project, "node_modules", name); |
| 43 | yield* fs.makeDirectory(dir, { recursive: true }); |
| 44 | yield* fs.writeFileString(path.join(dir, "package.json"), conditional(name)); |
| 45 | yield* fs.writeFileString(path.join(dir, "lazy.js"), 'export const file = "lazy";\n'); |
| 46 | yield* fs.writeFileString(path.join(dir, "full.js"), 'export const file = "full";\n'); |
| 47 | } |
| 48 | yield* fs.writeFileString( |
| 49 | path.join(bin, "alchemy.js"), |
| 50 | [ |
| 51 | 'import { createRequire } from "node:module";', |
| 52 | `const require = createRequire(${JSON.stringify(path.join(project, "package.json"))});`, |
| 53 | 'const { file: distilled } = await import("@distilled.cloud/fly-io/machines");', |
| 54 | 'const { file: other } = await import("other/machines");', |
| 55 | 'const { file: required } = require("@distilled.cloud/fly-io/machines");', |
| 56 | "const inherited = process.env.ALCHEMY_FAST_DISTILLED ?? null;", |
| 57 | "console.log(JSON.stringify({ distilled, required, other, inherited }));", |
| 58 | "", |
| 59 | ].join("\n"), |
| 60 | ); |
| 61 | const handle = yield* ChildProcess.make(nodePath!, [path.join(bin, "cli.js"), ...args], { |
| 62 | cwd: project, |
| 63 | env: { npm_execpath: "", npm_config_user_agent: "", ALCHEMY_FAST_DISTILLED: "", ...env }, |
| 64 | extendEnv: true, |
| 65 | stdin: "ignore", |
| 66 | stdout: "pipe", |
| 67 | stderr: "pipe", |
| 68 | forceKillAfter: "1 second", |
| 69 | }); |
| 70 | const [stdout, stderr, exitCode] = yield* Effect.all( |
| 71 | [ |
| 72 | handle.stdout.pipe(Stream.decodeText, Stream.mkString), |
| 73 | handle.stderr.pipe(Stream.decodeText, Stream.mkString), |
| 74 | handle.exitCode, |
| 75 | ], |
| 76 | { concurrency: 3 }, |
| 77 | ); |
| 78 | expect({ exitCode, stderr }).toEqual({ exitCode: 0, stderr: "" }); |
| 79 | return JSON.parse(stdout.trim()); |
| 80 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)); |
| 81 | |
| 82 | describe("distilled lazy service stubs", { tags: ["unit", "local"] }, () => { |
| 83 | it.live.skipIf(!nodeSupportsDevMode)( |
| 84 | "the CLI under Node loads full distilled modules by default", |
| 85 | () => |
| 86 | Effect.gen(function* () { |
| 87 | expect(yield* resolved(["deploy"])).toEqual({ |
| 88 | distilled: "full", |
| 89 | required: "full", |
| 90 | other: "full", |
| 91 | inherited: "", |
| 92 | }); |
| 93 | }), |
| 94 | ); |
| 95 | |
| 96 | it.live.skipIf(!nodeSupportsDevMode)( |
| 97 | "--fast-distilled opts in to distilled-lazy for @distilled.cloud packages only", |
| 98 | () => |
| 99 | Effect.gen(function* () { |
| 100 | expect(yield* resolved(["deploy", "--fast-distilled"])).toEqual({ |
| 101 | distilled: "lazy", |
| 102 | required: "lazy", |
| 103 | other: "full", |
| 104 | inherited: "1", |
| 105 | }); |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | it.live.skipIf(!nodeSupportsDevMode)("--fast-distilled after a -- separator is ignored", () => |
| 110 | Effect.gen(function* () { |
| 111 | expect(yield* resolved(["exec", "--", "--fast-distilled"])).toMatchObject({ |
| 112 | distilled: "full", |
| 113 | }); |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | it.live.skipIf(!nodeSupportsDevMode)("ALCHEMY_FAST_DISTILLED=1 opts in without the flag", () => |
| 118 | Effect.gen(function* () { |
| 119 | expect(yield* resolved(["deploy"], { ALCHEMY_FAST_DISTILLED: "1" })).toMatchObject({ |
| 120 | distilled: "lazy", |
| 121 | other: "full", |
| 122 | }); |
| 123 | }), |
| 124 | ); |
| 125 | }); |