@sigil-dev/grimoire 0.9.4 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/index.ts +37 -37
  2. package/package.json +3 -3
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +36 -36
  6. package/src/client/enhance.ts +102 -102
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +5 -5
  9. package/src/client/router.ts +42 -20
  10. package/src/client/scope.ts +22 -22
  11. package/src/dev/compile-module.ts +215 -163
  12. package/src/dev/effect-registry.ts +23 -23
  13. package/src/dev/graph.ts +109 -114
  14. package/src/dev/hmr-client.ts +202 -185
  15. package/src/dev/hmr-server.ts +261 -187
  16. package/src/dev/loader.ts +120 -47
  17. package/src/dev/paths.ts +14 -14
  18. package/src/dev/resolve.ts +70 -0
  19. package/src/dev/runtime-bundle.ts +52 -52
  20. package/src/dev/scan.ts +78 -0
  21. package/src/dev/watcher.ts +69 -47
  22. package/src/env/dynamic-private.ts +19 -19
  23. package/src/env/dynamic-public.ts +22 -22
  24. package/src/env/index.ts +25 -25
  25. package/src/env/plugin.ts +19 -19
  26. package/src/env/private.ts +5 -5
  27. package/src/env/public.ts +7 -7
  28. package/src/env/typegen.ts +61 -61
  29. package/src/headers.ts +114 -114
  30. package/src/integrations/vite.ts +73 -73
  31. package/src/logger/index.ts +4 -4
  32. package/src/logger/instance.ts +6 -6
  33. package/src/logger/logger.ts +113 -113
  34. package/src/logger/request-middleware.ts +24 -24
  35. package/src/logger/transports.ts +117 -117
  36. package/src/logger/types.ts +39 -39
  37. package/src/rendering/head.ts +52 -47
  38. package/src/rendering/hydrate.ts +16 -3
  39. package/src/rendering/index.ts +10 -6
  40. package/src/rendering/ssrPlugin.ts +78 -69
  41. package/src/routing/manifest-gen.ts +55 -55
  42. package/src/routing/router.ts +135 -135
  43. package/src/routing/scanner.ts +196 -187
  44. package/src/routing/transform-routes.ts +102 -102
  45. package/src/sentinels/error.ts +52 -52
  46. package/src/sentinels/fail.ts +41 -41
  47. package/src/sentinels/redirect.ts +35 -35
  48. package/src/server/build.ts +250 -255
  49. package/src/server/context.ts +10 -10
  50. package/src/server/cookie-utils.ts +66 -66
  51. package/src/server/coordinator.ts +364 -364
  52. package/src/server/hooks.ts +114 -114
  53. package/src/server/index.ts +711 -677
  54. package/src/server/interactive.ts +56 -56
  55. package/src/server/plugins.ts +121 -121
  56. package/src/server/worker.ts +58 -58
  57. package/src/typegen/index.ts +389 -389
  58. package/src/types.ts +286 -286
  59. package/test/client-router.test.ts +295 -298
  60. package/test/context.test.ts +52 -52
  61. package/test/env.test.ts +90 -90
  62. package/test/exports.test.ts +37 -37
  63. package/test/fail.test.ts +46 -46
  64. package/test/headers.test.ts +100 -100
  65. package/test/hmr.test.ts +426 -0
  66. package/test/hydration.test.ts +119 -119
  67. package/test/middleware.test.ts +223 -223
  68. package/test/preload.ts +5 -5
  69. package/test/redirect-error.test.ts +112 -112
  70. package/test/rendering.test.ts +719 -728
  71. package/test/routing.test.ts +197 -194
  72. package/test/scanning.test.ts +314 -254
  73. package/test/scope.test.ts +195 -195
  74. package/test/server.test.ts +360 -354
  75. package/test/streaming.test.ts +242 -242
  76. package/test/transform-routes.test.ts +87 -87
  77. package/test/typegen.test.ts +697 -697
  78. package/tsconfig.json +10 -10
