@sigil-dev/grimoire 0.4.0 → 0.5.0

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 (43) hide show
  1. package/index.ts +29 -23
  2. package/package.json +1 -1
  3. package/public/__grimoire__/client.js +5 -36
  4. package/public/__grimoire__/hydrate.js +7 -45
  5. package/src/build.ts +88 -0
  6. package/src/cookie-utils.ts +66 -66
  7. package/src/plugins.ts +19 -5
  8. package/src/renderer.ts +12 -1
  9. package/src/server.ts +81 -81
  10. package/src/ssrPlugin.ts +7 -2
  11. package/src/transform-routes.ts +6 -1
  12. package/src/typegen.ts +77 -3
  13. package/src/types.ts +45 -1
  14. package/test/fail.test.ts +46 -46
  15. package/test/headers.test.ts +96 -96
  16. package/test/middleware.test.ts +217 -217
  17. package/test/redirect-error.test.ts +112 -112
  18. package/test/rendering.test.ts +310 -310
  19. package/test/server.test.ts +120 -0
  20. package/test/streaming.test.ts +132 -132
  21. package/test/typegen.test.ts +6 -6
  22. package/.grimoire/_routes.dom.js +0 -4
  23. package/.grimoire/_routes.hydrate.js +0 -4
  24. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503001657_protected__page.dom.js +0 -9
  25. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503001657_protected__page.hydrate.js +0 -11
  26. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503161592_protected__page.dom.js +0 -4
  27. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503161592_protected__page.hydrate.js +0 -4
  28. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503395872_login.dom.js +0 -4
  29. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503395872_login.hydrate.js +0 -4
  30. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503614350_login.dom.js +0 -4
  31. package/.grimoire/compiled/0-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503614350_login.hydrate.js +0 -4
  32. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503001657_login__page.dom.js +0 -8
  33. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503001657_login__page.hydrate.js +0 -9
  34. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503161592_login__page.dom.js +0 -4
  35. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503161592_login__page.hydrate.js +0 -4
  36. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503395872_protected.dom.js +0 -4
  37. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503395872_protected.hydrate.js +0 -4
  38. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503614350_protected.dom.js +0 -4
  39. package/.grimoire/compiled/1-C__Users_Cane1712_AppData_Local_Temp_grimoire-actions-1780503614350_protected.hydrate.js +0 -4
  40. package/.grimoire/tsconfig.generated.json +0 -11
  41. package/.grimoire/types/ambient.d.ts +0 -6
  42. package/.grimoire/types/api/hello/$types.d.ts +0 -29
  43. package/.grimoire/types/api/items/$types.d.ts +0 -29
