The best way to control Claude Code, Codex and more from any computer or phone

main
ts 179 lines 6.74 KB
Raw
1 import { expect, layer } from "@alchemy.run/test";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Bundle from "alchemy/Bundle/Bundle";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Result from "effect/Result";
7 import {
8 isolatedProject,
9 materializeIsolatedProject,
10 removeIsolatedProject,
11 } from "../IsolatedProject.ts";
12
13 /** Bare (non-relative, non-builtin) `import` statements left in the chunks. */
14 const bareImports = (result: Bundle.BundleOutput) =>
15 result.files
16 .filter((file) => typeof file.content === "string")
17 .flatMap((file) => (file.content as string).split("\n"))
18 .filter((line) => /^import\b/.test(line))
19 .filter((line) => !/["'](\.\/|node:)/.test(line));
20
21 /** Every generated-entry bootstrap module: each provider's `Bootstrap`. */
22 const BOOTSTRAP_MODULES = [
23 "@alchemy.run/aws/Bootstrap/Ecs",
24 "@alchemy.run/aws/Bootstrap/AppRunner",
25 "@alchemy.run/aws/Bootstrap/Batch",
26 "@alchemy.run/aws/Bootstrap/Ec2",
27 "@alchemy.run/aws/Bootstrap/Lambda",
28 "@alchemy.run/aws/Bootstrap/MicrovmBun",
29 "@alchemy.run/aws/Bootstrap/MicrovmNode",
30 "@alchemy.run/docker/Bootstrap",
31 ] as const;
32
33 layer(NodeServices.layer)("generated entry bootstraps", (it) => {
34 // The contract every platform's generated entry relies on: a consumer
35 // project that has the provider installed and NOTHING else reachable (bun's
36 // isolated linker / pnpm) must be able to bundle the provider's
37 // `Bootstrap` module — i.e. alchemy's own dependencies
38 // (`@distilled.cloud/*`, `@effect/platform-*`) resolve from alchemy's
39 // location, never from the consumer's. Before the bootstraps became real
40 // modules, the entry imported those directly and an isolated project left
41 // them external (the deployed process died with `Cannot find module`).
42 for (const specifier of BOOTSTRAP_MODULES) {
43 const name = specifier.split("/").slice(1).join("-");
44 it.effect(
45 `${specifier} bundles from an isolated project`,
46 () =>
47 Effect.gen(function* () {
48 // The consumer depends on the provider that generated the entry.
49 const provider = specifier.split("/").slice(0, 2).join("/");
50 const project = isolatedProject(`bootstrap-${name.toLowerCase()}`, import.meta.filename, [
51 provider,
52 ]);
53 yield* materializeIsolatedProject(project);
54 const virtualEntryPlugin = yield* Bundle.virtualEntryPlugin;
55 // The exact production condition sets: bun modules resolve
56 // `alchemy/*` through the `bun` condition (`src/*.ts`); node
57 // modules (Lambda zip, the *Node container variants) resolve
58 // through `import` → `lib/*.js`, so this test requires a built
59 // lib (`pnpm build`), exactly like a deployed Node bundle.
60 const isNode = specifier.endsWith("Node") || specifier.endsWith("/Lambda");
61
62 try {
63 const result = yield* Bundle.build(
64 {
65 cwd: project.dir,
66 input: project.main,
67 platform: "node",
68 external: ["bun", "bun:*"],
69 resolve: {
70 conditionNames: isNode
71 ? [...Bundle.NODE_CONDITION_NAMES]
72 : [...Bundle.BUN_CONDITION_NAMES],
73 },
74 plugins: [
75 virtualEntryPlugin(
76 () => `
77 import * as bootstrap from ${JSON.stringify(specifier)};
78 export default bootstrap;
79 `,
80 ),
81 ],
82 },
83 { format: "esm" },
84 );
85 expect(bareImports(result)).toEqual([]);
86 expect(
87 result.files.some(
88 (file) => typeof file.content === "string" && file.content.includes("bootstrap"),
89 ),
90 ).toBe(true);
91 } finally {
92 yield* removeIsolatedProject(project);
93 }
94 }),
95 { tags: ["unit", "local"], timeout: 120_000 },
96 );
97 }
98
99 // An unresolvable import in a generated entry is a BUILD error with the
100 // cause spelled out — never a warning that leaves the import external and
101 // the deployed process dead at boot. (Doubles as the negative control for
102 // the harness: without the `alchemy` link the project resolves nothing.)
103 it.effect(
104 "an unresolvable import in a generated entry fails the build",
105 () =>
106 Effect.gen(function* () {
107 const fs = yield* FileSystem.FileSystem;
108 const project = isolatedProject("bootstrap-control", import.meta.filename);
109 yield* materializeIsolatedProject(project);
110 yield* fs.remove(`${project.dir}/node_modules`, { recursive: true });
111 const virtualEntryPlugin = yield* Bundle.virtualEntryPlugin;
112 try {
113 const result = yield* Effect.result(
114 Bundle.build(
115 {
116 cwd: project.dir,
117 input: project.main,
118 platform: "node",
119 plugins: [
120 virtualEntryPlugin(
121 () => `
122 import * as bootstrap from "@alchemy.run/aws/Bootstrap/Ecs";
123 export default bootstrap;
124 `,
125 ),
126 ],
127 },
128 { format: "esm" },
129 ),
130 );
131 expect(Result.isFailure(result)).toBe(true);
132 if (Result.isFailure(result)) {
133 expect(result.failure.message).toContain(
134 'imports "@alchemy.run/aws/Bootstrap/Ecs", which cannot be resolved',
135 );
136 }
137 } finally {
138 yield* removeIsolatedProject(project);
139 }
140 }),
141 { tags: ["unit", "local"] },
142 );
143
144 // Deliberate externals (runtime-provided modules such as `bun:*`,
145 // `cloudflare:workers`) resolve to `{ external: true }`, not to nothing, so
146 // the guard leaves them alone.
147 it.effect(
148 "a declared external in a generated entry is not an error",
149 () =>
150 Effect.gen(function* () {
151 const project = isolatedProject("bootstrap-external", import.meta.filename);
152 yield* materializeIsolatedProject(project);
153 const virtualEntryPlugin = yield* Bundle.virtualEntryPlugin;
154 try {
155 const result = yield* Bundle.build(
156 {
157 cwd: project.dir,
158 input: project.main,
159 platform: "node",
160 external: ["bun:sqlite"],
161 plugins: [
162 virtualEntryPlugin(
163 () => `
164 import { Database } from "bun:sqlite";
165 export default Database;
166 `,
167 ),
168 ],
169 },
170 { format: "esm" },
171 );
172 expect(bareImports(result)).toEqual(['import { Database } from "bun:sqlite";']);
173 } finally {
174 yield* removeIsolatedProject(project);
175 }
176 }),
177 { tags: ["unit", "local"] },
178 );
179 });