@lemonriver/core 0.4.0 → 0.5.0

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 (80) hide show
  1. package/README.md +14 -4
  2. package/dist/{build-Nl7EeJ6x.js → build-CS1goZxt.js} +1 -1
  3. package/dist/cli/bin.js +4 -4
  4. package/dist/{config-CpC0nM3r.js → config-BpbcCbUv.js} +3673 -2065
  5. package/dist/{dev-BB3THd5I.js → dev-BpYnfgLL.js} +2 -2
  6. package/dist/{preview-BGjgTg7H.js → preview-HSzPPkSs.js} +1 -1
  7. package/dist/{update-DsxZBtwv.js → update-CMRxtTc9.js} +18 -8
  8. package/dist/vite/index.js +1 -1
  9. package/package.json +2 -2
  10. package/src/agent/types.ts +14 -2
  11. package/src/app/components/agent/agent-panel.tsx +17 -5
  12. package/src/app/components/agent/connect-view.tsx +1 -1
  13. package/src/app/components/agent/provider-logo.tsx +10 -1
  14. package/src/app/lemonriver/agent/provider-marks.tsx +53 -0
  15. package/src/app/lemonriver/agent-providers/section.tsx +75 -0
  16. package/src/app/lemonriver/agent-providers/use-providers.ts +61 -0
  17. package/src/app/lemonriver/board/blocks/block-panel.tsx +666 -0
  18. package/src/app/lemonriver/board/blocks/cache.ts +28 -0
  19. package/src/app/lemonriver/board/blocks/change-list.tsx +189 -0
  20. package/src/app/lemonriver/board/blocks/classify.test.ts +204 -0
  21. package/src/app/lemonriver/board/blocks/classify.ts +125 -0
  22. package/src/app/lemonriver/board/blocks/snapshot.ts +86 -0
  23. package/src/app/lemonriver/board/board-canvas.tsx +627 -104
  24. package/src/app/lemonriver/board/board-page.tsx +1687 -91
  25. package/src/app/lemonriver/board/board.css +58 -7
  26. package/src/app/lemonriver/board/layout.test.ts +84 -0
  27. package/src/app/lemonriver/board/layout.ts +112 -11
  28. package/src/app/lemonriver/board/model/agent-tasks.test.ts +200 -0
  29. package/src/app/lemonriver/board/model/agent-tasks.ts +191 -0
  30. package/src/app/lemonriver/board/model/apply-prompt.test.ts +121 -0
  31. package/src/app/lemonriver/board/model/apply-prompt.ts +115 -0
  32. package/src/app/lemonriver/board/model/apply.ts +71 -0
  33. package/src/app/lemonriver/board/model/cards.test.ts +95 -2
  34. package/src/app/lemonriver/board/model/cards.ts +88 -20
  35. package/src/app/lemonriver/board/model/changes.test.ts +244 -0
  36. package/src/app/lemonriver/board/model/changes.ts +268 -0
  37. package/src/app/lemonriver/board/model/filter.test.ts +50 -0
  38. package/src/app/lemonriver/board/model/filter.ts +42 -0
  39. package/src/app/lemonriver/board/model/history.test.ts +71 -0
  40. package/src/app/lemonriver/board/model/history.ts +51 -0
  41. package/src/app/lemonriver/board/model/restructure.test.ts +205 -0
  42. package/src/app/lemonriver/board/model/restructure.ts +169 -0
  43. package/src/app/lemonriver/board/model/structure.test.ts +249 -0
  44. package/src/app/lemonriver/board/model/structure.ts +209 -0
  45. package/src/app/lemonriver/board/model/timing.test.ts +113 -0
  46. package/src/app/lemonriver/board/model/timing.ts +117 -0
  47. package/src/app/lemonriver/board/page-node.tsx +236 -55
  48. package/src/app/lemonriver/board/status-style.ts +25 -0
  49. package/src/app/lemonriver/board/use-board-apply.ts +206 -0
  50. package/src/app/lemonriver/board/use-board-cards.ts +108 -5
  51. package/src/app/lemonriver/board/use-board-changes.ts +108 -0
  52. package/src/app/lemonriver/board/use-board-columns.ts +37 -56
  53. package/src/app/lemonriver/board/use-board-comments.ts +36 -0
  54. package/src/app/lemonriver/board/use-board-restructures.ts +107 -0
  55. package/src/app/lemonriver/board/use-history.ts +86 -0
  56. package/src/app/lemonriver/board/use-page-order.ts +65 -38
  57. package/src/app/lemonriver/board/use-target-minutes.ts +47 -0
  58. package/src/app/lemonriver/board/views/block-readings.tsx +46 -0
  59. package/src/app/lemonriver/board/views/list-view.tsx +356 -0
  60. package/src/app/lemonriver/board/views/progress.tsx +32 -0
  61. package/src/app/lemonriver/board/views/timeline-view.tsx +341 -0
  62. package/src/app/lemonriver/outline/model.test.ts +39 -0
  63. package/src/app/lemonriver/outline/model.ts +12 -0
  64. package/src/app/lemonriver/outline/outline-page.tsx +34 -1
  65. package/src/app/lemonriver/settings/settings-page.tsx +3 -0
  66. package/src/app/lemonriver/slide-extensions.tsx +14 -2
  67. package/src/app/lemonriver/text/en.ts +171 -0
  68. package/src/app/lemonriver/text/es.ts +163 -0
  69. package/src/app/lemonriver/text/ja.ts +163 -0
  70. package/src/app/lemonriver/text/types.ts +170 -0
  71. package/src/app/lemonriver/text/zh-cn.ts +161 -0
  72. package/src/app/lemonriver/text/zh-tw.ts +161 -0
  73. package/src/app/lemonriver/theme/lemon-icon.tsx +6 -0
  74. package/src/app/lemonriver/theme/theme-menu.tsx +3 -2
  75. package/src/app/lemonriver/theme/use-theme.tsx +2 -2
  76. package/src/app/lib/agent/use-agent.ts +4 -3
  77. package/src/app/routes/slide.tsx +1 -1
  78. package/src/app/assets/open-slide.svg +0 -39
  79. package/src/app/favicon.ico +0 -0
  80. package/src/app/favicon.svg +0 -39