@@ -1,310 +1,310 @@
1
- import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
- import { mkdir, rm, writeFile } from "fs/promises";
3
- import { tmpdir as osTmpDir } from "os";
4
- import { join } from "path";
5
- import { renderRoute } from "../src/renderer";
6
- import { matchRoute } from "../src/router";
7
- import { scanRoutes } from "../src/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
- // root error page — renders status and message
68
- await writeFile(
69
- join(tmpDir, "+error.tsx"),
70
- `
71
- export default function ErrorPage({ status, message }) {
72
- return <div class="error"><span class="status">{status}</span><span class="msg">{message}</span></div>;
73
- }
74
- `,
75
- );
76
-
77
- // page whose load throws redirect — use inline object so no grimoire import needed from /tmp
78
- await mkdir(join(tmpDir, "redirect-page"), { recursive: true });
79
- await writeFile(
80
- join(tmpDir, "redirect-page", "+page.server.ts"),
81
- `export async function load() { throw { __redirect: true, status: 302, location: "/login" }; }`,
82
- );
83
- await writeFile(
84
- join(tmpDir, "redirect-page", "+page.tsx"),
85
- `export default function P() { return <div>never</div>; }`,
86
- );
87
-
88
- // page whose load throws error — same pattern
89
- await mkdir(join(tmpDir, "error-page"), { recursive: true });
90
- await writeFile(
91
- join(tmpDir, "error-page", "+page.server.ts"),
92
- `export async function load() { throw { __error: true, status: 404, message: "Not found" }; }`,
93
- );
94
- await writeFile(
95
- join(tmpDir, "error-page", "+page.tsx"),
96
- `export default function P() { return <div>never</div>; }`,
97
- );
98
-
99
- // page whose load returns locals.tag (tests locals passthrough)
100
- await mkdir(join(tmpDir, "locals-test"), { recursive: true });
101
- await writeFile(
102
- join(tmpDir, "locals-test", "+page.server.ts"),
103
- `
104
- export async function load({ locals }) { return { tag: locals.tag ?? "none" }; }
105
- `,
106
- );
107
- await writeFile(
108
- join(tmpDir, "locals-test", "+page.tsx"),
109
- `export default function P({ data }) { return <div class="tag">{data.tag}</div>; }`,
110
- );
111
-
112
- // layout.server test — nested layout with server data
113
- await mkdir(join(tmpDir, "dashboard"), { recursive: true });
114
- await writeFile(
115
- join(tmpDir, "dashboard", "+layout.server.ts"),
116
- `export async function load() { return { user: "admin" }; }`,
117
- );
118
- await writeFile(
119
- join(tmpDir, "dashboard", "+layout.tsx"),
120
- `export default function DashLayout({ data, children }) {
121
- return <div class="dash" data-user={data.user} innerHTML={children}></div>;
122
- }`,
123
- );
124
- await writeFile(
125
- join(tmpDir, "dashboard", "index.tsx"),
126
- `export default function Dash() { return <p>dash content</p>; }`,
127
- );
128
- });
129
-
130
- afterAll(async () => {
131
- await rm(tmpDir, { recursive: true });
132
- });
133
-
134
- describe("renderRoute", () => {
135
- test("renders static page", async () => {
136
- const tree = await scanRoutes(tmpDir);
137
- const matched = matchRoute(tree, new URL("http://localhost/"));
138
- const res = await renderRoute(matched!, new Request("http://localhost/"));
139
- const html = await readStream(res);
140
- expect(html).toContain("<h1>Hello Grimoire</h1>");
141
- expect(html).toContain("<!DOCTYPE html>");
142
- });
143
-
144
- test("renders page with load data", async () => {
145
- const tree = await scanRoutes(tmpDir);
146
- const matched = matchRoute(tree, new URL("http://localhost/blog"));
147
- console.log("matched.route.type:", matched?.route.type);
148
- console.log("matched.pageServer:", matched?.pageServer?.filePath);
149
- const res = await renderRoute(
150
- matched!,
151
- new Request("http://localhost/blog"),
152
- );
153
- const html = await readStream(res);
154
- expect(html).toContain("<!--g-->My Blog<!--/g-->");
155
- });
156
-
157
- test("wraps page in layout", async () => {
158
- const tree = await scanRoutes(tmpDir);
159
- const matched = matchRoute(tree, new URL("http://localhost/"));
160
- const res = await renderRoute(matched!, new Request("http://localhost/"));
161
- const html = await readStream(res);
162
- expect(html).toContain('class="app"');
163
- expect(html).toContain("<h1>Hello Grimoire</h1>");
164
- });
165
-
166
- test("returns 404 shape for unmatched route", async () => {
167
- const tree = await scanRoutes(tmpDir);
168
- const matched = matchRoute(tree, new URL("http://localhost/nonexistent"));
169
- expect(matched).toBeNull();
170
- });
171
-
172
- test("injects grimoire state script", async () => {
173
- const tree = await scanRoutes(tmpDir);
174
- const matched = matchRoute(tree, new URL("http://localhost/"));
175
- const res = await renderRoute(matched!, new Request("http://localhost/"));
176
- const html = await readStream(res);
177
- expect(html).toContain("__grimoire_state__");
178
- });
179
-
180
- test("component output is not escaped", async () => {
181
- await writeFile(
182
- join(tmpDir, "inner.tsx"),
183
- `
184
- export default function Inner() {
185
- return <strong>bold</strong>;
186
- }
187
- `,
188
- );
189
- await writeFile(
190
- join(tmpDir, "outer.tsx"),
191
- `
192
- import Inner from './inner';
193
- export default function Outer() {
194
- return <div><Inner /></div>;
195
- }
196
- `,
197
- );
198
-
199
- const tree = await scanRoutes(tmpDir);
200
- const matched = matchRoute(tree, new URL("http://localhost/outer"));
201
- const res = await renderRoute(
202
- matched!,
203
- new Request("http://localhost/outer"),
204
- );
205
- const html = await readStream(res);
206
-
207
- // should contain actual HTML tags, not escaped versions
208
- expect(html).toContain("<strong>bold</strong>");
209
- expect(html).not.toContain("&lt;strong&gt;");
210
- });
211
-
212
- test("user data is escaped", async () => {
213
- await writeFile(
214
- join(tmpDir, "xss.tsx"),
215
- `
216
- export default function XSS() {
217
- const userInput = '<script>alert("xss")</script>';
218
- return <div>{userInput}</div>;
219
- }
220
- `,
221
- );
222
-
223
- const tree = await scanRoutes(tmpDir);
224
- const matched = matchRoute(tree, new URL("http://localhost/xss"));
225
- const res = await renderRoute(
226
- matched!,
227
- new Request("http://localhost/xss"),
228
- );
229
- const html = await readStream(res);
230
-
231
- expect(html).toContain("&lt;script&gt;");
232
- expect(html).not.toContain("<script>alert");
233
- });
234
-
235
- test("load return value is passed as the data prop (SvelteKit convention)", async () => {
236
- const tree = await scanRoutes(tmpDir);
237
- const matched = matchRoute(tree, new URL("http://localhost/blog"));
238
- const res = await renderRoute(matched!, new Request("http://localhost/blog"));
239
- const html = await readStream(res);
240
- // blog/+page.server.ts returns { title: "My Blog" }
241
- // blog/+page.tsx receives { data: { title: "My Blog" } } — not spread
242
- expect(html).toContain("My Blog");
243
- // the state JSON embeds data under the data key
244
- const stateJson = JSON.parse(
245
- html.match(/<script[^>]*>([^<]+)<\/script>/)?.[1] ?? "{}",
246
- );
247
- expect(stateJson.data?.title).toBe("My Blog");
248
- expect(html).not.toContain("[object Object]");
249
- });
250
-
251
- test("redirect() from load returns 3xx with Location header", async () => {
252
- const tree = await scanRoutes(tmpDir);
253
- const matched = matchRoute(tree, new URL("http://localhost/redirect-page"));
254
- const res = await renderRoute(matched!, new Request("http://localhost/redirect-page"));
255
- expect(res.status).toBe(302);
256
- expect(res.headers.get("Location")).toBe("/login");
257
- });
258
-
259
- test("error() from load renders +error.tsx when present", async () => {
260
- const tree = await scanRoutes(tmpDir);
261
- const matched = matchRoute(tree, new URL("http://localhost/error-page"));
262
- const res = await renderRoute(
263
- matched!,
264
- new Request("http://localhost/error-page"),
265
- tree.errors,
266
- );
267
- expect(res.status).toBe(404);
268
- const html = await res.text();
269
- expect(html).toContain('class="error"');
270
- expect(html).toContain("404");
271
- expect(html).toContain("Not found");
272
- });
273
-
274
- test("error() from load falls back to plain text when no error routes provided", async () => {
275
- const tree = await scanRoutes(tmpDir);
276
- const matched = matchRoute(tree, new URL("http://localhost/error-page"));
277
- const res = await renderRoute(
278
- matched!,
279
- new Request("http://localhost/error-page"),
280
- [], // no error routes
281
- );
282
- expect(res.status).toBe(404);
283
- const text = await res.text();
284
- expect(text).toBe("Not found");
285
- });
286
-
287
- test("locals passed to renderRoute are visible inside load()", async () => {
288
- const tree = await scanRoutes(tmpDir);
289
- const matched = matchRoute(tree, new URL("http://localhost/locals-test"));
290
- const res = await renderRoute(
291
- matched!,
292
- new Request("http://localhost/locals-test"),
293
- [],
294
- undefined,
295
- { tag: "from-hook" },
296
- );
297
- const html = await readStream(res);
298
- expect(html).toContain("from-hook");
299
- });
300
-
301
- test("+layout.server.ts load data is passed to the layout component", async () => {
302
- const tree = await scanRoutes(tmpDir);
303
- const matched = matchRoute(tree, new URL("http://localhost/dashboard"));
304
- const res = await renderRoute(matched!, new Request("http://localhost/dashboard"));
305
- const html = await readStream(res);
306
- expect(html).toContain('class="dash"');
307
- expect(html).toContain('data-user="admin"');
308
- expect(html).toContain("dash content");
309
- });
310
- });
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdir, rm, writeFile } from "fs/promises";
3
+ import { tmpdir as osTmpDir } from "os";
4
+ import { join } from "path";
5
+ import { renderRoute } from "../src/renderer";
6
+ import { matchRoute } from "../src/router";
7
+ import { scanRoutes } from "../src/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
+ // root error page — renders status and message
68
+ await writeFile(
69
+ join(tmpDir, "+error.tsx"),
70
+ `
71
+ export default function ErrorPage({ status, message }) {
72
+ return <div class="error"><span class="status">{status}</span><span class="msg">{message}</span></div>;
73
+ }
74
+ `,
75
+ );
76
+
77
+ // page whose load throws redirect — use inline object so no grimoire import needed from /tmp
78
+ await mkdir(join(tmpDir, "redirect-page"), { recursive: true });
79
+ await writeFile(
80
+ join(tmpDir, "redirect-page", "+page.server.ts"),
81
+ `export async function load() { throw { __redirect: true, status: 302, location: "/login" }; }`,
82
+ );
83
+ await writeFile(
84
+ join(tmpDir, "redirect-page", "+page.tsx"),
85
+ `export default function P() { return <div>never</div>; }`,
86
+ );
87
+
88
+ // page whose load throws error — same pattern
89
+ await mkdir(join(tmpDir, "error-page"), { recursive: true });
90
+ await writeFile(
91
+ join(tmpDir, "error-page", "+page.server.ts"),
92
+ `export async function load() { throw { __error: true, status: 404, message: "Not found" }; }`,
93
+ );
94
+ await writeFile(
95
+ join(tmpDir, "error-page", "+page.tsx"),
96
+ `export default function P() { return <div>never</div>; }`,
97
+ );
98
+
99
+ // page whose load returns locals.tag (tests locals passthrough)
100
+ await mkdir(join(tmpDir, "locals-test"), { recursive: true });
101
+ await writeFile(
102
+ join(tmpDir, "locals-test", "+page.server.ts"),
103
+ `
104
+ export async function load({ locals }) { return { tag: locals.tag ?? "none" }; }
105
+ `,
106
+ );
107
+ await writeFile(
108
+ join(tmpDir, "locals-test", "+page.tsx"),
109
+ `export default function P({ data }) { return <div class="tag">{data.tag}</div>; }`,
110
+ );
111
+
112
+ // layout.server test — nested layout with server data
113
+ await mkdir(join(tmpDir, "dashboard"), { recursive: true });
114
+ await writeFile(
115
+ join(tmpDir, "dashboard", "+layout.server.ts"),
116
+ `export async function load() { return { user: "admin" }; }`,
117
+ );
118
+ await writeFile(
119
+ join(tmpDir, "dashboard", "+layout.tsx"),
120
+ `export default function DashLayout({ data, children }) {
121
+ return <div class="dash" data-user={data.user} innerHTML={children}></div>;
122
+ }`,
123
+ );
124
+ await writeFile(
125
+ join(tmpDir, "dashboard", "index.tsx"),
126
+ `export default function Dash() { return <p>dash content</p>; }`,
127
+ );
128
+ });
129
+
130
+ afterAll(async () => {
131
+ await rm(tmpDir, { recursive: true });
132
+ });
133
+
134
+ describe("renderRoute", () => {
135
+ test("renders static page", async () => {
136
+ const tree = await scanRoutes(tmpDir);
137
+ const matched = matchRoute(tree, new URL("http://localhost/"));
138
+ const res = await renderRoute(matched!, new Request("http://localhost/"));
139
+ const html = await readStream(res);
140
+ expect(html).toContain("<h1>Hello Grimoire</h1>");
141
+ expect(html).toContain("<!DOCTYPE html>");
142
+ });
143
+
144
+ test("renders page with load data", async () => {
145
+ const tree = await scanRoutes(tmpDir);
146
+ const matched = matchRoute(tree, new URL("http://localhost/blog"));
147
+ console.log("matched.route.type:", matched?.route.type);
148
+ console.log("matched.pageServer:", matched?.pageServer?.filePath);
149
+ const res = await renderRoute(
150
+ matched!,
151
+ new Request("http://localhost/blog"),
152
+ );
153
+ const html = await readStream(res);
154
+ expect(html).toContain("<!--g-->My Blog<!--/g-->");
155
+ });
156
+
157
+ test("wraps page in layout", async () => {
158
+ const tree = await scanRoutes(tmpDir);
159
+ const matched = matchRoute(tree, new URL("http://localhost/"));
160
+ const res = await renderRoute(matched!, new Request("http://localhost/"));
161
+ const html = await readStream(res);
162
+ expect(html).toContain('class="app"');
163
+ expect(html).toContain("<h1>Hello Grimoire</h1>");
164
+ });
165
+
166
+ test("returns 404 shape for unmatched route", async () => {
167
+ const tree = await scanRoutes(tmpDir);
168
+ const matched = matchRoute(tree, new URL("http://localhost/nonexistent"));
169
+ expect(matched).toBeNull();
170
+ });
171
+
172
+ test("injects grimoire state script", async () => {
173
+ const tree = await scanRoutes(tmpDir);
174
+ const matched = matchRoute(tree, new URL("http://localhost/"));
175
+ const res = await renderRoute(matched!, new Request("http://localhost/"));
176
+ const html = await readStream(res);
177
+ expect(html).toContain("__grimoire_state__");
178
+ });
179
+
180
+ test("component output is not escaped", async () => {
181
+ await writeFile(
182
+ join(tmpDir, "inner.tsx"),
183
+ `
184
+ export default function Inner() {
185
+ return <strong>bold</strong>;
186
+ }
187
+ `,
188
+ );
189
+ await writeFile(
190
+ join(tmpDir, "outer.tsx"),
191
+ `
192
+ import Inner from './inner';
193
+ export default function Outer() {
194
+ return <div><Inner /></div>;
195
+ }
196
+ `,
197
+ );
198
+
199
+ const tree = await scanRoutes(tmpDir);
200
+ const matched = matchRoute(tree, new URL("http://localhost/outer"));
201
+ const res = await renderRoute(
202
+ matched!,
203
+ new Request("http://localhost/outer"),
204
+ );
205
+ const html = await readStream(res);
206
+
207
+ // should contain actual HTML tags, not escaped versions
208
+ expect(html).toContain("<strong>bold</strong>");
209
+ expect(html).not.toContain("&lt;strong&gt;");
210
+ });
211
+
212
+ test("user data is escaped", async () => {
213
+ await writeFile(
214
+ join(tmpDir, "xss.tsx"),
215
+ `
216
+ export default function XSS() {
217
+ const userInput = '<script>alert("xss")</script>';
218
+ return <div>{userInput}</div>;
219
+ }
220
+ `,
221
+ );
222
+
223
+ const tree = await scanRoutes(tmpDir);
224
+ const matched = matchRoute(tree, new URL("http://localhost/xss"));
225
+ const res = await renderRoute(
226
+ matched!,
227
+ new Request("http://localhost/xss"),
228
+ );
229
+ const html = await readStream(res);
230
+
231
+ expect(html).toContain("&lt;script&gt;");
232
+ expect(html).not.toContain("<script>alert");
233
+ });
234
+
235
+ test("load return value is passed as the data prop (SvelteKit convention)", async () => {
236
+ const tree = await scanRoutes(tmpDir);
237
+ const matched = matchRoute(tree, new URL("http://localhost/blog"));
238
+ const res = await renderRoute(matched!, new Request("http://localhost/blog"));
239
+ const html = await readStream(res);
240
+ // blog/+page.server.ts returns { title: "My Blog" }
241
+ // blog/+page.tsx receives { data: { title: "My Blog" } } — not spread
242
+ expect(html).toContain("My Blog");
243
+ // the state JSON embeds data under the data key
244
+ const stateJson = JSON.parse(
245
+ html.match(/<script[^>]*>([^<]+)<\/script>/)?.[1] ?? "{}",
246
+ );
247
+ expect(stateJson.data?.title).toBe("My Blog");
248
+ expect(html).not.toContain("[object Object]");
249
+ });
250
+
251
+ test("redirect() from load returns 3xx with Location header", async () => {
252
+ const tree = await scanRoutes(tmpDir);
253
+ const matched = matchRoute(tree, new URL("http://localhost/redirect-page"));
254
+ const res = await renderRoute(matched!, new Request("http://localhost/redirect-page"));
255
+ expect(res.status).toBe(302);
256
+ expect(res.headers.get("Location")).toBe("/login");
257
+ });
258
+
259
+ test("error() from load renders +error.tsx when present", async () => {
260
+ const tree = await scanRoutes(tmpDir);
261
+ const matched = matchRoute(tree, new URL("http://localhost/error-page"));
262
+ const res = await renderRoute(
263
+ matched!,
264
+ new Request("http://localhost/error-page"),
265
+ tree.errors,
266
+ );
267
+ expect(res.status).toBe(404);
268
+ const html = await res.text();
269
+ expect(html).toContain('class="error"');
270
+ expect(html).toContain("404");
271
+ expect(html).toContain("Not found");
272
+ });
273
+
274
+ test("error() from load falls back to plain text when no error routes provided", async () => {
275
+ const tree = await scanRoutes(tmpDir);
276
+ const matched = matchRoute(tree, new URL("http://localhost/error-page"));
277
+ const res = await renderRoute(
278
+ matched!,
279
+ new Request("http://localhost/error-page"),
280
+ [], // no error routes
281
+ );
282
+ expect(res.status).toBe(404);
283
+ const text = await res.text();
284
+ expect(text).toBe("Not found");
285
+ });
286
+
287
+ test("locals passed to renderRoute are visible inside load()", async () => {
288
+ const tree = await scanRoutes(tmpDir);
289
+ const matched = matchRoute(tree, new URL("http://localhost/locals-test"));
290
+ const res = await renderRoute(
291
+ matched!,
292
+ new Request("http://localhost/locals-test"),
293
+ [],
294
+ undefined,
295
+ { tag: "from-hook" },
296
+ );
297
+ const html = await readStream(res);
298
+ expect(html).toContain("from-hook");
299
+ });
300
+
301
+ test("+layout.server.ts load data is passed to the layout component", async () => {
302
+ const tree = await scanRoutes(tmpDir);
303
+ const matched = matchRoute(tree, new URL("http://localhost/dashboard"));
304
+ const res = await renderRoute(matched!, new Request("http://localhost/dashboard"));
305
+ const html = await readStream(res);
306
+ expect(html).toContain('class="dash"');
307
+ expect(html).toContain('data-user="admin"');
308
+ expect(html).toContain("dash content");
309
+ });
310
+ });