@sigil-dev/grimoire 0.9.2 → 0.9.3
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/index.ts +35 -35
- package/package.json +8 -8
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +13 -7
- package/src/client/enhance.ts +98 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +445 -371
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +163 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +114 -114
- package/src/dev/hmr-client.ts +185 -185
- package/src/dev/loader.ts +47 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/watcher.ts +47 -46
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +47 -47
- package/src/rendering/hydrate.ts +98 -79
- package/src/rendering/index.ts +455 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +12 -11
- package/src/routing/router.ts +109 -109
- package/src/routing/scanner.ts +141 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +243 -219
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -356
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +655 -658
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -120
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +356 -356
- package/src/types.ts +4 -4
- package/test/client-router.test.ts +37 -31
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +70 -70
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +615 -579
- package/test/routing.test.ts +81 -81
- package/test/scanning.test.ts +200 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +249 -249
- package/test/streaming.test.ts +125 -125
- package/test/transform-routes.test.ts +84 -84
- package/test/typegen.test.ts +682 -682
- package/tsconfig.json +10 -10
- package/.grimoire/_routes.dom.js +0 -8
- package/.grimoire/_routes.hydrate.js +0 -8
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -59
- package/.grimoire/types/api/hello/$types.d.ts +0 -50
- package/.grimoire/types/api/items/$types.d.ts +0 -50
- package/.grimoire/types/echo/$types.d.ts +0 -50
- package/.grimoire/types/env-private.d.ts +0 -5
- package/.grimoire/types/env-public.d.ts +0 -5
- package/.grimoire/types/mixed/$types.d.ts +0 -50
- package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
- package/.grimoire/types/reject/$types.d.ts +0 -50
- package/public/__grimoire__/hydrate.js +0 -585
- package/public/__grimoire__/index.js +0 -490
package/test/headers.test.ts
CHANGED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { securityHeaders } from "../src/headers";
|
|
3
|
-
|
|
4
|
-
function fakeRequest(path = "/") {
|
|
5
|
-
return new Request(`http://localhost${path}`);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
async function runPlugin(
|
|
9
|
-
config: Parameters<typeof securityHeaders>[0],
|
|
10
|
-
req?: Request,
|
|
11
|
-
): Promise<Headers> {
|
|
12
|
-
const plugin = securityHeaders(config);
|
|
13
|
-
const res = new Response("<html></html>", {
|
|
14
|
-
headers: { "Content-Type": "text/html" },
|
|
15
|
-
});
|
|
16
|
-
const result = await plugin.onRequest!(req ?? fakeRequest(), async () => res);
|
|
17
|
-
return result.headers;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
describe("securityHeaders()", () => {
|
|
21
|
-
test("applies default headers", async () => {
|
|
22
|
-
const headers = await runPlugin({});
|
|
23
|
-
expect(headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
24
|
-
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
25
|
-
expect(headers.get("Referrer-Policy")).toBe(
|
|
26
|
-
"strict-origin-when-cross-origin",
|
|
27
|
-
);
|
|
28
|
-
expect(headers.get("Permissions-Policy")).toBe(
|
|
29
|
-
"camera=(), microphone=(), geolocation=()",
|
|
30
|
-
);
|
|
31
|
-
expect(headers.get("X-XSS-Protection")).toBe("0");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test("applies CSP by default", async () => {
|
|
35
|
-
const headers = await runPlugin({});
|
|
36
|
-
expect(headers.get("Content-Security-Policy")).toContain(
|
|
37
|
-
"default-src 'self'",
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("allows overriding individual headers", async () => {
|
|
42
|
-
const headers = await runPlugin({
|
|
43
|
-
frameOptions: "SAMEORIGIN",
|
|
44
|
-
contentTypeOptions: false,
|
|
45
|
-
});
|
|
46
|
-
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
47
|
-
expect(headers.has("X-Content-Type-Options")).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("disables headers when set to false", async () => {
|
|
51
|
-
const headers = await runPlugin({
|
|
52
|
-
contentSecurityPolicy: false,
|
|
53
|
-
strictTransportSecurity: false,
|
|
54
|
-
permissionsPolicy: false,
|
|
55
|
-
});
|
|
56
|
-
expect(headers.has("Content-Security-Policy")).toBe(false);
|
|
57
|
-
expect(headers.has("Strict-Transport-Security")).toBe(false);
|
|
58
|
-
expect(headers.has("Permissions-Policy")).toBe(false);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test("applies route overrides", async () => {
|
|
62
|
-
const headers = await runPlugin(
|
|
63
|
-
{
|
|
64
|
-
frameOptions: "DENY",
|
|
65
|
-
routes: {
|
|
66
|
-
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
fakeRequest("/admin/settings"),
|
|
70
|
-
);
|
|
71
|
-
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("route override only applies to matching prefix", async () => {
|
|
75
|
-
const headers = await runPlugin(
|
|
76
|
-
{
|
|
77
|
-
frameOptions: "DENY",
|
|
78
|
-
routes: {
|
|
79
|
-
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
fakeRequest("/dashboard"),
|
|
83
|
-
);
|
|
84
|
-
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("preserves existing response headers", async () => {
|
|
88
|
-
const plugin = securityHeaders({});
|
|
89
|
-
const res = new Response("<html></html>", {
|
|
90
|
-
headers: {
|
|
91
|
-
"Content-Type": "text/html",
|
|
92
|
-
"Set-Cookie": "session=abc",
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
const result = await plugin.onRequest!(fakeRequest(), async () => res);
|
|
96
|
-
expect(result.headers.get("Content-Type")).toBe("text/html");
|
|
97
|
-
expect(result.headers.get("Set-Cookie")).toBe("session=abc");
|
|
98
|
-
expect(result.headers.get("X-Frame-Options")).toBe("DENY");
|
|
99
|
-
});
|
|
100
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { securityHeaders } from "../src/headers";
|
|
3
|
+
|
|
4
|
+
function fakeRequest(path = "/") {
|
|
5
|
+
return new Request(`http://localhost${path}`);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async function runPlugin(
|
|
9
|
+
config: Parameters<typeof securityHeaders>[0],
|
|
10
|
+
req?: Request,
|
|
11
|
+
): Promise<Headers> {
|
|
12
|
+
const plugin = securityHeaders(config);
|
|
13
|
+
const res = new Response("<html></html>", {
|
|
14
|
+
headers: { "Content-Type": "text/html" },
|
|
15
|
+
});
|
|
16
|
+
const result = await plugin.onRequest!(req ?? fakeRequest(), async () => res);
|
|
17
|
+
return result.headers;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("securityHeaders()", () => {
|
|
21
|
+
test("applies default headers", async () => {
|
|
22
|
+
const headers = await runPlugin({});
|
|
23
|
+
expect(headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
24
|
+
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
25
|
+
expect(headers.get("Referrer-Policy")).toBe(
|
|
26
|
+
"strict-origin-when-cross-origin",
|
|
27
|
+
);
|
|
28
|
+
expect(headers.get("Permissions-Policy")).toBe(
|
|
29
|
+
"camera=(), microphone=(), geolocation=()",
|
|
30
|
+
);
|
|
31
|
+
expect(headers.get("X-XSS-Protection")).toBe("0");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("applies CSP by default", async () => {
|
|
35
|
+
const headers = await runPlugin({});
|
|
36
|
+
expect(headers.get("Content-Security-Policy")).toContain(
|
|
37
|
+
"default-src 'self'",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("allows overriding individual headers", async () => {
|
|
42
|
+
const headers = await runPlugin({
|
|
43
|
+
frameOptions: "SAMEORIGIN",
|
|
44
|
+
contentTypeOptions: false,
|
|
45
|
+
});
|
|
46
|
+
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
47
|
+
expect(headers.has("X-Content-Type-Options")).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("disables headers when set to false", async () => {
|
|
51
|
+
const headers = await runPlugin({
|
|
52
|
+
contentSecurityPolicy: false,
|
|
53
|
+
strictTransportSecurity: false,
|
|
54
|
+
permissionsPolicy: false,
|
|
55
|
+
});
|
|
56
|
+
expect(headers.has("Content-Security-Policy")).toBe(false);
|
|
57
|
+
expect(headers.has("Strict-Transport-Security")).toBe(false);
|
|
58
|
+
expect(headers.has("Permissions-Policy")).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("applies route overrides", async () => {
|
|
62
|
+
const headers = await runPlugin(
|
|
63
|
+
{
|
|
64
|
+
frameOptions: "DENY",
|
|
65
|
+
routes: {
|
|
66
|
+
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
fakeRequest("/admin/settings"),
|
|
70
|
+
);
|
|
71
|
+
expect(headers.get("X-Frame-Options")).toBe("SAMEORIGIN");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("route override only applies to matching prefix", async () => {
|
|
75
|
+
const headers = await runPlugin(
|
|
76
|
+
{
|
|
77
|
+
frameOptions: "DENY",
|
|
78
|
+
routes: {
|
|
79
|
+
"/admin": { frameOptions: "SAMEORIGIN" },
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
fakeRequest("/dashboard"),
|
|
83
|
+
);
|
|
84
|
+
expect(headers.get("X-Frame-Options")).toBe("DENY");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("preserves existing response headers", async () => {
|
|
88
|
+
const plugin = securityHeaders({});
|
|
89
|
+
const res = new Response("<html></html>", {
|
|
90
|
+
headers: {
|
|
91
|
+
"Content-Type": "text/html",
|
|
92
|
+
"Set-Cookie": "session=abc",
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const result = await plugin.onRequest!(fakeRequest(), async () => res);
|
|
96
|
+
expect(result.headers.get("Content-Type")).toBe("text/html");
|
|
97
|
+
expect(result.headers.get("Set-Cookie")).toBe("session=abc");
|
|
98
|
+
expect(result.headers.get("X-Frame-Options")).toBe("DENY");
|
|
99
|
+
});
|
|
100
|
+
});
|
package/test/hydration.test.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { Window } from "happy-dom";
|
|
2
|
-
|
|
3
|
-
// set up DOM globals before tests
|
|
4
|
-
const window = new Window();
|
|
5
|
-
const document = window.document;
|
|
6
|
-
|
|
7
|
-
// @ts-expect-error
|
|
8
|
-
globalThis.document = document;
|
|
9
|
-
// @ts-expect-error
|
|
10
|
-
globalThis.window = window;
|
|
11
|
-
// @ts-expect-error
|
|
12
|
-
globalThis.HTMLElement = window.HTMLElement;
|
|
13
|
-
// @ts-expect-error
|
|
14
|
-
globalThis.Event = window.Event;
|
|
15
|
-
// @ts-expect-error
|
|
16
|
-
globalThis.MouseEvent = window.MouseEvent;
|
|
17
|
-
// @ts-expect-error
|
|
18
|
-
globalThis.SyntaxError = SyntaxError;
|
|
19
|
-
|
|
20
|
-
import { beforeEach, describe, expect, test } from "bun:test";
|
|
21
|
-
|
|
22
|
-
// mock routes - simulates what #grimoire-routes generates
|
|
23
|
-
const mockRoutes: Record<string, (props: any) => any> = {
|
|
24
|
-
"/spells/:id": (props: any) => {
|
|
25
|
-
return { params: props.params };
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// simulate hydrate logic (same as hydrate.ts but without the import)
|
|
30
|
-
function runHydrate(routes: Record<string, (props: any) => any>) {
|
|
31
|
-
const stateEl = document.getElementById("__grimoire_state__");
|
|
32
|
-
if (stateEl) {
|
|
33
|
-
const state = JSON.parse(stateEl.textContent!);
|
|
34
|
-
const Page = routes[state.pattern];
|
|
35
|
-
if (Page) {
|
|
36
|
-
Page({ ...state.data, params: state.params });
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// simulate initRouter (same logic as router.ts)
|
|
42
|
-
let clickHandlerAttached = false;
|
|
43
|
-
function initRouter(_routes: Record<string, (props: any) => any>) {
|
|
44
|
-
document.addEventListener("click", () => {});
|
|
45
|
-
clickHandlerAttached = true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
describe("hydration", () => {
|
|
49
|
-
beforeEach(() => {
|
|
50
|
-
clickHandlerAttached = false;
|
|
51
|
-
document.body.innerHTML = `
|
|
52
|
-
<div id="app">
|
|
53
|
-
<div id="grimoire-page">
|
|
54
|
-
<div class="spell-detail">
|
|
55
|
-
<h1>Fireball</h1>
|
|
56
|
-
</div>
|
|
57
|
-
</div>
|
|
58
|
-
</div>
|
|
59
|
-
<script id="__grimoire_state__" type="application/json">
|
|
60
|
-
${JSON.stringify({ params: { id: "fireball" }, data: {}, pattern: "/spells/:id" })}
|
|
61
|
-
</script>
|
|
62
|
-
`;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// 1. hydrate claims existing DOM, doesn't create new nodes
|
|
66
|
-
test("hydrate claims existing h1 instead of creating new one", () => {
|
|
67
|
-
const before = document.getElementById("app")!.innerHTML;
|
|
68
|
-
runHydrate(mockRoutes);
|
|
69
|
-
// DOM unchanged - hydrate only reads state, doesn't write
|
|
70
|
-
expect(document.getElementById("app")!.innerHTML).toBe(before);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// 2. router attaches after hydration
|
|
74
|
-
test("initRouter attaches click handler", () => {
|
|
75
|
-
initRouter(mockRoutes);
|
|
76
|
-
expect(clickHandlerAttached).toBe(true);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
// 3. state script is readable
|
|
80
|
-
test("grimoire state is parseable", () => {
|
|
81
|
-
const el = document.getElementById("__grimoire_state__");
|
|
82
|
-
const state = JSON.parse(el!.textContent!);
|
|
83
|
-
expect(state.pattern).toBe("/spells/:id");
|
|
84
|
-
expect(state.params).toEqual({ id: "fireball" });
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// 4. hydrate doesn't touch DOM structure
|
|
88
|
-
test("hydrate preserves DOM structure", () => {
|
|
89
|
-
const before = document.body.innerHTML;
|
|
90
|
-
runHydrate(mockRoutes);
|
|
91
|
-
expect(document.body.innerHTML).toBe(before);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// 5. hydrate with missing pattern is safe
|
|
95
|
-
test("hydrate with unknown pattern does not throw", () => {
|
|
96
|
-
const el = document.getElementById("__grimoire_state__");
|
|
97
|
-
const state = JSON.parse(el!.textContent!);
|
|
98
|
-
state.pattern = "/nonexistent";
|
|
99
|
-
el!.textContent = JSON.stringify(state);
|
|
100
|
-
|
|
101
|
-
expect(() => runHydrate(mockRoutes)).not.toThrow();
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// 6. hydrate passes data and params correctly
|
|
105
|
-
test("hydrate passes data and params to page component", () => {
|
|
106
|
-
let receivedProps: any = null;
|
|
107
|
-
const testRoutes: Record<string, (props: any) => any> = {
|
|
108
|
-
"/spells/:id": (props: any) => {
|
|
109
|
-
receivedProps = props;
|
|
110
|
-
return null;
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
runHydrate(testRoutes);
|
|
115
|
-
expect(receivedProps).toEqual({
|
|
116
|
-
params: { id: "fireball" },
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
});
|
|
1
|
+
import { Window } from "happy-dom";
|
|
2
|
+
|
|
3
|
+
// set up DOM globals before tests
|
|
4
|
+
const window = new Window();
|
|
5
|
+
const document = window.document;
|
|
6
|
+
|
|
7
|
+
// @ts-expect-error
|
|
8
|
+
globalThis.document = document;
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
globalThis.window = window;
|
|
11
|
+
// @ts-expect-error
|
|
12
|
+
globalThis.HTMLElement = window.HTMLElement;
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
globalThis.Event = window.Event;
|
|
15
|
+
// @ts-expect-error
|
|
16
|
+
globalThis.MouseEvent = window.MouseEvent;
|
|
17
|
+
// @ts-expect-error
|
|
18
|
+
globalThis.SyntaxError = SyntaxError;
|
|
19
|
+
|
|
20
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
21
|
+
|
|
22
|
+
// mock routes - simulates what #grimoire-routes generates
|
|
23
|
+
const mockRoutes: Record<string, (props: any) => any> = {
|
|
24
|
+
"/spells/:id": (props: any) => {
|
|
25
|
+
return { params: props.params };
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// simulate hydrate logic (same as hydrate.ts but without the import)
|
|
30
|
+
function runHydrate(routes: Record<string, (props: any) => any>) {
|
|
31
|
+
const stateEl = document.getElementById("__grimoire_state__");
|
|
32
|
+
if (stateEl) {
|
|
33
|
+
const state = JSON.parse(stateEl.textContent!);
|
|
34
|
+
const Page = routes[state.pattern];
|
|
35
|
+
if (Page) {
|
|
36
|
+
Page({ ...state.data, params: state.params });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// simulate initRouter (same logic as router.ts)
|
|
42
|
+
let clickHandlerAttached = false;
|
|
43
|
+
function initRouter(_routes: Record<string, (props: any) => any>) {
|
|
44
|
+
document.addEventListener("click", () => {});
|
|
45
|
+
clickHandlerAttached = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe("hydration", () => {
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
clickHandlerAttached = false;
|
|
51
|
+
document.body.innerHTML = `
|
|
52
|
+
<div id="app">
|
|
53
|
+
<div id="grimoire-page">
|
|
54
|
+
<div class="spell-detail">
|
|
55
|
+
<h1>Fireball</h1>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
</div>
|
|
59
|
+
<script id="__grimoire_state__" type="application/json">
|
|
60
|
+
${JSON.stringify({ params: { id: "fireball" }, data: {}, pattern: "/spells/:id" })}
|
|
61
|
+
</script>
|
|
62
|
+
`;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// 1. hydrate claims existing DOM, doesn't create new nodes
|
|
66
|
+
test("hydrate claims existing h1 instead of creating new one", () => {
|
|
67
|
+
const before = document.getElementById("app")!.innerHTML;
|
|
68
|
+
runHydrate(mockRoutes);
|
|
69
|
+
// DOM unchanged - hydrate only reads state, doesn't write
|
|
70
|
+
expect(document.getElementById("app")!.innerHTML).toBe(before);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// 2. router attaches after hydration
|
|
74
|
+
test("initRouter attaches click handler", () => {
|
|
75
|
+
initRouter(mockRoutes);
|
|
76
|
+
expect(clickHandlerAttached).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// 3. state script is readable
|
|
80
|
+
test("grimoire state is parseable", () => {
|
|
81
|
+
const el = document.getElementById("__grimoire_state__");
|
|
82
|
+
const state = JSON.parse(el!.textContent!);
|
|
83
|
+
expect(state.pattern).toBe("/spells/:id");
|
|
84
|
+
expect(state.params).toEqual({ id: "fireball" });
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// 4. hydrate doesn't touch DOM structure
|
|
88
|
+
test("hydrate preserves DOM structure", () => {
|
|
89
|
+
const before = document.body.innerHTML;
|
|
90
|
+
runHydrate(mockRoutes);
|
|
91
|
+
expect(document.body.innerHTML).toBe(before);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// 5. hydrate with missing pattern is safe
|
|
95
|
+
test("hydrate with unknown pattern does not throw", () => {
|
|
96
|
+
const el = document.getElementById("__grimoire_state__");
|
|
97
|
+
const state = JSON.parse(el!.textContent!);
|
|
98
|
+
state.pattern = "/nonexistent";
|
|
99
|
+
el!.textContent = JSON.stringify(state);
|
|
100
|
+
|
|
101
|
+
expect(() => runHydrate(mockRoutes)).not.toThrow();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// 6. hydrate passes data and params correctly
|
|
105
|
+
test("hydrate passes data and params to page component", () => {
|
|
106
|
+
let receivedProps: any = null;
|
|
107
|
+
const testRoutes: Record<string, (props: any) => any> = {
|
|
108
|
+
"/spells/:id": (props: any) => {
|
|
109
|
+
receivedProps = props;
|
|
110
|
+
return null;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
runHydrate(testRoutes);
|
|
115
|
+
expect(receivedProps).toEqual({
|
|
116
|
+
params: { id: "fireball" },
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
});
|