@@ -4,8 +4,10 @@ export type CardStatus = (typeof CARD_STATUSES)[number];
4
4
  /** What the board knows about a page that the deck never sees. */
5
5
  export type BoardCard = { title?: string; status?: CardStatus; memo?: string };
6
6
  export type BoardCards = Record<string, BoardCard>;
7
- /** No direction, and no say in the deck's order. */
8
- export type BoardLink = { a: string; b: string };
7
+ /** What links two pages, when the board knows; a plain link is just an association. */
8
+ export const LINK_KINDS = ['cause', 'contrast', 'echo'] as const;
9
+ export type LinkKind = (typeof LINK_KINDS)[number];
10
+ export type BoardLink = { a: string; b: string; kind?: LinkKind };
9
11
  export type CardState = { cards: BoardCards; links: BoardLink[] };
10
12
  /** `null` or an empty string clears a field. */
11
13
  export type CardPatch = { title?: string | null; status?: CardStatus | null; memo?: string | null };
@@ -23,6 +25,10 @@ export function isCardStatus(value: unknown): value is CardStatus {
23
25
  return typeof value === 'string' && (CARD_STATUSES as readonly string[]).includes(value);
24
26
  }
25
27
 
28
+ export function isLinkKind(value: unknown): value is LinkKind {
29
+ return typeof value === 'string' && (LINK_KINDS as readonly string[]).includes(value);
30
+ }
31
+
26
32
  function cleanCard(raw: unknown): BoardCard | null {
27
33
  if (!raw || typeof raw !== 'object') return null;
28
34
  const { title, status, memo } = raw as Record<string, unknown>;
@@ -46,8 +52,12 @@ export function sanitizeCards(raw: unknown): BoardCards {
46
52
  const sameLink = (link: BoardLink, a: string, b: string) =>
47
53
  (link.a === a && link.b === b) || (link.a === b && link.b === a);
48
54
 
55
+ export function hasLink(links: BoardLink[], a: string, b: string): boolean {
56
+ return links.some((link) => sameLink(link, a, b));
57
+ }
58
+
49
59
  export function linkCards(links: BoardLink[], a: string, b: string): BoardLink[] {
50
- if (!a || !b || a === b || links.some((link) => sameLink(link, a, b))) return links;
60
+ if (!a || !b || a === b || hasLink(links, a, b)) return links;
51
61
  return [...links, { a, b }];
52
62
  }
53
63
 
@@ -56,12 +66,38 @@ export function unlinkCards(links: BoardLink[], a: string, b: string): BoardLink
56
66
  return kept.length === links.length ? links : kept;
57
67
  }
58
68
 
69
+ /** The same link under another name, keeping what the board said about it. */
70
+ const relink = (link: BoardLink, a: string, b: string): BoardLink =>
71
+ link.kind ? { a, b, kind: link.kind } : { a, b };
72
+
73
+ /** Names what links two pages, or clears it back to a plain association. */
74
+ export function setLinkKind(
75
+ links: BoardLink[],
76
+ a: string,
77
+ b: string,
78
+ kind: LinkKind | null,
79
+ ): BoardLink[] {
80
+ let changed = false;
81
+ const next = links.map((link) => {
82
+ if (!sameLink(link, a, b) || (link.kind ?? null) === kind) return link;
83
+ changed = true;
84
+ return kind ? { a: link.a, b: link.b, kind } : { a: link.a, b: link.b };
85
+ });
86
+ return changed ? next : links;
87
+ }
88
+
59
89
  export function sanitizeLinks(raw: unknown): BoardLink[] {
60
90
  if (!Array.isArray(raw)) return [];
61
- return raw.reduce<BoardLink[]>((links, item) => {
62
- const { a, b } = (item ?? {}) as { a?: unknown; b?: unknown };
63
- return typeof a === 'string' && typeof b === 'string' ? linkCards(links, a, b) : links;
64
- }, []);
91
+ const links: BoardLink[] = [];
92
+ for (const item of raw) {
93
+ const { a, b, kind } = (item ?? {}) as { a?: unknown; b?: unknown; kind?: unknown };
94
+ if (typeof a !== 'string' || typeof b !== 'string') continue;
95
+ if (!a || !b || a === b || hasLink(links, a, b)) continue;
96
+ // An unknown kind loses the label, not the link: the two pages are still
97
+ // linked, which is what the board said for certain.
98
+ links.push(isLinkKind(kind) ? { a, b, kind } : { a, b });
99
+ }
100
+ return links;
65
101
  }
66
102
 
67
103
  export function patchCard(cards: BoardCards, key: string, patch: CardPatch): BoardCards {
@@ -79,6 +115,11 @@ export function patchCard(cards: BoardCards, key: string, patch: CardPatch): Boa
79
115
  return next;
80
116
  }
81
117
 
118
+ /** One change across several cards, so a multi-card edit is a single write. */
119
+ export function patchCards(cards: BoardCards, keys: string[], patch: CardPatch): BoardCards {
120
+ return keys.reduce((next, key) => patchCard(next, key, patch), cards);
121
+ }
122
+
82
123
  /**
83
124
  * Keys a page by its entry in the page array, so its card follows it through a
84
125
  * reorder made anywhere — the thumbnail rail, or an agent editing the file —
@@ -106,20 +147,28 @@ export function pageKeys(entries: string[] | null | undefined, count: number): s
106
147
  }
107
148
 
108
149
  /** Position pairs for the links whose pages are both in the deck. */
109
- export function linkedPairs(keys: string[], links: BoardLink[]): [number, number][] {
150
+ export type LinkedPair = { a: number; b: number; kind?: LinkKind };
151
+
152
+ export function linkedPairs(keys: string[], links: BoardLink[]): LinkedPair[] {
110
153
  const at = new Map(keys.map((key, i) => [key, i]));
111
154
  return links.flatMap((link) => {
112
155
  const a = at.get(link.a);
113
156
  const b = at.get(link.b);
114
- return a === undefined || b === undefined ? [] : [[a, b] as [number, number]];
157
+ if (a === undefined || b === undefined) return [];
158
+ return [link.kind ? { a, b, kind: link.kind } : { a, b }];
115
159
  });
116
160
  }
117
161
 
118
162
  export type PageOp =
119
163
  | { kind: 'move'; from: number; to: number }
164
+ | { kind: 'moveMany'; moving: number[]; to: number }
165
+ | { kind: 'reorder'; order: number[] }
120
166
  | { kind: 'duplicate'; index: number }
121
167
  | { kind: 'insert'; index: number }
122
- | { kind: 'delete'; index: number };
168
+ /** A page put back as the source text it was removed as, with what it carried. */
169
+ | { kind: 'restore'; index: number; entry: string; card?: BoardCard | null }
170
+ | { kind: 'delete'; index: number }
171
+ | { kind: 'deleteMany'; indices: number[] };
123
172
 
124
173
  /** Where each page sat before `op`; -1 marks the page `op` created. */
125
174
  export function originAfter(op: PageOp, total: number): number[] {
@@ -130,15 +179,26 @@ export function originAfter(op: PageOp, total: number): number[] {
130
179
  order.splice(op.to, 0, moved);
131
180
  return order;
132
181
  }
182
+ case 'moveMany': {
183
+ const moving = [...new Set(op.moving)].sort((a, b) => a - b);
184
+ const staying = order.filter((index) => !moving.includes(index));
185
+ const at = Math.max(0, Math.min(staying.length, op.to));
186
+ return [...staying.slice(0, at), ...moving, ...staying.slice(at)];
187
+ }
188
+ case 'reorder':
189
+ return op.order;
133
190
  case 'duplicate':
134
191
  order.splice(op.index + 1, 0, -1);
135
192
  return order;
136
193
  case 'insert':
194
+ case 'restore':
137
195
  order.splice(op.index, 0, -1);
138
196
  return order;
139
197
  case 'delete':
140
198
  order.splice(op.index, 1);
141
199
  return order;
200
+ case 'deleteMany':
201
+ return order.filter((index) => !op.indices.includes(index));
142
202
  }
143
203
  }
144
204
 
@@ -150,7 +210,11 @@ export function entriesAfter<E extends string[] | null | undefined>(
150
210
  if (!entries || entries.length !== count) return entries;
151
211
  return originAfter(op, count).map((from) => {
152
212
  if (from >= 0) return entries[from];
153
- return op.kind === 'duplicate' ? entries[op.index] : BLANK_ENTRY;
213
+ if (op.kind === 'duplicate') return entries[op.index];
214
+ // A restored page already knows the name it was written as, so its card is
215
+ // found again straight away instead of waiting to be adopted under a blank.
216
+ if (op.kind === 'restore') return op.entry;
217
+ return BLANK_ENTRY;
154
218
  }) as E;
155
219
  }
156
220
 
@@ -190,11 +254,13 @@ export function carryCards(
190
254
  if (next !== null) cards[next] = card;
191
255
  }
192
256
  }
