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

main
ts 136 lines 4.76 KB
Raw
1 import path from "node:path";
2 import * as vite from "vite";
3 import { describe, expect, it, vi } from "vitest";
4 import cloudflareVitePlugin, {
5 ALCHEMY_CLOUDFLARE_VITE_PLUGIN_NAME,
6 OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME,
7 disableOfficialCloudflareVitePlugins,
8 } from "../plugin.ts";
9
10 const root = path.join(import.meta.dirname, "fixtures");
11
12 const flattenPlugins = (plugins: ReadonlyArray<unknown>): Array<vite.Plugin> => {
13 const flat: Array<vite.Plugin> = [];
14 const visit = (entry: unknown): void => {
15 if (Array.isArray(entry)) {
16 for (const child of entry) visit(child);
17 return;
18 }
19 if (entry && typeof entry === "object" && "name" in entry) {
20 flat.push(entry as vite.Plugin);
21 }
22 };
23 for (const entry of plugins) visit(entry);
24 return flat;
25 };
26
27 const hasCloudflarePlugin = (plugins: ReadonlyArray<vite.Plugin>): boolean =>
28 plugins.some(
29 (plugin) =>
30 plugin.name === OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME ||
31 plugin.name.startsWith(`${OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME}:`),
32 );
33
34 describe("alchemy Cloudflare Vite plugin name", () => {
35 it("registers a prefixed presence plugin, not the official exact name", () => {
36 const plugins = flattenPlugins([cloudflareVitePlugin({ main: "./worker-entry.ts" })]);
37 expect(plugins.some((plugin) => plugin.name === ALCHEMY_CLOUDFLARE_VITE_PLUGIN_NAME)).toBe(
38 true,
39 );
40 expect(plugins.some((plugin) => plugin.name === OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME)).toBe(
41 false,
42 );
43 expect(hasCloudflarePlugin(plugins)).toBe(true);
44 });
45
46 it("no-ops official plugin hooks already present in config.plugins", async () => {
47 const officialConfig = vi.fn(() => ({
48 define: { "import.meta.official": JSON.stringify(true) },
49 }));
50 const officialResolved = vi.fn();
51 const official: vite.Plugin = {
52 name: OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME,
53 enforce: "pre",
54 config: officialConfig,
55 configResolved: officialResolved,
56 };
57
58 let seenByLaterConfig = false;
59 const scanner: vite.Plugin = {
60 name: "vinext-style-scan",
61 enforce: "pre",
62 config(config) {
63 seenByLaterConfig = hasCloudflarePlugin(flattenPlugins(config.plugins ?? []));
64 },
65 };
66
67 const resolved = await vite.resolveConfig(
68 {
69 configFile: false,
70 root,
71 logLevel: "silent",
72 plugins: [official, scanner, cloudflareVitePlugin({ main: "./worker-entry.ts" })],
73 },
74 "serve",
75 );
76
77 // The root plugin still resolves its own config (running the user's
78 // customizer), but what it returns is discarded.
79 expect(officialConfig).toHaveBeenCalledTimes(1);
80 expect(officialResolved).not.toHaveBeenCalled();
81 expect(seenByLaterConfig).toBe(true);
82 expect(resolved.define?.["import.meta.official"]).toBeUndefined();
83 expect(
84 resolved.plugins.some((plugin) => plugin.name === ALCHEMY_CLOUDFLARE_VITE_PLUGIN_NAME),
85 ).toBe(true);
86 });
87
88 it("disables nested official child plugins by object identity", async () => {
89 const childConfig = vi.fn();
90 const child: vite.Plugin = {
91 name: `${OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME}:compat`,
92 config: childConfig,
93 };
94 const disabled = await disableOfficialCloudflareVitePlugins([[child]]);
95 expect(disabled).toBe(1);
96 void (child.config as () => void)();
97 expect(childConfig).not.toHaveBeenCalled();
98 });
99
100 it("keeps the official root plugin's config resolution, which runs the user's customizer", async () => {
101 const customizer = vi.fn();
102 const official: vite.Plugin = {
103 name: OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME,
104 config: () => customizer(),
105 configureServer: () => {},
106 };
107 const failing: vite.Plugin = {
108 name: OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME,
109 config: () => {
110 throw new Error("no wrangler config");
111 },
112 };
113 expect(await disableOfficialCloudflareVitePlugins([official, failing])).toBe(2);
114 await (official.config as () => Promise<void>)();
115 expect(customizer).toHaveBeenCalledTimes(1);
116 await expect((failing.config as () => Promise<void>)()).resolves.toBeUndefined();
117 });
118
119 it("disables official plugins an async plugin factory resolves to", async () => {
120 const configureServer = vi.fn();
121 const applyToEnvironment = vi.fn(() => true);
122 const official: vite.Plugin = {
123 name: OFFICIAL_CLOUDFLARE_VITE_PLUGIN_NAME,
124 configureServer,
125 applyToEnvironment,
126 };
127 const disabled = await disableOfficialCloudflareVitePlugins([
128 Promise.resolve([{ name: "framework" }, official]),
129 ]);
130 expect(disabled).toBe(1);
131 void (official.configureServer as () => void)();
132 expect(configureServer).not.toHaveBeenCalled();
133 expect((official.applyToEnvironment as () => boolean)()).toBe(false);
134 expect(applyToEnvironment).not.toHaveBeenCalled();
135 });
136 });