@@ -1,298 +1,295 @@
1
- import { beforeEach, describe, expect, test, vi } from "bun:test";
2
- import type { Document, History, Location, Window } from "happy-dom";
3
- import {
4
- clearDeps,
5
- depends,
6
- hasDep,
7
- setDeps,
8
- startCollecting,
9
- stopCollecting,
10
- } from "../src/client/deps.ts";
11
-
12
- // test/preload.ts installs happy-dom globals at runtime; this package's
13
- // compiler options have no DOM lib, so declare the shapes we rely on.
14
- declare global {
15
- // biome-ignore-all lint/style/noVar: ambient global declarations require var
16
- var window: Window;
17
- var document: Document;
18
- var location: Location;
19
- var history: History;
20
- var MouseEvent: Window["MouseEvent"];
21
- var Event: Window["Event"];
22
- var requestAnimationFrame: Window["requestAnimationFrame"];
23
- }
24
-
25
- // router.ts reads `location` at module load — DOM globals must exist first.
26
- // Dynamic import is required because the module under test captures location
27
- // during evaluation, which static-hoisted imports would race. The default
28
- // about:blank URL has origin "null", which breaks new URL(href, origin) —
29
- // point the window at a real origin first.
30
- window.location.href = "http://localhost/";
31
- const globalEnv = globalThis as typeof globalThis & {
32
- location: Location;
33
- history: History;
34
- requestAnimationFrame: Window["requestAnimationFrame"];
35
- };
36
- globalEnv.location = window.location;
37
- globalEnv.history = window.history;
38
- globalEnv.requestAnimationFrame = window.requestAnimationFrame.bind(window);
39
- const router = await import("../src/client/router.ts");
40
-
41
- function jsonResponse(pattern: string): Response {
42
- return new Response(
43
- JSON.stringify({
44
- data: {},
45
- layoutData: [],
46
- params: {},
47
- pattern,
48
- head: "",
49
- }),
50
- { headers: { "content-type": "application/json" } },
51
- );
52
- }
53
-
54
- function captureFetch(): {
55
- calls: Array<{ input: string; init?: RequestInit }>;
56
- restore: () => void;
57
- } {
58
- const calls: Array<{ input: string; init?: RequestInit }> = [];
59
- const original = globalThis.fetch;
60
- // test shim over the global — precise fetch typing is impossible here.
61
- // Echoes the requested pathname as the route pattern, mirroring renderRoute.
62
- globalThis.fetch = (async (
63
- input: string,
64
- init?: RequestInit,
65
- ): Promise<Response> => {
66
- calls.push({ input, init });
67
- const pattern = new URL(input, "http://localhost").pathname;
68
- return jsonResponse(pattern);
69
- }) as unknown as typeof fetch;
70
- return {
71
- calls,
72
- restore: () => {
73
- globalThis.fetch = original;
74
- },
75
- };
76
- }
77
-
78
- describe("depends", () => {
79
- test("no-op outside collection", () => {
80
- depends("posts");
81
- expect(hasDep("/", "posts")).toBe(false);
82
- });
83
-
84
- test("registers key during collection", () => {
85
- startCollecting();
86
- depends("posts");
87
- stopCollecting("/");
88
- expect(hasDep("/", "posts")).toBe(true);
89
- });
90
-
91
- test("multiple keys", () => {
92
- startCollecting();
93
- depends("posts");
94
- depends("tags");
95
- stopCollecting("/");
96
- expect(hasDep("/", "posts")).toBe(true);
97
- expect(hasDep("/", "tags")).toBe(true);
98
- });
99
-
100
- test("cleared on next collection", () => {
101
- startCollecting();
102
- depends("posts");
103
- stopCollecting("/");
104
- startCollecting();
105
- depends("comments");
106
- stopCollecting("/");
107
- expect(hasDep("/", "posts")).toBe(false);
108
- expect(hasDep("/", "comments")).toBe(true);
109
- });
110
- });
111
- describe("navigate — view transitions", () => {
112
- test("falls back to direct swap when startViewTransition is missing", async () => {
113
- const root = document.createElement("div");
114
- root.id = "grimoire-root";
115
- document.body.appendChild(root);
116
- const fetchShim = captureFetch();
117
- try {
118
- router.initRouter({ "/": () => document.createElement("main") }, []);
119
- await router.navigate("/");
120
- expect(root.childNodes.length).toBeGreaterThan(0);
121
- } finally {
122
- fetchShim.restore();
123
- root.remove();
124
- }
125
- });
126
-
127
- test("routes the swap through startViewTransition when available", async () => {
128
- const root = document.createElement("div");
129
- root.id = "grimoire-root";
130
- document.body.appendChild(root);
131
- let vtCalled = false;
132
- const docEnv = document as Document & {
133
- startViewTransition?: (cb: () => void) => unknown;
134
- };
135
- docEnv.startViewTransition = (cb: () => void) => {
136
- vtCalled = true;
137
- cb();
138
- return undefined;
139
- };
140
- const fetchShim = captureFetch();
141
- try {
142
- router.initRouter({ "/vt": () => document.createElement("main") }, []);
143
- await router.navigate("/vt");
144
- expect(vtCalled).toBe(true);
145
- expect(root.childNodes.length).toBeGreaterThan(0);
146
- } finally {
147
- delete docEnv.startViewTransition;
148
- fetchShim.restore();
149
- root.remove();
150
- }
151
- });
152
- });
153
-
154
- describe("hover prefetch", () => {
155
- // The 80ms debounce is real timer logic under test — fake timers advance
156
- // it deterministically instead of sleeping past it.
157
- test("plain internal link prefetches route data; preload=off skips", () => {
158
- vi.useFakeTimers();
159
- router.initRouter(
160
- {
161
- "/": () => document.createElement("div"),
162
- "/off": () => document.createElement("div"),
163
- },
164
- [],
165
- );
166
- const fetchShim = captureFetch();
167
- try {
168
- const plain = document.createElement("a");
169
- plain.setAttribute("href", "/");
170
- document.body.appendChild(plain);
171
- plain.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
172
- vi.advanceTimersByTime(80);
173
- expect(fetchShim.calls.length).toBe(1);
174
- expect(fetchShim.calls[0]?.init?.headers).toEqual({
175
- "x-grimoire-navigate": "1",
176
- });
177
-
178
- const optedOut = document.createElement("a");
179
- optedOut.setAttribute("href", "/off");
180
- optedOut.setAttribute("data-sigil-preload", "off");
181
- document.body.appendChild(optedOut);
182
- optedOut.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
183
- vi.advanceTimersByTime(80);
184
- expect(fetchShim.calls.length).toBe(1); // unchanged
185
- } finally {
186
- vi.useRealTimers();
187
- fetchShim.restore();
188
- }
189
- });
190
-
191
- test("external links are never prefetched", () => {
192
- vi.useFakeTimers();
193
- const fetchShim = captureFetch();
194
- try {
195
- const ext = document.createElement("a");
196
- ext.setAttribute("href", "https://example.com/page");
197
- document.body.appendChild(ext);
198
- ext.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
199
- vi.advanceTimersByTime(80);
200
- expect(fetchShim.calls.length).toBe(0);
201
- } finally {
202
- vi.useRealTimers();
203
- fetchShim.restore();
204
- }
205
- });
206
- });
207
-
208
- describe("enhance — named action URLs", () => {
209
- test("action starting with ?/ submits to the current pathname", async () => {
210
- // earlier navigate() tests moved history — reset to a clean path
211
- window.history.pushState(null, "", "/");
212
- const { enhance } = await import("../src/client/enhance.ts");
213
- const calls: string[] = [];
214
- const original = globalThis.fetch;
215
- globalThis.fetch = (async (input: string) => {
216
- calls.push(input);
217
- return new Response(null, { status: 303, headers: { location: "/" } });
218
- }) as unknown as typeof fetch;
219
-
220
- const form = document.createElement("form");
221
- document.body.appendChild(form);
222
- const cleanup = enhance(form, { action: "?/create" });
223
- try {
224
- form.dispatchEvent(
225
- new Event("submit", { bubbles: true, cancelable: true }),
226
- );
227
- await new Promise((r) => setTimeout(r, 0));
228
- expect(calls).toEqual([`${window.location.pathname}?/create`]);
229
- } finally {
230
- cleanup();
231
- form.remove();
232
- globalThis.fetch = original;
233
- }
234
- });
235
- });
236
-
237
- describe("Page.load — parent()", () => {
238
- test("parent() merges layoutData entries outermost-first", async () => {
239
- let seenParent: Record<string, unknown> | undefined;
240
- const PageWithLoad = Object.assign(
241
- () => document.createElement("div"),
242
- {
243
- load: async (ctx: { parent: () => Promise<Record<string, unknown>> }) => {
244
- seenParent = await ctx.parent();
245
- return {};
246
- },
247
- },
248
- );
249
-
250
- const original = globalThis.fetch;
251
- globalThis.fetch = (async (input: string) => {
252
- const pattern = new URL(input, "http://localhost").pathname;
253
- return new Response(
254
- JSON.stringify({
255
- data: {},
256
- layoutData: [{ a: 1 }, { b: 2 }],
257
- params: {},
258
- pattern,
259
- head: "",
260
- }),
261
- { headers: { "content-type": "application/json" } },
262
- );
263
- }) as unknown as typeof fetch;
264
-
265
- try {
266
- router.initRouter({ "/pl": PageWithLoad }, []);
267
- await router.navigate("/pl");
268
- expect(seenParent).toEqual({ a: 1, b: 2 });
269
- } finally {
270
- globalThis.fetch = original;
271
- }
272
- });
273
- });
274
-
275
- describe("invalidate", () => {
276
- test("refetches only on key intersection; empty deps always refetch", async () => {
277
- router.initRouter({ "/": () => document.createElement("div") }, []);
278
- const shim = captureFetch();
279
- try {
280
- await router.navigate("/"); // establishes lastState.pattern = "/"
281
-
282
- setDeps("/", ["user:1"]);
283
- shim.calls.length = 0;
284
- await router.invalidate("other");
285
- expect(shim.calls.length).toBe(0); // no intersection → skip
286
-
287
- await router.invalidate("user:1");
288
- expect(shim.calls.length).toBe(1); // intersect → refetch
289
-
290
- clearDeps("/");
291
- await router.invalidate("anything");
292
- expect(shim.calls.length).toBe(2); // no deps → conservative refetch
293
- } finally {
294
- shim.restore();
295
- clearDeps("/");
296
- }
297
- });
298
- });
1
+ import { beforeEach, describe, expect, test, vi } from "bun:test";
2
+ import type { Document, History, Location, Window } from "happy-dom";
3
+ import {
4
+ clearDeps,
5
+ depends,
6
+ hasDep,
7
+ setDeps,
8
+ startCollecting,
9
+ stopCollecting,
10
+ } from "../src/client/deps.ts";
11
+
12
+ // test/preload.ts installs happy-dom globals at runtime; this package's
13
+ // compiler options have no DOM lib, so declare the shapes we rely on.
14
+ declare global {
15
+ // biome-ignore-all lint/style/noVar: ambient global declarations require var
16
+ var window: Window;
17
+ var document: Document;
18
+ var location: Location;
19
+ var history: History;
20
+ var MouseEvent: Window["MouseEvent"];
21
+ var Event: Window["Event"];
22
+ var requestAnimationFrame: Window["requestAnimationFrame"];
23
+ }
24
+
25
+ // router.ts reads `location` at module load — DOM globals must exist first.
26
+ // Dynamic import is required because the module under test captures location
27
+ // during evaluation, which static-hoisted imports would race. The default
28
+ // about:blank URL has origin "null", which breaks new URL(href, origin) —
29
+ // point the window at a real origin first.
30
+ window.location.href = "http://localhost/";
31
+ const globalEnv = globalThis as typeof globalThis & {
32
+ location: Location;
33
+ history: History;
34
+ requestAnimationFrame: Window["requestAnimationFrame"];
35
+ };
36
+ globalEnv.location = window.location;
37
+ globalEnv.history = window.history;
38
+ globalEnv.requestAnimationFrame = window.requestAnimationFrame.bind(window);
39
+ const router = await import("../src/client/router.ts");
40
+
41
+ function jsonResponse(pattern: string): Response {
42
+ return new Response(
43
+ JSON.stringify({
44
+ data: {},
45
+ layoutData: [],
46
+ params: {},
47
+ pattern,
48
+ head: "",
49
+ }),
50
+ { headers: { "content-type": "application/json" } },
51
+ );
52
+ }
53
+
54
+ function captureFetch(): {
55
+ calls: Array<{ input: string; init?: RequestInit }>;
56
+ restore: () => void;
57
+ } {
58
+ const calls: Array<{ input: string; init?: RequestInit }> = [];
59
+ const original = globalThis.fetch;
60
+ // test shim over the global — precise fetch typing is impossible here.
61
+ // Echoes the requested pathname as the route pattern, mirroring renderRoute.
62
+ globalThis.fetch = (async (
63
+ input: string,
64
+ init?: RequestInit,
65
+ ): Promise<Response> => {
66
+ calls.push({ input, init });
67
+ const pattern = new URL(input, "http://localhost").pathname;
68
+ return jsonResponse(pattern);
69
+ }) as unknown as typeof fetch;
70
+ return {
71
+ calls,
72
+ restore: () => {
73
+ globalThis.fetch = original;
74
+ },
75
+ };
76
+ }
77
+
78
+ describe("depends", () => {
79
+ test("no-op outside collection", () => {
80
+ depends("posts");
81
+ expect(hasDep("/", "posts")).toBe(false);
82
+ });
83
+
84
+ test("registers key during collection", () => {
85
+ startCollecting();
86
+ depends("posts");
87
+ stopCollecting("/");
88
+ expect(hasDep("/", "posts")).toBe(true);
89
+ });
90
+
91
+ test("multiple keys", () => {
92
+ startCollecting();
93
+ depends("posts");
94
+ depends("tags");
95
+ stopCollecting("/");
96
+ expect(hasDep("/", "posts")).toBe(true);
97
+ expect(hasDep("/", "tags")).toBe(true);
98
+ });
99
+
100
+ test("cleared on next collection", () => {
101
+ startCollecting();
102
+ depends("posts");
103
+ stopCollecting("/");
104
+ startCollecting();
105
+ depends("comments");
106
+ stopCollecting("/");
107
+ expect(hasDep("/", "posts")).toBe(false);
108
+ expect(hasDep("/", "comments")).toBe(true);
109
+ });
110
+ });
111
+ describe("navigate — view transitions", () => {
112
+ test("falls back to direct swap when startViewTransition is missing", async () => {
113
+ const root = document.createElement("div");
114
+ root.id = "grimoire-root";
115
+ document.body.appendChild(root);
116
+ const fetchShim = captureFetch();
117
+ try {
118
+ router.initRouter({ "/": () => document.createElement("main") }, []);
119
+ await router.navigate("/");
120
+ expect(root.childNodes.length).toBeGreaterThan(0);
121
+ } finally {
122
+ fetchShim.restore();
123
+ root.remove();
124
+ }
125
+ });
126
+
127
+ test("routes the swap through startViewTransition when available", async () => {
128
+ const root = document.createElement("div");
129
+ root.id = "grimoire-root";
130
+ document.body.appendChild(root);
131
+ let vtCalled = false;
132
+ const docEnv = document as Document & {
133
+ startViewTransition?: (cb: () => void) => unknown;
134
+ };
135
+ docEnv.startViewTransition = (cb: () => void) => {
136
+ vtCalled = true;
137
+ cb();
138
+ return undefined;
139
+ };
140
+ const fetchShim = captureFetch();
141
+ try {
142
+ router.initRouter({ "/vt": () => document.createElement("main") }, []);
143
+ await router.navigate("/vt");
144
+ expect(vtCalled).toBe(true);
145
+ expect(root.childNodes.length).toBeGreaterThan(0);
146
+ } finally {
147
+ delete docEnv.startViewTransition;
148
+ fetchShim.restore();
149
+ root.remove();
150
+ }
151
+ });
152
+ });
153
+
154
+ describe("hover prefetch", () => {
155
+ // The 80ms debounce is real timer logic under test — fake timers advance
156
+ // it deterministically instead of sleeping past it.
157
+ test("plain internal link prefetches route data; preload=off skips", () => {
158
+ vi.useFakeTimers();
159
+ router.initRouter(
160
+ {
161
+ "/": () => document.createElement("div"),
162
+ "/off": () => document.createElement("div"),
163
+ },
164
+ [],
165
+ );
166
+ const fetchShim = captureFetch();
167
+ try {
168
+ const plain = document.createElement("a");
169
+ plain.setAttribute("href", "/");
170
+ document.body.appendChild(plain);
171
+ plain.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
172
+ vi.advanceTimersByTime(80);
173
+ expect(fetchShim.calls.length).toBe(1);
174
+ expect(fetchShim.calls[0]?.init?.headers).toEqual({
175
+ "x-grimoire-navigate": "1",
176
+ });
177
+
178
+ const optedOut = document.createElement("a");
179
+ optedOut.setAttribute("href", "/off");
180
+ optedOut.setAttribute("data-sigil-preload", "off");
181
+ document.body.appendChild(optedOut);
182
+ optedOut.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
183
+ vi.advanceTimersByTime(80);
184
+ expect(fetchShim.calls.length).toBe(1); // unchanged
185
+ } finally {
186
+ vi.useRealTimers();
187
+ fetchShim.restore();
188
+ }
189
+ });
190
+
191
+ test("external links are never prefetched", () => {
192
+ vi.useFakeTimers();
193
+ const fetchShim = captureFetch();
194
+ try {
195
+ const ext = document.createElement("a");
196
+ ext.setAttribute("href", "https://example.com/page");
197
+ document.body.appendChild(ext);
198
+ ext.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
199
+ vi.advanceTimersByTime(80);
200
+ expect(fetchShim.calls.length).toBe(0);
201
+ } finally {
202
+ vi.useRealTimers();
203
+ fetchShim.restore();
204
+ }
205
+ });
206
+ });
207
+
208
+ describe("enhance — named action URLs", () => {
209
+ test("action starting with ?/ submits to the current pathname", async () => {
210
+ // earlier navigate() tests moved history — reset to a clean path
211
+ window.history.pushState(null, "", "/");
212
+ const { enhance } = await import("../src/client/enhance.ts");
213
+ const calls: string[] = [];
214
+ const original = globalThis.fetch;
215
+ globalThis.fetch = (async (input: string) => {
216
+ calls.push(input);
217
+ return new Response(null, { status: 303, headers: { location: "/" } });
218
+ }) as unknown as typeof fetch;
219
+
220
+ const form = document.createElement("form");
221
+ document.body.appendChild(form);
222
+ const cleanup = enhance(form, { action: "?/create" });
223
+ try {
224
+ form.dispatchEvent(
225
+ new Event("submit", { bubbles: true, cancelable: true }),
226
+ );
227
+ await new Promise((r) => setTimeout(r, 0));
228
+ expect(calls).toEqual([`${window.location.pathname}?/create`]);
229
+ } finally {
230
+ cleanup();
231
+ form.remove();
232
+ globalThis.fetch = original;
233
+ }
234
+ });
235
+ });
236
+
237
+ describe("Page.load — parent()", () => {
238
+ test("parent() merges layoutData entries outermost-first", async () => {
239
+ let seenParent: Record<string, unknown> | undefined;
240
+ const PageWithLoad = Object.assign(() => document.createElement("div"), {
241
+ load: async (ctx: { parent: () => Promise<Record<string, unknown>> }) => {
242
+ seenParent = await ctx.parent();
243
+ return {};
244
+ },
245
+ });
246
+
247
+ const original = globalThis.fetch;
248
+ globalThis.fetch = (async (input: string) => {
249
+ const pattern = new URL(input, "http://localhost").pathname;
250
+ return new Response(
251
+ JSON.stringify({
252
+ data: {},
253
+ layoutData: [{ a: 1 }, { b: 2 }],
254
+ params: {},
255
+ pattern,
256
+ head: "",
257
+ }),
258
+ { headers: { "content-type": "application/json" } },
259
+ );
260
+ }) as unknown as typeof fetch;
261
+
262
+ try {
263
+ router.initRouter({ "/pl": PageWithLoad }, []);
264
+ await router.navigate("/pl");
265
+ expect(seenParent).toEqual({ a: 1, b: 2 });
266
+ } finally {
267
+ globalThis.fetch = original;
268
+ }
269
+ });
270
+ });
271
+
272
+ describe("invalidate", () => {
273
+ test("refetches only on key intersection; empty deps always refetch", async () => {
274
+ router.initRouter({ "/": () => document.createElement("div") }, []);
275
+ const shim = captureFetch();
276
+ try {
277
+ await router.navigate("/"); // establishes lastState.pattern = "/"
278
+
279
+ setDeps("/", ["user:1"]);
280
+ shim.calls.length = 0;
281
+ await router.invalidate("other");
282
+ expect(shim.calls.length).toBe(0); // no intersection → skip
283
+
284
+ await router.invalidate("user:1");
285
+ expect(shim.calls.length).toBe(1); // intersect → refetch
286
+
287
+ clearDeps("/");
288
+ await router.invalidate("anything");
289
+ expect(shim.calls.length).toBe(2); // no deps → conservative refetch
290
+ } finally {
291
+ shim.restore();
292
+ clearDeps("/");
293
+ }
294
+ });
295
+ });