193
- const links = state.links.reduce<BoardLink[]>((kept, link) => {
257
+ const links: BoardLink[] = [];
258
+ for (const link of state.links) {
194
259
  const a = resolve(link.a);
195
260
  const b = resolve(link.b);
196
- return a !== null && b !== null ? linkCards(kept, a, b) : kept;
197
- }, []);
261
+ if (a === null || b === null || a === b || hasLink(links, a, b)) continue;
262
+ links.push(relink(link, a, b));
263
+ }
198
264
  return { cards, links };
199
265
  }
200
266
 
@@ -224,11 +290,13 @@ export function adoptEntries(
224
290
  else delete cards[named];
225
291
  delete cards[blank];
226
292
  }
227
- const links = state.links
228
- .filter((link) => !targets.has(link.a) && !targets.has(link.b))
229
- .reduce<BoardLink[]>(
230
- (kept, link) => linkCards(kept, rename.get(link.a) ?? link.a, rename.get(link.b) ?? link.b),
231
- [],
232
- );
293
+ const links: BoardLink[] = [];
294
+ for (const link of state.links) {
295
+ if (targets.has(link.a) || targets.has(link.b)) continue;
296
+ const a = rename.get(link.a) ?? link.a;
297
+ const b = rename.get(link.b) ?? link.b;
298
+ if (hasLink(links, a, b)) continue;
299
+ links.push(relink(link, a, b));
300
+ }
233
301
  return { cards, links };
