@krate/runtime 0.5.0-rc.1 → 0.5.0-rc.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.
Files changed (153) hide show
  1. package/dist/actions.d.ts +7 -0
  2. package/dist/actions.js +23 -8
  3. package/dist/actions.test.js +8 -410
  4. package/dist/after.d.ts +28 -0
  5. package/dist/after.js +48 -0
  6. package/dist/after.test.js +58 -0
  7. package/dist/assets-static.d.ts +10 -1
  8. package/dist/assets-static.js +10 -2
  9. package/dist/base-path.d.ts +7 -0
  10. package/dist/base-path.js +20 -0
  11. package/dist/builtin-tags.d.ts +12 -0
  12. package/dist/builtin-tags.js +42 -0
  13. package/dist/builtin-tags.test.js +12 -0
  14. package/dist/cache-adapter.test.js +4 -203
  15. package/dist/cache-scope.d.ts +2 -0
  16. package/dist/cache.test.js +1 -197
  17. package/dist/cloudflare-workers.d.ts +2 -0
  18. package/dist/cloudflare-workers.js +27 -0
  19. package/dist/dev-overlay.d.ts +54 -3
  20. package/dist/dev-overlay.js +469 -76
  21. package/dist/dom.js +17 -2
  22. package/dist/events.test.js +2 -407
  23. package/dist/handler-html.test.js +1 -202
  24. package/dist/handler-routing.js +3 -0
  25. package/dist/handler-segments.d.ts +6 -0
  26. package/dist/handler-segments.js +16 -2
  27. package/dist/handler-segments.test.js +8 -200
  28. package/dist/head-tags.d.ts +9 -0
  29. package/dist/head-tags.js +85 -1
  30. package/dist/head-tags.test.js +18 -125
  31. package/dist/index.d.ts +1 -0
  32. package/dist/index.js +3 -0
  33. package/dist/inline-action-token.test.js +31 -0
  34. package/dist/jsx-svg-types.test.js +58 -0
  35. package/dist/jsx-types.d.ts +92 -1
  36. package/dist/krate-core.js +1 -1
  37. package/dist/krate-dev.js +384 -65
  38. package/dist/krate-f-actions.js +1 -1
  39. package/dist/krate-f-navigation.js +1 -1
  40. package/dist/krate-f-router-lite.js +1 -1
  41. package/dist/krate-f-router.js +1 -1
  42. package/dist/krate-features.json +2 -0
  43. package/dist/krate-hydrate-lite.js +5 -5
  44. package/dist/krate-hydrate.js +5 -5
  45. package/dist/krate-runtime.js +5 -5
  46. package/dist/link-base-path.test.js +28 -0
  47. package/dist/metadata.js +3 -0
  48. package/dist/node-server.test.js +1 -355
  49. package/dist/platform.d.ts +52 -0
  50. package/dist/platform.js +57 -0
  51. package/dist/platform.test.js +44 -0
  52. package/dist/router-lite.js +3 -9
  53. package/dist/router-nav.test.js +15 -444
  54. package/dist/router.js +51 -18
  55. package/dist/server-handler.d.ts +49 -0
  56. package/dist/server-handler.js +311 -21
  57. package/dist/server-handler.test.js +56 -344
  58. package/dist/server-renderer.test.js +0 -104
  59. package/dist/server.d.ts +15 -5
  60. package/dist/server.js +150 -22
  61. package/dist/server.test.js +52 -60
  62. package/dist/site-config.d.ts +13 -0
  63. package/dist/site-config.js +28 -0
  64. package/dist/site-config.test.js +30 -0
  65. package/dist/ssr-core-behavior.test.js +3 -515
  66. package/dist/testing.d.ts +59 -0
  67. package/dist/testing.js +88 -0
  68. package/dist/testing.test.js +73 -0
  69. package/package.json +9 -1
  70. package/dist/action-context.test.js +0 -72
  71. package/dist/class-component.test.js +0 -462
  72. package/dist/class-helpers.test.js +0 -169
  73. package/dist/context.test.js +0 -57
  74. package/dist/derived.test.js +0 -140
  75. package/dist/dev-overlay.test.js +0 -165
  76. package/dist/discrete.test.js +0 -100
  77. package/dist/dom.test.js +0 -39
  78. package/dist/effect-errors.test.d.ts +0 -1
  79. package/dist/effect-errors.test.js +0 -199
  80. package/dist/effects.test.d.ts +0 -1
  81. package/dist/effects.test.js +0 -485
  82. package/dist/handler-routing.test.d.ts +0 -1
  83. package/dist/handler-routing.test.js +0 -99
  84. package/dist/highlight-bind.test.d.ts +0 -1
  85. package/dist/highlight-bind.test.js +0 -124
  86. package/dist/highlight.test.d.ts +0 -1
  87. package/dist/highlight.test.js +0 -56
  88. package/dist/lanes.test.d.ts +0 -1
  89. package/dist/lanes.test.js +0 -59
  90. package/dist/list.test.d.ts +0 -1
  91. package/dist/list.test.js +0 -435
  92. package/dist/metadata.test.d.ts +0 -1
  93. package/dist/metadata.test.js +0 -167
  94. package/dist/navigation.test.d.ts +0 -1
  95. package/dist/navigation.test.js +0 -130
  96. package/dist/optimistic-lanes.test.d.ts +0 -1
  97. package/dist/optimistic-lanes.test.js +0 -45
  98. package/dist/patterns.test.d.ts +0 -1
  99. package/dist/patterns.test.js +0 -125
  100. package/dist/portal.test.d.ts +0 -1
  101. package/dist/portal.test.js +0 -99
  102. package/dist/react-effects.test.d.ts +0 -1
  103. package/dist/react-effects.test.js +0 -133
  104. package/dist/react-suspense.test.d.ts +0 -1
  105. package/dist/react-suspense.test.js +0 -194
  106. package/dist/react.test.d.ts +0 -1
  107. package/dist/react.test.js +0 -60
  108. package/dist/react19.test.d.ts +0 -1
  109. package/dist/react19.test.js +0 -176
  110. package/dist/reactivity-core.test.d.ts +0 -1
  111. package/dist/reactivity-core.test.js +0 -152
  112. package/dist/reconcile.test.d.ts +0 -1
  113. package/dist/reconcile.test.js +0 -110
  114. package/dist/refs.test.d.ts +0 -1
  115. package/dist/refs.test.js +0 -257
  116. package/dist/router-layout.test.d.ts +0 -1
  117. package/dist/router-layout.test.js +0 -249
  118. package/dist/router-slots.test.d.ts +0 -1
  119. package/dist/router-slots.test.js +0 -144
  120. package/dist/router-speculation.test.d.ts +0 -1
  121. package/dist/router-speculation.test.js +0 -25
  122. package/dist/router-styles.test.d.ts +0 -1
  123. package/dist/router-styles.test.js +0 -98
  124. package/dist/router.test.d.ts +0 -1
  125. package/dist/router.test.js +0 -36
  126. package/dist/server-async.test.d.ts +0 -1
  127. package/dist/server-async.test.js +0 -216
  128. package/dist/server-highlight.test.d.ts +0 -1
  129. package/dist/server-highlight.test.js +0 -32
  130. package/dist/signal-lanes.test.d.ts +0 -1
  131. package/dist/signal-lanes.test.js +0 -32
  132. package/dist/slots.test.d.ts +0 -1
  133. package/dist/slots.test.js +0 -187
  134. package/dist/snapshots.test.d.ts +0 -1
  135. package/dist/snapshots.test.js +0 -208
  136. package/dist/ssr-core.test.d.ts +0 -1
  137. package/dist/ssr-core.test.js +0 -324
  138. package/dist/static-memory.test.d.ts +0 -1
  139. package/dist/static-memory.test.js +0 -26
  140. package/dist/storage.test.d.ts +0 -1
  141. package/dist/storage.test.js +0 -46
  142. package/dist/suspense-actions.test.d.ts +0 -1
  143. package/dist/suspense-actions.test.js +0 -72
  144. package/dist/suspense.test.d.ts +0 -1
  145. package/dist/suspense.test.js +0 -556
  146. /package/dist/{action-context.test.d.ts → after.test.d.ts} +0 -0
  147. /package/dist/{class-component.test.d.ts → builtin-tags.test.d.ts} +0 -0
  148. /package/dist/{class-helpers.test.d.ts → inline-action-token.test.d.ts} +0 -0
  149. /package/dist/{context.test.d.ts → jsx-svg-types.test.d.ts} +0 -0
  150. /package/dist/{derived.test.d.ts → link-base-path.test.d.ts} +0 -0
  151. /package/dist/{dev-overlay.test.d.ts → platform.test.d.ts} +0 -0
  152. /package/dist/{discrete.test.d.ts → site-config.test.d.ts} +0 -0
  153. /package/dist/{dom.test.d.ts → testing.test.d.ts} +0 -0
