@sigil-dev/grimoire 0.9.20 → 0.9.22

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 (94) hide show
  1. package/README.md +83 -83
  2. package/index.ts +50 -48
  3. package/package.json +16 -5
  4. package/public/__grimoire__/grimoire-client.js +12 -0
  5. package/public/__grimoire__/runtime.js +1241 -0
  6. package/server.ts +22 -20
  7. package/src/adapters/bun.ts +17 -0
  8. package/src/adapters/static.ts +41 -0
  9. package/src/adapters/types.ts +36 -0
  10. package/src/base.ts +91 -0
  11. package/src/client/csrf.ts +47 -47
  12. package/src/client/deferred.ts +179 -179
  13. package/src/client/enhance.ts +160 -160
  14. package/src/client/router.ts +870 -833
  15. package/src/dev/compile-module.ts +211 -211
  16. package/src/dev/loader.ts +141 -141
  17. package/src/dev/resolve.ts +70 -70
  18. package/src/dev/runtime-bundle.ts +51 -51
  19. package/src/env/modules.ts +62 -62
  20. package/src/env/required.ts +42 -0
  21. package/src/env/typegen.ts +61 -61
  22. package/src/headers.ts +105 -114
  23. package/src/logger/transports.ts +117 -117
  24. package/src/rendering/head.ts +49 -49
  25. package/src/rendering/hydrate.ts +161 -161
  26. package/src/rendering/images.ts +43 -43
  27. package/src/rendering/index.ts +755 -745
  28. package/src/rendering/preload.ts +101 -95
  29. package/src/rendering/ssrPlugin.ts +108 -108
  30. package/src/rendering/timing.ts +40 -40
  31. package/src/routing/router.ts +186 -186
  32. package/src/routing/scanner.ts +225 -196
  33. package/src/routing/transform-routes.ts +102 -102
  34. package/src/server/after-response.ts +206 -194
  35. package/src/server/asset-cache.ts +57 -55
  36. package/src/server/build.ts +262 -249
  37. package/src/server/cached.ts +114 -114
  38. package/src/server/client-address.ts +82 -82
  39. package/src/server/client-dir.ts +57 -57
  40. package/src/server/client-entry.ts +44 -0
  41. package/src/server/cookie-utils.ts +80 -80
  42. package/src/server/coordinator.ts +476 -471
  43. package/src/server/hooks.ts +122 -122
  44. package/src/server/index.ts +1138 -1022
  45. package/src/server/plugins.ts +172 -172
  46. package/src/server/prerender.ts +444 -0
  47. package/src/server/reserved.ts +17 -0
  48. package/src/server/resolver.ts +98 -98
  49. package/src/server/rewrite.ts +28 -0
  50. package/src/server/warm.ts +91 -91
  51. package/src/server/with-headers.ts +27 -0
  52. package/src/server/worker.ts +74 -72
  53. package/src/testing/dom.ts +73 -0
  54. package/src/testing/index.ts +169 -0
  55. package/src/testing/server.ts +162 -0
  56. package/src/typegen/index.ts +450 -450
  57. package/src/types.ts +501 -432
  58. package/test/after-response.test.ts +132 -113
  59. package/test/asset-cache-policy.test.ts +96 -96
  60. package/test/base.test.ts +134 -0
  61. package/test/cached.test.ts +126 -126
  62. package/test/client-address.test.ts +78 -78
  63. package/test/client-csrf.test.ts +61 -61
  64. package/test/client-dir.test.ts +101 -69
  65. package/test/client-router.test.ts +789 -734
  66. package/test/coordinator.test.ts +96 -94
  67. package/test/deferred.test.ts +256 -256
  68. package/test/dev-loader.test.ts +30 -30
  69. package/test/env.test.ts +168 -168
  70. package/test/force-production.test.ts +44 -0
  71. package/test/images.test.ts +49 -49
  72. package/test/locals-header.test.ts +61 -0
  73. package/test/middleware.test.ts +243 -243
  74. package/test/parser-plugins.test.ts +92 -92
  75. package/test/plugin-handle.test.ts +62 -62
  76. package/test/plugin-services-context.test.ts +97 -97
  77. package/test/preload.test.ts +48 -48
  78. package/test/prerender.test.ts +353 -0
  79. package/test/rendering.test.ts +798 -798
  80. package/test/required-env.test.ts +32 -0
  81. package/test/rewrite-html.test.ts +90 -0
  82. package/test/scanning.test.ts +349 -314
  83. package/test/server.test.ts +640 -619
  84. package/test/services.test.ts +90 -90
  85. package/test/streaming.test.ts +378 -378
  86. package/test/testing-helpers.test.ts +166 -0
  87. package/test/typegen.test.ts +805 -799
  88. package/test/warm.test.ts +96 -96
  89. package/test/worker-context.test.ts +59 -59
  90. package/test/workspace-files.test.ts +149 -146
  91. package/test/workspace-versions.test.ts +131 -131
  92. package/.grimoire/_routes.ts +0 -4
  93. package/public/__grimoire__/client.js +0 -55
  94. package/public/__grimoire__/hydrate.js +0 -63
