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

main
ts 89 lines 3.81 KB
Raw
1 /**
2 * Enforces the deploy-target decoupling: the framework-generic half of this
3 * package (everything reachable from `src/index.ts`) must contain ZERO
4 * Cloudflare imports — no `@alchemy.run/cloudflare-runtime` package, no
5 * `cloudflare:` module ids, no `@cloudflare/*` types, and no reach into the
6 * Cloudflare target module (`src/cloudflare.ts`), the integration fork, or
7 * the vendored runtime. All of that lives exclusively behind the
8 * `@alchemy.run/frontend-frameworks/astro/cloudflare` subpath (and `./source`, which is the
9 * alchemy *Cloudflare Worker* source provider by definition).
10 */
11 import * as NodeFsPromises from "node:fs/promises";
12 import * as NodePath from "node:path";
13 import { describe, expect, it } from "vitest";
14
15 const SRC = NodePath.resolve(import.meta.dirname, "..");
16
17 /** Import/export specifiers of a TS module (static, type-only, and dynamic). */
18 const collectSpecifiers = (source: string): Array<string> => {
19 const specifiers: Array<string> = [];
20 const patterns = [
21 /(?:import|export)\s[^"']*?from\s*["']([^"']+)["']/g,
22 /import\s*["']([^"']+)["']/g,
23 /import\s*\(\s*["']([^"']+)["']\s*\)/g,
24 ];
25 for (const pattern of patterns) {
26 for (const match of source.matchAll(pattern)) {
27 specifiers.push(match[1]!);
28 }
29 }
30 return specifiers;
31 };
32
33 /** Transitively walk relative imports starting from the given entry module. */
34 const walkModuleGraph = async (entry: string): Promise<Map<string, Array<string>>> => {
35 const graph = new Map<string, Array<string>>();
36 const queue = [entry];
37 while (queue.length > 0) {
38 const file = queue.pop()!;
39 if (graph.has(file)) continue;
40 const source = await NodeFsPromises.readFile(file, "utf8");
41 const specifiers = collectSpecifiers(source);
42 graph.set(file, specifiers);
43 for (const specifier of specifiers) {
44 if (!specifier.startsWith(".")) continue;
45 queue.push(NodePath.resolve(NodePath.dirname(file), specifier));
46 }
47 }
48 return graph;
49 };
50
51 describe("framework-generic core is cloudflare-free", () => {
52 it("no module reachable from src/index.ts imports anything cloudflare", async () => {
53 const graph = await walkModuleGraph(NodePath.join(SRC, "index.ts"));
54
55 // The walk must have actually covered the core modules.
56 const reached = [...graph.keys()].map((file) => NodePath.relative(SRC, file));
57 expect(reached).toContain("index.ts");
58 expect(reached).toContain("Astro.ts");
59 expect(reached).toContain("Target.ts");
60 expect(reached).toContain("environments.ts");
61
62 for (const [file, specifiers] of graph) {
63 const relative = NodePath.relative(SRC, file);
64
65 // The cloudflare halves must be unreachable from the core.
66 expect(relative, `core reaches cloudflare module ${relative}`).not.toMatch(
67 /^(cloudflare\.ts|integration\.ts|config-plugin\.ts|prerender-middleware\.ts|source\.ts|runtime[/\\])/,
68 );
69
70 for (const specifier of specifiers) {
71 // The package name itself is Cloudflare-specific, so allow its generic
72 // `/core` export while rejecting actual platform runtime modules and
73 // vendor types — including type-only imports.
74 expect(specifier, `${relative} imports ${specifier}`).not.toMatch(
75 /^(?:@alchemy\.run\/cloudflare-runtime(?:\/|$)|@cloudflare\/|cloudflare:)/,
76 );
77 }
78 }
79 });
80
81 it("the cloudflare target module is self-contained under the ./cloudflare subpath", async () => {
82 // Sanity check the inverse direction: the target module exists and is the
83 // only public seam re-exporting the integration fork.
84 const index = await NodeFsPromises.readFile(NodePath.join(SRC, "index.ts"), "utf8");
85 expect(index).not.toContain("./integration.ts");
86 const cloudflare = await NodeFsPromises.readFile(NodePath.join(SRC, "cloudflare.ts"), "utf8");
87 expect(cloudflare).toContain("./integration.ts");
88 });
89 });