@sigil-dev/grimoire 0.9.3 → 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 -35
  2. package/package.json +9 -12
  3. package/preload.js +3 -3
  4. package/server.ts +19 -19
  5. package/src/client/deps.ts +36 -19
  6. package/src/client/enhance.ts +102 -98
  7. package/src/client/head.ts +30 -30
  8. package/src/client/index.ts +2 -2
  9. package/src/client/router.ts +174 -68
  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 +159 -138
  39. package/src/rendering/index.ts +140 -18
  40. package/src/rendering/ssrPlugin.ts +78 -69
  41. package/src/routing/manifest-gen.ts +13 -1
  42. package/src/routing/router.ts +135 -109
  43. package/src/routing/scanner.ts +196 -141
  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 -243
  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 -655
  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 -356
  58. package/src/types.ts +286 -282
  59. package/test/client-router.test.ts +295 -44
  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 -615
  71. package/test/routing.test.ts +197 -81
  72. package/test/scanning.test.ts +314 -200
  73. package/test/scope.test.ts +195 -195
  74. package/test/server.test.ts +360 -249
  75. package/test/streaming.test.ts +242 -125
  76. package/test/transform-routes.test.ts +87 -84
  77. package/test/typegen.test.ts +697 -682
  78. package/tsconfig.json +10 -10