@@ -1,314 +1,349 @@
1
- import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
- import { mkdir, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import { filePathToRoutePath, scanRoutes } from "../src/routing/scanner";
6
-
7
- describe("File scanning", () => {
8
- test("index.tsx → /", () => {
9
- expect(filePathToRoutePath("/app/routes/index.tsx", "/app/routes")).toEqual(
10
- {
11
- pattern: "/",
12
- params: [],
13
- optionalParams: [],
14
- paramMatchers: {},
15
- restParams: [],
16
- },
17
- );
18
- });
19
-
20
- test("about.tsx → /about", () => {
21
- expect(filePathToRoutePath("/app/routes/about.tsx", "/app/routes")).toEqual(
22
- {
23
- pattern: "/about",
24
- params: [],
25
- optionalParams: [],
26
- paramMatchers: {},
27
- restParams: [],
28
- },
29
- );
30
- });
31
-
32
- test("[slug].tsx → /:slug", () => {
33
- expect(
34
- filePathToRoutePath("/app/routes/[slug].tsx", "/app/routes"),
35
- ).toEqual({
36
- pattern: "/:slug",
37
- params: ["slug"],
38
- optionalParams: [],
39
- paramMatchers: {},
40
- restParams: [],
41
- });
42
- });
43
-
44
- test("blog/+page.tsx → /blog", () => {
45
- expect(
46
- filePathToRoutePath("/app/routes/blog/+page.tsx", "/app/routes"),
47
- ).toEqual({
48
- pattern: "/blog",
49
- params: [],
50
- optionalParams: [],
51
- paramMatchers: {},
52
- restParams: [],
53
- });
54
- });
55
-
56
- test("blog/[slug]/+page.tsx → /blog/:slug", () => {
57
- expect(
58
- filePathToRoutePath("/app/routes/blog/[slug]/+page.tsx", "/app/routes"),
59
- ).toEqual({
60
- pattern: "/blog/:slug",
61
- params: ["slug"],
62
- optionalParams: [],
63
- paramMatchers: {},
64
- restParams: [],
65
- });
66
- });
67
-
68
- test("blog/[slug]/+page.server.ts → /blog/:slug", () => {
69
- expect(
70
- filePathToRoutePath(
71
- "/app/routes/blog/[slug]/+page.server.ts",
72
- "/app/routes",
73
- ),
74
- ).toEqual({
75
- pattern: "/blog/:slug",
76
- params: ["slug"],
77
- optionalParams: [],
78
- paramMatchers: {},
79
- restParams: [],
80
- });
81
- });
82
-
83
- test("+layout.tsx → /", () => {
84
- expect(
85
- filePathToRoutePath("/app/routes/+layout.tsx", "/app/routes"),
86
- ).toEqual({
87
- pattern: "/",
88
- params: [],
89
- optionalParams: [],
90
- paramMatchers: {},
91
- restParams: [],
92
- });
93
- });
94
-
95
- test("nested +layout.tsx → /blog", () => {
96
- expect(
97
- filePathToRoutePath("/app/routes/blog/+layout.tsx", "/app/routes"),
98
- ).toEqual({
99
- pattern: "/blog",
100
- params: [],
101
- optionalParams: [],
102
- paramMatchers: {},
103
- restParams: [],
104
- });
105
- });
106
-
107
- test("[...rest].tsx → /*rest (catch-all)", () => {
108
- expect(
109
- filePathToRoutePath("/app/routes/[...rest].tsx", "/app/routes"),
110
- ).toEqual({
111
- pattern: "/*rest",
112
- params: [],
113
- optionalParams: [],
114
- paramMatchers: {},
115
- restParams: ["rest"],
116
- });
117
- });
118
-
119
- test("docs/[...path]/+server.ts → /docs/*path (nested catch-all)", () => {
120
- expect(
121
- filePathToRoutePath(
122
- "/app/routes/docs/[...path]/+server.ts",
123
- "/app/routes",
124
- ),
125
- ).toEqual({
126
- pattern: "/docs/*path",
127
- params: [],
128
- optionalParams: [],
129
- paramMatchers: {},
130
- restParams: ["path"],
131
- });
132
- });
133
-
134
- test("about/index.tsx, about/+page.tsx, and about.tsx all resolve to /about", () => {
135
- const index = filePathToRoutePath(
136
- "/app/routes/about/index.tsx",
137
- "/app/routes",
138
- );
139
- const page = filePathToRoutePath(
140
- "/app/routes/about/+page.tsx",
141
- "/app/routes",
142
- );
143
- const bare = filePathToRoutePath("/app/routes/about.tsx", "/app/routes");
144
- expect(index.pattern).toBe("/about");
145
- expect(page.pattern).toBe("/about");
146
- expect(bare.pattern).toBe("/about");
147
- });
148
-
149
- test("+server.ts resolves to the same path as +page.tsx", () => {
150
- const server = filePathToRoutePath(
151
- "/app/routes/api/items/+server.ts",
152
- "/app/routes",
153
- );
154
- expect(server.pattern).toBe("/api/items");
155
- });
156
-
157
- test("+error.tsx resolves to its directory path", () => {
158
- const root = filePathToRoutePath("/app/routes/+error.tsx", "/app/routes");
159
- const nested = filePathToRoutePath(
160
- "/app/routes/blog/+error.tsx",
161
- "/app/routes",
162
- );
163
- expect(root.pattern).toBe("/");
164
- expect(nested.pattern).toBe("/blog");
165
- });
166
-
167
- test("(group)/page/+page.tsx → /page", () => {
168
- const { pattern } = filePathToRoutePath(
169
- "/routes/(marketing)/about/+page.tsx",
170
- "/routes",
171
- );
172
- expect(pattern).toBe("/about");
173
- });
174
-
175
- test("nested groups collapse correctly", () => {
176
- const { pattern } = filePathToRoutePath(
177
- "/routes/(app)/(auth)/login/+page.tsx",
178
- "/routes",
179
- );
180
- expect(pattern).toBe("/login");
181
- });
182
-
183
- test("[[lang]]/index.tsx → /:lang? (optional)", () => {
184
- const result = filePathToRoutePath(
185
- "/app/routes/[[lang]]/index.tsx",
186
- "/app/routes",
187
- );
188
- expect(result.pattern).toBe("/:lang?");
189
- expect(result.optionalParams).toEqual(["lang"]);
190
- expect(result.params).toEqual([]);
191
- });
192
-
193
- test("optional and required params are collected separately", () => {
194
- const result = filePathToRoutePath(
195
- "/app/routes/[[lang]]/blog/[slug]/+page.tsx",
196
- "/app/routes",
197
- );
198
- expect(result.pattern).toBe("/:lang?/blog/:slug");
199
- expect(result.optionalParams).toEqual(["lang"]);
200
- expect(result.params).toEqual(["slug"]);
201
- });
202
- });
203
-
204
- describe("Custom param matchers", () => {
205
- test("[id=int].tsx → /:id with matcher recorded", () => {
206
- const result = filePathToRoutePath(
207
- "/app/routes/post/[id=int].tsx",
208
- "/app/routes",
209
- );
210
- expect(result.pattern).toBe("/post/:id");
211
- expect(result.paramMatchers).toEqual({ id: "int" });
212
- expect(result.params).toEqual(["id"]);
213
- });
214
-
215
- test("scanRoutes picks up src/params/*.ts beside src/routes", async () => {
216
- const root = join(tmpdir(), `grimoire-matchers-${Date.now()}`);
217
- const routesDir = join(root, "routes");
218
- const paramsDir = join(root, "params");
219
- await mkdir(join(routesDir, "post"), { recursive: true });
220
- await mkdir(paramsDir, { recursive: true });
221
- await writeFile(
222
- join(routesDir, "post", "[id=int].tsx"),
223
- "export default () => null",
224
- );
225
- await writeFile(
226
- join(paramsDir, "int.ts"),
227
- "export function match(v: string) { return /^\\d+$/.test(v); }",
228
- );
229
- const tree = await scanRoutes(routesDir);
230
- expect(tree.paramMatcherFiles).toEqual([
231
- { name: "int", filePath: join(paramsDir, "int.ts") },
232
- ]);
233
- expect(tree.routes[0]?.paramMatchers).toEqual({ id: "int" });
234
- await rm(root, { recursive: true });
235
- });
236
- });
237
-
238
- describe("Duplicate route detection", () => {
239
- let tmpDir: string;
240
-
241
- beforeAll(async () => {
242
- tmpDir = join(tmpdir(), `grimoire-scan-${Date.now()}`);
243
- await mkdir(tmpDir, { recursive: true });
244
- });
245
-
246
- afterAll(async () => {
247
- await rm(tmpDir, { recursive: true });
248
- });
249
-
250
- test("throws when two page files resolve to the same path", async () => {
251
- const dir = join(tmpDir, "dup-pages");
252
- await mkdir(join(dir, "about"), { recursive: true });
253
- await writeFile(join(dir, "about.tsx"), "export default () => null");
254
- await writeFile(
255
- join(dir, "about", "index.tsx"),
256
- "export default () => null",
257
- );
258
- await expect(scanRoutes(dir)).rejects.toThrow('Duplicate page at "/about"');
259
- });
260
-
261
- test("+page.tsx and +page.server.ts at the same path do NOT conflict", async () => {
262
- const dir = join(tmpDir, "no-conflict");
263
- await mkdir(join(dir, "blog"), { recursive: true });
264
- await writeFile(
265
- join(dir, "blog", "+page.tsx"),
266
- "export default () => null",
267
- );
268
- await writeFile(
269
- join(dir, "blog", "+page.server.ts"),
270
- "export async function load() {}",
271
- );
272
- await expect(scanRoutes(dir)).resolves.toBeDefined();
273
- });
274
-
275
- test("throws when two API routes resolve to the same path", async () => {
276
- const dir = join(tmpDir, "dup-server");
277
- await mkdir(join(dir, "api", "items"), { recursive: true });
278
- // Bun glob finds all files; we simulate two +server files at same path via subdirs
279
- // Use two different nesting depths that produce the same URL
280
- await mkdir(join(dir, "api2"), { recursive: true });
281
- await writeFile(
282
- join(dir, "api2", "+server.ts"),
283
- "export async function GET() {}",
284
- );
285
- await mkdir(join(dir, "api2", "index"), { recursive: true });
286
- // Can't easily duplicate +server.ts via nesting — test the named-file vs +server collision instead
287
- // Two +server.ts under different folder structures that map to same URL isn't possible without
288
- // [param] collisions, so verify a simple valid tree is fine
289
- await expect(scanRoutes(dir)).resolves.toBeDefined();
290
- });
291
-
292
- test("throws when two error pages resolve to the same path", async () => {
293
- const dir = join(tmpDir, "dup-error");
294
- await mkdir(join(dir, "blog"), { recursive: true });
295
- // Two +error.tsx at root level isn't possible via filesystem (same filename).
296
- // The real collision scenario is two dynamic segments mapping to the same pattern.
297
- // Test that a single error page is accepted without error.
298
- await writeFile(join(dir, "+error.tsx"), "export default () => null");
299
- await expect(scanRoutes(dir)).resolves.toBeDefined();
300
- });
301
-
302
- test("throws when a simple file and +page.tsx both resolve to the same path", async () => {
303
- const dir = join(tmpDir, "dup-mixed");
304
- await mkdir(join(dir, "contact"), { recursive: true });
305
- await writeFile(join(dir, "contact.tsx"), "export default () => null");
306
- await writeFile(
307
- join(dir, "contact", "+page.tsx"),
308
- "export default () => null",
309
- );
310
- await expect(scanRoutes(dir)).rejects.toThrow(
311
- 'Duplicate page at "/contact"',
312
- );
313
- });
314
- });
1
+ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2
+ import { mkdir, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { filePathToRoutePath, scanRoutes } from "../src/routing/scanner";
6
+
7
+ describe("File scanning", () => {
8
+ test("index.tsx → /", () => {
9
+ expect(filePathToRoutePath("/app/routes/index.tsx", "/app/routes")).toEqual(
10
+ {
11
+ pattern: "/",
12
+ params: [],
13
+ optionalParams: [],
14
+ paramMatchers: {},
15
+ restParams: [],
16
+ },
17
+ );
18
+ });
19
+
20
+ test("about.tsx → /about", () => {
21
+ expect(filePathToRoutePath("/app/routes/about.tsx", "/app/routes")).toEqual(
22
+ {
23
+ pattern: "/about",
24
+ params: [],
25
+ optionalParams: [],
26
+ paramMatchers: {},
27
+ restParams: [],
28
+ },
29
+ );
30
+ });
31
+
32
+ test("[slug].tsx → /:slug", () => {
33
+ expect(
34
+ filePathToRoutePath("/app/routes/[slug].tsx", "/app/routes"),
35
+ ).toEqual({
36
+ pattern: "/:slug",
37
+ params: ["slug"],
38
+ optionalParams: [],
39
+ paramMatchers: {},
40
+ restParams: [],
41
+ });
42
+ });
43
+
44
+ test("blog/+page.tsx → /blog", () => {
45
+ expect(
46
+ filePathToRoutePath("/app/routes/blog/+page.tsx", "/app/routes"),
47
+ ).toEqual({
48
+ pattern: "/blog",
49
+ params: [],
50
+ optionalParams: [],
51
+ paramMatchers: {},
52
+ restParams: [],
53
+ });
54
+ });
55
+
56
+ test("blog/[slug]/+page.tsx → /blog/:slug", () => {
57
+ expect(
58
+ filePathToRoutePath("/app/routes/blog/[slug]/+page.tsx", "/app/routes"),
59
+ ).toEqual({
60
+ pattern: "/blog/:slug",
61
+ params: ["slug"],
62
+ optionalParams: [],
63
+ paramMatchers: {},
64
+ restParams: [],
65
+ });
66
+ });
67
+
68
+ test("blog/[slug]/+page.server.ts → /blog/:slug", () => {
69
+ expect(
70
+ filePathToRoutePath(
71
+ "/app/routes/blog/[slug]/+page.server.ts",
72
+ "/app/routes",
73
+ ),
74
+ ).toEqual({
75
+ pattern: "/blog/:slug",
76
+ params: ["slug"],
77
+ optionalParams: [],
78
+ paramMatchers: {},
79
+ restParams: [],
80
+ });
81
+ });
82
+
83
+ test("+layout.tsx → /", () => {
84
+ expect(
85
+ filePathToRoutePath("/app/routes/+layout.tsx", "/app/routes"),
86
+ ).toEqual({
87
+ pattern: "/",
88
+ params: [],
89
+ optionalParams: [],
90
+ paramMatchers: {},
91
+ restParams: [],
92
+ });
93
+ });
94
+
95
+ test("nested +layout.tsx → /blog", () => {
96
+ expect(
97
+ filePathToRoutePath("/app/routes/blog/+layout.tsx", "/app/routes"),
98
+ ).toEqual({
99
+ pattern: "/blog",
100
+ params: [],
101
+ optionalParams: [],
102
+ paramMatchers: {},
103
+ restParams: [],
104
+ });
105
+ });
106
+
107
+ test("[...rest].tsx → /*rest (catch-all)", () => {
108
+ expect(
109
+ filePathToRoutePath("/app/routes/[...rest].tsx", "/app/routes"),
110
+ ).toEqual({
111
+ pattern: "/*rest",
112
+ params: [],
113
+ optionalParams: [],
114
+ paramMatchers: {},
115
+ restParams: ["rest"],
116
+ });
117
+ });
118
+
119
+ test("docs/[...path]/+server.ts → /docs/*path (nested catch-all)", () => {
120
+ expect(
121
+ filePathToRoutePath(
122
+ "/app/routes/docs/[...path]/+server.ts",
123
+ "/app/routes",
124
+ ),
125
+ ).toEqual({
126
+ pattern: "/docs/*path",
127
+ params: [],
128
+ optionalParams: [],
129
+ paramMatchers: {},
130
+ restParams: ["path"],
131
+ });
132
+ });
133
+
134
+ test("about/index.tsx, about/+page.tsx, and about.tsx all resolve to /about", () => {
135
+ const index = filePathToRoutePath(
136
+ "/app/routes/about/index.tsx",
137
+ "/app/routes",
138
+ );
139
+ const page = filePathToRoutePath(
140
+ "/app/routes/about/+page.tsx",
141
+ "/app/routes",
142
+ );
143
+ const bare = filePathToRoutePath("/app/routes/about.tsx", "/app/routes");
144
+ expect(index.pattern).toBe("/about");
145
+ expect(page.pattern).toBe("/about");
146
+ expect(bare.pattern).toBe("/about");
147
+ });
148
+
149
+ test("+server.ts resolves to the same path as +page.tsx", () => {
150
+ const server = filePathToRoutePath(
151
+ "/app/routes/api/items/+server.ts",
152
+ "/app/routes",
153
+ );
154
+ expect(server.pattern).toBe("/api/items");
155
+ });
156
+
157
+ test("+error.tsx resolves to its directory path", () => {
158
+ const root = filePathToRoutePath("/app/routes/+error.tsx", "/app/routes");
159
+ const nested = filePathToRoutePath(
160
+ "/app/routes/blog/+error.tsx",
161
+ "/app/routes",
162
+ );
163
+ expect(root.pattern).toBe("/");
164
+ expect(nested.pattern).toBe("/blog");
165
+ });
166
+
167
+ test("(group)/page/+page.tsx → /page", () => {
168
+ const { pattern } = filePathToRoutePath(
169
+ "/routes/(marketing)/about/+page.tsx",
170
+ "/routes",
171
+ );
172
+ expect(pattern).toBe("/about");
173
+ });
174
+
175
+ test("nested groups collapse correctly", () => {
176
+ const { pattern } = filePathToRoutePath(
177
+ "/routes/(app)/(auth)/login/+page.tsx",
178
+ "/routes",
179
+ );
180
+ expect(pattern).toBe("/login");
181
+ });
182
+
183
+ test("[[lang]]/index.tsx → /:lang? (optional)", () => {
184
+ const result = filePathToRoutePath(
185
+ "/app/routes/[[lang]]/index.tsx",
186
+ "/app/routes",
187
+ );
188
+ expect(result.pattern).toBe("/:lang?");
189
+ expect(result.optionalParams).toEqual(["lang"]);
190
+ expect(result.params).toEqual([]);
191
+ });
192
+
193
+ test("optional and required params are collected separately", () => {
194
+ const result = filePathToRoutePath(
195
+ "/app/routes/[[lang]]/blog/[slug]/+page.tsx",
196
+ "/app/routes",
197
+ );
198
+ expect(result.pattern).toBe("/:lang?/blog/:slug");
199
+ expect(result.optionalParams).toEqual(["lang"]);
200
+ expect(result.params).toEqual(["slug"]);
201
+ });
202
+ });
203
+
204
+ describe("Custom param matchers", () => {
205
+ test("[id=int].tsx → /:id with matcher recorded", () => {
206
+ const result = filePathToRoutePath(
207
+ "/app/routes/post/[id=int].tsx",
208
+ "/app/routes",
209
+ );
210
+ expect(result.pattern).toBe("/post/:id");
211
+ expect(result.paramMatchers).toEqual({ id: "int" });
212
+ expect(result.params).toEqual(["id"]);
213
+ });
214
+
215
+ test("scanRoutes picks up src/params/*.ts beside src/routes", async () => {
216
+ const root = join(tmpdir(), `grimoire-matchers-${Date.now()}`);
217
+ const routesDir = join(root, "routes");
218
+ const paramsDir = join(root, "params");
219
+ await mkdir(join(routesDir, "post"), { recursive: true });
220
+ await mkdir(paramsDir, { recursive: true });
221
+ await writeFile(
222
+ join(routesDir, "post", "[id=int].tsx"),
223
+ "export default () => null",
224
+ );
225
+ await writeFile(
226
+ join(paramsDir, "int.ts"),
227
+ "export function match(v: string) { return /^\\d+$/.test(v); }",
228
+ );
229
+ const tree = await scanRoutes(routesDir);
230
+ expect(tree.paramMatcherFiles).toEqual([
231
+ { name: "int", filePath: join(paramsDir, "int.ts") },
232
+ ]);
233
+ expect(tree.routes[0]?.paramMatchers).toEqual({ id: "int" });
234
+ await rm(root, { recursive: true });
235
+ });
236
+ });
237
+
238
+ describe("Duplicate route detection", () => {
239
+ let tmpDir: string;
240
+
241
+ beforeAll(async () => {
242
+ tmpDir = join(tmpdir(), `grimoire-scan-${Date.now()}`);
243
+ await mkdir(tmpDir, { recursive: true });
244
+ });
245
+
246
+ afterAll(async () => {
247
+ await rm(tmpDir, { recursive: true });
248
+ });
249
+
250
+ test("throws when two page files resolve to the same path", async () => {
251
+ const dir = join(tmpDir, "dup-pages");
252
+ await mkdir(join(dir, "about"), { recursive: true });
253
+ await writeFile(join(dir, "about.tsx"), "export default () => null");
254
+ await writeFile(
255
+ join(dir, "about", "index.tsx"),
256
+ "export default () => null",
257
+ );
258
+ await expect(scanRoutes(dir)).rejects.toThrow('Duplicate page at "/about"');
259
+ });
260
+
261
+ test("+page.tsx and +page.server.ts at the same path do NOT conflict", async () => {
262
+ const dir = join(tmpDir, "no-conflict");
263
+ await mkdir(join(dir, "blog"), { recursive: true });
264
+ await writeFile(
265
+ join(dir, "blog", "+page.tsx"),
266
+ "export default () => null",
267
+ );
268
+ await writeFile(
269
+ join(dir, "blog", "+page.server.ts"),
270
+ "export async function load() {}",
271
+ );
272
+ await expect(scanRoutes(dir)).resolves.toBeDefined();
273
+ });
274
+
275
+ test("test files beside routes are not routes", async () => {
276
+ const dir = join(tmpDir, "tests-beside");
277
+ await mkdir(join(dir, "blog"), { recursive: true });
278
+ await writeFile(join(dir, "blog", "+page.tsx"), "export default () => null");
279
+ await writeFile(join(dir, "blog", "+page.server.ts"), "export async function load() {}");
280
+ await writeFile(join(dir, "blog", "+page.test.tsx"), "export default () => null");
281
+ await writeFile(join(dir, "blog", "load.server.test.ts"), "export {}");
282
+ await writeFile(join(dir, "blog", "util.spec.ts"), "export {}");
283
+ await writeFile(join(dir, "blog", "util_test.ts"), "export {}");
284
+ const tree = await scanRoutes(dir);
285
+ const files = [...tree.routes, ...tree.layouts, ...tree.servers].map((r) => r.clientPath);
286
+ expect(files.some((f) => /[._](test|spec)\./.test(f))).toBe(false);
287
+ expect(tree.routes.map((r) => r.path)).toEqual(["/blog", "/blog"]);
288
+ });
289
+
290
+ test("throws when a +page.server.ts has no page beside it", async () => {
291
+ const dir = join(tmpDir, "orphan-page-server");
292
+ await mkdir(join(dir, "logout"), { recursive: true });
293
+ await writeFile(join(dir, "logout", "+page.server.ts"), "export async function POST() {}");
294
+ const err = await scanRoutes(dir).then(
295
+ () => null,
296
+ (e: Error) => e,
297
+ );
298
+ expect(err?.message).toContain("logout/+page.server.ts");
299
+ expect(err?.message).toContain("+server");
300
+ });
301
+
302
+ test("a +page.server.ts beside an index.tsx is fine", async () => {
303
+ const dir = join(tmpDir, "simple-plus-server");
304
+ await mkdir(join(dir, "blog"), { recursive: true });
305
+ await writeFile(join(dir, "blog", "index.tsx"), "export default () => null");
306
+ await writeFile(join(dir, "blog", "+page.server.ts"), "export async function load() {}");
307
+ await expect(scanRoutes(dir)).resolves.toBeDefined();
308
+ });
309
+
310
+ test("throws when two API routes resolve to the same path", async () => {
311
+ const dir = join(tmpDir, "dup-server");
312
+ await mkdir(join(dir, "api", "items"), { recursive: true });
313
+ // Bun glob finds all files; we simulate two +server files at same path via subdirs
314
+ // Use two different nesting depths that produce the same URL
315
+ await mkdir(join(dir, "api2"), { recursive: true });
316
+ await writeFile(
317
+ join(dir, "api2", "+server.ts"),
318
+ "export async function GET() {}",
319
+ );
320
+ await mkdir(join(dir, "api2", "index"), { recursive: true });
321
+ // Can't easily duplicate +server.ts via nesting — test the named-file vs +server collision instead
322
+ // Two +server.ts under different folder structures that map to same URL isn't possible without
323
+ // [param] collisions, so verify a simple valid tree is fine
324
+ await expect(scanRoutes(dir)).resolves.toBeDefined();
325
+ });
326
+
327
+ test("throws when two error pages resolve to the same path", async () => {
328
+ const dir = join(tmpDir, "dup-error");
329
+ await mkdir(join(dir, "blog"), { recursive: true });
330
+ // Two +error.tsx at root level isn't possible via filesystem (same filename).
331
+ // The real collision scenario is two dynamic segments mapping to the same pattern.
332
+ // Test that a single error page is accepted without error.
333
+ await writeFile(join(dir, "+error.tsx"), "export default () => null");
334
+ await expect(scanRoutes(dir)).resolves.toBeDefined();
335
+ });
336
+
337
+ test("throws when a simple file and +page.tsx both resolve to the same path", async () => {
338
+ const dir = join(tmpDir, "dup-mixed");
339
+ await mkdir(join(dir, "contact"), { recursive: true });
340
+ await writeFile(join(dir, "contact.tsx"), "export default () => null");
341
+ await writeFile(
342
+ join(dir, "contact", "+page.tsx"),
343
+ "export default () => null",
344
+ );
345
+ await expect(scanRoutes(dir)).rejects.toThrow(
346
+ 'Duplicate page at "/contact"',
347
+ );
348
+ });
349
+ });