234
302
  }
@@ -0,0 +1,244 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Block } from '../blocks/classify.ts';
3
+ import {
4
+ type BlockInfo,
5
+ carryChanges,
6
+ changedPages,
7
+ effectiveBlocks,
8
+ parkedBlocks,
9
+ placeBlock,
10
+ removeChange,
11
+ sanitizeChanges,
12
+ staleChanges,
13
+ unparkBlock,
14
+ } from './changes.ts';
15
+
16
+ const block = (id: string, text = id): Block => ({
17
+ id,
18
+ kind: 'text',
19
+ text,
20
+ loc: null,
21
+ box: { x: 0, y: 0, w: 10, h: 10 },
22
+ });
23
+
24
+ const info = (text: string): BlockInfo => ({ kind: 'text', text, loc: null });
25
+ const at = (page: string, before: string | null, id: string) => ({ page, before, block: id });
26
+
27
+ const A = [block('a1', 'Alpha page one'), block('a2', 'Opening content')];
28
+ const B = [block('b1', 'Second page')];
29
+
30
+ describe('placeBlock', () => {
31
+ it('keeps one entry per block', () => {
32
+ let changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), {
33
+ page: 'p2',
34
+ before: null,
35
+ });
36
+ changes = placeBlock(changes, at('p1', 'a2', 'a1'), info('Alpha page one'), {
37
+ page: 'p3',
38
+ before: null,
39
+ });
40
+ expect(changes).toHaveLength(1);
41
+ expect(changes[0].to).toEqual({ page: 'p3', before: null });
42
+ });
43
+
44
+ it('drops the entry when the block goes back where it was', () => {
45
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), {
46
+ page: 'p1',
47
+ before: 'a2',
48
+ });
49
+ expect(changes).toEqual([]);
50
+ });
51
+
52
+ it('keeps the code when parking', () => {
53
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), 'parked', '<h1/>');
54
+ expect(changes[0].to).toBe('parked');
55
+ expect(changes[0].code).toBe('<h1/>');
56
+ });
57
+ });
58
+
59
+ describe('effectiveBlocks', () => {
60
+ it('reorders inside a page', () => {
61
+ const changes = placeBlock([], at('p1', 'a2', 'a2'), info('Opening content'), {
62
+ page: 'p1',
63
+ before: 'a1',
64
+ });
65
+ expect(effectiveBlocks('p1', A, changes).map((b) => b.id)).toEqual(['a2', 'a1']);
66
+ });
67
+
68
+ it('moves a block to the end of another page', () => {
69
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), {
70
+ page: 'p2',
71
+ before: null,
72
+ });
73
+ const one = effectiveBlocks('p1', A, changes);
74
+ const two = effectiveBlocks('p2', B, changes);
75
+ expect(one.map((b) => b.id)).toEqual(['a2']);
76
+ expect(two.map((b) => b.id)).toEqual(['b1', 'a1']);
77
+ expect(two[1]).toMatchObject({ pending: true, from: 'p1' });
78
+ });
79
+
80
+ it('puts the block last when its anchor is gone', () => {
81
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), {
82
+ page: 'p2',
83
+ before: 'gone',
84
+ });
85
+ expect(effectiveBlocks('p2', B, changes).map((b) => b.id)).toEqual(['b1', 'a1']);
86
+ });
87
+
88
+ it('inserts two blocks moved to the same page in order', () => {
89
+ let changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), { page: 'p2', before: 'b1' });
90
+ changes = placeBlock(changes, at('p3', null, 'c1'), info('c'), { page: 'p2', before: 'b1' });
91
+ expect(effectiveBlocks('p2', B, changes).map((b) => b.id)).toEqual(['a1', 'c1', 'b1']);
92
+ });
93
+
94
+ it('takes a parked block off its page', () => {
95
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), 'parked');
96
+ expect(effectiveBlocks('p1', A, changes).map((b) => b.id)).toEqual(['a2']);
97
+ });
98
+ });
99
+
100
+ describe('parkedBlocks and removeChange', () => {
101
+ it('lists parked blocks in change order', () => {
102
+ let changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), 'parked', 'x');
103
+ changes = placeBlock(changes, at('p2', null, 'b1'), info('b'), 'parked', 'y');
104
+ expect(parkedBlocks(changes).map((p) => p.origin.block)).toEqual(['a1', 'b1']);
105
+ });
106
+
107
+ it('removes one entry by its origin', () => {
108
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), 'parked');
109
+ expect(removeChange(changes, at('p1', 'a2', 'a1'))).toEqual([]);
110
+ });
111
+ });
112
+
113
+ describe('carryChanges', () => {
114
+ it('leaves a reorder alone, because a page key travels with its page', () => {
115
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), { page: 'p2', before: null });
116
+ const carried = carryChanges(changes, ['p1', 'p2'], ['p2', 'p1'], [1, 0]);
117
+ expect(carried[0].origin.page).toBe('p1');
118
+ expect(carried[0].to).toEqual({ page: 'p2', before: null });
119
+ });
120
+
121
+ it('follows a duplicated page, whose copy gets a key of its own', () => {
122
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), 'parked', 'x');
123
+ const carried = carryChanges(changes, ['p1'], ['p1', 'p1#2'], [0, -1]);
124
+ expect(carried).toHaveLength(1);
125
+ expect(carried[0].origin.page).toBe('p1');
126
+ });
127
+
128
+ it('drops a change whose source page was deleted', () => {
129
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), { page: 'p2', before: null });
130
+ expect(carryChanges(changes, ['p1', 'p2'], ['p2'], [1])).toEqual([]);
131
+ });
132
+
133
+ it('drops a change whose target page was deleted', () => {
134
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), { page: 'p2', before: null });
135
+ expect(carryChanges(changes, ['p1', 'p2'], ['p1'], [0])).toEqual([]);
136
+ });
137
+
138
+ it('keeps a parked change when its page survives elsewhere', () => {
139
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), 'parked', 'x');
140
+ const carried = carryChanges(changes, ['p0', 'p1'], ['p1', 'p9'], [1, 0]);
141
+ expect(carried).toHaveLength(1);
142
+ expect(carried[0].origin.page).toBe('p1');
143
+ });
144
+ });
145
+
146
+ describe('staleChanges', () => {
147
+ it('flags a block whose text changed on the page', () => {
148
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), {
149
+ page: 'p2',
150
+ before: null,
151
+ });
152
+ const readOf = (page: string) =>
153
+ page === 'p1' ? [block('a1', 'Edited heading'), block('a2')] : null;
154
+ expect([...staleChanges(changes, readOf)]).toEqual([0]);
155
+ });
156
+
157
+ it('flags a block that is no longer on the page', () => {
158
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), 'parked', 'x');
159
+ expect([...staleChanges(changes, () => [block('a2')])]).toEqual([0]);
160
+ });
161
+
162
+ it('leaves a change alone when the page cannot be read', () => {
163
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), 'parked', 'x');
164
+ expect([...staleChanges(changes, () => null)]).toEqual([]);
165
+ });
166
+ });
167
+
168
+ describe('changedPages', () => {
169
+ it('counts both ends of a move', () => {
170
+ const changes = placeBlock([], at('p1', 'a2', 'a1'), info('a'), { page: 'p2', before: null });
171
+ expect([...changedPages(changes)].sort()).toEqual(['p1', 'p2']);
172
+ });
173
+ });
174
+
175
+ describe('sanitizeChanges', () => {
176
+ it('drops malformed entries and keeps good ones', () => {
177
+ const raw = [
178
+ {
179
+ origin: { page: 'p1', before: null, block: 'a1' },
180
+ block: { kind: 'text', text: 'A', loc: null },
181
+ to: 'parked',
182
+ },
183
+ { origin: { page: 'p1' }, block: { kind: 'nope', text: 'x' }, to: 'parked' },
184
+ 'nonsense',
185
+ null,
186
+ ];
187
+ const clean = sanitizeChanges(raw);
188
+ expect(clean).toHaveLength(1);
189
+ expect(clean[0].origin.block).toBe('a1');
190
+ });
191
+
192
+ it('clips long text and long code', () => {
193
+ const clean = sanitizeChanges([
194
+ {
195
+ origin: { page: 'p1', before: null, block: 'a1' },
196
+ block: { kind: 'text', text: 'x'.repeat(500), loc: null },
197
+ to: 'parked',
198
+ code: 'y'.repeat(9000),
199
+ },
200
+ ]);
201
+ expect(clean[0].block.text).toHaveLength(80);
202
+ expect(clean[0].code).toHaveLength(4000);
203
+ });
204
+
205
+ it('keeps at most 200 entries', () => {
206
+ const raw = Array.from({ length: 250 }, (_, i) => ({
207
+ origin: { page: 'p1', before: null, block: `b${i}` },
208
+ block: { kind: 'text', text: `t${i}`, loc: null },
209
+ to: 'parked',
210
+ }));
211
+ expect(sanitizeChanges(raw)).toHaveLength(200);
212
+ });
213
+ });
214
+
215
+ describe('unparkBlock', () => {
216
+ it('puts a parked block on a page, keeping its code and saying where it came from', () => {
217
+ const parked = placeBlock(
218
+ [],
219
+ at('p1', 'a2', 'a1'),
220
+ info('Alpha page one'),
221
+ 'parked',
222
+ '<h1>x</h1>',
223
+ );
224
+ expect(parked).toHaveLength(1);
225
+
226
+ const moved = unparkBlock(parked, parked[0], { page: 'p2', before: null });
227
+ expect(moved).toHaveLength(1);
228
+ expect(moved[0].to).toEqual({ page: 'p2', before: null });
229
+ expect(moved[0].fromParking).toBe(true);
230
+ expect(moved[0].code).toBe('<h1>x</h1>');
231
+ expect(parkedBlocks(moved)).toHaveLength(0);
232
+ });
233
+
234
+ it('drops the entry when the block goes back where it came from', () => {
235
+ const parked = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), 'parked', 'x');
236
+ expect(unparkBlock(parked, parked[0], { page: 'p1', before: 'a2' })).toEqual([]);
237
+ });
238
+
239
+ it('survives a round trip through the stored form', () => {
240
+ const parked = placeBlock([], at('p1', 'a2', 'a1'), info('Alpha page one'), 'parked', 'x');
241
+ const moved = unparkBlock(parked, parked[0], { page: 'p2', before: null });
242
+ expect(sanitizeChanges(JSON.parse(JSON.stringify(moved)))).toEqual(moved);
243
+ });
244
+ });