@sigil-dev/grimoire 0.9.21 → 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.
- package/README.md +83 -83
- package/index.ts +50 -48
- package/package.json +16 -5
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +870 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -749
- package/src/rendering/preload.ts +101 -101
- package/src/rendering/ssrPlugin.ts +108 -108
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +262 -262
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -44
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +1138 -1018
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +92 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +798 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +378 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +805 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +131 -131
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/test/typegen.test.ts
CHANGED
|
@@ -1,799 +1,805 @@
|
|
|
1
|
-
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join, sep } from "node:path";
|
|
6
|
-
import type { RouteFile } from "../src/routing/scanner";
|
|
7
|
-
import { scanRoutes } from "../src/routing/scanner";
|
|
8
|
-
import {
|
|
9
|
-
buildParams,
|
|
10
|
-
generateTypes,
|
|
11
|
-
groupByDirectory,
|
|
12
|
-
toImportPath,
|
|
13
|
-
} from "../src/typegen";
|
|
14
|
-
|
|
15
|
-
// ---------------------------------------------------------------------------
|
|
16
|
-
// toImportPath
|
|
17
|
-
// ---------------------------------------------------------------------------
|
|
18
|
-
|
|
19
|
-
describe("toImportPath", () => {
|
|
20
|
-
test(".ts extension becomes .js", () => {
|
|
21
|
-
const result = toImportPath("/a/b", "/a/b/c/file.ts");
|
|
22
|
-
expect(result).toBe("c/file.js");
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test(".tsx extension becomes .js", () => {
|
|
26
|
-
const result = toImportPath("/a/b", "/a/b/c/file.tsx");
|
|
27
|
-
expect(result).toBe("c/file.js");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("produces ../ prefixes for parent directories", () => {
|
|
31
|
-
const result = toImportPath(
|
|
32
|
-
"/project/.grimoire/types/src/routes/blog/[slug]",
|
|
33
|
-
"/project/src/routes/blog/[slug]/+page.server.ts",
|
|
34
|
-
);
|
|
35
|
-
// From .grimoire/types/src/routes/blog/[slug] up 6 levels to /project/, then down
|
|
36
|
-
expect(result).toBe(
|
|
37
|
-
"../../../../../../src/routes/blog/[slug]/+page.server.js",
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("never contains backslashes regardless of platform separator", () => {
|
|
42
|
-
// Construct paths using the OS separator to exercise the replace logic
|
|
43
|
-
// from: C:\project\.grimoire\types\src\routes (4 levels below C:\project)
|
|
44
|
-
// to: C:\project\src\routes\+page.server.ts
|
|
45
|
-
// → 4 "../" to escape back to C:\project, then down to src/routes/file
|
|
46
|
-
const from = ["C:", "project", ".grimoire", "types", "src", "routes"].join(
|
|
47
|
-
sep,
|
|
48
|
-
);
|
|
49
|
-
const to = ["C:", "project", "src", "routes", "+page.server.ts"].join(sep);
|
|
50
|
-
const result = toImportPath(from, to);
|
|
51
|
-
expect(result).not.toContain("\\");
|
|
52
|
-
expect(result).toBe("../../../../src/routes/+page.server.js");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("sibling file produces just the filename", () => {
|
|
56
|
-
const result = toImportPath("/a/b/c", "/a/b/c/file.ts");
|
|
57
|
-
expect(result).toBe("file.js");
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
// buildParams
|
|
63
|
-
// ---------------------------------------------------------------------------
|
|
64
|
-
|
|
65
|
-
describe("buildParams", () => {
|
|
66
|
-
test("empty array returns {}", () => {
|
|
67
|
-
expect(buildParams([])).toBe("{}");
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test("single param", () => {
|
|
71
|
-
expect(buildParams(["id"])).toBe("{\n id: string;\n}");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("multiple params", () => {
|
|
75
|
-
expect(buildParams(["org", "repo"])).toBe(
|
|
76
|
-
"{\n org: string;\n repo: string;\n}",
|
|
77
|
-
);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
test("produces valid TypeScript type syntax", () => {
|
|
81
|
-
const result = buildParams(["slug"]);
|
|
82
|
-
expect(result).toContain("slug: string;");
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test("optional params are typed string | undefined", () => {
|
|
86
|
-
const result = buildParams(["slug"], ["lang"], ["rest"]);
|
|
87
|
-
expect(result).toContain("slug: string;");
|
|
88
|
-
expect(result).toContain("lang: string | undefined;");
|
|
89
|
-
expect(result).toContain("rest: string;");
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// ---------------------------------------------------------------------------
|
|
94
|
-
// groupByDirectory
|
|
95
|
-
// ---------------------------------------------------------------------------
|
|
96
|
-
|
|
97
|
-
function makeRouteFile(
|
|
98
|
-
overrides: Partial<RouteFile> & { filePath: string; type: RouteFile["type"] },
|
|
99
|
-
): RouteFile {
|
|
100
|
-
return {
|
|
101
|
-
path: "/",
|
|
102
|
-
clientPath: "/src/routes/+page.tsx",
|
|
103
|
-
paramNames: [],
|
|
104
|
-
optionalParamNames: [],
|
|
105
|
-
restParamNames: [],
|
|
106
|
-
paramMatchers: {},
|
|
107
|
-
...overrides,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
describe("groupByDirectory", () => {
|
|
112
|
-
test("simple files are excluded", () => {
|
|
113
|
-
const tree = {
|
|
114
|
-
routes: [
|
|
115
|
-
makeRouteFile({ filePath: "/app/routes/index.tsx", type: "simple" }),
|
|
116
|
-
makeRouteFile({ filePath: "/app/routes/about.tsx", type: "simple" }),
|
|
117
|
-
],
|
|
118
|
-
layouts: [],
|
|
119
|
-
servers: [],
|
|
120
|
-
errors: [],
|
|
121
|
-
paramMatcherFiles: [],
|
|
122
|
-
};
|
|
123
|
-
const groups = groupByDirectory(tree);
|
|
124
|
-
expect(groups.size).toBe(0);
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test("+page.tsx and +page.server.ts in the same directory form one group", () => {
|
|
128
|
-
const tree = {
|
|
129
|
-
routes: [
|
|
130
|
-
makeRouteFile({
|
|
131
|
-
filePath: "/app/routes/blog/[slug]/+page.tsx",
|
|
132
|
-
type: "page",
|
|
133
|
-
paramNames: ["slug"],
|
|
134
|
-
}),
|
|
135
|
-
makeRouteFile({
|
|
136
|
-
filePath: "/app/routes/blog/[slug]/+page.server.ts",
|
|
137
|
-
type: "pageServer",
|
|
138
|
-
paramNames: ["slug"],
|
|
139
|
-
}),
|
|
140
|
-
],
|
|
141
|
-
layouts: [],
|
|
142
|
-
servers: [],
|
|
143
|
-
errors: [],
|
|
144
|
-
paramMatcherFiles: [],
|
|
145
|
-
};
|
|
146
|
-
const groups = groupByDirectory(tree);
|
|
147
|
-
expect(groups.size).toBe(1);
|
|
148
|
-
const group = groups.get("/app/routes/blog/[slug]")!;
|
|
149
|
-
expect(group).toBeDefined();
|
|
150
|
-
expect(group.page?.filePath).toBe("/app/routes/blog/[slug]/+page.tsx");
|
|
151
|
-
expect(group.pageServer?.filePath).toBe(
|
|
152
|
-
"/app/routes/blog/[slug]/+page.server.ts",
|
|
153
|
-
);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test("+layout files go into their own group separate from +page files", () => {
|
|
157
|
-
const tree = {
|
|
158
|
-
routes: [
|
|
159
|
-
makeRouteFile({ filePath: "/app/routes/+page.tsx", type: "page" }),
|
|
160
|
-
],
|
|
161
|
-
layouts: [
|
|
162
|
-
makeRouteFile({ filePath: "/app/routes/+layout.tsx", type: "layout" }),
|
|
163
|
-
],
|
|
164
|
-
servers: [],
|
|
165
|
-
errors: [],
|
|
166
|
-
paramMatcherFiles: [],
|
|
167
|
-
};
|
|
168
|
-
const groups = groupByDirectory(tree);
|
|
169
|
-
expect(groups.size).toBe(1);
|
|
170
|
-
const group = groups.get("/app/routes")!;
|
|
171
|
-
expect(group.page).toBeDefined();
|
|
172
|
-
expect(group.layout).toBeDefined();
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test("+server.ts creates a group with server field set", () => {
|
|
176
|
-
const tree = {
|
|
177
|
-
routes: [],
|
|
178
|
-
layouts: [],
|
|
179
|
-
servers: [
|
|
180
|
-
makeRouteFile({
|
|
181
|
-
filePath: "/app/routes/api/hello/+server.ts",
|
|
182
|
-
type: "server",
|
|
183
|
-
}),
|
|
184
|
-
],
|
|
185
|
-
errors: [],
|
|
186
|
-
paramMatcherFiles: [],
|
|
187
|
-
};
|
|
188
|
-
const groups = groupByDirectory(tree);
|
|
189
|
-
expect(groups.size).toBe(1);
|
|
190
|
-
expect(groups.get("/app/routes/api/hello")!.server).toBeDefined();
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
test("+error.tsx creates a group with error field set", () => {
|
|
194
|
-
const tree = {
|
|
195
|
-
routes: [],
|
|
196
|
-
layouts: [],
|
|
197
|
-
servers: [],
|
|
198
|
-
errors: [
|
|
199
|
-
makeRouteFile({
|
|
200
|
-
filePath: "/app/routes/+error.tsx",
|
|
201
|
-
type: "error",
|
|
202
|
-
}),
|
|
203
|
-
],
|
|
204
|
-
};
|
|
205
|
-
const groups = groupByDirectory(tree);
|
|
206
|
-
expect(groups.size).toBe(1);
|
|
207
|
-
expect(groups.get("/app/routes")!.error).toBeDefined();
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
test("paramNames taken from the file with most params", () => {
|
|
211
|
-
const tree = {
|
|
212
|
-
routes: [
|
|
213
|
-
makeRouteFile({
|
|
214
|
-
filePath: "/app/routes/[org]/[repo]/+page.tsx",
|
|
215
|
-
type: "page",
|
|
216
|
-
paramNames: ["org", "repo"],
|
|
217
|
-
}),
|
|
218
|
-
makeRouteFile({
|
|
219
|
-
filePath: "/app/routes/[org]/[repo]/+page.server.ts",
|
|
220
|
-
type: "pageServer",
|
|
221
|
-
paramNames: ["org", "repo"],
|
|
222
|
-
}),
|
|
223
|
-
],
|
|
224
|
-
layouts: [],
|
|
225
|
-
servers: [],
|
|
226
|
-
errors: [],
|
|
227
|
-
paramMatcherFiles: [],
|
|
228
|
-
};
|
|
229
|
-
const groups = groupByDirectory(tree);
|
|
230
|
-
const group = groups.values().next().value!;
|
|
231
|
-
expect(group.paramNames).toEqual(["org", "repo"]);
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
test("simple files mixed with +page files: only +page files contribute to groups", () => {
|
|
235
|
-
const tree = {
|
|
236
|
-
routes: [
|
|
237
|
-
makeRouteFile({ filePath: "/app/routes/about.tsx", type: "simple" }),
|
|
238
|
-
makeRouteFile({
|
|
239
|
-
filePath: "/app/routes/blog/+page.tsx",
|
|
240
|
-
type: "page",
|
|
241
|
-
}),
|
|
242
|
-
],
|
|
243
|
-
layouts: [],
|
|
244
|
-
servers: [],
|
|
245
|
-
errors: [],
|
|
246
|
-
paramMatcherFiles: [],
|
|
247
|
-
};
|
|
248
|
-
const groups = groupByDirectory(tree);
|
|
249
|
-
expect(groups.size).toBe(1);
|
|
250
|
-
expect(groups.has("/app/routes/blog")).toBe(true);
|
|
251
|
-
expect(groups.has("/app/routes")).toBe(false);
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
// ---------------------------------------------------------------------------
|
|
256
|
-
// generateTypes — integration (file system)
|
|
257
|
-
// ---------------------------------------------------------------------------
|
|
258
|
-
|
|
259
|
-
let tmpDir: string;
|
|
260
|
-
|
|
261
|
-
beforeAll(async () => {
|
|
262
|
-
tmpDir = join(tmpdir(), `grimoire-typegen-${Date.now()}`);
|
|
263
|
-
await mkdir(join(tmpDir, "src", "routes", "blog", "[slug]"), {
|
|
264
|
-
recursive: true,
|
|
265
|
-
});
|
|
266
|
-
await mkdir(join(tmpDir, "src", "routes", "api", "posts"), {
|
|
267
|
-
recursive: true,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// Layout at root
|
|
271
|
-
await writeFile(
|
|
272
|
-
join(tmpDir, "src", "routes", "+layout.tsx"),
|
|
273
|
-
"export default () => null;",
|
|
274
|
-
);
|
|
275
|
-
|
|
276
|
-
// Page-only route (no server)
|
|
277
|
-
await writeFile(
|
|
278
|
-
join(tmpDir, "src", "routes", "blog", "+page.tsx"),
|
|
279
|
-
"export default () => null;",
|
|
280
|
-
);
|
|
281
|
-
|
|
282
|
-
// Route with +page.server.ts that has a load function
|
|
283
|
-
await writeFile(
|
|
284
|
-
join(tmpDir, "src", "routes", "blog", "[slug]", "+page.tsx"),
|
|
285
|
-
"export default () => null;",
|
|
286
|
-
);
|
|
287
|
-
await writeFile(
|
|
288
|
-
join(tmpDir, "src", "routes", "blog", "[slug]", "+page.server.ts"),
|
|
289
|
-
`export async function load({ params }: { params: { slug: string } }) {
|
|
290
|
-
return { post: { title: "Hello", views: 42 } };
|
|
291
|
-
}
|
|
292
|
-
export async function DELETE({ request }: { request: Request }) {
|
|
293
|
-
return {};
|
|
294
|
-
}`,
|
|
295
|
-
);
|
|
296
|
-
|
|
297
|
-
// Route with +layout.server.ts
|
|
298
|
-
await writeFile(
|
|
299
|
-
join(tmpDir, "src", "routes", "blog", "+layout.server.ts"),
|
|
300
|
-
`export async function load() { return { nav: ["Home", "Blog"] }; }`,
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
// API route (+server.ts)
|
|
304
|
-
await writeFile(
|
|
305
|
-
join(tmpDir, "src", "routes", "api", "posts", "+server.ts"),
|
|
306
|
-
`export async function GET() { return Response.json([]); }
|
|
307
|
-
export async function POST() { return Response.json({}); }`,
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
// Error page
|
|
311
|
-
await writeFile(
|
|
312
|
-
join(tmpDir, "src", "routes", "+error.tsx"),
|
|
313
|
-
"export default () => null;",
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
// Simple file (should be ignored)
|
|
317
|
-
await writeFile(
|
|
318
|
-
join(tmpDir, "src", "routes", "about.tsx"),
|
|
319
|
-
"export default () => null;",
|
|
320
|
-
);
|
|
321
|
-
|
|
322
|
-
// Rest (catch-all) param route
|
|
323
|
-
await mkdir(join(tmpDir, "src", "routes", "files", "[...path]"), {
|
|
324
|
-
recursive: true,
|
|
325
|
-
});
|
|
326
|
-
await writeFile(
|
|
327
|
-
join(tmpDir, "src", "routes", "files", "[...path]", "+server.ts"),
|
|
328
|
-
`export async function GET() { return new Response("ok"); }`,
|
|
329
|
-
);
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
afterAll(async () => {
|
|
333
|
-
await rm(tmpDir, { recursive: true, force: true });
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
async function runGenerate() {
|
|
337
|
-
const routesDir = join(tmpDir, "src", "routes");
|
|
338
|
-
const tree = await scanRoutes(routesDir, tmpDir);
|
|
339
|
-
await generateTypes(tree, {
|
|
340
|
-
projectRoot: tmpDir,
|
|
341
|
-
routesDir,
|
|
342
|
-
outDir: join(tmpDir, ".grimoire", "types"),
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
async function readGenerated(routeRelPath: string) {
|
|
347
|
-
const p = join(tmpDir, ".grimoire", "types", routeRelPath, "$types.d.ts");
|
|
348
|
-
return readFile(p, "utf-8");
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
describe("generateTypes — output structure", () => {
|
|
352
|
-
test("creates ambient.d.ts with App.Locals namespace", async () => {
|
|
353
|
-
await runGenerate();
|
|
354
|
-
const ambient = await readFile(
|
|
355
|
-
join(tmpDir, ".grimoire", "types", "ambient.d.ts"),
|
|
356
|
-
"utf-8",
|
|
357
|
-
);
|
|
358
|
-
expect(ambient).toContain("declare namespace App");
|
|
359
|
-
expect(ambient).toContain("interface Locals");
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
test("creates tsconfig.generated.json with rootDirs", async () => {
|
|
363
|
-
const config = JSON.parse(
|
|
364
|
-
await readFile(
|
|
365
|
-
join(tmpDir, ".grimoire", "tsconfig.generated.json"),
|
|
366
|
-
"utf-8",
|
|
367
|
-
),
|
|
368
|
-
);
|
|
369
|
-
expect(config.compilerOptions.rootDirs).toContain("..");
|
|
370
|
-
expect(config.compilerOptions.rootDirs).toContain("./types");
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
test("generates $types.d.ts for each route directory with + files", async () => {
|
|
374
|
-
expect(
|
|
375
|
-
existsSync(
|
|
376
|
-
join(tmpDir, ".grimoire", "types", "src", "routes", "$types.d.ts"),
|
|
377
|
-
),
|
|
378
|
-
).toBe(true);
|
|
379
|
-
expect(
|
|
380
|
-
existsSync(
|
|
381
|
-
join(
|
|
382
|
-
tmpDir,
|
|
383
|
-
".grimoire",
|
|
384
|
-
"types",
|
|
385
|
-
"src",
|
|
386
|
-
"routes",
|
|
387
|
-
"blog",
|
|
388
|
-
"$types.d.ts",
|
|
389
|
-
),
|
|
390
|
-
),
|
|
391
|
-
).toBe(true);
|
|
392
|
-
expect(
|
|
393
|
-
existsSync(
|
|
394
|
-
join(
|
|
395
|
-
tmpDir,
|
|
396
|
-
".grimoire",
|
|
397
|
-
"types",
|
|
398
|
-
"src",
|
|
399
|
-
"routes",
|
|
400
|
-
"blog",
|
|
401
|
-
"[slug]",
|
|
402
|
-
"$types.d.ts",
|
|
403
|
-
),
|
|
404
|
-
),
|
|
405
|
-
).toBe(true);
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
test("does NOT generate $types.d.ts for simple (non-+ convention) files", async () => {
|
|
409
|
-
// about.tsx is a simple file in src/routes/ — there's no $types for it
|
|
410
|
-
// (src/routes/ does get $types from +layout.tsx, but it's not created for about.tsx alone)
|
|
411
|
-
// Confirm that about/ directory itself has no $types
|
|
412
|
-
expect(
|
|
413
|
-
existsSync(
|
|
414
|
-
join(
|
|
415
|
-
tmpDir,
|
|
416
|
-
".grimoire",
|
|
417
|
-
"types",
|
|
418
|
-
"src",
|
|
419
|
-
"routes",
|
|
420
|
-
"about",
|
|
421
|
-
"$types.d.ts",
|
|
422
|
-
),
|
|
423
|
-
),
|
|
424
|
-
).toBe(false);
|
|
425
|
-
});
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
describe("generateTypes — route without +page.server.ts", () => {
|
|
429
|
-
test("PageData = Record<string, never>", async () => {
|
|
430
|
-
const content = await readGenerated("src/routes/blog");
|
|
431
|
-
expect(content).toContain("export type PageData = Record<string, never>;");
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
test("PageServerLoad = never", async () => {
|
|
435
|
-
const content = await readGenerated("src/routes/blog");
|
|
436
|
-
expect(content).toContain("export type PageServerLoad = never;");
|
|
437
|
-
});
|
|
438
|
-
|
|
439
|
-
test("Actions = Record<string, never>", async () => {
|
|
440
|
-
const content = await readGenerated("src/routes/blog");
|
|
441
|
-
expect(content).toContain("export type Actions = Record<string, never>;");
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
test("PageProps contains data and params", async () => {
|
|
445
|
-
const content = await readGenerated("src/routes/blog");
|
|
446
|
-
expect(content).toContain(
|
|
447
|
-
"export type PageProps = { data: PageData; params: Params; form?: ActionData };",
|
|
448
|
-
);
|
|
449
|
-
});
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
describe("generateTypes — route with +page.server.ts", () => {
|
|
453
|
-
test("uses typeof import() for the server module", async () => {
|
|
454
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
455
|
-
expect(content).toMatch(/type _PS = typeof import\("/);
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
test("import path ends with .js", async () => {
|
|
459
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
460
|
-
const match = content.match(/typeof import\("([^"]+)"\)/);
|
|
461
|
-
expect(match?.[1]).toMatch(/\.js$/);
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
test("import path contains only forward slashes", async () => {
|
|
465
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
466
|
-
const match = content.match(/typeof import\("([^"]+)"\)/);
|
|
467
|
-
expect(match?.[1]).not.toContain("\\");
|
|
468
|
-
});
|
|
469
|
-
|
|
470
|
-
test("PageData inferred from load() via ReturnType", async () => {
|
|
471
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
472
|
-
expect(content).toContain(
|
|
473
|
-
"export type PageData = _PS extends { load: (...args: any[]) => infer R }",
|
|
474
|
-
);
|
|
475
|
-
expect(content).toContain("? _Streamed<Awaited<R>>");
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
test("PageProps = { data: PageData; params: Params }", async () => {
|
|
479
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
480
|
-
expect(content).toContain(
|
|
481
|
-
"export type PageProps = { data: PageData; params: Params; form?: ActionData };",
|
|
482
|
-
);
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
test("Actions excludes load and default from keys", async () => {
|
|
486
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
487
|
-
expect(content).toContain(
|
|
488
|
-
'type _ActionKeys = Exclude<keyof _PS, "load" | "default" | "canMatch" | "csrf" | "loading">;',
|
|
489
|
-
);
|
|
490
|
-
});
|
|
491
|
-
|
|
492
|
-
test("Params has slug typed as string", async () => {
|
|
493
|
-
const content = await readGenerated("src/routes/blog/[slug]");
|
|
494
|
-
expect(content).toContain("slug: string;");
|
|
495
|
-
});
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
describe("generateTypes — +layout.server.ts", () => {
|
|
499
|
-
test("LayoutData inferred from load() via typeof import", async () => {
|
|
500
|
-
const content = await readGenerated("src/routes/blog");
|
|
501
|
-
expect(content).toMatch(/type _LS = typeof import\("/);
|
|
502
|
-
expect(content).toContain(
|
|
503
|
-
"export type LayoutData = _LS extends { load: (...args: any[]) => infer R }",
|
|
504
|
-
);
|
|
505
|
-
expect(content).toContain("? Awaited<R>");
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
test("LayoutProps includes children", async () => {
|
|
509
|
-
const content = await readGenerated("src/routes/blog");
|
|
510
|
-
expect(content).toContain("children?: unknown");
|
|
511
|
-
});
|
|
512
|
-
});
|
|
513
|
-
|
|
514
|
-
describe("generateTypes — +server.ts (API route)", () => {
|
|
515
|
-
test("generates ServerHandlers type", async () => {
|
|
516
|
-
const content = await readGenerated("src/routes/api/posts");
|
|
517
|
-
expect(content).toContain("export type ServerHandlers");
|
|
518
|
-
});
|
|
519
|
-
|
|
520
|
-
test("uses typeof import() for server module", async () => {
|
|
521
|
-
const content = await readGenerated("src/routes/api/posts");
|
|
522
|
-
expect(content).toMatch(/type _SRV = typeof import\("/);
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
test("extracts HTTP method keys", async () => {
|
|
526
|
-
const content = await readGenerated("src/routes/api/posts");
|
|
527
|
-
expect(content).toContain("_HttpMethods");
|
|
528
|
-
expect(content).toContain("_ServerKeys");
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
//
|
|
556
|
-
//
|
|
557
|
-
//
|
|
558
|
-
//
|
|
559
|
-
//
|
|
560
|
-
//
|
|
561
|
-
//
|
|
562
|
-
//
|
|
563
|
-
//
|
|
564
|
-
|
|
565
|
-
//
|
|
566
|
-
//
|
|
567
|
-
|
|
568
|
-
//
|
|
569
|
-
//
|
|
570
|
-
|
|
571
|
-
//
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
//
|
|
575
|
-
//
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
//
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
);
|
|
605
|
-
await
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
export
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
props.data.
|
|
625
|
-
|
|
626
|
-
const
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
test("
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
await
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
const
|
|
724
|
-
await
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
);
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
await rm(
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
),
|
|
788
|
-
);
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join, sep } from "node:path";
|
|
6
|
+
import type { RouteFile } from "../src/routing/scanner";
|
|
7
|
+
import { scanRoutes } from "../src/routing/scanner";
|
|
8
|
+
import {
|
|
9
|
+
buildParams,
|
|
10
|
+
generateTypes,
|
|
11
|
+
groupByDirectory,
|
|
12
|
+
toImportPath,
|
|
13
|
+
} from "../src/typegen";
|
|
14
|
+
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// toImportPath
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
|
|
19
|
+
describe("toImportPath", () => {
|
|
20
|
+
test(".ts extension becomes .js", () => {
|
|
21
|
+
const result = toImportPath("/a/b", "/a/b/c/file.ts");
|
|
22
|
+
expect(result).toBe("c/file.js");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test(".tsx extension becomes .js", () => {
|
|
26
|
+
const result = toImportPath("/a/b", "/a/b/c/file.tsx");
|
|
27
|
+
expect(result).toBe("c/file.js");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("produces ../ prefixes for parent directories", () => {
|
|
31
|
+
const result = toImportPath(
|
|
32
|
+
"/project/.grimoire/types/src/routes/blog/[slug]",
|
|
33
|
+
"/project/src/routes/blog/[slug]/+page.server.ts",
|
|
34
|
+
);
|
|
35
|
+
// From .grimoire/types/src/routes/blog/[slug] up 6 levels to /project/, then down
|
|
36
|
+
expect(result).toBe(
|
|
37
|
+
"../../../../../../src/routes/blog/[slug]/+page.server.js",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("never contains backslashes regardless of platform separator", () => {
|
|
42
|
+
// Construct paths using the OS separator to exercise the replace logic
|
|
43
|
+
// from: C:\project\.grimoire\types\src\routes (4 levels below C:\project)
|
|
44
|
+
// to: C:\project\src\routes\+page.server.ts
|
|
45
|
+
// → 4 "../" to escape back to C:\project, then down to src/routes/file
|
|
46
|
+
const from = ["C:", "project", ".grimoire", "types", "src", "routes"].join(
|
|
47
|
+
sep,
|
|
48
|
+
);
|
|
49
|
+
const to = ["C:", "project", "src", "routes", "+page.server.ts"].join(sep);
|
|
50
|
+
const result = toImportPath(from, to);
|
|
51
|
+
expect(result).not.toContain("\\");
|
|
52
|
+
expect(result).toBe("../../../../src/routes/+page.server.js");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("sibling file produces just the filename", () => {
|
|
56
|
+
const result = toImportPath("/a/b/c", "/a/b/c/file.ts");
|
|
57
|
+
expect(result).toBe("file.js");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// buildParams
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
describe("buildParams", () => {
|
|
66
|
+
test("empty array returns {}", () => {
|
|
67
|
+
expect(buildParams([])).toBe("{}");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("single param", () => {
|
|
71
|
+
expect(buildParams(["id"])).toBe("{\n id: string;\n}");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("multiple params", () => {
|
|
75
|
+
expect(buildParams(["org", "repo"])).toBe(
|
|
76
|
+
"{\n org: string;\n repo: string;\n}",
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("produces valid TypeScript type syntax", () => {
|
|
81
|
+
const result = buildParams(["slug"]);
|
|
82
|
+
expect(result).toContain("slug: string;");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("optional params are typed string | undefined", () => {
|
|
86
|
+
const result = buildParams(["slug"], ["lang"], ["rest"]);
|
|
87
|
+
expect(result).toContain("slug: string;");
|
|
88
|
+
expect(result).toContain("lang: string | undefined;");
|
|
89
|
+
expect(result).toContain("rest: string;");
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// groupByDirectory
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
|
|
97
|
+
function makeRouteFile(
|
|
98
|
+
overrides: Partial<RouteFile> & { filePath: string; type: RouteFile["type"] },
|
|
99
|
+
): RouteFile {
|
|
100
|
+
return {
|
|
101
|
+
path: "/",
|
|
102
|
+
clientPath: "/src/routes/+page.tsx",
|
|
103
|
+
paramNames: [],
|
|
104
|
+
optionalParamNames: [],
|
|
105
|
+
restParamNames: [],
|
|
106
|
+
paramMatchers: {},
|
|
107
|
+
...overrides,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
describe("groupByDirectory", () => {
|
|
112
|
+
test("simple files are excluded", () => {
|
|
113
|
+
const tree = {
|
|
114
|
+
routes: [
|
|
115
|
+
makeRouteFile({ filePath: "/app/routes/index.tsx", type: "simple" }),
|
|
116
|
+
makeRouteFile({ filePath: "/app/routes/about.tsx", type: "simple" }),
|
|
117
|
+
],
|
|
118
|
+
layouts: [],
|
|
119
|
+
servers: [],
|
|
120
|
+
errors: [],
|
|
121
|
+
paramMatcherFiles: [],
|
|
122
|
+
};
|
|
123
|
+
const groups = groupByDirectory(tree);
|
|
124
|
+
expect(groups.size).toBe(0);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("+page.tsx and +page.server.ts in the same directory form one group", () => {
|
|
128
|
+
const tree = {
|
|
129
|
+
routes: [
|
|
130
|
+
makeRouteFile({
|
|
131
|
+
filePath: "/app/routes/blog/[slug]/+page.tsx",
|
|
132
|
+
type: "page",
|
|
133
|
+
paramNames: ["slug"],
|
|
134
|
+
}),
|
|
135
|
+
makeRouteFile({
|
|
136
|
+
filePath: "/app/routes/blog/[slug]/+page.server.ts",
|
|
137
|
+
type: "pageServer",
|
|
138
|
+
paramNames: ["slug"],
|
|
139
|
+
}),
|
|
140
|
+
],
|
|
141
|
+
layouts: [],
|
|
142
|
+
servers: [],
|
|
143
|
+
errors: [],
|
|
144
|
+
paramMatcherFiles: [],
|
|
145
|
+
};
|
|
146
|
+
const groups = groupByDirectory(tree);
|
|
147
|
+
expect(groups.size).toBe(1);
|
|
148
|
+
const group = groups.get("/app/routes/blog/[slug]")!;
|
|
149
|
+
expect(group).toBeDefined();
|
|
150
|
+
expect(group.page?.filePath).toBe("/app/routes/blog/[slug]/+page.tsx");
|
|
151
|
+
expect(group.pageServer?.filePath).toBe(
|
|
152
|
+
"/app/routes/blog/[slug]/+page.server.ts",
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("+layout files go into their own group separate from +page files", () => {
|
|
157
|
+
const tree = {
|
|
158
|
+
routes: [
|
|
159
|
+
makeRouteFile({ filePath: "/app/routes/+page.tsx", type: "page" }),
|
|
160
|
+
],
|
|
161
|
+
layouts: [
|
|
162
|
+
makeRouteFile({ filePath: "/app/routes/+layout.tsx", type: "layout" }),
|
|
163
|
+
],
|
|
164
|
+
servers: [],
|
|
165
|
+
errors: [],
|
|
166
|
+
paramMatcherFiles: [],
|
|
167
|
+
};
|
|
168
|
+
const groups = groupByDirectory(tree);
|
|
169
|
+
expect(groups.size).toBe(1);
|
|
170
|
+
const group = groups.get("/app/routes")!;
|
|
171
|
+
expect(group.page).toBeDefined();
|
|
172
|
+
expect(group.layout).toBeDefined();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("+server.ts creates a group with server field set", () => {
|
|
176
|
+
const tree = {
|
|
177
|
+
routes: [],
|
|
178
|
+
layouts: [],
|
|
179
|
+
servers: [
|
|
180
|
+
makeRouteFile({
|
|
181
|
+
filePath: "/app/routes/api/hello/+server.ts",
|
|
182
|
+
type: "server",
|
|
183
|
+
}),
|
|
184
|
+
],
|
|
185
|
+
errors: [],
|
|
186
|
+
paramMatcherFiles: [],
|
|
187
|
+
};
|
|
188
|
+
const groups = groupByDirectory(tree);
|
|
189
|
+
expect(groups.size).toBe(1);
|
|
190
|
+
expect(groups.get("/app/routes/api/hello")!.server).toBeDefined();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("+error.tsx creates a group with error field set", () => {
|
|
194
|
+
const tree = {
|
|
195
|
+
routes: [],
|
|
196
|
+
layouts: [],
|
|
197
|
+
servers: [],
|
|
198
|
+
errors: [
|
|
199
|
+
makeRouteFile({
|
|
200
|
+
filePath: "/app/routes/+error.tsx",
|
|
201
|
+
type: "error",
|
|
202
|
+
}),
|
|
203
|
+
],
|
|
204
|
+
};
|
|
205
|
+
const groups = groupByDirectory(tree);
|
|
206
|
+
expect(groups.size).toBe(1);
|
|
207
|
+
expect(groups.get("/app/routes")!.error).toBeDefined();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("paramNames taken from the file with most params", () => {
|
|
211
|
+
const tree = {
|
|
212
|
+
routes: [
|
|
213
|
+
makeRouteFile({
|
|
214
|
+
filePath: "/app/routes/[org]/[repo]/+page.tsx",
|
|
215
|
+
type: "page",
|
|
216
|
+
paramNames: ["org", "repo"],
|
|
217
|
+
}),
|
|
218
|
+
makeRouteFile({
|
|
219
|
+
filePath: "/app/routes/[org]/[repo]/+page.server.ts",
|
|
220
|
+
type: "pageServer",
|
|
221
|
+
paramNames: ["org", "repo"],
|
|
222
|
+
}),
|
|
223
|
+
],
|
|
224
|
+
layouts: [],
|
|
225
|
+
servers: [],
|
|
226
|
+
errors: [],
|
|
227
|
+
paramMatcherFiles: [],
|
|
228
|
+
};
|
|
229
|
+
const groups = groupByDirectory(tree);
|
|
230
|
+
const group = groups.values().next().value!;
|
|
231
|
+
expect(group.paramNames).toEqual(["org", "repo"]);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("simple files mixed with +page files: only +page files contribute to groups", () => {
|
|
235
|
+
const tree = {
|
|
236
|
+
routes: [
|
|
237
|
+
makeRouteFile({ filePath: "/app/routes/about.tsx", type: "simple" }),
|
|
238
|
+
makeRouteFile({
|
|
239
|
+
filePath: "/app/routes/blog/+page.tsx",
|
|
240
|
+
type: "page",
|
|
241
|
+
}),
|
|
242
|
+
],
|
|
243
|
+
layouts: [],
|
|
244
|
+
servers: [],
|
|
245
|
+
errors: [],
|
|
246
|
+
paramMatcherFiles: [],
|
|
247
|
+
};
|
|
248
|
+
const groups = groupByDirectory(tree);
|
|
249
|
+
expect(groups.size).toBe(1);
|
|
250
|
+
expect(groups.has("/app/routes/blog")).toBe(true);
|
|
251
|
+
expect(groups.has("/app/routes")).toBe(false);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// ---------------------------------------------------------------------------
|
|
256
|
+
// generateTypes — integration (file system)
|
|
257
|
+
// ---------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
let tmpDir: string;
|
|
260
|
+
|
|
261
|
+
beforeAll(async () => {
|
|
262
|
+
tmpDir = join(tmpdir(), `grimoire-typegen-${Date.now()}`);
|
|
263
|
+
await mkdir(join(tmpDir, "src", "routes", "blog", "[slug]"), {
|
|
264
|
+
recursive: true,
|
|
265
|
+
});
|
|
266
|
+
await mkdir(join(tmpDir, "src", "routes", "api", "posts"), {
|
|
267
|
+
recursive: true,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
// Layout at root
|
|
271
|
+
await writeFile(
|
|
272
|
+
join(tmpDir, "src", "routes", "+layout.tsx"),
|
|
273
|
+
"export default () => null;",
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// Page-only route (no server)
|
|
277
|
+
await writeFile(
|
|
278
|
+
join(tmpDir, "src", "routes", "blog", "+page.tsx"),
|
|
279
|
+
"export default () => null;",
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
// Route with +page.server.ts that has a load function
|
|
283
|
+
await writeFile(
|
|
284
|
+
join(tmpDir, "src", "routes", "blog", "[slug]", "+page.tsx"),
|
|
285
|
+
"export default () => null;",
|
|
286
|
+
);
|
|
287
|
+
await writeFile(
|
|
288
|
+
join(tmpDir, "src", "routes", "blog", "[slug]", "+page.server.ts"),
|
|
289
|
+
`export async function load({ params }: { params: { slug: string } }) {
|
|
290
|
+
return { post: { title: "Hello", views: 42 } };
|
|
291
|
+
}
|
|
292
|
+
export async function DELETE({ request }: { request: Request }) {
|
|
293
|
+
return {};
|
|
294
|
+
}`,
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
// Route with +layout.server.ts
|
|
298
|
+
await writeFile(
|
|
299
|
+
join(tmpDir, "src", "routes", "blog", "+layout.server.ts"),
|
|
300
|
+
`export async function load() { return { nav: ["Home", "Blog"] }; }`,
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
// API route (+server.ts)
|
|
304
|
+
await writeFile(
|
|
305
|
+
join(tmpDir, "src", "routes", "api", "posts", "+server.ts"),
|
|
306
|
+
`export async function GET() { return Response.json([]); }
|
|
307
|
+
export async function POST() { return Response.json({}); }`,
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
// Error page
|
|
311
|
+
await writeFile(
|
|
312
|
+
join(tmpDir, "src", "routes", "+error.tsx"),
|
|
313
|
+
"export default () => null;",
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
// Simple file (should be ignored)
|
|
317
|
+
await writeFile(
|
|
318
|
+
join(tmpDir, "src", "routes", "about.tsx"),
|
|
319
|
+
"export default () => null;",
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
// Rest (catch-all) param route
|
|
323
|
+
await mkdir(join(tmpDir, "src", "routes", "files", "[...path]"), {
|
|
324
|
+
recursive: true,
|
|
325
|
+
});
|
|
326
|
+
await writeFile(
|
|
327
|
+
join(tmpDir, "src", "routes", "files", "[...path]", "+server.ts"),
|
|
328
|
+
`export async function GET() { return new Response("ok"); }`,
|
|
329
|
+
);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
afterAll(async () => {
|
|
333
|
+
await rm(tmpDir, { recursive: true, force: true });
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
async function runGenerate() {
|
|
337
|
+
const routesDir = join(tmpDir, "src", "routes");
|
|
338
|
+
const tree = await scanRoutes(routesDir, tmpDir);
|
|
339
|
+
await generateTypes(tree, {
|
|
340
|
+
projectRoot: tmpDir,
|
|
341
|
+
routesDir,
|
|
342
|
+
outDir: join(tmpDir, ".grimoire", "types"),
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async function readGenerated(routeRelPath: string) {
|
|
347
|
+
const p = join(tmpDir, ".grimoire", "types", routeRelPath, "$types.d.ts");
|
|
348
|
+
return readFile(p, "utf-8");
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
describe("generateTypes — output structure", () => {
|
|
352
|
+
test("creates ambient.d.ts with App.Locals namespace", async () => {
|
|
353
|
+
await runGenerate();
|
|
354
|
+
const ambient = await readFile(
|
|
355
|
+
join(tmpDir, ".grimoire", "types", "ambient.d.ts"),
|
|
356
|
+
"utf-8",
|
|
357
|
+
);
|
|
358
|
+
expect(ambient).toContain("declare namespace App");
|
|
359
|
+
expect(ambient).toContain("interface Locals");
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test("creates tsconfig.generated.json with rootDirs", async () => {
|
|
363
|
+
const config = JSON.parse(
|
|
364
|
+
await readFile(
|
|
365
|
+
join(tmpDir, ".grimoire", "tsconfig.generated.json"),
|
|
366
|
+
"utf-8",
|
|
367
|
+
),
|
|
368
|
+
);
|
|
369
|
+
expect(config.compilerOptions.rootDirs).toContain("..");
|
|
370
|
+
expect(config.compilerOptions.rootDirs).toContain("./types");
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("generates $types.d.ts for each route directory with + files", async () => {
|
|
374
|
+
expect(
|
|
375
|
+
existsSync(
|
|
376
|
+
join(tmpDir, ".grimoire", "types", "src", "routes", "$types.d.ts"),
|
|
377
|
+
),
|
|
378
|
+
).toBe(true);
|
|
379
|
+
expect(
|
|
380
|
+
existsSync(
|
|
381
|
+
join(
|
|
382
|
+
tmpDir,
|
|
383
|
+
".grimoire",
|
|
384
|
+
"types",
|
|
385
|
+
"src",
|
|
386
|
+
"routes",
|
|
387
|
+
"blog",
|
|
388
|
+
"$types.d.ts",
|
|
389
|
+
),
|
|
390
|
+
),
|
|
391
|
+
).toBe(true);
|
|
392
|
+
expect(
|
|
393
|
+
existsSync(
|
|
394
|
+
join(
|
|
395
|
+
tmpDir,
|
|
396
|
+
".grimoire",
|
|
397
|
+
"types",
|
|
398
|
+
"src",
|
|
399
|
+
"routes",
|
|
400
|
+
"blog",
|
|
401
|
+
"[slug]",
|
|
402
|
+
"$types.d.ts",
|
|
403
|
+
),
|
|
404
|
+
),
|
|
405
|
+
).toBe(true);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
test("does NOT generate $types.d.ts for simple (non-+ convention) files", async () => {
|
|
409
|
+
// about.tsx is a simple file in src/routes/ — there's no $types for it
|
|
410
|
+
// (src/routes/ does get $types from +layout.tsx, but it's not created for about.tsx alone)
|
|
411
|
+
// Confirm that about/ directory itself has no $types
|
|
412
|
+
expect(
|
|
413
|
+
existsSync(
|
|
414
|
+
join(
|
|
415
|
+
tmpDir,
|
|
416
|
+
".grimoire",
|
|
417
|
+
"types",
|
|
418
|
+
"src",
|
|
419
|
+
"routes",
|
|
420
|
+
"about",
|
|
421
|
+
"$types.d.ts",
|
|
422
|
+
),
|
|
423
|
+
),
|
|
424
|
+
).toBe(false);
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
describe("generateTypes — route without +page.server.ts", () => {
|
|
429
|
+
test("PageData = Record<string, never>", async () => {
|
|
430
|
+
const content = await readGenerated("src/routes/blog");
|
|
431
|
+
expect(content).toContain("export type PageData = Record<string, never>;");
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
test("PageServerLoad = never", async () => {
|
|
435
|
+
const content = await readGenerated("src/routes/blog");
|
|
436
|
+
expect(content).toContain("export type PageServerLoad = never;");
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
test("Actions = Record<string, never>", async () => {
|
|
440
|
+
const content = await readGenerated("src/routes/blog");
|
|
441
|
+
expect(content).toContain("export type Actions = Record<string, never>;");
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
test("PageProps contains data and params", async () => {
|
|
445
|
+
const content = await readGenerated("src/routes/blog");
|
|
446
|
+
expect(content).toContain(
|
|
447
|
+
"export type PageProps = { data: PageData; params: Params; form?: ActionData };",
|
|
448
|
+
);
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
describe("generateTypes — route with +page.server.ts", () => {
|
|
453
|
+
test("uses typeof import() for the server module", async () => {
|
|
454
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
455
|
+
expect(content).toMatch(/type _PS = typeof import\("/);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
test("import path ends with .js", async () => {
|
|
459
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
460
|
+
const match = content.match(/typeof import\("([^"]+)"\)/);
|
|
461
|
+
expect(match?.[1]).toMatch(/\.js$/);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
test("import path contains only forward slashes", async () => {
|
|
465
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
466
|
+
const match = content.match(/typeof import\("([^"]+)"\)/);
|
|
467
|
+
expect(match?.[1]).not.toContain("\\");
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
test("PageData inferred from load() via ReturnType", async () => {
|
|
471
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
472
|
+
expect(content).toContain(
|
|
473
|
+
"export type PageData = _PS extends { load: (...args: any[]) => infer R }",
|
|
474
|
+
);
|
|
475
|
+
expect(content).toContain("? _Streamed<Awaited<R>>");
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
test("PageProps = { data: PageData; params: Params }", async () => {
|
|
479
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
480
|
+
expect(content).toContain(
|
|
481
|
+
"export type PageProps = { data: PageData; params: Params; form?: ActionData };",
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
test("Actions excludes load and default from keys", async () => {
|
|
486
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
487
|
+
expect(content).toContain(
|
|
488
|
+
'type _ActionKeys = Exclude<keyof _PS, "load" | "default" | "canMatch" | "csrf" | "loading">;',
|
|
489
|
+
);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
test("Params has slug typed as string", async () => {
|
|
493
|
+
const content = await readGenerated("src/routes/blog/[slug]");
|
|
494
|
+
expect(content).toContain("slug: string;");
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
describe("generateTypes — +layout.server.ts", () => {
|
|
499
|
+
test("LayoutData inferred from load() via typeof import", async () => {
|
|
500
|
+
const content = await readGenerated("src/routes/blog");
|
|
501
|
+
expect(content).toMatch(/type _LS = typeof import\("/);
|
|
502
|
+
expect(content).toContain(
|
|
503
|
+
"export type LayoutData = _LS extends { load: (...args: any[]) => infer R }",
|
|
504
|
+
);
|
|
505
|
+
expect(content).toContain("? Awaited<R>");
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
test("LayoutProps includes children", async () => {
|
|
509
|
+
const content = await readGenerated("src/routes/blog");
|
|
510
|
+
expect(content).toContain("children?: unknown");
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
describe("generateTypes — +server.ts (API route)", () => {
|
|
515
|
+
test("generates ServerHandlers type", async () => {
|
|
516
|
+
const content = await readGenerated("src/routes/api/posts");
|
|
517
|
+
expect(content).toContain("export type ServerHandlers");
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
test("uses typeof import() for server module", async () => {
|
|
521
|
+
const content = await readGenerated("src/routes/api/posts");
|
|
522
|
+
expect(content).toMatch(/type _SRV = typeof import\("/);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
test("extracts HTTP method keys", async () => {
|
|
526
|
+
const content = await readGenerated("src/routes/api/posts");
|
|
527
|
+
expect(content).toContain("_HttpMethods");
|
|
528
|
+
expect(content).toContain("_ServerKeys");
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
test("needs no Bun types and does not use the Buffer global", async () => {
|
|
532
|
+
const content = await readGenerated("src/routes/api/posts");
|
|
533
|
+
expect(content).not.toContain('from "bun"');
|
|
534
|
+
expect(content).not.toMatch(/bBufferb/);
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
describe("generateTypes — +error.tsx", () => {
|
|
539
|
+
test("generates ErrorProps type", async () => {
|
|
540
|
+
const content = await readGenerated("src/routes");
|
|
541
|
+
expect(content).toContain(
|
|
542
|
+
"export type ErrorProps = { status: number; message: string; error: unknown; route: string };",
|
|
543
|
+
);
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
describe("generateTypes — rest (catch-all) params", () => {
|
|
548
|
+
test("Params includes rest param name as string", async () => {
|
|
549
|
+
const content = await readGenerated("src/routes/files/[...path]");
|
|
550
|
+
expect(content).toContain("path: string;");
|
|
551
|
+
expect(content).toMatch(/export type Params = \{\s+path: string;\s+\};/);
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
// This behavior no lnger exists
|
|
556
|
+
// describe("generateTypes — clears stale output", () => {
|
|
557
|
+
// test("removes files from a deleted route on re-run", async () => {
|
|
558
|
+
// // First run already happened in beforeAll via runGenerate()
|
|
559
|
+
// // Write a stale file manually
|
|
560
|
+
// const staleDir = join(
|
|
561
|
+
// tmpDir,
|
|
562
|
+
// ".grimoire",
|
|
563
|
+
// "types",
|
|
564
|
+
// "src",
|
|
565
|
+
// "routes",
|
|
566
|
+
// "old-route",
|
|
567
|
+
// );
|
|
568
|
+
// await mkdir(staleDir, { recursive: true });
|
|
569
|
+
// await writeFile(join(staleDir, "$types.d.ts"), "// stale");
|
|
570
|
+
|
|
571
|
+
// // Re-run
|
|
572
|
+
// await runGenerate();
|
|
573
|
+
|
|
574
|
+
// // Stale file should be gone
|
|
575
|
+
// expect(existsSync(join(staleDir, "$types.d.ts"))).toBe(false);
|
|
576
|
+
// });
|
|
577
|
+
// });
|
|
578
|
+
|
|
579
|
+
// ---------------------------------------------------------------------------
|
|
580
|
+
// tsc integration — proves the generated types actually type-check
|
|
581
|
+
// ---------------------------------------------------------------------------
|
|
582
|
+
|
|
583
|
+
describe("tsc integration", () => {
|
|
584
|
+
let tscDir: string;
|
|
585
|
+
|
|
586
|
+
beforeAll(async () => {
|
|
587
|
+
tscDir = join(tmpdir(), `grimoire-tsc-${Date.now()}`);
|
|
588
|
+
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
589
|
+
await mkdir(routeDir, { recursive: true });
|
|
590
|
+
|
|
591
|
+
// Minimal +page.server.ts whose return type flows through to PageProps
|
|
592
|
+
await writeFile(
|
|
593
|
+
join(routeDir, "+page.server.ts"),
|
|
594
|
+
`export async function load({ params }: { params: { slug: string } }) {
|
|
595
|
+
return { post: { title: "Hello", views: 42 }, comments: Promise.resolve(["first"]) };
|
|
596
|
+
}
|
|
597
|
+
// what fail(400, { problem }) returns
|
|
598
|
+
export async function comment() {
|
|
599
|
+
if (Math.random() > 1) return { __fail: true as const, status: 400, data: { problem: "Say something" } };
|
|
600
|
+
}`,
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
// a +server.ts route: its types must compile without Bun's types installed
|
|
604
|
+
const chatDir = join(tscDir, "src", "routes", "chat");
|
|
605
|
+
await mkdir(chatDir, { recursive: true });
|
|
606
|
+
await writeFile(
|
|
607
|
+
join(chatDir, "+server.ts"),
|
|
608
|
+
`export function GET() { return new Response("ok"); }
|
|
609
|
+
export function upgrade() { return { room: "lobby" }; }`,
|
|
610
|
+
);
|
|
611
|
+
await writeFile(
|
|
612
|
+
join(chatDir, "check.ts"),
|
|
613
|
+
`import type { WebSocketHandler } from "./$types";
|
|
614
|
+
export const websocket: WebSocketHandler = {
|
|
615
|
+
message(ws, data) { ws.send(\`\${ws.data.room}: \${String(data)}\`); },
|
|
616
|
+
};`,
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
// File that uses the generated types — must live in the route dir for ./$types to resolve
|
|
620
|
+
await writeFile(
|
|
621
|
+
join(routeDir, "check.ts"),
|
|
622
|
+
`import type { PageProps } from "./$types";
|
|
623
|
+
declare const props: PageProps;
|
|
624
|
+
const _title: string = props.data.post.title;
|
|
625
|
+
const _views: number = props.data.post.views;
|
|
626
|
+
const _slug: string = props.params.slug;
|
|
627
|
+
// a promise left unawaited streams in: undefined, then the value (null on rejection)
|
|
628
|
+
const _comments: string[] | null | undefined = props.data.comments;
|
|
629
|
+
// @ts-expect-error not a promise on the page
|
|
630
|
+
props.data.comments?.then;
|
|
631
|
+
// the action's fail() data comes back as \`form\`
|
|
632
|
+
const _problem: string | undefined = props.form?.problem;
|
|
633
|
+
export {};`,
|
|
634
|
+
);
|
|
635
|
+
|
|
636
|
+
// tsconfig for this mini-project
|
|
637
|
+
await writeFile(
|
|
638
|
+
join(tscDir, "tsconfig.json"),
|
|
639
|
+
JSON.stringify(
|
|
640
|
+
{
|
|
641
|
+
extends: "./.grimoire/tsconfig.generated.json",
|
|
642
|
+
compilerOptions: {
|
|
643
|
+
target: "ESNext",
|
|
644
|
+
module: "ESNext",
|
|
645
|
+
moduleResolution: "bundler",
|
|
646
|
+
strict: true,
|
|
647
|
+
skipLibCheck: true,
|
|
648
|
+
noEmit: true,
|
|
649
|
+
},
|
|
650
|
+
include: ["src", ".grimoire/types"],
|
|
651
|
+
},
|
|
652
|
+
null,
|
|
653
|
+
2,
|
|
654
|
+
),
|
|
655
|
+
);
|
|
656
|
+
|
|
657
|
+
// Generate types
|
|
658
|
+
const routesDir = join(tscDir, "src", "routes");
|
|
659
|
+
const tree = await scanRoutes(routesDir, tscDir);
|
|
660
|
+
await generateTypes(tree, {
|
|
661
|
+
projectRoot: tscDir,
|
|
662
|
+
routesDir,
|
|
663
|
+
outDir: join(tscDir, ".grimoire", "types"),
|
|
664
|
+
});
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
afterAll(async () => {
|
|
668
|
+
await rm(tscDir, { recursive: true, force: true });
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
function runTsc(cwd: string) {
|
|
672
|
+
const proc = Bun.spawnSync(
|
|
673
|
+
[
|
|
674
|
+
process.execPath,
|
|
675
|
+
"x",
|
|
676
|
+
"--bun",
|
|
677
|
+
"@typescript/native-preview",
|
|
678
|
+
"--noEmit",
|
|
679
|
+
"--project",
|
|
680
|
+
"tsconfig.json",
|
|
681
|
+
],
|
|
682
|
+
{ cwd },
|
|
683
|
+
);
|
|
684
|
+
return proc;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
test("check.ts with correct types passes tsc", () => {
|
|
688
|
+
const result = runTsc(tscDir);
|
|
689
|
+
// Ignore bun package-download messages; only care about exit code
|
|
690
|
+
expect(result.exitCode).toBe(0);
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
test("accessing a non-existent property fails tsc", async () => {
|
|
694
|
+
// Add a file with a wrong property access
|
|
695
|
+
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
696
|
+
await writeFile(
|
|
697
|
+
join(routeDir, "bad.ts"),
|
|
698
|
+
`import type { PageProps } from "./$types";
|
|
699
|
+
declare const props: PageProps;
|
|
700
|
+
const _bad = props.post.bogus;
|
|
701
|
+
export {};`,
|
|
702
|
+
);
|
|
703
|
+
|
|
704
|
+
const result = runTsc(tscDir);
|
|
705
|
+
expect(result.exitCode).not.toBe(0);
|
|
706
|
+
|
|
707
|
+
// Clean up so subsequent test runs are clean
|
|
708
|
+
const { unlink } = await import("node:fs/promises");
|
|
709
|
+
await unlink(join(routeDir, "bad.ts"));
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
test("form only has what the actions fail() with", async () => {
|
|
713
|
+
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
714
|
+
await writeFile(
|
|
715
|
+
join(routeDir, "bad-form.ts"),
|
|
716
|
+
`import type { PageProps } from "./$types";
|
|
717
|
+
declare const props: PageProps;
|
|
718
|
+
const _x = props.form?.nope;
|
|
719
|
+
export {};`,
|
|
720
|
+
);
|
|
721
|
+
const result = runTsc(tscDir);
|
|
722
|
+
expect(result.exitCode).not.toBe(0);
|
|
723
|
+
const { unlink } = await import("node:fs/promises");
|
|
724
|
+
await unlink(join(routeDir, "bad-form.ts"));
|
|
725
|
+
});
|
|
726
|
+
|
|
727
|
+
test("params properties are typed as string", async () => {
|
|
728
|
+
// Assigning params.slug to a number should fail
|
|
729
|
+
const routeDir = join(tscDir, "src", "routes", "blog", "[slug]");
|
|
730
|
+
await writeFile(
|
|
731
|
+
join(routeDir, "wrong-param-type.ts"),
|
|
732
|
+
`import type { PageProps } from "./$types";
|
|
733
|
+
declare const props: PageProps;
|
|
734
|
+
const _n: number = props.params.slug;
|
|
735
|
+
export {};`,
|
|
736
|
+
);
|
|
737
|
+
|
|
738
|
+
const result = runTsc(tscDir);
|
|
739
|
+
expect(result.exitCode).not.toBe(0);
|
|
740
|
+
|
|
741
|
+
const { unlink } = await import("node:fs/promises");
|
|
742
|
+
await unlink(join(routeDir, "wrong-param-type.ts"));
|
|
743
|
+
});
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
describe("stale route types", () => {
|
|
747
|
+
test("a deleted route's $types.d.ts is removed on the next run", async () => {
|
|
748
|
+
const root = join(tmpdir(), `grimoire-stale-${Date.now()}`);
|
|
749
|
+
const routesDir = join(root, "src", "routes");
|
|
750
|
+
await mkdir(join(routesDir, "gone"), { recursive: true });
|
|
751
|
+
await writeFile(join(routesDir, "+page.tsx"), "export default () => null;");
|
|
752
|
+
await writeFile(
|
|
753
|
+
join(routesDir, "gone", "+page.tsx"),
|
|
754
|
+
"export default () => null;",
|
|
755
|
+
);
|
|
756
|
+
const outDir = join(root, ".grimoire", "types");
|
|
757
|
+
const run = async () =>
|
|
758
|
+
generateTypes(await scanRoutes(routesDir, root), {
|
|
759
|
+
projectRoot: root,
|
|
760
|
+
routesDir,
|
|
761
|
+
outDir,
|
|
762
|
+
});
|
|
763
|
+
await run();
|
|
764
|
+
const stale = join(outDir, "src", "routes", "gone", "$types.d.ts");
|
|
765
|
+
expect(await Bun.file(stale).exists()).toBe(true);
|
|
766
|
+
|
|
767
|
+
await rm(join(routesDir, "gone"), { recursive: true });
|
|
768
|
+
await run();
|
|
769
|
+
expect(await Bun.file(stale).exists()).toBe(false);
|
|
770
|
+
expect(
|
|
771
|
+
await Bun.file(join(outDir, "src", "routes", "$types.d.ts")).exists(),
|
|
772
|
+
).toBe(true);
|
|
773
|
+
await rm(root, { recursive: true, force: true });
|
|
774
|
+
});
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
describe("tsconfig paths for aliases", () => {
|
|
778
|
+
test("each alias gets a paths entry relative to .grimoire", async () => {
|
|
779
|
+
const root = join(tmpdir(), `grimoire-paths-${Date.now()}`);
|
|
780
|
+
await mkdir(join(root, "src/routes"), { recursive: true });
|
|
781
|
+
try {
|
|
782
|
+
const tree = await scanRoutes(join(root, "src/routes"), root);
|
|
783
|
+
await generateTypes(tree, {
|
|
784
|
+
projectRoot: root,
|
|
785
|
+
routesDir: join(root, "src/routes"),
|
|
786
|
+
outDir: join(root, ".grimoire", "types"),
|
|
787
|
+
aliases: { $lib: join(root, "src/lib"), "@ui": join(root, "ui") },
|
|
788
|
+
});
|
|
789
|
+
const config = JSON.parse(
|
|
790
|
+
await readFile(
|
|
791
|
+
join(root, ".grimoire", "tsconfig.generated.json"),
|
|
792
|
+
"utf-8",
|
|
793
|
+
),
|
|
794
|
+
);
|
|
795
|
+
expect(config.compilerOptions.paths).toEqual({
|
|
796
|
+
$lib: ["../src/lib"],
|
|
797
|
+
"$lib/*": ["../src/lib/*"],
|
|
798
|
+
"@ui": ["../ui"],
|
|
799
|
+
"@ui/*": ["../ui/*"],
|
|
800
|
+
});
|
|
801
|
+
} finally {
|
|
802
|
+
await rm(root, { recursive: true, force: true });
|
|
803
|
+
}
|
|
804
|
+
});
|
|
805
|
+
});
|