@sigil-dev/grimoire 0.6.8 → 0.6.10

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.
@@ -1,145 +1,106 @@
1
1
  import { describe, expect, test } from "bun:test";
2
2
  import { renderRoute } from "../src/rendering";
3
3
 
4
+ function makeMatched(path: string, filePath: string, params = {}) {
5
+ return {
6
+ route: { path, params: {}, filePath },
7
+ params,
8
+ layouts: [],
9
+ layoutServers: [],
10
+ } as any;
11
+ }
12
+
4
13
  describe("Streaming SSR", () => {
5
- test("returns a ReadableStream response", async () => {
6
- const matched = {
7
- route: {
8
- path: "/",
9
- params: {},
10
- filePath:
11
- "data:text/javascript,export default (p) => '<div>hello</div>'",
12
- },
13
- params: {},
14
- } as any;
15
-
16
- const res = await renderRoute(matched, new Request("http://localhost/"));
17
- expect(res.body).toBeInstanceOf(ReadableStream);
18
- });
19
-
20
- test("stream contains full HTML document", async () => {
21
- const matched = {
22
- route: {
23
- path: "/test",
24
- params: {},
25
- filePath:
26
- "data:text/javascript,export default (p) => '<p>streaming works</p>'",
27
- },
28
- params: {},
29
- } as any;
30
-
31
- const res = await renderRoute(
32
- matched,
33
- new Request("http://localhost/test"),
34
- );
35
- const html = await res.text();
36
-
37
- expect(html).toContain("<!DOCTYPE html>");
38
- expect(html).toContain('<meta charset="UTF-8" />');
39
- expect(html).toContain("streaming works");
40
- expect(html).toContain("</html>");
41
- expect(html).toContain("__grimoire_state__");
42
- });
43
-
44
- test("stream includes state JSON with route pattern", async () => {
45
- const matched = {
46
- route: {
47
- path: "/spells/:id",
48
- params: {},
49
- filePath:
50
- "data:text/javascript,export default (p) => '<div>spell</div>'",
51
- },
52
- params: { id: "fireball" },
53
- } as any;
54
-
55
- const res = await renderRoute(
56
- matched,
57
- new Request("http://localhost/spells/fireball"),
58
- );
59
- const html = await res.text();
60
-
61
- expect(html).toContain('"pattern":"/spells/:id"');
62
- expect(html).toContain('"id":"fireball"');
63
- });
64
-
65
- test("head script tag comes before head content", async () => {
66
- const matched = {
67
- route: {
68
- path: "/head-test",
69
- params: {},
70
- filePath:
71
- "data:text/javascript,export default (p) => '<div>head</div>'",
72
- },
73
- params: {},
74
- } as any;
75
-
76
- const res = await renderRoute(
77
- matched,
78
- new Request("http://localhost/head-test"),
79
- );
80
- const html = await res.text();
81
-
82
- const hydrateScript = html.indexOf("hydrate.js");
83
- const headClosing = html.indexOf("</head>");
84
- // hydrate.js should be in <head>
85
- expect(hydrateScript).toBeGreaterThan(-1);
86
- expect(hydrateScript).toBeLessThan(headClosing);
87
- });
88
-
89
- test("stream chunks arrive incrementally", async () => {
90
- const matched = {
91
- route: {
92
- path: "/chunked",
93
- params: {},
94
- filePath:
95
- "data:text/javascript,export default (p) => '<div>chunked</div>'",
96
- },
97
- params: {},
98
- } as any;
99
-
100
- const res = await renderRoute(
101
- matched,
102
- new Request("http://localhost/chunked"),
103
- );
104
- const reader = res.body!.getReader();
105
- const chunks: string[] = [];
106
-
107
- while (true) {
108
- const { done, value } = await reader.read();
109
- if (done) break;
110
- // Bun ReadableStream returns Uint8Array
111
- chunks.push(
112
- typeof value === "string"
113
- ? value
114
- : new TextDecoder().decode(value.buffer),
115
- );
116
- }
117
-
118
- // Should have multiple chunks (at least DOCTYPE + body)
119
- expect(chunks.length).toBeGreaterThan(1);
120
- // First chunk should start with DOCTYPE
121
- expect(chunks[0]).toContain("<!DOCTYPE html>");
122
- // Last chunk should close the document
123
- expect(chunks[chunks.length - 1]).toContain("</html>");
124
- });
125
-
126
- test("navigation request returns JSON not stream", async () => {
127
- const matched = {
128
- route: {
129
- path: "/nav",
130
- params: {},
131
- filePath: "data:text/javascript,export default (p) => '<div>nav</div>'",
132
- },
133
- params: {},
134
- } as any;
135
-
136
- const req = new Request("http://localhost/nav", {
137
- headers: { "x-grimoire-navigate": "1" },
138
- });
139
- const res = await renderRoute(matched, req);
140
- const json = await res.json();
141
-
142
- expect(json.pattern).toBe("/nav");
143
- expect(json.data).toBeDefined();
144
- });
14
+ test("returns a ReadableStream response", async () => {
15
+ const matched = makeMatched("/", "data:text/javascript,export default (p) => '<div>hello</div>'");
16
+ const res = await renderRoute(matched, new Request("http://localhost/"));
17
+ expect(res.body).toBeInstanceOf(ReadableStream);
18
+ });
19
+
20
+ test("stream contains full HTML document", async () => {
21
+ const matched = makeMatched("/test", "data:text/javascript,export default (p) => '<p>streaming works</p>'");
22
+
23
+ const res = await renderRoute(
24
+ matched,
25
+ new Request("http://localhost/test"),
26
+ );
27
+ const html = await res.text();
28
+
29
+ expect(html).toContain("<!DOCTYPE html>");
30
+ expect(html).toContain('<meta charset="UTF-8" />');
31
+ expect(html).toContain("streaming works");
32
+ expect(html).toContain("</html>");
33
+ expect(html).toContain("__grimoire_state__");
34
+ });
35
+
36
+ test("stream includes state JSON with route pattern", async () => {
37
+ const matched = makeMatched("/spells/:id", "data:text/javascript,export default (p) => '<div>spell</div>'", { id: "fireball" });
38
+
39
+ const res = await renderRoute(
40
+ matched,
41
+ new Request("http://localhost/spells/fireball"),
42
+ );
43
+ const html = await res.text();
44
+
45
+ expect(html).toContain('"pattern":"/spells/:id"');
46
+ expect(html).toContain('"id":"fireball"');
47
+ });
48
+
49
+ test("head script tag comes before head content", async () => {
50
+ const matched = makeMatched("/head-test", "data:text/javascript,export default (p) => '<div>head</div>'");
51
+
52
+ const res = await renderRoute(
53
+ matched,
54
+ new Request("http://localhost/head-test"),
55
+ );
56
+ const html = await res.text();
57
+
58
+ const hydrateScript = html.indexOf("hydrate.js");
59
+ const headClosing = html.indexOf("</head>");
60
+ // hydrate.js should be in <head>
61
+ expect(hydrateScript).toBeGreaterThan(-1);
62
+ expect(hydrateScript).toBeLessThan(headClosing);
63
+ });
64
+
65
+ test("stream chunks arrive incrementally", async () => {
66
+ const matched = makeMatched("/chunked", "data:text/javascript,export default (p) => '<div>chunked</div>'");
67
+
68
+ const res = await renderRoute(
69
+ matched,
70
+ new Request("http://localhost/chunked"),
71
+ );
72
+ const reader = res.body!.getReader();
73
+ const chunks: string[] = [];
74
+
75
+ while (true) {
76
+ const { done, value } = await reader.read();
77
+ if (done) break;
78
+ // Bun ReadableStream returns Uint8Array
79
+ chunks.push(
80
+ typeof value === "string"
81
+ ? value
82
+ : new TextDecoder().decode(value.buffer),
83
+ );
84
+ }
85
+
86
+ // Should have multiple chunks (at least DOCTYPE + body)
87
+ expect(chunks.length).toBeGreaterThan(1);
88
+ // First chunk should start with DOCTYPE
89
+ expect(chunks[0]).toContain("<!DOCTYPE html>");
90
+ // Last chunk should close the document
91
+ expect(chunks[chunks.length - 1]).toContain("</html>");
92
+ });
93
+
94
+ test("navigation request returns JSON not stream", async () => {
95
+ const matched = makeMatched("/nav", "data:text/javascript,export default (p) => '<div>nav</div>'");
96
+
97
+ const req = new Request("http://localhost/nav", {
98
+ headers: { "x-grimoire-navigate": "1" },
99
+ });
100
+ const res = await renderRoute(matched, req);
101
+ const json = await res.json();
102
+
103
+ expect(json.pattern).toBe("/nav");
104
+ expect(json.data).toBeDefined();
105
+ });
145
106
  });