@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 183 lines 6.61 KB
Raw
1 import * as NodePath from "node:path";
2 import * as Effect from "effect/Effect";
3 import type * as ViteModule from "vite";
4 import { describe, expect, it } from "vitest";
5 import { isDeployTarget } from "../../core/index.ts";
6 import cloudflareTargetFactory, {
7 makeWakuCloudflareTarget,
8 makeWakuPluginOptions,
9 } from "../cloudflare.ts";
10 import { WAKU_SERVER_ENTRY_PATH } from "../index.ts";
11
12 const WAKU_DIR = "/project/node_modules/waku";
13
14 const context = {
15 root: "/project",
16 wakuDirectory: WAKU_DIR,
17 phase: "build",
18 } as const;
19
20 const flatten = (plugins: ReadonlyArray<ViteModule.PluginOption>): Array<ViteModule.Plugin> =>
21 (plugins as Array<unknown>)
22 .flat(8)
23 .filter(
24 (plugin): plugin is ViteModule.Plugin =>
25 typeof plugin === "object" && plugin !== null && "name" in plugin,
26 );
27
28 describe("makeWakuPluginOptions", () => {
29 it("defaults main to waku's rsc worker entry and pins the rsc/ssr topology", () => {
30 const options = makeWakuPluginOptions({
31 root: "/project",
32 wakuDirectory: WAKU_DIR,
33 });
34 expect(options.main).toBe(NodePath.join(WAKU_DIR, WAKU_SERVER_ENTRY_PATH));
35 expect(options.viteEnvironments).toEqual({
36 entry: "rsc",
37 children: ["ssr"],
38 });
39 });
40
41 it("preserves user cloudflare options but overrides viteEnvironments", () => {
42 const options = makeWakuPluginOptions({
43 root: "/project",
44 wakuDirectory: WAKU_DIR,
45 pluginOptions: {
46 compatibilityDate: "2026-03-10",
47 compatibilityFlags: ["nodejs_als"],
48 worker: { name: "fixtures-waku", bindings: [] },
49 viteEnvironments: { entry: "ssr" },
50 },
51 });
52 expect(options.compatibilityDate).toBe("2026-03-10");
53 expect(options.compatibilityFlags).toEqual(["nodejs_als"]);
54 expect(options.worker?.name).toBe("fixtures-waku");
55 // The environment topology always wins: the worker must run in the rsc
56 // environment.
57 expect(options.main).toBe(NodePath.join(WAKU_DIR, WAKU_SERVER_ENTRY_PATH));
58 expect(options.viteEnvironments).toEqual({
59 entry: "rsc",
60 children: ["ssr"],
61 });
62 });
63
64 it("lets a user main (the user-entry seam) take precedence over waku's entry", () => {
65 const absolute = makeWakuPluginOptions({
66 root: "/project",
67 wakuDirectory: WAKU_DIR,
68 pluginOptions: { main: "/project/src/worker-entry.ts" },
69 });
70 expect(absolute.main).toBe("/project/src/worker-entry.ts");
71
72 // A relative main resolves against the project root, not process.cwd().
73 const relative = makeWakuPluginOptions({
74 root: "/project",
75 wakuDirectory: WAKU_DIR,
76 pluginOptions: { main: "./src/worker-entry.ts" },
77 });
78 expect(relative.main).toBe(NodePath.resolve("/project", "src/worker-entry.ts"));
79 // The topology stays pinned even with a user main.
80 expect(relative.viteEnvironments).toEqual({
81 entry: "rsc",
82 children: ["ssr"],
83 });
84 });
85
86 it("defaults compatibilityFlags to nodejs_als (waku needs AsyncLocalStorage)", () => {
87 expect(
88 makeWakuPluginOptions({ root: "/project", wakuDirectory: WAKU_DIR }).compatibilityFlags,
89 ).toEqual(["nodejs_als"]);
90 expect(
91 makeWakuPluginOptions({
92 root: "/project",
93 wakuDirectory: WAKU_DIR,
94 pluginOptions: { compatibilityFlags: [] },
95 }).compatibilityFlags,
96 ).toEqual(["nodejs_als"]);
97 expect(
98 makeWakuPluginOptions({
99 root: "/project",
100 wakuDirectory: WAKU_DIR,
101 pluginOptions: { compatibilityFlags: ["global_fetch_strictly_public"] },
102 }).compatibilityFlags,
103 ).toEqual(["global_fetch_strictly_public", "nodejs_als"]);
104 });
105
106 it("keeps user compatibilityFlags that already provide AsyncLocalStorage", () => {
107 expect(
108 makeWakuPluginOptions({
109 root: "/project",
110 wakuDirectory: WAKU_DIR,
111 pluginOptions: { compatibilityFlags: ["nodejs_als"] },
112 }).compatibilityFlags,
113 ).toEqual(["nodejs_als"]);
114 expect(
115 makeWakuPluginOptions({
116 root: "/project",
117 wakuDirectory: WAKU_DIR,
118 pluginOptions: { compatibilityFlags: ["nodejs_compat"] },
119 }).compatibilityFlags,
120 ).toEqual(["nodejs_compat"]);
121 expect(
122 makeWakuPluginOptions({
123 root: "/project",
124 wakuDirectory: WAKU_DIR,
125 pluginOptions: { compatibilityFlags: ["nodejs_compat_v2"] },
126 }).compatibilityFlags,
127 ).toEqual(["nodejs_compat_v2"]);
128 });
129 });
130
131 describe("makeWakuCloudflareTarget", () => {
132 it("default-exports the target factory", () => {
133 expect(cloudflareTargetFactory).toBe(makeWakuCloudflareTarget);
134 expect(isDeployTarget(cloudflareTargetFactory({}))).toBe(true);
135 });
136
137 it("is a cloudflare DeployTarget carrying the config opaquely", () => {
138 const config = { compatibilityDate: "2026-03-10" };
139 const target = makeWakuCloudflareTarget(config);
140 expect(target.platform).toBe("cloudflare");
141 expect(target.config).toBe(config);
142 expect(isDeployTarget(target)).toBe(true);
143 // `config` must be an own property even when undefined — `isDeployTarget`
144 // checks for its presence.
145 expect(isDeployTarget(makeWakuCloudflareTarget())).toBe(true);
146 });
147
148 it("declares the workerd bundle conditions and cloudflare: externals", () => {
149 const target = makeWakuCloudflareTarget();
150 expect(target.bundle?.conditions).toEqual(["workerd", "worker", "module", "browser"]);
151 expect(target.bundle?.external).toEqual(["cloudflare:"]);
152 });
153
154 it("does not take over the framework build (no wholesale build hook)", () => {
155 const target = makeWakuCloudflareTarget();
156 expect(target.build).toBeUndefined();
157 expect(target.finish).toBeUndefined();
158 });
159
160 it("resolves the adapter hook to this package's wrangler-free fork", () => {
161 const target = makeWakuCloudflareTarget();
162 const adapterPath = Effect.runSync(target.adapter(context));
163 expect(NodePath.isAbsolute(adapterPath)).toBe(true);
164 expect(adapterPath).toMatch(/adapter\.(ts|js)$/);
165 });
166
167 it("produces the cloudflare vite plugin with the pinned waku options", () => {
168 const target = makeWakuCloudflareTarget({
169 compatibilityDate: "2026-03-10",
170 });
171 const plugins = flatten(Effect.runSync(target.vitePlugins(context)));
172 expect(plugins.length).toBeGreaterThan(0);
173 expect(plugins.some((plugin) => plugin.name.startsWith("distilled-cloudflare"))).toBe(true);
174 });
175
176 it("surfaces the config's main as the generic user-entry carriage", () => {
177 expect(makeWakuCloudflareTarget().entry).toBeUndefined();
178 expect(makeWakuCloudflareTarget({ compatibilityDate: "2026-03-10" }).entry).toBeUndefined();
179 expect(makeWakuCloudflareTarget({ main: "./src/worker-entry.ts" }).entry).toEqual({
180 main: "./src/worker-entry.ts",
181 });
182 });
183 });