package/dist/list.test.js DELETED
@@ -1,435 +0,0 @@
1
- import { describe, it, expect, vi, afterEach } from 'vitest';
2
- import { createSignal, onMount, onCleanup } from './signal';
3
- import { h } from './dom';
4
- import { krateList, krateBindList, longestIncreasingSubsequence } from './list';
5
- import { createEffectDeps } from './effects';
6
- const tick = () => new Promise((r) => setTimeout(r, 0));
7
- function mountList(node) {
8
- const root = document.createElement('div');
9
- document.body.appendChild(root);
10
- root.appendChild(node);
11
- return root;
12
- }
13
- function rowTexts(root, selector = 'li') {
14
- return Array.from(root.querySelectorAll(selector)).map((el) => el.textContent || '');
15
- }
16
- afterEach(() => {
17
- document.body.innerHTML = '';
18
- });
19
- describe('longestIncreasingSubsequence', () => {
20
- it('ignores negative entries and returns indices in order', () => {
21
- expect(longestIncreasingSubsequence([2, 0, 1, -1, 3])).toEqual([1, 2, 4]);
22
- expect(longestIncreasingSubsequence([3, 2, 1, 0])).toHaveLength(1);
23
- expect(longestIncreasingSubsequence([])).toEqual([]);
24
- });
25
- });
26
- describe('keyed lists', () => {
27
- it('reorders by key keeping DOM nodes, input values and focus', async () => {
28
- const [items, setItems] = createSignal([
29
- { id: 1, label: 'a' },
30
- { id: 2, label: 'b' },
31
- { id: 3, label: 'c' },
32
- ]);
33
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label, h('input', { class: () => 'in-' + t().id })), 1).mount());
34
- const inputs = Array.from(root.querySelectorAll('input'));
35
- inputs[0].value = 'typed-a';
36
- inputs[2].focus();
37
- const lis = Array.from(root.querySelectorAll('li'));
38
- setItems([...items()].reverse());
39
- await tick();
40
- expect(rowTexts(root)).toEqual(['c', 'b', 'a']);
41
- const after = Array.from(root.querySelectorAll('li'));
42
- expect(after).toEqual([lis[2], lis[1], lis[0]]);
43
- expect(root.querySelector('.in-1').value).toBe('typed-a');
44
- expect(document.activeElement).toBe(inputs[2]);
45
- });
46
- it('keeps component state per key and updates item values in place', async () => {
47
- const [items, setItems] = createSignal([
48
- { id: 1, label: 'one' },
49
- { id: 2, label: 'two' },
50
- ]);
51
- function Row(props) {
52
- const [clicks, setClicks] = createSignal(0);
53
- return h('li', null, h('span', { class: 'label' }, () => props.item.label), h('button', { onClick: () => setClicks(clicks() + 1) }, () => clicks()), h('em', null, () => props.index));
54
- }
55
- const root = mountList(krateList(() => items(), (t) => t.id, (t, i) => h(Row, {
56
- get item() {
57
- return t();
58
- },
59
- get index() {
60
- return i();
61
- },
62
- }), 1).mount());
63
- root.querySelectorAll('button')[1].click();
64
- root.querySelectorAll('button')[1].click();
65
- await tick();
66
- expect(root.querySelectorAll('button')[1].textContent).toBe('2');
67
- // New object for key 2 and a move to the front: state stays, label and index update.
68
- setItems([{ id: 2, label: 'TWO' }, { id: 1, label: 'one' }]);
69
- await tick();
70
- expect(rowTexts(root, '.label')).toEqual(['TWO', 'one']);
71
- expect(root.querySelectorAll('button')[0].textContent).toBe('2');
72
- expect(rowTexts(root, 'em')).toEqual(['0', '1']);
73
- });
74
- it('runs cleanups for removed rows before new rows mount', async () => {
75
- const log = [];
76
- const [items, setItems] = createSignal([1, 2, 3]);
77
- function Row(props) {
78
- const id = props.id;
79
- onMount(() => {
80
- log.push('mount ' + id);
81
- onCleanup(() => log.push('unmount ' + id));
82
- });
83
- return h('li', null, String(id));
84
- }
85
- const root = mountList(krateList(() => items(), (n) => n, (n) => h(Row, { get id() { return n(); } }), 1).mount());
86
- await tick();
87
- expect(log).toEqual(['mount 1', 'mount 2', 'mount 3']);
88
- log.length = 0;
89
- setItems([1, 4, 3]);
90
- await tick();
91
- expect(rowTexts(root)).toEqual(['1', '4', '3']);
92
- expect(log).toEqual(['unmount 2', 'mount 4']);
93
- });
94
- it('inserts and removes in the middle with minimal DOM work', async () => {
95
- const [items, setItems] = createSignal([1, 2, 3, 4]);
96
- const root = mountList(krateList(() => items(), (n) => n, (n) => h('li', null, () => String(n())), 1).mount());
97
- const before = Array.from(root.querySelectorAll('li'));
98
- setItems([1, 2, 9, 3, 4]);
99
- await tick();
100
- expect(rowTexts(root)).toEqual(['1', '2', '9', '3', '4']);
101
- const after = Array.from(root.querySelectorAll('li'));
102
- expect(after[0]).toBe(before[0]);
103
- expect(after[3]).toBe(before[2]);
104
- setItems([1, 9, 4]);
105
- await tick();
106
- expect(rowTexts(root)).toEqual(['1', '9', '4']);
107
- expect(root.querySelectorAll('li')[2]).toBe(before[3]);
108
- });
109
- it('moves only rows outside the longest increasing run', async () => {
110
- const [items, setItems] = createSignal([1, 2, 3, 4, 5]);
111
- const root = mountList(krateList(() => items(), (n) => n, (n) => h('li', null, String(n())), 1).mount());
112
- const ul = root;
113
- const spy = vi.spyOn(ul, 'insertBefore');
114
- setItems([5, 1, 2, 3, 4]);
115
- await tick();
116
- expect(rowTexts(root)).toEqual(['5', '1', '2', '3', '4']);
117
- // One row moved: its start marker, element and end marker.
118
- expect(spy.mock.calls.length).toBe(3);
119
- spy.mockRestore();
120
- });
121
- it('warns on duplicate keys and keeps the first occurrence on its row', async () => {
122
- const err = vi.spyOn(console, 'error').mockImplementation(() => { });
123
- const [items, setItems] = createSignal([
124
- { id: 1, label: 'a' },
125
- { id: 1, label: 'b' },
126
- ]);
127
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1).mount());
128
- expect(rowTexts(root)).toEqual(['a', 'b']);
129
- expect(err).toHaveBeenCalled();
130
- expect(String(err.mock.calls[0][0])).toContain('same key');
131
- const first = root.querySelector('li');
132
- setItems([{ id: 1, label: 'a2' }, { id: 1, label: 'b2' }, { id: 2, label: 'c' }]);
133
- await tick();
134
- expect(rowTexts(root)).toEqual(['a2', 'b2', 'c']);
135
- expect(root.querySelector('li')).toBe(first);
136
- err.mockRestore();
137
- });
138
- it('compares keys as strings like React', async () => {
139
- const [items, setItems] = createSignal([{ k: 1 }, { k: 2 }]);
140
- const root = mountList(krateList(() => items(), (t) => t.k, () => h('li', null, h('input', null)), 1).mount());
141
- root.querySelectorAll('input')[0].value = 'kept';
142
- setItems([{ k: '2' }, { k: '1' }]);
143
- await tick();
144
- expect(root.querySelectorAll('input')[1].value).toBe('kept');
145
- });
146
- it('remounts a keyed single element when its key changes', async () => {
147
- const log = [];
148
- const [user, setUser] = createSignal('ada');
149
- function Profile(props) {
150
- const [draft, setDraft] = createSignal('');
151
- const name = props.name;
152
- onMount(() => {
153
- log.push('mount ' + name);
154
- onCleanup(() => log.push('unmount ' + name));
155
- });
156
- return h('input', { value: () => draft(), onInput: (e) => setDraft(e.target.value), 'data-user': name });
157
- }
158
- const root = mountList(krateList(() => [user()], (k) => k, () => h(Profile, { get name() { return user(); } }), 1).mount());
159
- await tick();
160
- const input = root.querySelector('input');
161
- input.value = 'draft';
162
- input.dispatchEvent(new Event('input'));
163
- setUser('ada');
164
- await tick();
165
- expect(root.querySelector('input')).toBe(input);
166
- setUser('grace');
167
- await tick();
168
- const next = root.querySelector('input');
169
- expect(next).not.toBe(input);
170
- expect(next.value).toBe('');
171
- expect(next.getAttribute('data-user')).toBe('grace');
172
- expect(log).toEqual(['mount ada', 'unmount ada', 'mount grace']);
173
- });
174
- it('renders keyed fragments, text rows and nested lists', async () => {
175
- const [groups, setGroups] = createSignal([
176
- { id: 'g1', items: ['x', 'y'] },
177
- { id: 'g2', items: ['z'] },
178
- ]);
179
- const root = mountList(krateList(() => groups(), (g) => g.id, (g) => [
180
- h('dt', null, () => g().id),
181
- h('dd', null, krateList(() => g().items, null, (s) => () => s(), 1)),
182
- ], 1).mount());
183
- expect(rowTexts(root, 'dt')).toEqual(['g1', 'g2']);
184
- expect(rowTexts(root, 'dd')).toEqual(['xy', 'z']);
185
- const dts = Array.from(root.querySelectorAll('dt'));
186
- setGroups([{ id: 'g2', items: ['z', 'w'] }, { id: 'g1', items: ['y'] }]);
187
- await tick();
188
- expect(rowTexts(root, 'dt')).toEqual(['g2', 'g1']);
189
- expect(rowTexts(root, 'dd')).toEqual(['zw', 'y']);
190
- expect(Array.from(root.querySelectorAll('dt'))).toEqual([dts[1], dts[0]]);
191
- });
192
- it('switches between empty and non-empty and works inside tables', async () => {
193
- const [rows, setRows] = createSignal([]);
194
- const table = document.createElement('table');
195
- const tbody = document.createElement('tbody');
196
- table.appendChild(tbody);
197
- document.body.appendChild(table);
198
- tbody.appendChild(krateList(() => rows(), (n) => n, (n) => h('tr', null, h('td', null, () => String(n()))), 1).mount());
199
- expect(tbody.querySelectorAll('tr')).toHaveLength(0);
200
- setRows([1, 2]);
201
- await tick();
202
- expect(rowTexts(tbody, 'td')).toEqual(['1', '2']);
203
- setRows([]);
204
- await tick();
205
- expect(tbody.querySelectorAll('tr')).toHaveLength(0);
206
- expect(Array.from(tbody.childNodes).every((n) => n.nodeType === 8)).toBe(true);
207
- });
208
- it('uses index keys for unkeyed lists (state stays with the position)', async () => {
209
- const [items, setItems] = createSignal(['a', 'b']);
210
- const root = mountList(krateList(() => items(), null, (s) => h('li', null, () => s(), h('input', null)), 1).mount());
211
- root.querySelectorAll('input')[0].value = 'first';
212
- setItems(['b', 'a']);
213
- await tick();
214
- expect(rowTexts(root)).toEqual(['b', 'a']);
215
- expect(root.querySelectorAll('input')[0].value).toBe('first');
216
- });
217
- it('value mode re-renders a row whose item changed', async () => {
218
- const [items, setItems] = createSignal([{ id: 1, label: 'a' }]);
219
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, t.label), 0).mount());
220
- const li = root.querySelector('li');
221
- setItems([{ id: 1, label: 'b' }]);
222
- await tick();
223
- expect(rowTexts(root)).toEqual(['b']);
224
- expect(root.querySelector('li')).not.toBe(li);
225
- });
226
- it('detaches refs of removed rows', async () => {
227
- const refs = [];
228
- const [items, setItems] = createSignal([1]);
229
- mountList(krateList(() => items(), (n) => n, () => h('li', { ref: (el) => refs.push(el) }), 1).mount());
230
- setItems([]);
231
- await tick();
232
- expect(refs).toHaveLength(2);
233
- expect(refs[1]).toBeNull();
234
- });
235
- });
236
- describe('React effects in rows', () => {
237
- it('runs passive effects per key: none on reorder, cleanup on removal, setup for new keys', async () => {
238
- const log = [];
239
- const settle = async () => {
240
- for (let i = 0; i < 3; i++)
241
- await tick();
242
- };
243
- const [items, setItems] = createSignal([1, 2]);
244
- function Row(props) {
245
- const id = props.id;
246
- createEffectDeps(() => {
247
- log.push('effect ' + id);
248
- return () => log.push('cleanup ' + id);
249
- }, () => []);
250
- return h('li', null, String(id));
251
- }
252
- mountList(krateList(() => items(), (n) => n, (n) => h(Row, { get id() { return n(); } }), 1).mount());
253
- await settle();
254
- expect(log).toEqual(['effect 1', 'effect 2']);
255
- log.length = 0;
256
- setItems([2, 1]);
257
- await settle();
258
- expect(log).toEqual([]);
259
- setItems([2, 3]);
260
- await settle();
261
- expect(log).toEqual(['cleanup 1', 'effect 3']);
262
- });
263
- });
264
- describe('hydration adoption', () => {
265
- it('adopts server-rendered rows by key without replacing nodes', async () => {
266
- const root = document.createElement('ul');
267
- root.innerHTML =
268
- '<!--k:L--><!--k:L:$1--><li data-id="1">one<button>0</button></li><!--/k:L:$1-->' +
269
- '<!--k:L:$2--><li data-id="2">two<button>0</button></li><!--/k:L:$2--><!--/k:L-->';
270
- document.body.appendChild(root);
271
- const ssr = Array.from(root.querySelectorAll('li'));
272
- const [items, setItems] = createSignal([
273
- { id: 1, label: 'one' },
274
- { id: 2, label: 'two' },
275
- ]);
276
- function Row(props) {
277
- const [n, setN] = createSignal(0);
278
- return h('li', { 'data-id': () => props.item.id }, () => props.item.label, h('button', { onClick: () => setN(n() + 1) }, () => n()));
279
- }
280
- krateBindList(root.firstChild, () => items(), (t) => t.id, (t) => h(Row, { get item() { return t(); } }), 1);
281
- expect(Array.from(root.querySelectorAll('li'))).toEqual(ssr);
282
- root.querySelectorAll('button')[1].click();
283
- await tick();
284
- expect(root.querySelectorAll('button')[1].textContent).toBe('1');
285
- setItems([{ id: 2, label: 'two' }, { id: 1, label: 'uno' }]);
286
- await tick();
287
- expect(Array.from(root.querySelectorAll('li'))).toEqual([ssr[1], ssr[0]]);
288
- expect(rowTexts(root)).toEqual(['two1', 'uno0']);
289
- });
290
- it('adopts by position when the build could not compute keys and patches mismatches', async () => {
291
- const root = document.createElement('ul');
292
- root.innerHTML = '<!--k:M--><!--k:M:0--><li>stale</li><!--/k:M:0--><!--k:M:1--><li>gone</li><!--/k:M:1--><!--/k:M-->';
293
- document.body.appendChild(root);
294
- const first = root.querySelector('li');
295
- const [items] = createSignal([{ id: 'a', label: 'fresh' }]);
296
- krateBindList(root.firstChild, () => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1);
297
- expect(root.querySelectorAll('li')).toHaveLength(1);
298
- expect(root.querySelector('li')).toBe(first);
299
- expect(rowTexts(root)).toEqual(['fresh']);
300
- });
301
- it('adopts nested server-rendered lists inside rows', () => {
302
- const root = document.createElement('div');
303
- root.innerHTML =
304
- '<!--k:O--><!--k:O:$a--><section><h2>a</h2><ul><!--k:P--><!--k:P:$x--><li>x</li><!--/k:P:$x--><!--/k:P--></ul></section><!--/k:O:$a--><!--/k:O-->';
305
- document.body.appendChild(root);
306
- const li = root.querySelector('li');
307
- const [groups] = createSignal([{ id: 'a', items: ['x'] }]);
308
- krateBindList(root.firstChild, () => groups(), (g) => g.id, (g) => h('section', null, h('h2', null, () => g().id), h('ul', null, krateList(() => g().items, (s) => s, (s) => h('li', null, () => s()), 1))), 1);
309
- expect(root.querySelector('li')).toBe(li);
310
- expect(root.querySelectorAll('li')).toHaveLength(1);
311
- });
312
- });
313
- describe('focus events during list updates', () => {
314
- // Chromium blurs a focused node it moves or removes; emulate that, since
315
- // happy-dom does not.
316
- function blurOnMove() {
317
- const orig = Node.prototype.insertBefore;
318
- Node.prototype.insertBefore = function (node, ref) {
319
- const active = document.activeElement;
320
- const moving = active && active !== document.body && node.nodeType === 1 && node.contains(active) ? active : null;
321
- const out = orig.call(this, node, ref);
322
- if (moving) {
323
- moving.blur();
324
- moving.dispatchEvent(new FocusEvent('focusout', { bubbles: true }));
325
- }
326
- return out;
327
- };
328
- return () => {
329
- Node.prototype.insertBefore = orig;
330
- };
331
- }
332
- it('does not fire onBlur/onFocus when a reorder moves the focused row', async () => {
333
- const events = [];
334
- const [items, setItems] = createSignal([1, 2, 3]);
335
- const root = mountList(krateList(() => items(), (n) => n, (n) => h('div', { class: 'row' }, h('input', { onFocus: () => events.push('focus ' + n()), onBlur: () => events.push('blur ' + n()) })), 1).mount());
336
- const input = root.querySelectorAll('input')[1];
337
- input.focus();
338
- events.length = 0;
339
- const restore = blurOnMove();
340
- try {
341
- setItems([3, 2, 1]);
342
- await tick();
343
- }
344
- finally {
345
- restore();
346
- }
347
- expect(Array.from(root.querySelectorAll('input'))[1]).toBe(input);
348
- expect(document.activeElement).toBe(input);
349
- expect(events).toEqual([]);
350
- // A real blur afterwards still reaches the handler.
351
- input.blur();
352
- await tick();
353
- expect(events).toEqual(['blur 2']);
354
- });
355
- });
356
- describe('nested components in adopted rows', () => {
357
- it('orders effects children-first on mount and parent-first on removal, as for fresh rows', async () => {
358
- const settle = async () => {
359
- for (let i = 0; i < 3; i++)
360
- await tick();
361
- };
362
- const log = [];
363
- function Leaf(props) {
364
- createEffectDeps(() => {
365
- log.push('leaf ' + props.id);
366
- return () => log.push('leaf cleanup ' + props.id);
367
- }, () => []);
368
- return h('i', null, () => props.id);
369
- }
370
- function Row(props) {
371
- createEffectDeps(() => {
372
- log.push('row ' + props.id);
373
- return () => log.push('row cleanup ' + props.id);
374
- }, () => []);
375
- return h('li', null, krateList(() => [1], (n) => props.id + n, () => h(Leaf, { get id() { return props.id; } }), 1));
376
- }
377
- const render = (n) => h(Row, { get id() { return n(); } });
378
- // A fresh list is the reference.
379
- const [fresh, setFresh] = createSignal(['a', 'b']);
380
- mountList(krateList(() => fresh(), (n) => n, render, 1).mount());
381
- await settle();
382
- const freshMount = log.splice(0);
383
- setFresh(['b']);
384
- await settle();
385
- const freshRemove = log.splice(0);
386
- expect(freshMount).toEqual(['leaf a', 'row a', 'leaf b', 'row b']);
387
- expect(freshRemove).toEqual(['row cleanup a', 'leaf cleanup a']);
388
- // The same list adopted from server-rendered HTML must behave alike.
389
- document.body.innerHTML = '';
390
- const root = document.createElement('ul');
391
- root.innerHTML =
392
- '<!--k:Q--><!--k:Q:$a--><li><!--k:R--><!--k:R:$a1--><i>a</i><!--/k:R:$a1--><!--/k:R--></li><!--/k:Q:$a-->' +
393
- '<!--k:Q:$b--><li><!--k:S--><!--k:S:$b1--><i>b</i><!--/k:S:$b1--><!--/k:S--></li><!--/k:Q:$b--><!--/k:Q-->';
394
- document.body.appendChild(root);
395
- const [adopted, setAdopted] = createSignal(['a', 'b']);
396
- krateBindList(root.firstChild, () => adopted(), (n) => n, render, 1);
397
- await settle();
398
- expect(log.splice(0)).toEqual(freshMount);
399
- setAdopted(['b']);
400
- await settle();
401
- expect(log.splice(0)).toEqual(freshRemove);
402
- });
403
- });
404
- describe('list performance', () => {
405
- it('reorders, inserts and removes 1000 rows in roughly linear time', async () => {
406
- const make = (n) => Array.from({ length: n }, (_, i) => ({ id: i, label: 'row ' + i }));
407
- const [items, setItems] = createSignal(make(1000));
408
- const root = mountList(krateList(() => items(), (t) => t.id, (t) => h('li', null, () => t().label), 1).mount());
409
- expect(root.querySelectorAll('li')).toHaveLength(1000);
410
- const time = async (next) => {
411
- const t0 = performance.now();
412
- setItems(next);
413
- await tick();
414
- return performance.now() - t0;
415
- };
416
- const reversed = await time([...items()].reverse());
417
- expect(root.querySelector('li').textContent).toBe('row 999');
418
- const shuffled = [...items()];
419
- for (let i = shuffled.length - 1; i > 0; i--) {
420
- const j = (i * 7919) % (i + 1);
421
- [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
422
- }
423
- const shuffle = await time(shuffled);
424
- const inserted = [...items()];
425
- inserted.splice(500, 0, { id: 5000, label: 'inserted' });
426
- const insert = await time(inserted);
427
- const removed = items().filter((_, i) => i % 2 === 0);
428
- const remove = await time(removed);
429
- expect(root.querySelectorAll('li')).toHaveLength(removed.length);
430
- // Generous ceilings: happy-dom is slow, but a quadratic algorithm over
431
- // 1000 rows takes far longer than this.
432
- for (const ms of [reversed, shuffle, insert, remove])
433
- expect(ms).toBeLessThan(2000);
434
- });
435
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,167 +0,0 @@
1
- // @vitest-environment node
2
- import { describe, expect, it, vi } from 'vitest';
3
- import { metadataHeadTags, renderMetadataTags, resolveMetadata } from './metadata';
4
- import { forbidden, notFound } from './request-context';
5
- const tags = async (segments, props = {}) => metadataHeadTags(segments, props);
6
- describe('resolveMetadata merge order', () => {
7
- it('merges root layout to page: a deeper segment replaces a field wholesale', async () => {
8
- const { metadata } = await resolveMetadata([
9
- { mod: { metadata: { description: 'root', openGraph: { siteName: 'Site', title: 'root og' }, robots: 'noindex' } } },
10
- { mod: { metadata: { description: 'section' } } },
11
- { mod: { metadata: { openGraph: { title: 'page og' } } } },
12
- ], {});
13
- expect(metadata.description).toBe('section');
14
- // openGraph is replaced, not merged: siteName does not leak into the page.
15
- expect(metadata.openGraph).toEqual({ title: 'page og' });
16
- expect(metadata.robots).toBe('noindex');
17
- });
18
- it('applies the nearest title template to a title string, not to absolute or default', async () => {
19
- const r = await resolveMetadata([
20
- { mod: { metadata: { title: { default: 'Site', template: '%s | Site' } } } },
21
- { mod: { metadata: { title: 'About' } } },
22
- ], {});
23
- expect(r.metadata.title).toBe('About | Site');
24
- const abs = await resolveMetadata([{ mod: { metadata: { title: { template: '%s | Site', default: 'Site' } } } }, { mod: { metadata: { title: { absolute: 'Exactly' } } } }], {});
25
- expect(abs.metadata.title).toBe('Exactly');
26
- const own = await resolveMetadata([{ mod: { metadata: { title: { template: '%s | Site', default: 'Site' } } } }], {});
27
- expect(own.metadata.title).toBe('Site');
28
- const nested = await resolveMetadata([
29
- { mod: { metadata: { title: { template: '%s | A', default: 'A' } } } },
30
- { mod: { metadata: { title: { template: '%s | B', default: 'B' } } } },
31
- { mod: { metadata: { title: 'Page' } } },
32
- ], {});
33
- expect(nested.metadata.title).toBe('Page | B');
34
- });
35
- it('calls generateMetadata with awaitable params and searchParams and the parent metadata', async () => {
36
- const seen = [];
37
- const { metadata } = await resolveMetadata([
38
- { mod: { metadata: { description: 'from layout', title: { default: 'D', template: '%s!' } } } },
39
- {
40
- mod: {
41
- async generateMetadata(props, parent) {
42
- const params = await props.params;
43
- const sp = await props.searchParams;
44
- const above = await parent;
45
- seen.push([props.params.slug, params.slug, sp.q, above.description]);
46
- return { title: 'Post ' + params.slug };
47
- },
48
- },
49
- },
50
- ], { params: { slug: 'x' }, searchParams: { q: '1' } });
51
- expect(seen).toEqual([['x', 'x', '1', 'from layout']]);
52
- expect(metadata.title).toBe('Post x!');
53
- });
54
- it('skips a generateMetadata that throws and keeps the rest', async () => {
55
- const r = await resolveMetadata([
56
- { mod: { metadata: { description: 'layout' } }, file: 'layout' },
57
- {
58
- mod: {
59
- generateMetadata() {
60
- throw new Error('lookup failed');
61
- },
62
- metadata: { title: 'ignored when the function exists' },
63
- },
64
- file: 'page',
65
- },
66
- ], {});
67
- expect(r.metadata).toEqual({ description: 'layout' });
68
- expect(r.errors).toHaveLength(1);
69
- expect(r.errors[0].file).toBe('page');
70
- });
71
- it('lets notFound(), forbidden() and redirects through', async () => {
72
- await expect(resolveMetadata([{ mod: { generateMetadata: () => notFound() } }], {})).rejects.toMatchObject({ digest: 'NEXT_NOT_FOUND' });
73
- await expect(resolveMetadata([{ mod: { generateMetadata: () => forbidden() } }], {})).rejects.toMatchObject({ __krateHTTPStatus: 403 });
74
- await expect(resolveMetadata([{ mod: { generateMetadata: () => { throw Object.assign(new Error('r'), { __krateRedirect: '/x' }); } } }], {})).rejects.toMatchObject({ __krateRedirect: '/x' });
75
- });
76
- it('logs a failing generateMetadata and still returns the tags', async () => {
77
- const log = vi.fn();
78
- const out = await metadataHeadTags([{ mod: { metadata: { description: 'kept' } } }, { mod: { generateMetadata: () => { throw new Error('nope'); } }, file: 'page.tsx' }], {}, log);
79
- expect(out).toContain('<meta name="description" content="kept">');
80
- expect(log).toHaveBeenCalledOnce();
81
- expect(log.mock.calls[0][0]).toContain('page.tsx');
82
- });
83
- it('fills the fields a file convention supplies unless the segment sets them', async () => {
84
- const files = { openGraph: { images: ['/og.png'] }, twitter: { images: ['/tw.png'] } };
85
- const a = await resolveMetadata([{ mod: { metadata: { title: 'T' } }, files }], {});
86
- expect(a.metadata.openGraph).toEqual({ images: ['/og.png'] });
87
- expect(a.metadata.twitter).toMatchObject({ card: 'summary_large_image', images: ['/tw.png'] });
88
- const b = await resolveMetadata([{ mod: { metadata: { openGraph: { images: ['/mine.png'] } } }, files }], {});
89
- expect(b.metadata.openGraph).toEqual({ images: ['/mine.png'] });
90
- });
91
- });
92
- describe('renderMetadataTags', () => {
93
- it('renders the common fields', () => {
94
- const html = renderMetadataTags({
95
- title: 'Hello',
96
- description: 'A <b>"post"</b>',
97
- keywords: ['a', 'b'],
98
- authors: [{ name: 'Ada', url: 'https://ada.example' }],
99
- robots: { index: false, follow: true, googleBot: { index: true, maxImagePreview: 'large' } },
100
- alternates: { canonical: '/hello', languages: { 'en-US': '/en', de: '/de' } },
101
- themeColor: [{ media: '(prefers-color-scheme: dark)', color: '#000' }],
102
- metadataBase: 'https://example.com',
103
- icons: { icon: '/favicon.ico', apple: [{ url: '/apple.png', sizes: '180x180' }] },
104
- manifest: '/site.webmanifest',
105
- other: { custom: 'x' },
106
- });
107
- expect(html).toContain('<title>Hello</title>');
108
- expect(html).toContain('<meta name="description" content="A &lt;b&gt;&quot;post&quot;&lt;/b&gt;">');
109
- expect(html).toContain('<meta name="keywords" content="a,b">');
110
- expect(html).toContain('<meta name="author" content="Ada">');
111
- expect(html).toContain('<link rel="author" href="https://ada.example">');
112
- expect(html).toContain('<meta name="robots" content="noindex, follow">');
113
- expect(html).toContain('<meta name="googlebot" content="index, follow, max-image-preview:large">');
114
- expect(html).toContain('<link rel="canonical" href="https://example.com/hello">');
115
- expect(html).toContain('<link rel="alternate" hreflang="en-US" href="https://example.com/en">');
116
- expect(html).toContain('<meta name="theme-color" content="#000" media="(prefers-color-scheme: dark)">');
117
- expect(html).toContain('<link rel="icon" href="https://example.com/favicon.ico">');
118
- expect(html).toContain('<link rel="apple-touch-icon" href="https://example.com/apple.png" sizes="180x180">');
119
- expect(html).toContain('<link rel="manifest" href="https://example.com/site.webmanifest">');
120
- expect(html).toContain('<meta name="custom" content="x">');
121
- });
122
- it('renders Open Graph and Twitter with images resolved against metadataBase', () => {
123
- const html = renderMetadataTags({
124
- metadataBase: 'https://example.com/docs',
125
- openGraph: {
126
- type: 'article',
127
- title: 'T',
128
- url: '/a',
129
- images: ['/i.png', { url: 'https://cdn.example/x.png', width: 800, height: 600, alt: 'x' }],
130
- publishedTime: '2026-01-01',
131
- authors: ['Ada'],
132
- tags: ['a', 'b'],
133
- },
134
- twitter: { title: 'TT', images: ['/t.png'], creator: '@ada' },
135
- });
136
- expect(html).toContain('<meta property="og:url" content="https://example.com/docs/a">');
137
- expect(html).toContain('<meta property="og:image" content="https://example.com/docs/i.png">');
138
- expect(html).toContain('<meta property="og:image" content="https://cdn.example/x.png">');
139
- expect(html).toContain('<meta property="og:image:width" content="800">');
140
- expect(html).toContain('<meta property="og:image:alt" content="x">');
141
- expect(html).toContain('<meta property="article:published_time" content="2026-01-01">');
142
- expect(html).toContain('<meta property="article:tag" content="b">');
143
- expect(html).toContain('<meta name="twitter:card" content="summary_large_image">');
144
- expect(html).toContain('<meta name="twitter:image" content="https://example.com/docs/t.png">');
145
- expect(html).toContain('<meta name="twitter:creator" content="@ada">');
146
- });
147
- it('gives openGraph and twitter titles their own templates', async () => {
148
- const out = await tags([
149
- { mod: { metadata: { title: { default: 'D', template: '%s | Page' }, openGraph: { title: { default: 'OG', template: '%s | OG' } } } } },
150
- { mod: { metadata: { title: 'P', openGraph: { title: 'P' } } } },
151
- ]);
152
- expect(out).toContain('<title>P | Page</title>');
153
- expect(out).toContain('<meta property="og:title" content="P | OG">');
154
- });
155
- it('renders a viewport export with themeColor and colorScheme', async () => {
156
- const out = await tags([
157
- { mod: { viewport: { width: 'device-width', initialScale: 1, maximumScale: 5, userScalable: false, themeColor: '#fff', colorScheme: 'light dark' } } },
158
- ]);
159
- expect(out).toContain('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5, user-scalable=no">');
160
- expect(out).toContain('<meta name="theme-color" content="#fff">');
161
- expect(out).toContain('<meta name="color-scheme" content="light dark">');
162
- });
163
- it('renders nothing for an empty chain', async () => {
164
- expect(await tags([])).toBe('');
165
- expect(await tags([{ mod: {} }])).toBe('');
166
- });
167
- });
@@ -1 +0,0 @@
1
- export {};