@@ -1,615 +1,719 @@
1
- import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
- import { mkdir, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir as osTmpDir } from "node:os";
4
- import { join } from "node:path";
5
- import { renderRoute } from "../src/rendering";
6
- import { matchRoute } from "../src/routing/router";
7
- import { scanRoutes } from "../src/routing/scanner";
8
-
9
- /** Read a streaming Response body into a single string */
10
- async function readStream(res: Response): Promise<string> {
11
- const reader = res.body!.getReader();
12
- const parts: string[] = [];
13
- while (true) {
14
- const { done, value } = await reader.read();
15
- if (done) break;
16
- // Bun ReadableStream chunks can be string, Uint8Array, or other
17
- parts.push(String(value));
18
- }
19
- return parts.join("");
20
- }
21
-
22
- let tmpDir: string;
23
-
24
- beforeAll(async () => {
25
- tmpDir = join(osTmpDir(), `grimoire-render-${Date.now()}`);
26
- await mkdir(join(tmpDir, "blog"), { recursive: true });
27
-
28
- // simple static page
29
- await writeFile(
30
- join(tmpDir, "index.tsx"),
31
- `
32
- export default function Home() {
33
- return <h1>Hello Grimoire</h1>;
34
- }
35
- `,
36
- );
37
-
38
- // page with load function
39
- await writeFile(
40
- join(tmpDir, "blog", "+page.server.ts"),
41
- `
42
- export async function load({ params }) {
43
- return { title: 'My Blog' };
44
- }
45
- `,
46
- );
47
-
48
- await writeFile(
49
- join(tmpDir, "blog", "+page.tsx"),
50
- `
51
- export default function Blog({ data }) {
52
- return <h1>{data.title}</h1>;
53
- }
54
- `,
55
- );
56
-
57
- // layout
58
- await writeFile(
59
- join(tmpDir, "+layout.tsx"),
60
- `
61
- export default function Layout({ children }) {
62
- return <div class="app" innerHTML={children}></div>;
63
- }
64
- `,
65
- );
66
-
67
- // page declaring a stylesheet inline (the usual layout pattern)
68
- await mkdir(join(tmpDir, "css-page"), { recursive: true });
69
- await writeFile(
70
- join(tmpDir, "css-page", "+page.tsx"),
71
- `
72
- export default function CssPage() {
73
- return <div><link rel="stylesheet" href="/app.css" /><h1>Styled</h1></div>;
74
- }
75
- `,
76
- );
77
-
78
- // root error page — renders status and message
79
- await writeFile(
80
- join(tmpDir, "+error.tsx"),
81
- `
82
- export default function ErrorPage({ status, message }) {
83
- return <div class="error"><span class="status">{status}</span><span class="msg">{message}</span></div>;
84
- }
85
- `,
86
- );
87
-
88
- // page whose load throws redirect — use inline object so no grimoire import needed from /tmp
89
- await mkdir(join(tmpDir, "redirect-page"), { recursive: true });
90
- await writeFile(
91
- join(tmpDir, "redirect-page", "+page.server.ts"),
92
- `export async function load() { throw { __redirect: true, status: 302, location: "/login" }; }`,
93
- );
94
- await writeFile(
95
- join(tmpDir, "redirect-page", "+page.tsx"),
96
- `export default function P() { return <div>never</div>; }`,
97
- );
98
-
99
- // page whose load throws error — same pattern
100
- await mkdir(join(tmpDir, "error-page"), { recursive: true });
101
- await writeFile(
102
- join(tmpDir, "error-page", "+page.server.ts"),
103
- `export async function load() { throw { __error: true, status: 404, message: "Not found" }; }`,
104
- );
105
- await writeFile(
106
- join(tmpDir, "error-page", "+page.tsx"),
107
- `export default function P() { return <div>never</div>; }`,
108
- );
109
-
110
- // page whose load returns locals.tag (tests locals passthrough)
111
- await mkdir(join(tmpDir, "locals-test"), { recursive: true });
112
- await writeFile(
113
- join(tmpDir, "locals-test", "+page.server.ts"),
114
- `
115
- export async function load({ locals }) { return { tag: locals.tag ?? "none" }; }
116
- `,
117
- );
118
- await writeFile(
119
- join(tmpDir, "locals-test", "+page.tsx"),
120
- `export default function P({ data }) { return <div class="tag">{data.tag}</div>; }`,
121
- );
122
-
123
- // layout.server test — nested layout with server data
124
- await mkdir(join(tmpDir, "dashboard"), { recursive: true });
125
- await writeFile(
126
- join(tmpDir, "dashboard", "+layout.server.ts"),
127
- `export async function load() { return { user: "admin" }; }`,
128
- );
129
- await writeFile(
130
- join(tmpDir, "dashboard", "+layout.tsx"),
131
- `export default function DashLayout({ data, children }) {
132
- return <div class="dash" data-user={data.user} innerHTML={children}></div>;
133
- }`,
134
- );
135
- await writeFile(
136
- join(tmpDir, "dashboard", "index.tsx"),
137
- `export default function Dash() { return <p>dash content</p>; }`,
138
- );
139
-
140
- await mkdir(join(tmpDir, "section", "nested"), { recursive: true });
141
- await writeFile(
142
- join(tmpDir, "section", "+layout.tsx"),
143
- `export default function SectionLayout({ children }) {
144
- return <div class="section" innerHTML={children}></div>;
145
- }`,
146
- );
147
- await writeFile(
148
- join(tmpDir, "section", "nested", "+page.tsx"),
149
- `export default function NestedPage() { return <p class="nested-content">nested</p>; }`,
150
- );
151
-
152
- // triple nesting — root + section + deep
153
- await mkdir(join(tmpDir, "section", "deep", "page"), { recursive: true });
154
- await writeFile(
155
- join(tmpDir, "section", "deep", "+layout.tsx"),
156
- `export default function DeepLayout({ children }) {
157
- return <div class="deep" innerHTML={children}></div>;
158
- }`,
159
- );
160
- await writeFile(
161
- join(tmpDir, "section", "deep", "page", "+page.tsx"),
162
- `export default function DeepPage() { return <p class="deep-content">deep</p>; }`,
163
- );
164
- // canMatch — layout blocks
165
- await mkdir(join(tmpDir, "guarded"), { recursive: true });
166
- await writeFile(
167
- join(tmpDir, "guarded", "+layout.tsx"),
168
- `export const canMatch = async () => false;
169
- export default function L({ children }) { return <div innerHTML={children}></div>; }`,
170
- );
171
- await writeFile(
172
- join(tmpDir, "guarded", "+page.tsx"),
173
- `export default function P() { return <p>secret</p>; }`,
174
- );
175
-
176
- // canMatch — layout throws redirect
177
- await mkdir(join(tmpDir, "guarded-redirect"), { recursive: true });
178
- await writeFile(
179
- join(tmpDir, "guarded-redirect", "+layout.tsx"),
180
- `export const canMatch = async () => { throw { __redirect: true, status: 302, location: "/login" }; };
181
- export default function L({ children }) { return <div innerHTML={children}></div>; }`,
182
- );
183
- await writeFile(
184
- join(tmpDir, "guarded-redirect", "+page.tsx"),
185
- `export default function P() { return <p>secret</p>; }`,
186
- );
187
-
188
- // canMatch — page blocks
189
- await mkdir(join(tmpDir, "blocked"), { recursive: true });
190
- await writeFile(
191
- join(tmpDir, "blocked", "+page.tsx"),
192
- `export const canMatch = async () => false;
193
- export default function P() { return <p>secret</p>; }`,
194
- );
195
-
196
- // universal load — merge with server
197
- await mkdir(join(tmpDir, "universal-merge"), { recursive: true });
198
- await writeFile(
199
- join(tmpDir, "universal-merge", "+page.server.ts"),
200
- `export async function load() { return { b: "server" }; }`,
201
- );
202
- await writeFile(
203
- join(tmpDir, "universal-merge", "+page.tsx"),
204
- `export async function load() { return { a: "universal" }; }
205
- export default function P({ data }) { return <p>{data.a}-{data.b}</p>; }`,
206
- );
207
-
208
- // universal load — server wins on conflict
209
- await mkdir(join(tmpDir, "universal-conflict"), { recursive: true });
210
- await writeFile(
211
- join(tmpDir, "universal-conflict", "+page.server.ts"),
212
- `export async function load() { return { val: "server" }; }`,
213
- );
214
- await writeFile(
215
- join(tmpDir, "universal-conflict", "+page.tsx"),
216
- `export async function load() { return { val: "universal" }; }
217
- export default function P({ data }) { return <p class="val">{data.val}</p>; }`,
218
- );
219
-
220
- // universal load — layout load
221
- await mkdir(join(tmpDir, "universal-layout"), { recursive: true });
222
- await writeFile(
223
- join(tmpDir, "universal-layout", "+layout.tsx"),
224
- `export async function load() { return { user: "cane" }; }
225
- export default function L({ data, children }) { return <div data-user={data.user} innerHTML={children}></div>; }`,
226
- );
227
- await writeFile(
228
- join(tmpDir, "universal-layout", "+page.tsx"),
229
- `export default function P() { return <p>page</p>; }`,
230
- );
231
- });
232
-
233
- afterAll(async () => {
234
- await rm(tmpDir, { recursive: true });
235
- });
236
-
237
- describe("canMatch", () => {
238
- test("layout canMatch returning false returns 404", async () => {
239
- const tree = await scanRoutes(tmpDir);
240
- const matched = matchRoute(tree, new URL("http://localhost/guarded"));
241
- const res = await renderRoute(
242
- matched!,
243
- new Request("http://localhost/guarded"),
244
- );
245
- expect(res.status).toBe(404);
246
- });
247
-
248
- test("layout canMatch throwing redirect returns 3xx", async () => {
249
- const tree = await scanRoutes(tmpDir);
250
- const matched = matchRoute(
251
- tree,
252
- new URL("http://localhost/guarded-redirect"),
253
- );
254
- const res = await renderRoute(
255
- matched!,
256
- new Request("http://localhost/guarded-redirect"),
257
- );
258
- expect(res.status).toBe(302);
259
- expect(res.headers.get("Location")).toBe("/login");
260
- });
261
-
262
- test("page canMatch returning false returns 404", async () => {
263
- const tree = await scanRoutes(tmpDir);
264
- const matched = matchRoute(tree, new URL("http://localhost/blocked"));
265
- const res = await renderRoute(
266
- matched!,
267
- new Request("http://localhost/blocked"),
268
- );
269
- expect(res.status).toBe(404);
270
- });
271
- });
272
-
273
- describe("universal load", () => {
274
- test("page load merges with server data", async () => {
275
- const tree = await scanRoutes(tmpDir);
276
- const matched = matchRoute(
277
- tree,
278
- new URL("http://localhost/universal-merge"),
279
- );
280
- const res = await renderRoute(
281
- matched!,
282
- new Request("http://localhost/universal-merge"),
283
- );
284
- const html = await readStream(res);
285
- expect(html).toContain("universal");
286
- expect(html).toContain("server");
287
- expect(html).toContain('"a":"universal"');
288
- expect(html).toContain('"b":"server"');
289
- });
290
-
291
- test("server data wins over universal data on key conflict", async () => {
292
- const tree = await scanRoutes(tmpDir);
293
- const matched = matchRoute(
294
- tree,
295
- new URL("http://localhost/universal-conflict"),
296
- );
297
- const res = await renderRoute(
298
- matched!,
299
- new Request("http://localhost/universal-conflict"),
300
- );
301
- const html = await readStream(res);
302
- expect(html).toContain('"val":"server"');
303
- expect(html).not.toContain('"val":"universal"');
304
- });
305
-
306
- test("layout load runs and data reaches layout component", async () => {
307
- const tree = await scanRoutes(tmpDir);
308
- const matched = matchRoute(
309
- tree,
310
- new URL("http://localhost/universal-layout"),
311
- );
312
- const res = await renderRoute(
313
- matched!,
314
- new Request("http://localhost/universal-layout"),
315
- );
316
- const html = await readStream(res);
317
- expect(html).toContain('data-user="cane"');
318
- });
319
- });
320
-
321
- describe("renderRoute", () => {
322
- test("nested layouts stack from root to innermost", async () => {
323
- const tree = await scanRoutes(tmpDir);
324
- const matched = matchRoute(
325
- tree,
326
- new URL("http://localhost/section/nested"),
327
- );
328
- const res = await renderRoute(
329
- matched!,
330
- new Request("http://localhost/section/nested"),
331
- );
332
- const html = await readStream(res);
333
-
334
- // root layout wraps section layout wraps page
335
- expect(html).toContain('class="app"'); // root layout
336
- expect(html).toContain('class="section"'); // section layout
337
- expect(html).toContain('class="nested-content"'); // page
338
-
339
- // root must appear before section in the output
340
- const rootIdx = html.indexOf('class="app"');
341
- const sectionIdx = html.indexOf('class="section"');
342
- const pageIdx = html.indexOf('class="nested-content"');
343
- expect(rootIdx).toBeLessThan(sectionIdx);
344
- expect(sectionIdx).toBeLessThan(pageIdx);
345
- });
346
-
347
- test("triple nested layouts render in correct order", async () => {
348
- const tree = await scanRoutes(tmpDir);
349
- const matched = matchRoute(
350
- tree,
351
- new URL("http://localhost/section/deep/page"),
352
- );
353
- const res = await renderRoute(
354
- matched!,
355
- new Request("http://localhost/section/deep/page"),
356
- );
357
- const html = await readStream(res);
358
-
359
- expect(html).toContain('class="app"'); // root
360
- expect(html).toContain('class="section"'); // section
361
- expect(html).toContain('class="deep"'); // deep
362
- expect(html).toContain('class="deep-content"'); // page
363
-
364
- const rootIdx = html.indexOf('class="app"');
365
- const sectionIdx = html.indexOf('class="section"');
366
- const deepIdx = html.indexOf('class="deep"');
367
- const pageIdx = html.indexOf('class="deep-content"');
368
- expect(rootIdx).toBeLessThan(sectionIdx);
369
- expect(sectionIdx).toBeLessThan(deepIdx);
370
- expect(deepIdx).toBeLessThan(pageIdx);
371
- });
372
-
373
- test("page with no ancestor section layout only gets root layout", async () => {
374
- const tree = await scanRoutes(tmpDir);
375
- const matched = matchRoute(tree, new URL("http://localhost/blog"));
376
- const res = await renderRoute(
377
- matched!,
378
- new Request("http://localhost/blog"),
379
- );
380
- const html = await readStream(res);
381
-
382
- expect(html).toContain('class="app"'); // root layout
383
- expect(html).not.toContain('class="section"'); // no section layout
384
- expect(html).not.toContain('class="dash"'); // no dashboard layout
385
- });
386
-
387
- test("layout.server.ts data reaches the correct layout in a nested chain", async () => {
388
- const tree = await scanRoutes(tmpDir);
389
- const matched = matchRoute(tree, new URL("http://localhost/dashboard"));
390
- const res = await renderRoute(
391
- matched!,
392
- new Request("http://localhost/dashboard"),
393
- );
394
- const html = await readStream(res);
395
-
396
- // root layout wraps dashboard layout
397
- expect(html).toContain('class="app"');
398
- expect(html).toContain('class="dash"');
399
- expect(html).toContain('data-user="admin"');
400
-
401
- const rootIdx = html.indexOf('class="app"');
402
- const dashIdx = html.indexOf('class="dash"');
403
- expect(rootIdx).toBeLessThan(dashIdx);
404
- });
405
-
406
- test("renders static page", async () => {
407
- const tree = await scanRoutes(tmpDir);
408
- const matched = matchRoute(tree, new URL("http://localhost/"));
409
- const res = await renderRoute(matched!, new Request("http://localhost/"));
410
- const html = await readStream(res);
411
- expect(html).toContain("<h1>Hello Grimoire</h1>");
412
- expect(html).toContain("<!DOCTYPE html>");
413
- });
414
-
415
- test("hoists body stylesheets into <head> so CSS is render-blocking", async () => {
416
- const tree = await scanRoutes(tmpDir);
417
- const matched = matchRoute(tree, new URL("http://localhost/css-page"));
418
- const res = await renderRoute(
419
- matched!,
420
- new Request("http://localhost/css-page"),
421
- );
422
- const html = await readStream(res);
423
-
424
- const headEnd = html.indexOf("</head>");
425
- const headPart = html.slice(0, headEnd);
426
- expect(headPart).toContain('rel="stylesheet"');
427
- expect(headPart).toContain("/app.css");
428
- // original body copy stays for hydration claiming
429
- const bodyPart = html.slice(headEnd);
430
- expect(bodyPart).toContain('rel="stylesheet"');
431
- });
432
-
433
- test("renders page with load data", async () => {
434
- const tree = await scanRoutes(tmpDir);
435
- const matched = matchRoute(tree, new URL("http://localhost/blog"));
436
- console.log("matched.route.type:", matched?.route.type);
437
- console.log("matched.pageServer:", matched?.pageServer?.filePath);
438
- const res = await renderRoute(
439
- matched!,
440
- new Request("http://localhost/blog"),
441
- );
442
- const html = await readStream(res);
443
- expect(html).toContain("<!--g-->My Blog<!--/g-->");
444
- });
445
-
446
- test("wraps page in layout", async () => {
447
- const tree = await scanRoutes(tmpDir);
448
- const matched = matchRoute(tree, new URL("http://localhost/"));
449
- const res = await renderRoute(matched!, new Request("http://localhost/"));
450
- const html = await readStream(res);
451
- expect(html).toContain('class="app"');
452
- expect(html).toContain("<h1>Hello Grimoire</h1>");
453
- });
454
-
455
- test("returns 404 shape for unmatched route", async () => {
456
- const tree = await scanRoutes(tmpDir);
457
- const matched = matchRoute(tree, new URL("http://localhost/nonexistent"));
458
- expect(matched).toBeNull();
459
- });
460
-
461
- test("injects grimoire state script", async () => {
462
- const tree = await scanRoutes(tmpDir);
463
- const matched = matchRoute(tree, new URL("http://localhost/"));
464
- const res = await renderRoute(matched!, new Request("http://localhost/"));
465
- const html = await readStream(res);
466
- expect(html).toContain("__grimoire_state__");
467
- });
468
-
469
- test("component output is not escaped", async () => {
470
- await writeFile(
471
- join(tmpDir, "inner.tsx"),
472
- `
473
- export default function Inner() {
474
- return <strong>bold</strong>;
475
- }
476
- `,
477
- );
478
- await writeFile(
479
- join(tmpDir, "outer.tsx"),
480
- `
481
- import Inner from './inner';
482
- export default function Outer() {
483
- return <div><Inner /></div>;
484
- }
485
- `,
486
- );
487
-
488
- const tree = await scanRoutes(tmpDir);
489
- const matched = matchRoute(tree, new URL("http://localhost/outer"));
490
- const res = await renderRoute(
491
- matched!,
492
- new Request("http://localhost/outer"),
493
- );
494
- const html = await readStream(res);
495
-
496
- // should contain actual HTML tags, not escaped versions
497
- expect(html).toContain("<strong>bold</strong>");
498
- expect(html).not.toContain("&lt;strong&gt;");
499
- });
500
-
501
- test("user data is escaped", async () => {
502
- await writeFile(
503
- join(tmpDir, "xss.tsx"),
504
- `
505
- export default function XSS() {
506
- const userInput = '<script>alert("xss")</script>';
507
- return <div>{userInput}</div>;
508
- }
509
- `,
510
- );
511
-
512
- const tree = await scanRoutes(tmpDir);
513
- const matched = matchRoute(tree, new URL("http://localhost/xss"));
514
- const res = await renderRoute(
515
- matched!,
516
- new Request("http://localhost/xss"),
517
- );
518
- const html = await readStream(res);
519
-
520
- expect(html).toContain("&lt;script&gt;");
521
- expect(html).not.toContain("<script>alert");
522
- });
523
-
524
- test("load return value is passed as the data prop (SvelteKit convention)", async () => {
525
- const tree = await scanRoutes(tmpDir);
526
- const matched = matchRoute(tree, new URL("http://localhost/blog"));
527
- const res = await renderRoute(
528
- matched!,
529
- new Request("http://localhost/blog"),
530
- );
531
- const html = await readStream(res);
532
- // blog/+page.server.ts returns { title: "My Blog" }
533
- // blog/+page.tsx receives { data: { title: "My Blog" } } — not spread
534
- expect(html).toContain("My Blog");
535
- // the state JSON embeds data under the data key
536
- const stateJson = JSON.parse(
537
- html.match(/<script[^>]*>([^<]+)<\/script>/)?.[1] ?? "{}",
538
- );
539
- expect(stateJson.data?.title).toBe("My Blog");
540
- expect(html).not.toContain("[object Object]");
541
- });
542
-
543
- test("redirect() from load returns 3xx with Location header", async () => {
544
- const tree = await scanRoutes(tmpDir);
545
- const matched = matchRoute(tree, new URL("http://localhost/redirect-page"));
546
- const res = await renderRoute(
547
- matched!,
548
- new Request("http://localhost/redirect-page"),
549
- );
550
- expect(res.status).toBe(302);
551
- expect(res.headers.get("Location")).toBe("/login");
552
- });
553
-
554
- test("error() from load renders +error.tsx when present", async () => {
555
- const tree = await scanRoutes(tmpDir);
556
- const matched = matchRoute(tree, new URL("http://localhost/error-page"));
557
- const res = await renderRoute(
558
- matched!,
559
- new Request("http://localhost/error-page"),
560
- tree.errors,
561
- );
562
- expect(res.status).toBe(404);
563
- const html = await res.text();
564
- expect(html).toContain('class="error"');
565
- expect(html).toContain("404");
566
- expect(html).toContain("Not found");
567
- // error page must render as a full document, not a bare fragment —
568
- // same shell as a normal page, including hydration script and the
569
- // parent layout wrapper (root +layout.tsx renders class="app").
570
- expect(html).toContain("<!DOCTYPE html>");
571
- expect(html).toContain("<html>");
572
- expect(html).toContain("/__grimoire__/hydrate.js");
573
- expect(html).toContain('class="app"');
574
- });
575
-
576
- test("error() from load falls back to plain text when no error routes provided", async () => {
577
- const tree = await scanRoutes(tmpDir);
578
- const matched = matchRoute(tree, new URL("http://localhost/error-page"));
579
- const res = await renderRoute(
580
- matched!,
581
- new Request("http://localhost/error-page"),
582
- [], // no error routes
583
- );
584
- expect(res.status).toBe(404);
585
- const text = await res.text();
586
- expect(text).toBe("Not found");
587
- });
588
-
589
- test("locals passed to renderRoute are visible inside load()", async () => {
590
- const tree = await scanRoutes(tmpDir);
591
- const matched = matchRoute(tree, new URL("http://localhost/locals-test"));
592
- const res = await renderRoute(
593
- matched!,
594
- new Request("http://localhost/locals-test"),
595
- [],
596
- undefined,
597
- { tag: "from-hook" },
598
- );
599
- const html = await readStream(res);
600
- expect(html).toContain("from-hook");
601
- });
602
-
603
- test("+layout.server.ts load data is passed to the layout component", async () => {
604
- const tree = await scanRoutes(tmpDir);
605
- const matched = matchRoute(tree, new URL("http://localhost/dashboard"));
606
- const res = await renderRoute(
607
- matched!,
608
- new Request("http://localhost/dashboard"),
609
- );
610
- const html = await readStream(res);
611
- expect(html).toContain('class="dash"');
612
- expect(html).toContain('data-user="admin"');
613
- expect(html).toContain("dash content");
614
- });
615
- });
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdir, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir as osTmpDir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { renderRoute } from "../src/rendering";
6
+ import { matchRoute } from "../src/routing/router";
7
+ import { scanRoutes } from "../src/routing/scanner";
8
+
9
+ /** Read a streaming Response body into a single string */
10
+ async function readStream(res: Response): Promise<string> {
11
+ const reader = res.body!.getReader();
12
+ const parts: string[] = [];
13
+ while (true) {
14
+ const { done, value } = await reader.read();
15
+ if (done) break;
16
+ // Bun ReadableStream chunks can be string, Uint8Array, or other
17
+ parts.push(String(value));
18
+ }
19
+ return parts.join("");
20
+ }
21
+
22
+ let tmpDir: string;
23
+
24
+ beforeAll(async () => {
25
+ tmpDir = join(osTmpDir(), `grimoire-render-${Date.now()}`);
26
+ await mkdir(join(tmpDir, "blog"), { recursive: true });
27
+
28
+ // simple static page
29
+ await writeFile(
30
+ join(tmpDir, "index.tsx"),
31
+ `
32
+ export default function Home() {
33
+ return <h1>Hello Grimoire</h1>;
34
+ }
35
+ `,
36
+ );
37
+
38
+ // page with load function
39
+ await writeFile(
40
+ join(tmpDir, "blog", "+page.server.ts"),
41
+ `
42
+ export async function load({ params }) {
43
+ return { title: 'My Blog' };
44
+ }
45
+ `,
46
+ );
47
+
48
+ await writeFile(
49
+ join(tmpDir, "blog", "+page.tsx"),
50
+ `
51
+ export default function Blog({ data }) {
52
+ return <h1>{data.title}</h1>;
53
+ }
54
+ `,
55
+ );
56
+
57
+ // layout
58
+ await writeFile(
59
+ join(tmpDir, "+layout.tsx"),
60
+ `
61
+ export default function Layout({ children }) {
62
+ return <div class="app" innerHTML={children}></div>;
63
+ }
64
+ `,
65
+ );
66
+
67
+ // page declaring a stylesheet inline (the usual layout pattern)
68
+ await mkdir(join(tmpDir, "css-page"), { recursive: true });
69
+ await writeFile(
70
+ join(tmpDir, "css-page", "+page.tsx"),
71
+ `
72
+ export default function CssPage() {
73
+ return <div><link rel="stylesheet" href="/app.css" /><h1>Styled</h1></div>;
74
+ }
75
+ `,
76
+ );
77
+
78
+ // root error page — renders status and message
79
+ await writeFile(
80
+ join(tmpDir, "+error.tsx"),
81
+ `
82
+ export default function ErrorPage({ status, message }) {
83
+ return <div class="error"><span class="status">{status}</span><span class="msg">{message}</span></div>;
84
+ }
85
+ `,
86
+ );
87
+
88
+ // page whose load throws redirect — use inline object so no grimoire import needed from /tmp
89
+ await mkdir(join(tmpDir, "redirect-page"), { recursive: true });
90
+ await writeFile(
91
+ join(tmpDir, "redirect-page", "+page.server.ts"),
92
+ `export async function load() { throw { __redirect: true, status: 302, location: "/login" }; }`,
93
+ );
94
+ await writeFile(
95
+ join(tmpDir, "redirect-page", "+page.tsx"),
96
+ `export default function P() { return <div>never</div>; }`,
97
+ );
98
+
99
+ // page whose load throws error — same pattern
100
+ await mkdir(join(tmpDir, "error-page"), { recursive: true });
101
+ await writeFile(
102
+ join(tmpDir, "error-page", "+page.server.ts"),
103
+ `export async function load() { throw { __error: true, status: 404, message: "Not found" }; }`,
104
+ );
105
+ await writeFile(
106
+ join(tmpDir, "error-page", "+page.tsx"),
107
+ `export default function P() { return <div>never</div>; }`,
108
+ );
109
+
110
+ // page whose load returns locals.tag (tests locals passthrough)
111
+ await mkdir(join(tmpDir, "locals-test"), { recursive: true });
112
+ await writeFile(
113
+ join(tmpDir, "locals-test", "+page.server.ts"),
114
+ `
115
+ export async function load({ locals }) { return { tag: locals.tag ?? "none" }; }
116
+ `,
117
+ );
118
+ await writeFile(
119
+ join(tmpDir, "locals-test", "+page.tsx"),
120
+ `export default function P({ data }) { return <div class="tag">{data.tag}</div>; }`,
121
+ );
122
+
123
+ // layout.server test — nested layout with server data
124
+ await mkdir(join(tmpDir, "dashboard"), { recursive: true });
125
+ await writeFile(
126
+ join(tmpDir, "dashboard", "+layout.server.ts"),
127
+ `export async function load() { return { user: "admin" }; }`,
128
+ );
129
+ await writeFile(
130
+ join(tmpDir, "dashboard", "+layout.tsx"),
131
+ `export default function DashLayout({ data, children }) {
132
+ return <div class="dash" data-user={data.user} innerHTML={children}></div>;
133
+ }`,
134
+ );
135
+ await writeFile(
136
+ join(tmpDir, "dashboard", "index.tsx"),
137
+ `export default function Dash() { return <p>dash content</p>; }`,
138
+ );
139
+
140
+ await mkdir(join(tmpDir, "section", "nested"), { recursive: true });
141
+ await writeFile(
142
+ join(tmpDir, "section", "+layout.tsx"),
143
+ `export default function SectionLayout({ children }) {
144
+ return <div class="section" innerHTML={children}></div>;
145
+ }`,
146
+ );
147
+ await writeFile(
148
+ join(tmpDir, "section", "nested", "+page.tsx"),
149
+ `export default function NestedPage() { return <p class="nested-content">nested</p>; }`,
150
+ );
151
+
152
+ // triple nesting — root + section + deep
153
+ await mkdir(join(tmpDir, "section", "deep", "page"), { recursive: true });
154
+ await writeFile(
155
+ join(tmpDir, "section", "deep", "+layout.tsx"),
156
+ `export default function DeepLayout({ children }) {
157
+ return <div class="deep" innerHTML={children}></div>;
158
+ }`,
159
+ );
160
+ await writeFile(
161
+ join(tmpDir, "section", "deep", "page", "+page.tsx"),
162
+ `export default function DeepPage() { return <p class="deep-content">deep</p>; }`,
163
+ );
164
+ // canMatch — layout blocks
165
+ await mkdir(join(tmpDir, "guarded"), { recursive: true });
166
+ await writeFile(
167
+ join(tmpDir, "guarded", "+layout.tsx"),
168
+ `export const canMatch = async () => false;
169
+ export default function L({ children }) { return <div innerHTML={children}></div>; }`,
170
+ );
171
+ await writeFile(
172
+ join(tmpDir, "guarded", "+page.tsx"),
173
+ `export default function P() { return <p>secret</p>; }`,
174
+ );
175
+
176
+ // canMatch — layout throws redirect
177
+ await mkdir(join(tmpDir, "guarded-redirect"), { recursive: true });
178
+ await writeFile(
179
+ join(tmpDir, "guarded-redirect", "+layout.tsx"),
180
+ `export const canMatch = async () => { throw { __redirect: true, status: 302, location: "/login" }; };
181
+ export default function L({ children }) { return <div innerHTML={children}></div>; }`,
182
+ );
183
+ await writeFile(
184
+ join(tmpDir, "guarded-redirect", "+page.tsx"),
185
+ `export default function P() { return <p>secret</p>; }`,
186
+ );
187
+
188
+ // canMatch — page blocks
189
+ await mkdir(join(tmpDir, "blocked"), { recursive: true });
190
+ await writeFile(
191
+ join(tmpDir, "blocked", "+page.tsx"),
192
+ `export const canMatch = async () => false;
193
+ export default function P() { return <p>secret</p>; }`,
194
+ );
195
+
196
+ // universal load — merge with server
197
+ await mkdir(join(tmpDir, "universal-merge"), { recursive: true });
198
+ await writeFile(
199
+ join(tmpDir, "universal-merge", "+page.server.ts"),
200
+ `export async function load() { return { b: "server" }; }`,
201
+ );
202
+ await writeFile(
203
+ join(tmpDir, "universal-merge", "+page.tsx"),
204
+ `export async function load() { return { a: "universal" }; }
205
+ export default function P({ data }) { return <p>{data.a}-{data.b}</p>; }`,
206
+ );
207
+
208
+ // universal load — server wins on conflict
209
+ await mkdir(join(tmpDir, "universal-conflict"), { recursive: true });
210
+ await writeFile(
211
+ join(tmpDir, "universal-conflict", "+page.server.ts"),
212
+ `export async function load() { return { val: "server" }; }`,
213
+ );
214
+ await writeFile(
215
+ join(tmpDir, "universal-conflict", "+page.tsx"),
216
+ `export async function load() { return { val: "universal" }; }
217
+ export default function P({ data }) { return <p class="val">{data.val}</p>; }`,
218
+ );
219
+
220
+ // universal load — layout load
221
+ await mkdir(join(tmpDir, "universal-layout"), { recursive: true });
222
+ await writeFile(
223
+ join(tmpDir, "universal-layout", "+layout.tsx"),
224
+ `export async function load() { return { user: "cane" }; }
225
+ export default function L({ data, children }) { return <div data-user={data.user} innerHTML={children}></div>; }`,
226
+ );
227
+ await writeFile(
228
+ join(tmpDir, "universal-layout", "+page.tsx"),
229
+ `export default function P() { return <p>page</p>; }`,
230
+ );
231
+ });
232
+
233
+ afterAll(async () => {
234
+ await rm(tmpDir, { recursive: true });
235
+ });
236
+
237
+ describe("canMatch", () => {
238
+ test("layout canMatch returning false returns 404", async () => {
239
+ const tree = await scanRoutes(tmpDir);
240
+ const matched = matchRoute(tree, new URL("http://localhost/guarded"));
241
+ const res = await renderRoute(
242
+ matched!,
243
+ new Request("http://localhost/guarded"),
244
+ );
245
+ expect(res.status).toBe(404);
246
+ });
247
+
248
+ test("layout canMatch throwing redirect returns 3xx", async () => {
249
+ const tree = await scanRoutes(tmpDir);
250
+ const matched = matchRoute(
251
+ tree,
252
+ new URL("http://localhost/guarded-redirect"),
253
+ );
254
+ const res = await renderRoute(
255
+ matched!,
256
+ new Request("http://localhost/guarded-redirect"),
257
+ );
258
+ expect(res.status).toBe(302);
259
+ expect(res.headers.get("Location")).toBe("/login");
260
+ });
261
+
262
+ test("page canMatch returning false returns 404", async () => {
263
+ const tree = await scanRoutes(tmpDir);
264
+ const matched = matchRoute(tree, new URL("http://localhost/blocked"));
265
+ const res = await renderRoute(
266
+ matched!,
267
+ new Request("http://localhost/blocked"),
268
+ );
269
+ expect(res.status).toBe(404);
270
+ });
271
+ });
272
+
273
+ describe("universal load", () => {
274
+ test("page load merges with server data", async () => {
275
+ const tree = await scanRoutes(tmpDir);
276
+ const matched = matchRoute(
277
+ tree,
278
+ new URL("http://localhost/universal-merge"),
279
+ );
280
+ const res = await renderRoute(
281
+ matched!,
282
+ new Request("http://localhost/universal-merge"),
283
+ );
284
+ const html = await readStream(res);
285
+ expect(html).toContain("universal");
286
+ expect(html).toContain("server");
287
+ expect(html).toContain('"a":"universal"');
288
+ expect(html).toContain('"b":"server"');
289
+ });
290
+
291
+ test("server data wins over universal data on key conflict", async () => {
292
+ const tree = await scanRoutes(tmpDir);
293
+ const matched = matchRoute(
294
+ tree,
295
+ new URL("http://localhost/universal-conflict"),
296
+ );
297
+ const res = await renderRoute(
298
+ matched!,
299
+ new Request("http://localhost/universal-conflict"),
300
+ );
301
+ const html = await readStream(res);
302
+ expect(html).toContain('"val":"server"');
303
+ expect(html).not.toContain('"val":"universal"');
304
+ });
305
+
306
+ test("layout load runs and data reaches layout component", async () => {
307
+ const tree = await scanRoutes(tmpDir);
308
+ const matched = matchRoute(
309
+ tree,
310
+ new URL("http://localhost/universal-layout"),
311
+ );
312
+ const res = await renderRoute(
313
+ matched!,
314
+ new Request("http://localhost/universal-layout"),
315
+ );
316
+ const html = await readStream(res);
317
+ expect(html).toContain('data-user="cane"');
318
+ });
319
+ });
320
+
321
+ describe("renderRoute", () => {
322
+ test("nested layouts stack from root to innermost", async () => {
323
+ const tree = await scanRoutes(tmpDir);
324
+ const matched = matchRoute(
325
+ tree,
326
+ new URL("http://localhost/section/nested"),
327
+ );
328
+ const res = await renderRoute(
329
+ matched!,
330
+ new Request("http://localhost/section/nested"),
331
+ );
332
+ const html = await readStream(res);
333
+
334
+ // root layout wraps section layout wraps page
335
+ expect(html).toContain('class="app"'); // root layout
336
+ expect(html).toContain('class="section"'); // section layout
337
+ expect(html).toContain('class="nested-content"'); // page
338
+
339
+ // root must appear before section in the output
340
+ const rootIdx = html.indexOf('class="app"');
341
+ const sectionIdx = html.indexOf('class="section"');
342
+ const pageIdx = html.indexOf('class="nested-content"');
343
+ expect(rootIdx).toBeLessThan(sectionIdx);
344
+ expect(sectionIdx).toBeLessThan(pageIdx);
345
+ });
346
+
347
+ test("triple nested layouts render in correct order", async () => {
348
+ const tree = await scanRoutes(tmpDir);
349
+ const matched = matchRoute(
350
+ tree,
351
+ new URL("http://localhost/section/deep/page"),
352
+ );
353
+ const res = await renderRoute(
354
+ matched!,
355
+ new Request("http://localhost/section/deep/page"),
356
+ );
357
+ const html = await readStream(res);
358
+
359
+ expect(html).toContain('class="app"'); // root
360
+ expect(html).toContain('class="section"'); // section
361
+ expect(html).toContain('class="deep"'); // deep
362
+ expect(html).toContain('class="deep-content"'); // page
363
+
364
+ const rootIdx = html.indexOf('class="app"');
365
+ const sectionIdx = html.indexOf('class="section"');
366
+ const deepIdx = html.indexOf('class="deep"');
367
+ const pageIdx = html.indexOf('class="deep-content"');
368
+ expect(rootIdx).toBeLessThan(sectionIdx);
369
+ expect(sectionIdx).toBeLessThan(deepIdx);
370
+ expect(deepIdx).toBeLessThan(pageIdx);
371
+ });
372
+
373
+ test("page with no ancestor section layout only gets root layout", async () => {
374
+ const tree = await scanRoutes(tmpDir);
375
+ const matched = matchRoute(tree, new URL("http://localhost/blog"));
376
+ const res = await renderRoute(
377
+ matched!,
378
+ new Request("http://localhost/blog"),
379
+ );
380
+ const html = await readStream(res);
381
+
382
+ expect(html).toContain('class="app"'); // root layout
383
+ expect(html).not.toContain('class="section"'); // no section layout
384
+ expect(html).not.toContain('class="dash"'); // no dashboard layout
385
+ });
386
+
387
+ test("layout.server.ts data reaches the correct layout in a nested chain", async () => {
388
+ const tree = await scanRoutes(tmpDir);
389
+ const matched = matchRoute(tree, new URL("http://localhost/dashboard"));
390
+ const res = await renderRoute(
391
+ matched!,
392
+ new Request("http://localhost/dashboard"),
393
+ );
394
+ const html = await readStream(res);
395
+
396
+ // root layout wraps dashboard layout
397
+ expect(html).toContain('class="app"');
398
+ expect(html).toContain('class="dash"');
399
+ expect(html).toContain('data-user="admin"');
400
+
401
+ const rootIdx = html.indexOf('class="app"');
402
+ const dashIdx = html.indexOf('class="dash"');
403
+ expect(rootIdx).toBeLessThan(dashIdx);
404
+ });
405
+
406
+ test("renders static page", async () => {
407
+ const tree = await scanRoutes(tmpDir);
408
+ const matched = matchRoute(tree, new URL("http://localhost/"));
409
+ const res = await renderRoute(matched!, new Request("http://localhost/"));
410
+ const html = await readStream(res);
411
+ expect(html).toContain("<h1>Hello Grimoire</h1>");
412
+ expect(html).toContain("<!DOCTYPE html>");
413
+ });
414
+
415
+ test("hoists body stylesheets into <head> so CSS is render-blocking", async () => {
416
+ const tree = await scanRoutes(tmpDir);
417
+ const matched = matchRoute(tree, new URL("http://localhost/css-page"));
418
+ const res = await renderRoute(
419
+ matched!,
420
+ new Request("http://localhost/css-page"),
421
+ );
422
+ const html = await readStream(res);
423
+
424
+ const headEnd = html.indexOf("</head>");
425
+ const headPart = html.slice(0, headEnd);
426
+ expect(headPart).toContain('rel="stylesheet"');
427
+ expect(headPart).toContain("/app.css");
428
+ // original body copy stays for hydration claiming
429
+ const bodyPart = html.slice(headEnd);
430
+ expect(bodyPart).toContain('rel="stylesheet"');
431
+ });
432
+
433
+ test("renders page with load data", async () => {
434
+ const tree = await scanRoutes(tmpDir);
435
+ const matched = matchRoute(tree, new URL("http://localhost/blog"));
436
+ console.log("matched.route.type:", matched?.route.type);
437
+ console.log("matched.pageServer:", matched?.pageServer?.filePath);
438
+ const res = await renderRoute(
439
+ matched!,
440
+ new Request("http://localhost/blog"),
441
+ );
442
+ const html = await readStream(res);
443
+ expect(html).toContain("<!--g-->My Blog<!--/g-->");
444
+ });
445
+
446
+ test("wraps page in layout", async () => {
447
+ const tree = await scanRoutes(tmpDir);
448
+ const matched = matchRoute(tree, new URL("http://localhost/"));
449
+ const res = await renderRoute(matched!, new Request("http://localhost/"));
450
+ const html = await readStream(res);
451
+ expect(html).toContain('class="app"');
452
+ expect(html).toContain("<h1>Hello Grimoire</h1>");
453
+ });
454
+
455
+ test("returns 404 shape for unmatched route", async () => {
456
+ const tree = await scanRoutes(tmpDir);
457
+ const matched = matchRoute(tree, new URL("http://localhost/nonexistent"));
458
+ expect(matched).toBeNull();
459
+ });
460
+
461
+ test("injects grimoire state script", async () => {
462
+ const tree = await scanRoutes(tmpDir);
463
+ const matched = matchRoute(tree, new URL("http://localhost/"));
464
+ const res = await renderRoute(matched!, new Request("http://localhost/"));
465
+ const html = await readStream(res);
466
+ expect(html).toContain("__grimoire_state__");
467
+ });
468
+
469
+ test("component output is not escaped", async () => {
470
+ await writeFile(
471
+ join(tmpDir, "inner.tsx"),
472
+ `
473
+ export default function Inner() {
474
+ return <strong>bold</strong>;
475
+ }
476
+ `,
477
+ );
478
+ await writeFile(
479
+ join(tmpDir, "outer.tsx"),
480
+ `
481
+ import Inner from './inner';
482
+ export default function Outer() {
483
+ return <div><Inner /></div>;
484
+ }
485
+ `,
486
+ );
487
+
488
+ const tree = await scanRoutes(tmpDir);
489
+ const matched = matchRoute(tree, new URL("http://localhost/outer"));
490
+ const res = await renderRoute(
491
+ matched!,
492
+ new Request("http://localhost/outer"),
493
+ );
494
+ const html = await readStream(res);
495
+
496
+ // should contain actual HTML tags, not escaped versions
497
+ expect(html).toContain("<strong>bold</strong>");
498
+ expect(html).not.toContain("&lt;strong&gt;");
499
+ });
500
+
501
+ test("user data is escaped", async () => {
502
+ await writeFile(
503
+ join(tmpDir, "xss.tsx"),
504
+ `
505
+ export default function XSS() {
506
+ const userInput = '<script>alert("xss")</script>';
507
+ return <div>{userInput}</div>;
508
+ }
509
+ `,
510
+ );
511
+
512
+ const tree = await scanRoutes(tmpDir);
513
+ const matched = matchRoute(tree, new URL("http://localhost/xss"));
514
+ const res = await renderRoute(
515
+ matched!,
516
+ new Request("http://localhost/xss"),
517
+ );
518
+ const html = await readStream(res);
519
+
520
+ expect(html).toContain("&lt;script&gt;");
521
+ expect(html).not.toContain("<script>alert");
522
+ });
523
+
524
+ test("load return value is passed as the data prop (SvelteKit convention)", async () => {
525
+ const tree = await scanRoutes(tmpDir);
526
+ const matched = matchRoute(tree, new URL("http://localhost/blog"));
527
+ const res = await renderRoute(
528
+ matched!,
529
+ new Request("http://localhost/blog"),
530
+ );
531
+ const html = await readStream(res);
532
+ // blog/+page.server.ts returns { title: "My Blog" }
533
+ // blog/+page.tsx receives { data: { title: "My Blog" } } — not spread
534
+ expect(html).toContain("My Blog");
535
+ // the state JSON embeds data under the data key
536
+ const stateJson = JSON.parse(
537
+ html.match(/<script[^>]*>([^<]+)<\/script>/)?.[1] ?? "{}",
538
+ );
539
+ expect(stateJson.data?.title).toBe("My Blog");
540
+ expect(html).not.toContain("[object Object]");
541
+ });
542
+
543
+ test("redirect() from load returns 3xx with Location header", async () => {
544
+ const tree = await scanRoutes(tmpDir);
545
+ const matched = matchRoute(tree, new URL("http://localhost/redirect-page"));
546
+ const res = await renderRoute(
547
+ matched!,
548
+ new Request("http://localhost/redirect-page"),
549
+ );
550
+ expect(res.status).toBe(302);
551
+ expect(res.headers.get("Location")).toBe("/login");
552
+ });
553
+
554
+ test("error() from load renders +error.tsx when present", async () => {
555
+ const tree = await scanRoutes(tmpDir);
556
+ const matched = matchRoute(tree, new URL("http://localhost/error-page"));
557
+ const res = await renderRoute(
558
+ matched!,
559
+ new Request("http://localhost/error-page"),
560
+ tree.errors,
561
+ );
562
+ expect(res.status).toBe(404);
563
+ const html = await res.text();
564
+ expect(html).toContain('class="error"');
565
+ expect(html).toContain("404");
566
+ expect(html).toContain("Not found");
567
+ // error page must render as a full document, not a bare fragment —
568
+ // same shell as a normal page, including hydration script and the
569
+ // parent layout wrapper (root +layout.tsx renders class="app").
570
+ expect(html).toContain("<!DOCTYPE html>");
571
+ expect(html).toContain("<html>");
572
+ expect(html).toContain("/__grimoire__/hydrate.js");
573
+ expect(html).toContain('class="app"');
574
+ });
575
+
576
+ test("error() from load falls back to plain text when no error routes provided", async () => {
577
+ const tree = await scanRoutes(tmpDir);
578
+ const matched = matchRoute(tree, new URL("http://localhost/error-page"));
579
+ const res = await renderRoute(
580
+ matched!,
581
+ new Request("http://localhost/error-page"),
582
+ [], // no error routes
583
+ );
584
+ expect(res.status).toBe(404);
585
+ const text = await res.text();
586
+ expect(text).toBe("Not found");
587
+ });
588
+
589
+ test("locals passed to renderRoute are visible inside load()", async () => {
590
+ const tree = await scanRoutes(tmpDir);
591
+ const matched = matchRoute(tree, new URL("http://localhost/locals-test"));
592
+ const res = await renderRoute(
593
+ matched!,
594
+ new Request("http://localhost/locals-test"),
595
+ [],
596
+ undefined,
597
+ { tag: "from-hook" },
598
+ );
599
+ const html = await readStream(res);
600
+ expect(html).toContain("from-hook");
601
+ });
602
+
603
+ test("+layout.server.ts load data is passed to the layout component", async () => {
604
+ const tree = await scanRoutes(tmpDir);
605
+ const matched = matchRoute(tree, new URL("http://localhost/dashboard"));
606
+ const res = await renderRoute(
607
+ matched!,
608
+ new Request("http://localhost/dashboard"),
609
+ );
610
+ const html = await readStream(res);
611
+ expect(html).toContain('class="dash"');
612
+ expect(html).toContain('data-user="admin"');
613
+ expect(html).toContain("dash content");
614
+ });
615
+ });
616
+ describe("load context — await parent()", () => {
617
+ let parentDir: string;
618
+
619
+ beforeAll(async () => {
620
+ parentDir = join(osTmpDir(), `grimoire-parent-${Date.now()}`);
621
+ await mkdir(join(parentDir, "nested", "deep"), { recursive: true });
622
+
623
+ await writeFile(
624
+ join(parentDir, "+layout.tsx"),
625
+ `export default function L({ children }) { return <section innerHTML={children}></section>; }`,
626
+ );
627
+ await writeFile(
628
+ join(parentDir, "+layout.server.ts"),
629
+ `export async function load() { return { a: 1 }; }`,
630
+ );
631
+ await writeFile(
632
+ join(parentDir, "+page.server.ts"),
633
+ `export async function load({ parent }) { return { b: (await parent()).a }; }`,
634
+ );
635
+ await writeFile(
636
+ join(parentDir, "+page.tsx"),
637
+ `export default function P({ data }) { return <div>{JSON.stringify(data)}</div>; }`,
638
+ );
639
+
640
+ await writeFile(
641
+ join(parentDir, "nested", "+layout.tsx"),
642
+ `export default function L({ children }) { return <div innerHTML={children}></div>; }`,
643
+ );
644
+ await writeFile(
645
+ join(parentDir, "nested", "+layout.server.ts"),
646
+ `export async function load({ parent }) { return { b: (await parent()).a + 1 }; }`,
647
+ );
648
+ await writeFile(
649
+ join(parentDir, "nested", "deep", "+page.server.ts"),
650
+ `export async function load({ parent }) { const p = await parent(); return { c: p.a + p.b }; }`,
651
+ );
652
+ await writeFile(
653
+ join(parentDir, "nested", "deep", "+page.tsx"),
654
+ `export default function P({ data }) { return <div>{JSON.stringify(data)}</div>; }`,
655
+ );
656
+ });
657
+
658
+ afterAll(async () => {
659
+ await rm(parentDir, { recursive: true });
660
+ });
661
+
662
+ test("page load sees layout data via await parent()", async () => {
663
+ const tree = await scanRoutes(parentDir);
664
+ const matched = matchRoute(tree, new URL("http://localhost/"));
665
+ const html = await readStream(
666
+ await renderRoute(matched!, new Request("http://localhost/")),
667
+ );
668
+ expect(html).toContain('"b":1');
669
+ });
670
+
671
+ test("nested layouts accumulate outermost-first", async () => {
672
+ const tree = await scanRoutes(parentDir);
673
+ const matched = matchRoute(tree, new URL("http://localhost/nested/deep"));
674
+ const html = await readStream(
675
+ await renderRoute(matched!, new Request("http://localhost/nested/deep")),
676
+ );
677
+ // outer a=1, inner b=a+1=2, page c=a+b=3
678
+ expect(html).toContain('"c":3');
679
+ });
680
+ });
681
+
682
+ describe("server-side depends()", () => {
683
+ let depsDir: string;
684
+
685
+ beforeAll(async () => {
686
+ depsDir = join(osTmpDir(), `grimoire-deps-${Date.now()}`);
687
+ await mkdir(depsDir, { recursive: true });
688
+ const depsModule = join(import.meta.dir, "..", "src", "client", "deps.ts")
689
+ .replace(/\\/g, "/")
690
+ .replace(/^([A-Za-z]:)/, "file:///$1");
691
+ await writeFile(
692
+ join(depsDir, "+page.server.ts"),
693
+ [
694
+ "export async function load() {",
695
+ ` const { depends } = await import("${depsModule}");`,
696
+ ' depends("user:1");',
697
+ " return { x: 1 };",
698
+ "}",
699
+ ].join("\n"),
700
+ );
701
+ await writeFile(
702
+ join(depsDir, "+page.tsx"),
703
+ `export default function P({ data }) { return <div>{data.x}</div>; }`,
704
+ );
705
+ });
706
+
707
+ afterAll(async () => {
708
+ await rm(depsDir, { recursive: true });
709
+ });
710
+
711
+ test("depends keys land in the state payload", async () => {
712
+ const tree = await scanRoutes(depsDir);
713
+ const matched = matchRoute(tree, new URL("http://localhost/"));
714
+ const html = await readStream(
715
+ await renderRoute(matched!, new Request("http://localhost/")),
716
+ );
717
+ expect(html).toContain('"deps":["user:1"]');
718
+ });
719
+ });