@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, 04:45 UTC

main
ts 236 lines 7.02 KB
Raw
1 import type { Adapter } from "@sveltejs/kit";
2 import type * as ViteModule from "vite";
3 import { describe, expect, it } from "vitest";
4 import {
5 CONFIG_PLUGIN_NAME,
6 DEFAULT_VITE_CONFIG_FILES,
7 flattenPluginOption,
8 makeSvelteKitConfigPlugin,
9 mergeKitOptions,
10 SVELTEKIT_SETUP_PLUGIN_NAME,
11 } from "../UserConfig.ts";
12
13 const makeAdapter = (name: string): Adapter => ({
14 name,
15 adapt: async () => {},
16 });
17
18 /**
19 * A stand-in for the current SvelteKit `vite-plugin-sveltekit-setup` shape,
20 * which exposes the validated Kit config directly through `api.options`.
21 */
22 const makeSetupPlugin = (kit: Record<string, unknown>) =>
23 ({
24 name: SVELTEKIT_SETUP_PLUGIN_NAME,
25 api: { options: kit },
26 }) as ViteModule.Plugin;
27
28 /**
29 * A stand-in for the legacy setup-plugin shape, where the validated Kit
30 * config was nested under `api.options.kit`.
31 */
32 const makeLegacySetupPlugin = (kit: Record<string, unknown>) =>
33 ({
34 name: SVELTEKIT_SETUP_PLUGIN_NAME,
35 api: { options: { kit } },
36 }) as ViteModule.Plugin;
37
38 const configEnv: ViteModule.ConfigEnv = {
39 command: "build",
40 mode: "production",
41 };
42
43 /** Run the injector's config hook the way Vite would. */
44 const runConfigHook = async (
45 plugin: ViteModule.Plugin,
46 config: ViteModule.UserConfig,
47 ): Promise<void> => {
48 const hook = plugin.config as {
49 order?: string;
50 handler: (config: ViteModule.UserConfig, env: ViteModule.ConfigEnv) => Promise<unknown>;
51 };
52 await hook.handler(config, configEnv);
53 };
54
55 describe("flattenPluginOption", () => {
56 it("flattens nested arrays, promises, and falsy holes", async () => {
57 const a = { name: "a" } as ViteModule.Plugin;
58 const b = { name: "b" } as ViteModule.Plugin;
59 const c = { name: "c" } as ViteModule.Plugin;
60 const flattened = await flattenPluginOption([
61 a,
62 false,
63 Promise.resolve([b, undefined, [null, Promise.resolve(c)]]),
64 ]);
65 expect(flattened.map((plugin) => plugin.name)).toEqual(["a", "b", "c"]);
66 });
67
68 it("returns an empty list for an absent option", async () => {
69 expect(await flattenPluginOption(undefined)).toEqual([]);
70 });
71 });
72
73 describe("mergeKitOptions", () => {
74 it("merges nested plain objects key-by-key and replaces everything else", () => {
75 const target: Record<string, unknown> = {
76 alias: { $lib: "src/lib" },
77 prerender: { entries: ["*"], concurrency: 1 },
78 appDir: "_app",
79 };
80 mergeKitOptions(target, {
81 alias: { $extra: "src/extra" },
82 prerender: { entries: ["/about"] },
83 appDir: "custom",
84 ignored: undefined,
85 });
86 expect(target["alias"]).toEqual({
87 $lib: "src/lib",
88 $extra: "src/extra",
89 });
90 expect(target["prerender"]).toEqual({
91 entries: ["/about"],
92 concurrency: 1,
93 });
94 expect(target["appDir"]).toBe("custom");
95 expect("ignored" in target).toBe(false);
96 });
97 });
98
99 describe("makeSvelteKitConfigPlugin", () => {
100 it("is pre-enforced with a pre-ordered config hook (must run before kit's setup hook)", () => {
101 const plugin = makeSvelteKitConfigPlugin({
102 adapter: makeAdapter("target-adapter"),
103 });
104 expect(plugin.name).toBe(CONFIG_PLUGIN_NAME);
105 expect(plugin.enforce).toBe("pre");
106 expect((plugin.config as { order?: string }).order).toBe("pre");
107 });
108
109 it("injects the deploy target's adapter into the user's sveltekit() config", async () => {
110 const adapter = makeAdapter("target-adapter");
111 const kit: Record<string, unknown> = {};
112 const warnings: Array<string> = [];
113 const plugin = makeSvelteKitConfigPlugin({
114 adapter,
115 warn: (m) => warnings.push(m),
116 });
117
118 // sveltekit() returns Promise<Plugin[]> — mirror that shape
119 await runConfigHook(plugin, {
120 plugins: [Promise.resolve([makeSetupPlugin(kit)])],
121 });
122
123 expect(kit["adapter"]).toBe(adapter);
124 expect(warnings).toEqual([]);
125 });
126
127 it("supports the legacy nested api.options.kit shape", async () => {
128 const adapter = makeAdapter("target-adapter");
129 const kit: Record<string, unknown> = {};
130 const plugin = makeSvelteKitConfigPlugin({ adapter });
131
132 await runConfigHook(plugin, {
133 plugins: [makeLegacySetupPlugin(kit)],
134 });
135
136 expect(kit["adapter"]).toBe(adapter);
137 });
138
139 it("replaces a user-declared adapter and warns", async () => {
140 const adapter = makeAdapter("target-adapter");
141 const kit: Record<string, unknown> = {
142 adapter: makeAdapter("user-adapter"),
143 };
144 const warnings: Array<string> = [];
145 const plugin = makeSvelteKitConfigPlugin({
146 adapter,
147 warn: (m) => warnings.push(m),
148 });
149 await runConfigHook(plugin, {
150 plugins: [[makeSetupPlugin(kit)]],
151 });
152 expect(kit["adapter"]).toBe(adapter);
153 expect(warnings).toHaveLength(1);
154 expect(warnings[0]).toContain('"user-adapter"');
155 expect(warnings[0]).toContain("deploy target");
156 });
157
158 it("merges integration kit options over the user's (integration wins)", async () => {
159 const adapter = makeAdapter("target-adapter");
160 const kit: Record<string, unknown> = {
161 alias: { $lib: "src/lib" },
162 appDir: "_app",
163 };
164 const plugin = makeSvelteKitConfigPlugin({
165 adapter,
166 kit: {
167 alias: { $fixture: "src/fixture" },
168 appDir: "custom",
169 },
170 });
171 await runConfigHook(plugin, {
172 plugins: [makeSetupPlugin(kit)],
173 });
174 expect(kit["alias"]).toEqual({
175 $lib: "src/lib",
176 $fixture: "src/fixture",
177 });
178 expect(kit["appDir"]).toBe("custom");
179 expect(kit["adapter"]).toBe(adapter);
180 });
181
182 it("warns for construction-time options it cannot apply late", async () => {
183 const adapter = makeAdapter("target-adapter");
184 const kit: Record<string, unknown> = {};
185 const warnings: Array<string> = [];
186 const plugin = makeSvelteKitConfigPlugin({
187 adapter,
188 kit: {
189 preprocess: [],
190 alias: { $x: "src/x" },
191 },
192 warn: (m) => warnings.push(m),
193 });
194 await runConfigHook(plugin, {
195 plugins: [makeSetupPlugin(kit)],
196 });
197 expect(warnings).toHaveLength(1);
198 expect(warnings[0]).toContain('"preprocess"');
199 expect(kit["alias"]).toEqual({ $x: "src/x" });
200 });
201
202 it("fails clearly when the Vite config has no sveltekit() plugin", async () => {
203 const plugin = makeSvelteKitConfigPlugin({
204 adapter: makeAdapter("target-adapter"),
205 });
206 await expect(
207 runConfigHook(plugin, {
208 plugins: [{ name: "some-other-plugin" }],
209 }),
210 ).rejects.toThrow(/does not register the SvelteKit plugin/);
211 });
212
213 it("fails clearly when the setup plugin does not expose its options", async () => {
214 const plugin = makeSvelteKitConfigPlugin({
215 adapter: makeAdapter("target-adapter"),
216 });
217 await expect(
218 runConfigHook(plugin, {
219 plugins: [{ name: SVELTEKIT_SETUP_PLUGIN_NAME }],
220 }),
221 ).rejects.toThrow(/api\.options/);
222 });
223 });
224
225 describe("DEFAULT_VITE_CONFIG_FILES", () => {
226 it("mirrors Vite's default config discovery list", () => {
227 expect(DEFAULT_VITE_CONFIG_FILES).toEqual([
228 "vite.config.js",
229 "vite.config.mjs",
230 "vite.config.ts",
231 "vite.config.cjs",
232 "vite.config.mts",
233 "vite.config.cts",
234 ]);
235 });
236 });