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

main
ts 86 lines 3.75 KB
Raw
1 import type { FunctionZipProps } from "@alchemy.run/aws/Lambda/Function";
2 import { makeFunctionBundler } from "@alchemy.run/aws/Lambda/FunctionBundle";
3 import { expect, layer } from "@alchemy.run/test";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as Bundle from "alchemy/Bundle/Bundle";
6 import { exec } from "alchemy/Util/exec";
7 import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Path from "effect/Path";
10 import { ChildProcess } from "effect/process";
11
12 /**
13 * The side-effect-free Cloudflare barrel exposes Flagship through a nested
14 * namespace. Its Providers graph imports Flagship's AppProvider while App
15 * initializes `App = Resource(TypeId)`.
16 */
17 layer(NodeServices.layer)("Bundle namespace initialization", (it) => {
18 it.effect(
19 "initializes Flagship App before the Cloudflare namespace consumes it",
20 () =>
21 Effect.gen(function* () {
22 const fs = yield* FileSystem.FileSystem;
23 const path = yield* Path.Path;
24 const cwd = process.cwd();
25 const root = yield* fs.makeTempDirectory({
26 directory: path.join(cwd, ".alchemy"),
27 prefix: "namespace-initialization-",
28 });
29
30 try {
31 const cloudflare = path.join(cwd, "..", "cloudflare", "src", "index.ts");
32 const effect = path.join(cwd, "node_modules", "effect", "dist", "Effect.js");
33 const cause = path.join(cwd, "node_modules", "effect", "dist", "Cause.js");
34 const entry = path.join(root, "entry.ts");
35 yield* fs.writeFileString(
36 entry,
37 [
38 `import * as Cloudflare from ${JSON.stringify(cloudflare)};`,
39 `import * as Effect from ${JSON.stringify(effect)};`,
40 `import * as Cause from ${JSON.stringify(cause)};`,
41 `const program = Effect.gen(function* () {`,
42 ` return yield* Cloudflare.Flagship.App("Flags", {});`,
43 `}).pipe(Effect.catchCause((cause) => Effect.sync(() => {`,
44 ` const rendered = Cause.pretty(cause);`,
45 ` if (rendered.includes("is not iterable") || rendered.includes("not a function")) {`,
46 ` throw new Error(rendered);`,
47 ` }`,
48 `})));`,
49 `Effect.runSync(program);`,
50 `console.log("NAMESPACE_INITIALIZATION_MARKER");`,
51 ].join("\n"),
52 );
53
54 const bundler = yield* makeFunctionBundler;
55 const plan = yield* bundler.resolveBundlePlan({
56 main: entry,
57 isExternal: true,
58 build: { external: ["cloudflare:workers"] },
59 } as FunctionZipProps);
60 const bundle = yield* Bundle.build(plan.inputOptions, plan.outputOptions, plan.extra);
61 yield* Effect.forEach(bundle.files, (file) => {
62 const output = path.join(root, file.path);
63 return Effect.gen(function* () {
64 yield* fs.makeDirectory(path.dirname(output), { recursive: true });
65 if (typeof file.content === "string") {
66 yield* fs.writeFileString(output, file.content);
67 } else {
68 yield* fs.writeFile(output, file.content);
69 }
70 });
71 });
72 const output = path.join(root, bundle.files[0].path);
73 const result = yield* exec(
74 ChildProcess.make(process.execPath, [output], { shell: false }),
75 ).pipe(Effect.scoped);
76
77 expect(result.stderr).toBe("");
78 expect(result.exitCode).toBe(0);
79 expect(result.stdout).toContain("NAMESPACE_INITIALIZATION_MARKER");
80 } finally {
81 yield* fs.remove(root, { recursive: true }).pipe(Effect.ignore);
82 }
83 }),
84 { tags: ["unit", "provider:aws", "provider:aws:lambda", "local"] },
85 );
86 });