The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Console from "effect/Console"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Path from "effect/Path"; |
| 6 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { PlatformServices } from "@/Util/PlatformServices.ts"; |
| 10 | |
| 11 | const tarball = process.env.ALCHEMY_CLI_PACKAGE; |
| 12 | const tarballDirectory = process.env.ALCHEMY_CLI_PACKAGES; |
| 13 | const enabled = !!(tarball || tarballDirectory); |
| 14 | const selectedManager = process.env.ALCHEMY_CLI_PACKAGE_MANAGER; |
| 15 | const managers = ["npm", "pnpm", "bun"] as const; |
| 16 | type Manager = (typeof managers)[number]; |
| 17 | |
| 18 | interface Invocation { |
| 19 | readonly command: string; |
| 20 | readonly args: ReadonlyArray<string>; |
| 21 | readonly runtime: "bun" | "node"; |
| 22 | } |
| 23 | |
| 24 | const invocations = (manager: Manager): ReadonlyArray<Invocation> => [ |
| 25 | { command: "node", args: ["node_modules/alchemy/bin/cli.js"], runtime: "node" }, |
| 26 | { command: "bun", args: ["node_modules/alchemy/bin/cli.js"], runtime: "bun" }, |
| 27 | { command: "bun", args: ["--bun", "alchemy"], runtime: "bun" }, |
| 28 | { command: "bun", args: ["x", "--bun", "--no-install", "alchemy"], runtime: "bun" }, |
| 29 | ...packageCommands[manager], |
| 30 | ]; |
| 31 | |
| 32 | const packageCommands: Record<Manager, ReadonlyArray<Invocation>> = { |
| 33 | npm: [ |
| 34 | { command: "npm", args: ["run", "cli", "--"], runtime: "node" }, |
| 35 | { command: "npm", args: ["exec", "--offline", "--", "alchemy"], runtime: "node" }, |
| 36 | { command: "npx", args: ["--no-install", "alchemy"], runtime: "node" }, |
| 37 | ], |
| 38 | pnpm: [ |
| 39 | { command: "pnpm", args: ["run", "cli"], runtime: "node" }, |
| 40 | { command: "pnpm", args: ["exec", "alchemy"], runtime: "node" }, |
| 41 | { command: "pnpm", args: ["alchemy"], runtime: "node" }, |
| 42 | ], |
| 43 | bun: [ |
| 44 | { command: "bun", args: ["run", "cli"], runtime: "bun" }, |
| 45 | { command: "bun", args: ["run", "--bun", "cli"], runtime: "bun" }, |
| 46 | { command: "bun", args: ["alchemy"], runtime: "bun" }, |
| 47 | ], |
| 48 | }; |
| 49 | |
| 50 | const Probe = Schema.fromJsonString( |
| 51 | Schema.Struct({ |
| 52 | runtime: Schema.String, |
| 53 | nodeEnv: Schema.String, |
| 54 | home: Schema.String, |
| 55 | credentialVariables: Schema.Array(Schema.String), |
| 56 | cwd: Schema.String, |
| 57 | entry: Schema.String, |
| 58 | alchemy: Schema.String, |
| 59 | args: Schema.Array(Schema.String), |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const canary = (manager: Manager) => |
| 64 | Effect.gen(function* () { |
| 65 | const fs = yield* FileSystem.FileSystem; |
| 66 | const path = yield* Path.Path; |
| 67 | const packageDir = yield* path.fromFileUrl(new URL("../../", import.meta.url)); |
| 68 | const checkout = yield* fs.realPath(path.resolve(packageDir, "../..")); |
| 69 | const project = yield* fs.makeTempDirectoryScoped({ prefix: `alchemy-package-${manager}-` }); |
| 70 | const projectRoot = yield* fs.realPath(project); |
| 71 | expect(projectRoot.startsWith(`${checkout}${path.sep}`)).toBe(false); |
| 72 | |
| 73 | const dependencies: Record<string, string> = {}; |
| 74 | for (const name of ["effect", "@effect/platform-bun", "@effect/platform-node"]) { |
| 75 | const manifest = yield* fs.readFileString( |
| 76 | path.join(packageDir, "node_modules", name, "package.json"), |
| 77 | ); |
| 78 | const { version } = yield* Schema.decodeUnknownEffect( |
| 79 | Schema.fromJsonString(Schema.Struct({ version: Schema.String })), |
| 80 | )(manifest); |
| 81 | dependencies[name] = version; |
| 82 | } |
| 83 | yield* fs.copyFile( |
| 84 | path.join(packageDir, "test/Cli/fixtures/package-stack.ts"), |
| 85 | path.join(project, "stack.run.ts"), |
| 86 | ); |
| 87 | const inherited = yield* Effect.sync(() => process.env); |
| 88 | const home = path.join(projectRoot, "home"); |
| 89 | yield* fs.makeDirectory(home); |
| 90 | for (const config of [".npmrc", "global.npmrc"]) { |
| 91 | yield* fs.writeFileString(path.join(home, config), ""); |
| 92 | } |
| 93 | // Pass only runtime paths and temporary directories, never the caller's |
| 94 | // credentials, module hooks, registry configuration, or user profiles. |
| 95 | const env = { |
| 96 | CI: "true", |
| 97 | HOME: home, |
| 98 | USERPROFILE: home, |
| 99 | XDG_CONFIG_HOME: path.join(home, ".config"), |
| 100 | npm_config_userconfig: path.join(home, ".npmrc"), |
| 101 | npm_config_globalconfig: path.join(home, "global.npmrc"), |
| 102 | TMPDIR: inherited.TMPDIR, |
| 103 | TMP: inherited.TMP, |
| 104 | TEMP: inherited.TEMP, |
| 105 | SystemRoot: inherited.SystemRoot, |
| 106 | COMSPEC: inherited.COMSPEC, |
| 107 | PATHEXT: inherited.PATHEXT, |
| 108 | BUN_RUNTIME_TRANSPILER_CACHE_PATH: "0", |
| 109 | ALCHEMY_HOME: path.join(home, ".alchemy"), |
| 110 | PATH: inherited.PATH?.split(process.platform === "win32" ? ";" : ":") |
| 111 | .filter((part) => !part.startsWith(checkout)) |
| 112 | .join(process.platform === "win32" ? ";" : ":"), |
| 113 | }; |
| 114 | const run = ( |
| 115 | command: string, |
| 116 | args: ReadonlyArray<string>, |
| 117 | nodeEnv?: string, |
| 118 | installing = false, |
| 119 | ) => |
| 120 | Effect.gen(function* () { |
| 121 | yield* Console.log( |
| 122 | `${manager}: ${command} ${args.join(" ")} (NODE_ENV=${nodeEnv ?? "unset"})`, |
| 123 | ); |
| 124 | const handle = yield* ChildProcess.make(command, args, { |
| 125 | cwd: project, |
| 126 | env: { ...env, NODE_ENV: nodeEnv }, |
| 127 | extendEnv: false, |
| 128 | stdin: "ignore", |
| 129 | stdout: "pipe", |
| 130 | stderr: "pipe", |
| 131 | forceKillAfter: "1 second", |
| 132 | }); |
| 133 | const [stdout, stderr, exitCode] = yield* Effect.all( |
| 134 | [ |
| 135 | handle.stdout.pipe(Stream.decodeText, Stream.mkString), |
| 136 | handle.stderr.pipe(Stream.decodeText, Stream.mkString), |
| 137 | handle.exitCode, |
| 138 | ], |
| 139 | { concurrency: 3 }, |
| 140 | ); |
| 141 | return { stdout, stderr, exitCode }; |
| 142 | }).pipe(Effect.scoped, Effect.timeout(installing ? "90 seconds" : "20 seconds")); |
| 143 | |
| 144 | const packedFiles = tarballDirectory |
| 145 | ? (yield* fs.readDirectory(path.resolve(tarballDirectory))) |
| 146 | .filter((name) => name.endsWith(".tgz")) |
| 147 | .map((name) => path.resolve(tarballDirectory, name)) |
| 148 | : [path.resolve(tarball!)]; |
| 149 | const overrides: Record<string, string> = {}; |
| 150 | for (const packed of packedFiles) { |
| 151 | expect((yield* fs.stat(packed)).type).toBe("File"); |
| 152 | const manifest = yield* run("tar", ["-xOf", packed, "package/package.json"]); |
| 153 | expect(manifest.exitCode).toBe(0); |
| 154 | const { name } = yield* Schema.decodeUnknownEffect( |
| 155 | Schema.fromJsonString(Schema.Struct({ name: Schema.String })), |
| 156 | )(manifest.stdout); |
| 157 | dependencies[name] = `file:${packed}`; |
| 158 | if (name !== "alchemy") overrides[name] = dependencies[name]; |
| 159 | } |
| 160 | expect(dependencies.alchemy).toBeDefined(); |
| 161 | yield* fs.writeFileString( |
| 162 | path.join(project, "package.json"), |
| 163 | JSON.stringify({ |
| 164 | name: "alchemy-cli-package-canary", |
| 165 | private: true, |
| 166 | type: "module", |
| 167 | scripts: { cli: "alchemy" }, |
| 168 | dependencies, |
| 169 | overrides: |
| 170 | manager === "npm" |
| 171 | ? Object.fromEntries(Object.keys(overrides).map((name) => [name, `$${name}`])) |
| 172 | : overrides, |
| 173 | }), |
| 174 | ); |
| 175 | if (manager === "pnpm") { |
| 176 | yield* fs.writeFileString( |
| 177 | path.join(project, "pnpm-workspace.yaml"), |
| 178 | JSON.stringify({ allowBuilds: { esbuild: true, workerd: true }, overrides }), |
| 179 | ); |
| 180 | } |
| 181 | const installed = yield* run( |
| 182 | manager, |
| 183 | manager === "npm" |
| 184 | ? ["install", "--no-audit", "--no-fund"] |
| 185 | : manager === "bun" |
| 186 | ? ["install", "--minimum-release-age=0"] |
| 187 | : ["install"], |
| 188 | undefined, |
| 189 | true, |
| 190 | ); |
| 191 | if (installed.exitCode !== 0) { |
| 192 | yield* Console.error(installed.stdout + installed.stderr); |
| 193 | } |
| 194 | expect({ |
| 195 | code: installed.exitCode, |
| 196 | output: installed.exitCode === 0 ? "" : installed.stdout + installed.stderr, |
| 197 | }).toEqual({ code: 0, output: "" }); |
| 198 | for (const name of Object.keys(dependencies)) { |
| 199 | const resolved = yield* fs.realPath(path.join(project, "node_modules", name)); |
| 200 | expect(resolved.startsWith(`${projectRoot}${path.sep}`)).toBe(true); |
| 201 | } |
| 202 | for (const scenario of [ |
| 203 | { nodeEnv: undefined, jsx: undefined }, |
| 204 | { nodeEnv: "development", jsx: "react-jsxdev" }, |
| 205 | { nodeEnv: "production", jsx: "preserve" }, |
| 206 | ]) { |
| 207 | if (scenario.jsx !== undefined) { |
| 208 | yield* fs.writeFileString( |
| 209 | path.join(project, "tsconfig.json"), |
| 210 | JSON.stringify({ |
| 211 | compilerOptions: { |
| 212 | jsx: scenario.jsx, |
| 213 | ...(scenario.jsx === "preserve" ? { jsxImportSource: "solid-js" } : {}), |
| 214 | }, |
| 215 | }), |
| 216 | ); |
| 217 | } |
| 218 | for (const invocation of invocations(manager)) { |
| 219 | const args = ["deploy", "stack.run.ts", "--stage", "canary", "--yes"]; |
| 220 | const result = yield* run( |
| 221 | invocation.command, |
| 222 | [...invocation.args, ...args], |
| 223 | scenario.nodeEnv, |
| 224 | ); |
| 225 | expect({ |
| 226 | code: result.exitCode, |
| 227 | output: result.exitCode === 0 ? "" : result.stdout + result.stderr, |
| 228 | }).toEqual({ code: 0, output: "" }); |
| 229 | expect(result.stdout).toContain("Packed CLI progress"); |
| 230 | expect(result.stdout).toContain("Packed CLI deployment complete"); |
| 231 | const line = result.stdout |
| 232 | .split("\n") |
| 233 | .find((line) => line.startsWith("CLI_PACKAGE_PROBE=")); |
| 234 | expect(line).toBeDefined(); |
| 235 | const probe = yield* Schema.decodeUnknownEffect(Probe)( |
| 236 | line!.slice("CLI_PACKAGE_PROBE=".length), |
| 237 | ); |
| 238 | expect(probe.runtime).toBe(invocation.runtime); |
| 239 | expect(probe.nodeEnv).toBe("production"); |
| 240 | expect(probe.home).toBe(home); |
| 241 | expect(probe.credentialVariables).toEqual([]); |
| 242 | expect(probe.cwd).toBe(projectRoot); |
| 243 | expect(probe.args).toEqual(args); |
| 244 | const entry = yield* fs.realPath(probe.entry); |
| 245 | const imported = yield* fs.realPath(yield* path.fromFileUrl(new URL(probe.alchemy))); |
| 246 | expect(entry.startsWith(`${projectRoot}${path.sep}node_modules${path.sep}`)).toBe(true); |
| 247 | expect(imported.startsWith(`${projectRoot}${path.sep}node_modules${path.sep}`)).toBe(true); |
| 248 | } |
| 249 | } |
| 250 | for (const invocation of invocations(manager)) { |
| 251 | const result = yield* run(invocation.command, [...invocation.args, "profile"], "development"); |
| 252 | expect(result.exitCode).toBe(1); |
| 253 | expect(result.stdout + result.stderr).not.toContain("jsxDEV"); |
| 254 | } |
| 255 | const destroyed = yield* run("bun", [ |
| 256 | "--bun", |
| 257 | "alchemy", |
| 258 | "destroy", |
| 259 | "stack.run.ts", |
| 260 | "--stage", |
| 261 | "canary", |
| 262 | "--yes", |
| 263 | ]); |
| 264 | expect({ |
| 265 | code: destroyed.exitCode, |
| 266 | output: destroyed.exitCode === 0 ? "" : destroyed.stdout + destroyed.stderr, |
| 267 | }).toEqual({ code: 0, output: "" }); |
| 268 | }).pipe(Effect.scoped, Effect.provide(PlatformServices)); |
| 269 | |
| 270 | describe.sequential("packed CLI outside the checkout", { tags: ["live"] }, () => { |
| 271 | if ( |
| 272 | enabled && |
| 273 | selectedManager !== undefined && |
| 274 | !managers.some((manager) => manager === selectedManager) |
| 275 | ) { |
| 276 | throw new Error(`Unknown ALCHEMY_CLI_PACKAGE_MANAGER: ${selectedManager}`); |
| 277 | } |
| 278 | for (const manager of managers) { |
| 279 | it.live.skipIf(!enabled || (selectedManager !== undefined && selectedManager !== manager))( |
| 280 | `installs with ${manager} and runs production CLI across runtimes and entrypoints`, |
| 281 | () => canary(manager), |
| 282 | { timeout: 120_000 }, |
| 283 | ); |
| 284 | } |
| 285 | }); |