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

main
ts 253 lines 9.54 KB
Raw
1 import * as NodeFsPromises from "node:fs/promises";
2 import * as NodePath from "node:path";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import type * as ViteModule from "vite";
6 import { mergeConfig } from "vite";
7 import { describe, expect, it } from "vitest";
8 import framework, {
9 DEFAULT_TARGET_SPECIFIER,
10 layer,
11 make,
12 makeWakuConfigInput,
13 makeWakuServerEntryPlugin,
14 mergeUserWakuConfig,
15 selectWakuTargetInput,
16 WAKU_CONFIG_FILES,
17 WAKU_SERVER_ENTRY_ID,
18 WAKU_SERVER_ENTRY_MODULE,
19 type WakuTarget,
20 } from "../index.ts";
21
22 const ADAPTER = "/project/node_modules/@alchemy.run/frontend-frameworks/dist/waku/adapter.js";
23
24 const flatten = (plugins: Array<ViteModule.PluginOption> | undefined): Array<ViteModule.Plugin> =>
25 ((plugins ?? []) as Array<unknown>)
26 .flat(8)
27 .filter(
28 (plugin): plugin is ViteModule.Plugin =>
29 typeof plugin === "object" && plugin !== null && "name" in plugin,
30 );
31
32 describe("makeWakuConfigInput", () => {
33 it("defaults unstable_adapter to the target's adapter module", () => {
34 const config = makeWakuConfigInput({ adapterPath: ADAPTER });
35 expect(config.unstable_adapter).toBe(ADAPTER);
36 });
37
38 it("pins unstable_adapter to the target's adapter module (the caller rejects user adapters)", () => {
39 const config = makeWakuConfigInput({
40 adapterPath: ADAPTER,
41 userConfig: { unstable_adapter: "/custom/adapter.ts" },
42 });
43 expect(config.unstable_adapter).toBe(ADAPTER);
44 });
45
46 it("injects the target's vite plugins ahead of user plugins", () => {
47 const targetPlugin: ViteModule.Plugin = { name: "target-plugin" };
48 const userPlugin: ViteModule.Plugin = { name: "user-plugin" };
49 const config = makeWakuConfigInput({
50 adapterPath: ADAPTER,
51 plugins: [targetPlugin],
52 userConfig: { vite: { plugins: [userPlugin] } },
53 });
54 const plugins = flatten(config.vite?.plugins);
55 const targetIndex = plugins.findIndex((plugin) => plugin.name === "target-plugin");
56 const userIndex = plugins.findIndex((plugin) => plugin.name === "user-plugin");
57 expect(targetIndex).toBeGreaterThanOrEqual(0);
58 expect(userIndex).toBeGreaterThan(targetIndex);
59 });
60
61 it("injects no plugins when the target provides none", () => {
62 const config = makeWakuConfigInput({ adapterPath: ADAPTER });
63 expect(flatten(config.vite?.plugins)).toEqual([]);
64 });
65
66 it("dedupes waku and hono so the adapter resolves the project's copies", () => {
67 const config = makeWakuConfigInput({
68 adapterPath: ADAPTER,
69 userConfig: { vite: { resolve: { dedupe: ["react", "waku"] } } },
70 });
71 expect(config.vite?.resolve?.dedupe).toEqual(["waku", "hono", "react"]);
72 });
73
74 it("applies the rsc/ssr optimizeDeps includes and neutral platform", () => {
75 const config = makeWakuConfigInput({ adapterPath: ADAPTER });
76 const environments = config.vite?.environments as Record<string, ViteModule.EnvironmentOptions>;
77 expect(environments.rsc?.optimizeDeps?.include).toContain("hono/tiny");
78 expect(environments.ssr?.optimizeDeps?.include).toContain("waku > rsc-html-stream/server");
79 expect(environments.rsc?.build?.rolldownOptions?.platform).toBe("neutral");
80 expect(environments.ssr?.build?.rolldownOptions?.platform).toBe("neutral");
81 });
82
83 it("merges user environment config instead of clobbering it", () => {
84 const config = makeWakuConfigInput({
85 adapterPath: ADAPTER,
86 userConfig: {
87 vite: {
88 environments: {
89 rsc: { optimizeDeps: { include: ["extra-dep"] } },
90 custom: { optimizeDeps: { include: ["custom-dep"] } },
91 },
92 },
93 },
94 });
95 const environments = config.vite?.environments as Record<string, ViteModule.EnvironmentOptions>;
96 expect(environments.rsc?.optimizeDeps?.include).toEqual(["hono/tiny", "extra-dep"]);
97 expect(environments.custom?.optimizeDeps?.include).toEqual(["custom-dep"]);
98 });
99 });
100
101 describe("mergeUserWakuConfig", () => {
102 const merge = (
103 file: Parameters<typeof mergeUserWakuConfig>[0]["file"],
104 inline: Parameters<typeof mergeUserWakuConfig>[0]["inline"],
105 ) => mergeUserWakuConfig({ file, inline, mergeViteConfig: mergeConfig });
106
107 it("probes the same config files as waku's CLI, in the same order", () => {
108 expect(WAKU_CONFIG_FILES).toEqual(["waku.config.ts", "waku.config.js"]);
109 });
110
111 it("passes a lone file config or lone inline config through", () => {
112 expect(merge({ basePath: "/docs/" }, undefined)).toStrictEqual({
113 basePath: "/docs/",
114 });
115 expect(merge(undefined, { srcDir: "app" })).toStrictEqual({
116 srcDir: "app",
117 });
118 });
119
120 it("merges inline options over the file config per key", () => {
121 expect(merge({ basePath: "/docs/", srcDir: "app" }, { srcDir: "source" })).toEqual({
122 basePath: "/docs/",
123 srcDir: "source",
124 });
125 });
126
127 it("does not let explicit undefined inline keys clobber file values", () => {
128 expect(merge({ srcDir: "app" }, { srcDir: undefined, distDir: "out" })).toEqual({
129 srcDir: "app",
130 distDir: "out",
131 });
132 });
133
134 it("combines vite configs with vite's mergeConfig (file plugins first)", () => {
135 const filePlugin = { name: "file-plugin" };
136 const inlinePlugin = { name: "inline-plugin" };
137 const merged = merge(
138 { vite: { plugins: [filePlugin], base: "/docs/" } },
139 { vite: { plugins: [inlinePlugin] } },
140 );
141 expect(merged?.vite?.base).toBe("/docs/");
142 const names = flatten(merged?.vite?.plugins).map((plugin) => plugin.name);
143 expect(names.indexOf("file-plugin")).toBeGreaterThanOrEqual(0);
144 expect(names.indexOf("inline-plugin")).toBeGreaterThan(names.indexOf("file-plugin"));
145 });
146
147 it("keeps the only vite config present", () => {
148 const vite = { base: "/docs/" };
149 expect(merge({ vite }, { srcDir: "app" })?.vite).toBe(vite);
150 expect(merge({ srcDir: "app" }, { vite })?.vite).toBe(vite);
151 });
152 });
153
154 describe("selectWakuTargetInput", () => {
155 const makeTarget = (config: unknown): WakuTarget => ({
156 platform: "test",
157 config,
158 adapter: () => Effect.succeed("/adapter.js"),
159 vitePlugins: () => Effect.succeed([]),
160 });
161
162 it("defaults to the cloudflare target module specifier", () => {
163 const { input, config } = selectWakuTargetInput();
164 expect(input).toBe(DEFAULT_TARGET_SPECIFIER);
165 expect(config).toBeUndefined();
166 });
167
168 it("feeds the deprecated vite alias to the default target", () => {
169 const vite = { compatibilityDate: "2026-03-10" };
170 const { input, config } = selectWakuTargetInput({ vite });
171 expect(input).toBe(DEFAULT_TARGET_SPECIFIER);
172 expect(config).toBe(vite);
173 });
174
175 it("reads the harness target-scoped carriage (worker wins over the vite alias)", () => {
176 const worker = { compatibilityDate: "2026-03-10" };
177 const { input, config } = selectWakuTargetInput({
178 target: { name: "cloudflare", cloudflare: { worker } },
179 vite: { compatibilityDate: "1999-01-01" },
180 });
181 expect(input).toBe(DEFAULT_TARGET_SPECIFIER);
182 expect(config).toBe(worker);
183 });
184
185 it("falls back to the vite alias when the carriage has no worker config", () => {
186 const vite = { compatibilityDate: "2026-03-10" };
187 const { input, config } = selectWakuTargetInput({
188 target: { name: "cloudflare" },
189 vite,
190 });
191 expect(input).toBe(DEFAULT_TARGET_SPECIFIER);
192 expect(config).toBe(vite);
193 });
194
195 it("passes an explicit target value through unchanged", () => {
196 const target = makeTarget({ worker: { name: "explicit" } });
197 const { input } = selectWakuTargetInput({ target });
198 expect(input).toBe(target);
199 });
200
201 it("passes a target factory through with the vite alias as its config", () => {
202 const factory = (config: unknown) => makeTarget(config);
203 const vite = { compatibilityDate: "2026-03-10" };
204 const { input, config } = selectWakuTargetInput({ target: factory, vite });
205 expect(input).toBe(factory);
206 expect(config).toBe(vite);
207 });
208
209 it("passes a target module specifier through", () => {
210 const { input } = selectWakuTargetInput({ target: "@my/waku-aws" });
211 expect(input).toBe("@my/waku-aws");
212 });
213 });
214
215 describe("adapter fork", () => {
216 it("drops the wrangler-writing build enhancer and never imports wrangler", async () => {
217 const source = await NodeFsPromises.readFile(
218 NodePath.resolve(import.meta.dirname, "../adapter.ts"),
219 "utf8",
220 );
221 expect(source).toContain("buildEnhancers: []");
222 expect(source).not.toContain("cloudflare-build-enhancer");
223 expect(source).not.toMatch(/from\s+["']wrangler/);
224 expect(source).not.toMatch(/import\(["']wrangler/);
225 });
226 });
227
228 describe("framework factory", () => {
229 it("default-exports a factory producing a Layer<Framework>", () => {
230 expect(Layer.isLayer(framework({}))).toBe(true);
231 expect(Layer.isLayer(layer())).toBe(true);
232 // The alchemy-side FrameworkModule contract: `make` returns the service
233 // implementation as an Effect.
234 expect(Effect.isEffect(make())).toBe(true);
235 });
236
237 it("pins the server entry to waku's rsc index module", () => {
238 expect(WAKU_SERVER_ENTRY_MODULE).toBe("server/index.js");
239 });
240 });
241
242 describe("virtual:waku/server-entry", () => {
243 it("resolves the wrappable-handler id to the installed waku server entry", () => {
244 const wakuDirectory = "/project/node_modules/waku";
245 const plugin = makeWakuServerEntryPlugin(wakuDirectory);
246 const resolveId = plugin.resolveId as (id: string) => string | undefined;
247 expect(resolveId(WAKU_SERVER_ENTRY_ID)).toBe(
248 NodePath.join(wakuDirectory, "dist/lib/vite-entries/entry.server.js"),
249 );
250 expect(resolveId("virtual:waku/other")).toBeUndefined();
251 expect(resolveId("./src/worker-entry.ts")).toBeUndefined();
252 });
253 });