@lemonriver/core 1.1.0 → 1.3.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 (98) hide show
  1. package/dist/{build-Dm7gVRhb.js → build-CSkdswfd.js} +1 -1
  2. package/dist/cli/bin.js +5 -5
  3. package/dist/{config-CubOIzrs.js → config-C5-myPY6.js} +129 -2
  4. package/dist/{dev-DgLeXPqp.js → dev-DHtYH8sw.js} +2 -2
  5. package/dist/{mcp-CjYDzcJa.js → mcp-B2UeO1un.js} +1 -1
  6. package/dist/{preview-CxXQpPk5.js → preview-C1B6-umF.js} +1 -1
  7. package/dist/{update-b1dSg_FW.js → update-DzL8fpcI.js} +1 -1
  8. package/dist/vite/index.js +1 -1
  9. package/package.json +5 -2
  10. package/release-notes.json +33 -0
  11. package/src/app/components/player.tsx +6 -1
  12. package/src/app/lemonriver/chrome/app-chrome.tsx +52 -0
  13. package/src/app/lemonriver/chrome/sidebar-extras.tsx +8 -1
  14. package/src/app/lemonriver/feedback/dialog.tsx +222 -0
  15. package/src/app/lemonriver/feedback/export.ts +21 -2
  16. package/src/app/lemonriver/feedback/first-success.ts +30 -0
  17. package/src/app/lemonriver/feedback/install.ts +18 -0
  18. package/src/app/lemonriver/feedback/model.ts +88 -0
  19. package/src/app/lemonriver/help/help-page.tsx +16 -1
  20. package/src/app/lemonriver/links.ts +16 -0
  21. package/src/app/lemonriver/present/dual-screen.ts +154 -0
  22. package/src/app/lemonriver/present/presenter-blocked-hint.tsx +28 -0
  23. package/src/app/lemonriver/present/wake-lock.ts +74 -0
  24. package/src/app/lemonriver/settings/backup-section.tsx +13 -0
  25. package/src/app/lemonriver/settings/settings-page.tsx +20 -0
  26. package/src/app/lemonriver/share/editor-share.tsx +6 -3
  27. package/src/app/lemonriver/share/share-dialog.tsx +2 -0
  28. package/src/app/lemonriver/text/en.ts +48 -0
  29. package/src/app/lemonriver/text/es.ts +45 -0
  30. package/src/app/lemonriver/text/ja.ts +44 -0
  31. package/src/app/lemonriver/text/types.ts +48 -0
  32. package/src/app/lemonriver/text/zh-cn.ts +48 -0
  33. package/src/app/lemonriver/text/zh-tw.ts +48 -0
  34. package/src/app/lemonriver/theme/pages.ts +4 -0
  35. package/src/app/lemonriver/update/release-notes.ts +22 -0
  36. package/src/app/lemonriver/update/update-prompt.tsx +28 -4
  37. package/src/app/routes/presenter.tsx +2 -0
  38. package/src/app/routes/slide.tsx +14 -1
  39. package/src/app/components/note-markdown.test.tsx +0 -42
  40. package/src/app/components/thumbnail-rail-scroll.test.ts +0 -60
  41. package/src/app/lemonriver/agent-prefs/prefs.test.ts +0 -173
  42. package/src/app/lemonriver/backup/guide.test.ts +0 -45
  43. package/src/app/lemonriver/backup/model.test.ts +0 -108
  44. package/src/app/lemonriver/backup/places.test.ts +0 -154
  45. package/src/app/lemonriver/board/blocks/classify.test.ts +0 -209
  46. package/src/app/lemonriver/board/layout.test.ts +0 -245
  47. package/src/app/lemonriver/board/model/agent-tasks.test.ts +0 -200
  48. package/src/app/lemonriver/board/model/apply-prompt.test.ts +0 -121
  49. package/src/app/lemonriver/board/model/cards.test.ts +0 -332
  50. package/src/app/lemonriver/board/model/changes.test.ts +0 -244
  51. package/src/app/lemonriver/board/model/filter.test.ts +0 -50
  52. package/src/app/lemonriver/board/model/history.test.ts +0 -71
  53. package/src/app/lemonriver/board/model/restructure.test.ts +0 -205
  54. package/src/app/lemonriver/board/model/self-check.test.ts +0 -329
  55. package/src/app/lemonriver/board/model/structure.test.ts +0 -350
  56. package/src/app/lemonriver/board/model/timing.test.ts +0 -113
  57. package/src/app/lemonriver/features.test.ts +0 -49
  58. package/src/app/lemonriver/feedback/pending-toast.test.ts +0 -44
  59. package/src/app/lemonriver/feedback/upload.test.ts +0 -26
  60. package/src/app/lemonriver/help/model.test.ts +0 -179
  61. package/src/app/lemonriver/home/brief.test.ts +0 -38
  62. package/src/app/lemonriver/home/drafts.test.ts +0 -23
  63. package/src/app/lemonriver/home/sorting.test.ts +0 -24
  64. package/src/app/lemonriver/home/time.test.ts +0 -33
  65. package/src/app/lemonriver/outline/files.test.ts +0 -90
  66. package/src/app/lemonriver/outline/model.test.ts +0 -751
  67. package/src/app/lemonriver/public-links.test.ts +0 -32
  68. package/src/app/lemonriver/routes.test.tsx +0 -54
  69. package/src/app/lemonriver/share/codes.test.ts +0 -49
  70. package/src/app/lemonriver/share/compare.test.ts +0 -199
  71. package/src/app/lemonriver/share/folder.test.ts +0 -42
  72. package/src/app/lemonriver/share/guide.test.ts +0 -78
  73. package/src/app/lemonriver/share/naming.test.ts +0 -70
  74. package/src/app/lemonriver/share/paths.test.ts +0 -116
  75. package/src/app/lemonriver/share/pdf.test.ts +0 -79
  76. package/src/app/lemonriver/templates/knobs.test.ts +0 -62
  77. package/src/app/lemonriver/templates/model.test.ts +0 -175
  78. package/src/app/lemonriver/templates/preview-content.test.ts +0 -117
  79. package/src/app/lemonriver/templates/recommend.test.ts +0 -71
  80. package/src/app/lemonriver/text/text.test.ts +0 -90
  81. package/src/app/lemonriver/theme/pages.test.ts +0 -74
  82. package/src/app/lemonriver/theme/themes.test.ts +0 -18
  83. package/src/app/lemonriver/trash/api.test.ts +0 -19
  84. package/src/app/lemonriver/update/prompt.test.ts +0 -84
  85. package/src/app/lemonriver/update/version.test.ts +0 -14
  86. package/src/app/lib/asset-filter.test.ts +0 -176
  87. package/src/app/lib/inspector/fiber.test.ts +0 -154
  88. package/src/app/lib/inspector/geometry.test.ts +0 -324
  89. package/src/app/lib/inspector/text-edit-timeline.test.ts +0 -121
  90. package/src/app/lib/note-markdown.test.ts +0 -147
  91. package/src/app/lib/pptx/css.test.ts +0 -205
  92. package/src/app/lib/pptx/ooxml.test.ts +0 -199
  93. package/src/app/lib/pptx/text-lines.test.ts +0 -82
  94. package/src/app/lib/print-ready.test.ts +0 -32
  95. package/src/app/lib/sdk.test.ts +0 -13
  96. package/src/app/lib/transition.test.ts +0 -31
  97. package/src/app/lib/use-document-title.test.ts +0 -14
  98. package/src/app/lib/utils.test.ts +0 -49
@@ -1,751 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import type { AgentItem } from '../../../agent/types';
3
- import { DEFAULT_PREFS } from '../agent-prefs/prefs';
4
- import {
5
- afterTurn,
6
- collectDecisions,
7
- driverAction,
8
- insertPage,
9
- lastErrorText,
10
- MAX_BRIEF,
11
- MAX_DECISIONS,
12
- MAX_ITEMS,
13
- MAX_TITLE,
14
- movePage,
15
- nextStep,
16
- type Outline,
17
- type OutlinePage,
18
- outlinePrompt,
19
- pageAwaitingCheck,
20
- pagePrompt,
21
- parseOutlineBlock,
22
- removePage,
23
- renamePage,
24
- reviewPrompt,
25
- sanitizeDecisions,
26
- sanitizeItems,
27
- sanitizeOutline,
28
- setKind,
29
- stripOutlineBlock,
30
- teamState,
31
- } from './model';
32
-
33
- const items: OutlinePage[] = [
34
- { id: 'a', title: 'Cover', kind: 'cover' },
35
- { id: 'b', title: 'Why now', kind: 'list' },
36
- { id: 'c', title: 'Numbers', kind: 'kpi' },
37
- { id: 'd', title: 'End', kind: 'end' },
38
- ];
39
-
40
- const outline: Outline = {
41
- state: 'ready',
42
- brief: 'Explain the expense process',
43
- pages: 8,
44
- files: ['costs.pdf'],
45
- items,
46
- updatedAt: 1,
47
- built: 0,
48
- paused: false,
49
- stage: 'pages',
50
- dispatched: null,
51
- lastTurn: null,
52
- prefs: DEFAULT_PREFS,
53
- decisions: [],
54
- occasion: 'report',
55
- audience: '董事会成员',
56
- templatePrompt: '',
57
- templateSkeletons: [],
58
- templateId: null,
59
- };
60
-
61
- describe('sanitizeOutline', () => {
62
- it('keeps a well-formed outline', () => {
63
- expect(sanitizeOutline(outline)).toEqual(outline);
64
- });
65
-
66
- it('keeps the occasion and audience, and drops ones it cannot use', () => {
67
- expect(sanitizeOutline(outline)).toMatchObject({ occasion: 'report', audience: '董事会成员' });
68
- expect(sanitizeOutline({ ...outline, occasion: 'nope', audience: 7 })).toMatchObject({
69
- occasion: null,
70
- audience: '',
71
- });
72
- expect(sanitizeOutline({ ...outline, audience: ` ${'字'.repeat(80)} ` })?.audience).toBe(
73
- '字'.repeat(40),
74
- );
75
- });
76
-
77
- it('reads a record written before the occasion fields existed', () => {
78
- expect(sanitizeOutline({ ...outline, occasion: undefined, audience: undefined })).toMatchObject(
79
- { occasion: null, audience: '' },
80
- );
81
- });
82
-
83
- it('fills the build fields on a record written before they existed', () => {
84
- const old = {
85
- state: 'building',
86
- brief: 'b',
87
- pages: 8,
88
- files: [],
89
- items,
90
- updatedAt: 3,
91
- };
92
- const cleaned = sanitizeOutline(old);
93
- expect(cleaned).toMatchObject({
94
- built: 0,
95
- paused: false,
96
- stage: 'pages',
97
- dispatched: null,
98
- lastTurn: null,
99
- prefs: DEFAULT_PREFS,
100
- decisions: [],
101
- });
102
- });
103
-
104
- it('keeps the preferences a record carries and repairs the ones it cannot trust', () => {
105
- expect(
106
- sanitizeOutline({ ...outline, prefs: { ...DEFAULT_PREFS, design: 'dense' } })?.prefs.design,
107
- ).toBe('dense');
108
- expect(sanitizeOutline({ ...outline, prefs: { design: 'sparse' } })?.prefs).toEqual(
109
- DEFAULT_PREFS,
110
- );
111
- expect(sanitizeOutline({ ...outline, decisions: 'none' })?.decisions).toEqual([]);
112
- });
113
-
114
- it('clamps the build fields it cannot trust', () => {
115
- const cleaned = sanitizeOutline({
116
- ...outline,
117
- state: 'building',
118
- built: 99,
119
- paused: 'yes',
120
- stage: 'later',
121
- dispatched: { kind: 'page', n: 99, at: 5 },
122
- lastTurn: { kind: 'nope', n: 1, ok: true, at: 5 },
123
- });
124
- expect(cleaned?.built).toBe(items.length);
125
- expect(cleaned?.paused).toBe(false);
126
- expect(cleaned?.stage).toBe('pages');
127
- expect(cleaned?.dispatched).toEqual({ kind: 'page', n: items.length, at: 5 });
128
- expect(cleaned?.lastTurn).toBeNull();
129
- expect(
130
- sanitizeOutline({ ...outline, dispatched: { kind: 'page', n: 1, at: 'soon' } })?.dispatched,
131
- ).toBeNull();
132
- expect(
133
- sanitizeOutline({ ...outline, lastTurn: { kind: 'review', n: 0, ok: true, at: 9 } })
134
- ?.lastTurn,
135
- ).toEqual({ kind: 'review', n: 0, ok: true, at: 9 });
136
- });
137
-
138
- it('drops what it cannot trust', () => {
139
- expect(sanitizeOutline(null)).toBeNull();
140
- expect(sanitizeOutline('ready')).toBeNull();
141
- expect(sanitizeOutline({ ...outline, state: 'unknown' })).toBeNull();
142
- });
143
-
144
- it('repairs the parts it can', () => {
145
- const cleaned = sanitizeOutline({
146
- state: 'ready',
147
- brief: 'x'.repeat(MAX_BRIEF + 50),
148
- pages: 7,
149
- files: ['a.pdf', 3, ''],
150
- updatedAt: 'soon',
151
- items: [
152
- { id: 'Bad Id!', title: ' Spaced ', kind: 'mystery' },
153
- { id: 'ok', title: 'y'.repeat(MAX_TITLE + 30), kind: 'chart' },
154
- { title: 'no id' },
155
- 'nope',
156
- ],
157
- });
158
- expect(cleaned).not.toBeNull();
159
- if (!cleaned) return;
160
- expect(cleaned.brief.length).toBe(MAX_BRIEF);
161
- expect(cleaned.pages).toBe(8);
162
- expect(cleaned.files).toEqual(['a.pdf']);
163
- expect(cleaned.updatedAt).toBe(0);
164
- expect(cleaned.items).toHaveLength(3);
165
- expect(cleaned.items[0].title).toBe('Spaced');
166
- expect(cleaned.items[0].kind).toBe('page');
167
- expect(cleaned.items[0].id).toMatch(/^[a-z0-9-]{1,40}$/);
168
- expect(cleaned.items[0].id).not.toBe('Bad Id!');
169
- expect(cleaned.items[1].title.length).toBe(MAX_TITLE);
170
- expect(cleaned.items[2].title).toBe('no id');
171
- });
172
-
173
- it('never keeps more than thirty pages', () => {
174
- const many = Array.from({ length: MAX_ITEMS + 5 }, (_, i) => ({
175
- id: `p-${i}`,
176
- title: `Page ${i}`,
177
- kind: 'page',
178
- }));
179
- expect(sanitizeOutline({ ...outline, items: many })?.items).toHaveLength(MAX_ITEMS);
180
- expect(sanitizeItems(many)).toHaveLength(MAX_ITEMS);
181
- });
182
- });
183
-
184
- describe('parseOutlineBlock', () => {
185
- const block = (list: unknown) =>
186
- `Here is the plan.\n<lemonriver-outline>${JSON.stringify(list)}</lemonriver-outline>`;
187
-
188
- it('reads the array', () => {
189
- const parsed = parseOutlineBlock(
190
- block([
191
- { title: ' Opening ', kind: 'cover' },
192
- { title: 'Close', kind: 'end' },
193
- ]),
194
- );
195
- expect(parsed?.map((page) => [page.title, page.kind])).toEqual([
196
- ['Opening', 'cover'],
197
- ['Close', 'end'],
198
- ]);
199
- });
200
-
201
- it('takes the last block when a reply carries more than one', () => {
202
- const text = `${block([
203
- { title: 'Old one', kind: 'cover' },
204
- { title: 'Old two', kind: 'end' },
205
- ])}\n${block([
206
- { title: 'New one', kind: 'cover' },
207
- { title: 'New two', kind: 'end' },
208
- ])}`;
209
- expect(parseOutlineBlock(text)?.map((page) => page.title)).toEqual(['New one', 'New two']);
210
- });
211
-
212
- it('needs at least two pages', () => {
213
- expect(parseOutlineBlock(block([{ title: 'Only', kind: 'cover' }]))).toBeNull();
214
- });
215
-
216
- it('returns nothing for missing or broken blocks', () => {
217
- expect(parseOutlineBlock('no block here')).toBeNull();
218
- expect(parseOutlineBlock('<lemonriver-outline>{oops</lemonriver-outline>')).toBeNull();
219
- expect(parseOutlineBlock('<lemonriver-outline>{"a":1}</lemonriver-outline>')).toBeNull();
220
- });
221
- });
222
-
223
- describe('stripOutlineBlock', () => {
224
- it('removes the block and keeps the prose', () => {
225
- const text =
226
- 'Plan ready.\n<lemonriver-outline>[{"title":"a"},{"title":"b"}]</lemonriver-outline>';
227
- expect(stripOutlineBlock(text)).toBe('Plan ready.');
228
- });
229
-
230
- it('also removes a block still streaming in', () => {
231
- expect(stripOutlineBlock('Plan ready.\n<lemonriver-outline>[{"title"')).toBe('Plan ready.');
232
- });
233
- });
234
-
235
- describe('page edits', () => {
236
- it('moves a page and leaves the original alone', () => {
237
- const moved = movePage(items, 0, 2);
238
- expect(moved.map((p) => p.id)).toEqual(['b', 'c', 'a', 'd']);
239
- expect(items.map((p) => p.id)).toEqual(['a', 'b', 'c', 'd']);
240
- expect(movePage(items, -1, 2)).toBe(items);
241
- expect(movePage(items, 0, 9)).toBe(items);
242
- });
243
-
244
- it('inserts a blank page at the index', () => {
245
- const next = insertPage(items, 1);
246
- expect(next).toHaveLength(5);
247
- expect(next[1]).toMatchObject({ title: '', kind: 'page' });
248
- expect(next[1].id).not.toBe(items[0].id);
249
- expect(insertPage(items, 99)[4].title).toBe('');
250
- });
251
-
252
- it('refuses to delete below two pages', () => {
253
- const two = items.slice(0, 2);
254
- expect(removePage(two, 0)).toBe(two);
255
- expect(removePage(items, 1).map((p) => p.id)).toEqual(['a', 'c', 'd']);
256
- });
257
-
258
- it('renames and retypes one page', () => {
259
- expect(renamePage(items, 1, 'New title')[1].title).toBe('New title');
260
- expect(renamePage(items, 1, 'typing ')[1].title).toBe('typing ');
261
- expect(renamePage(items, 1, 'x'.repeat(MAX_TITLE + 1))[1].title.length).toBe(MAX_TITLE);
262
- expect(setKind(items, 2, 'chart')[2].kind).toBe('chart');
263
- expect(setKind(items, 9, 'chart')).toBe(items);
264
- });
265
- });
266
-
267
- describe('prompts', () => {
268
- it('asks for the outline only, with the files and the language', () => {
269
- const text = outlinePrompt(outline, 'deck-1', 'zh-CN');
270
- expect(text).toContain('Explain the expense process');
271
- expect(text).toContain('slides/deck-1/assets/');
272
- expect(text).toContain('costs.pdf');
273
- expect(text).toContain('"zh-CN"');
274
- expect(text).toContain('<lemonriver-outline>');
275
- expect(text).toContain('do not write');
276
- });
277
-
278
- it('says so when there are no files', () => {
279
- expect(outlinePrompt({ ...outline, files: [] }, 'deck-1', 'en')).toContain(
280
- 'no reference files',
281
- );
282
- });
283
-
284
- it('opens the build with page one, the whole list and the kinds', () => {
285
- const text = pagePrompt(outline, 'deck-1', 1);
286
- expect(text).toContain('slides/deck-1/index.tsx');
287
- expect(text).toContain('write page 1 only');
288
- expect(text).toContain('meta.title');
289
- expect(text).toContain('1. [cover] Cover');
290
- expect(text).toContain('4. [end] End');
291
- expect(text).toContain('key figure — one large number');
292
- expect(text).toContain('Numbers from the reference files stay exactly as written there.');
293
- });
294
-
295
- it('keeps later pages to one line, then the preferences', () => {
296
- const text = pagePrompt(outline, 'deck-1', 3);
297
- expect(text.split('\n')[0]).toBe(
298
- 'Page 3 of 4: [kpi] Numbers. Write only this page; leave the others as they are.',
299
- );
300
- expect(text).not.toContain('1. [cover]');
301
- expect(text).toContain('Balance text and visuals.');
302
- });
303
-
304
- it('asks for a review pass without changing the pages', () => {
305
- const text = reviewPrompt(outline, 'deck-1');
306
- expect(text).toContain('All 4 pages are written.');
307
- expect(text).toContain('slides/deck-1/index.tsx');
308
- expect(text).toContain('slides/deck-1/assets/');
309
- expect(text).toContain('ask the user which to use');
310
- expect(text).toContain('Do not add or remove pages.');
311
- });
312
-
313
- it('ends every step with the preferences', () => {
314
- const prefs = { ...DEFAULT_PREFS, tone: 'friendly' as const, research: 'web' as const };
315
- const outlineText = outlinePrompt({ ...outline, prefs }, 'deck-1', 'en');
316
- expect(outlineText).toContain('Write in a light, friendly tone.');
317
- expect(
318
- outlineText
319
- .trim()
320
- .endsWith('You may look up facts the files do not cover; say where each one came from.'),
321
- ).toBe(true);
322
- expect(pagePrompt({ ...outline, prefs }, 'deck-1', 2)).toContain(
323
- 'Write in a light, friendly tone.',
324
- );
325
- expect(reviewPrompt({ ...outline, prefs }, 'deck-1')).toContain(
326
- 'Write in a light, friendly tone.',
327
- );
328
- });
329
-
330
- it('stops asking about a mismatch when the user said to use the spreadsheet', () => {
331
- const lenient = { ...outline, prefs: { ...DEFAULT_PREFS, askOnMismatch: false } };
332
- const text = reviewPrompt(lenient, 'deck-1');
333
- expect(text).not.toContain('ask the user which to use');
334
- expect(text).toContain('use the one from the spreadsheet');
335
- });
336
- });
337
-
338
- describe('template prompts', () => {
339
- const applied: Outline = {
340
- ...outline,
341
- templatePrompt: 'This deck uses the "Board" template.',
342
- templateSkeletons: ['cover', 'overview', 'figure', 'cards', 'compare', 'ask', 'end'],
343
- };
344
-
345
- it('puts the template instructions first on page one', () => {
346
- const text = pagePrompt(applied, 'deck-1', 1);
347
- expect(text.startsWith('This deck uses the "Board" template.')).toBe(true);
348
- expect(text).toContain('Build slides/deck-1/index.tsx');
349
- });
350
-
351
- it('suggests a skeleton per page from the ones the template has', () => {
352
- const text = pagePrompt(applied, 'deck-1', 1);
353
- expect(text).toContain('1. [cover] Cover → cover');
354
- expect(text).toContain('2. [list] Why now → cards');
355
- expect(text).toContain('3. [kpi] Numbers → overview');
356
- expect(text).toContain('4. [end] End → end');
357
- });
358
-
359
- it('leaves a page kind alone when the template has no skeleton for it', () => {
360
- const partial: Outline = { ...outline, templateSkeletons: ['cover', 'end'] };
361
- const text = pagePrompt(partial, 'deck-1', 1);
362
- expect(text).toContain('2. [list] Why now\n');
363
- expect(text).toContain('3. [kpi] Numbers\n');
364
- });
365
-
366
- it('tells later pages to keep the template and its guide', () => {
367
- const text = pagePrompt(applied, 'deck-1', 3);
368
- expect(text.split('\n')[0]).toBe(
369
- 'Page 3 of 4: [kpi] Numbers → overview. Write only this page; leave the others as they are.',
370
- );
371
- expect(text).toContain('Keep using the template in ./kit/');
372
- expect(text).toContain('kit/docs/GUIDE.md');
373
- });
374
-
375
- it('adds the quality checklist to the review pass', () => {
376
- const withTemplate = reviewPrompt(applied, 'deck-1');
377
- expect(withTemplate).toContain('kit/docs/质量清单.md');
378
- expect(withTemplate).toContain('take a screenshot of every page');
379
- const plain = reviewPrompt(outline, 'deck-1');
380
- expect(plain).not.toContain('质量清单');
381
- expect(plain).toContain('pre-delivery checklist at the end of the slide-authoring skill');
382
- });
383
-
384
- it('reads only the skeletons the model knows', () => {
385
- const read = sanitizeOutline({
386
- ...outline,
387
- templatePrompt: 'x',
388
- templateSkeletons: ['cover', 'nope', 3, 'cover', 'end'],
389
- });
390
- expect(read?.templateSkeletons).toEqual(['cover', 'end']);
391
- expect(read?.templatePrompt).toBe('x');
392
- });
393
- });
394
-
395
- const building = (patch: Partial<Outline> = {}): Outline => ({
396
- ...outline,
397
- state: 'building',
398
- ...patch,
399
- });
400
-
401
- describe('nextStep', () => {
402
- it('walks the pages, then the review', () => {
403
- expect(nextStep(building())).toEqual({ kind: 'page', n: 1 });
404
- expect(nextStep(building({ built: 3 }))).toEqual({ kind: 'page', n: 4 });
405
- expect(nextStep(building({ stage: 'review' }))).toEqual({ kind: 'review' });
406
- expect(nextStep(building({ built: 4 }))).toBeNull();
407
- });
408
-
409
- it('does nothing outside building', () => {
410
- expect(nextStep(outline)).toBeNull();
411
- expect(nextStep({ ...outline, state: 'built' })).toBeNull();
412
- });
413
- });
414
-
415
- describe('afterTurn', () => {
416
- it('keeps the step while the turn waits for an answer', () => {
417
- const waiting = building({ dispatched: { kind: 'page', n: 1, at: 10 } });
418
- expect(afterTurn(waiting, { ok: true, waitingForAnswer: true }, 20)).toBe(waiting);
419
- });
420
-
421
- it('records a failed turn without moving the build on', () => {
422
- const next = afterTurn(
423
- building({ built: 1, dispatched: { kind: 'page', n: 2, at: 10 } }),
424
- { ok: false, waitingForAnswer: false },
425
- 30,
426
- );
427
- expect(next.built).toBe(1);
428
- expect(next.dispatched).toBeNull();
429
- expect(next.lastTurn).toEqual({ kind: 'page', n: 2, ok: false, at: 30 });
430
- });
431
-
432
- it('counts a finished page and switches to the review after the last one', () => {
433
- const mid = afterTurn(
434
- building({ dispatched: { kind: 'page', n: 2, at: 10 } }),
435
- { ok: true, waitingForAnswer: false },
436
- 30,
437
- );
438
- expect(mid.built).toBe(2);
439
- expect(mid.stage).toBe('pages');
440
- expect(mid.dispatched).toBeNull();
441
- expect(mid.lastTurn).toEqual({ kind: 'page', n: 2, ok: true, at: 30 });
442
-
443
- const last = afterTurn(
444
- building({ built: 3, dispatched: { kind: 'page', n: 4, at: 10 } }),
445
- { ok: true, waitingForAnswer: false },
446
- 30,
447
- );
448
- expect(last.built).toBe(4);
449
- expect(last.stage).toBe('review');
450
- });
451
-
452
- it('closes the build when the review passes', () => {
453
- const next = afterTurn(
454
- building({ built: 4, stage: 'review', dispatched: { kind: 'review', n: 0, at: 10 } }),
455
- { ok: true, waitingForAnswer: false },
456
- 30,
457
- );
458
- expect(next.state).toBe('built');
459
- expect(next.lastTurn).toEqual({ kind: 'review', n: 0, ok: true, at: 30 });
460
- });
461
-
462
- it('leaves an outline with nothing dispatched alone', () => {
463
- const idle = building();
464
- expect(afterTurn(idle, { ok: true, waitingForAnswer: false }, 30)).toBe(idle);
465
- const ready = outline;
466
- expect(afterTurn(ready, { ok: true, waitingForAnswer: false }, 30)).toBe(ready);
467
- });
468
-
469
- it('stops after each page when the user asked to check them', () => {
470
- const ask = building({
471
- prefs: { ...DEFAULT_PREFS, confirmEachPage: true },
472
- dispatched: { kind: 'page', n: 2, at: 10 },
473
- });
474
- const next = afterTurn(ask, { ok: true, waitingForAnswer: false }, 30);
475
- expect(next.built).toBe(2);
476
- expect(next.paused).toBe(true);
477
- });
478
-
479
- it('never stops after the last page, so the review can start', () => {
480
- const ask = building({
481
- built: 3,
482
- paused: true,
483
- prefs: { ...DEFAULT_PREFS, confirmEachPage: true },
484
- dispatched: { kind: 'page', n: 4, at: 10 },
485
- });
486
- const next = afterTurn(ask, { ok: true, waitingForAnswer: false }, 30);
487
- expect(next.built).toBe(4);
488
- expect(next.stage).toBe('review');
489
- expect(next.paused).toBe(false);
490
- });
491
-
492
- it('keeps the user pause between pages when they do not ask for checks', () => {
493
- const paused = building({ paused: true, dispatched: { kind: 'page', n: 2, at: 10 } });
494
- expect(afterTurn(paused, { ok: true, waitingForAnswer: false }, 30).paused).toBe(true);
495
- });
496
- });
497
-
498
- describe('pageAwaitingCheck', () => {
499
- const waiting = (patch: Partial<Outline> = {}): Outline =>
500
- building({
501
- built: 2,
502
- paused: true,
503
- prefs: { ...DEFAULT_PREFS, confirmEachPage: true },
504
- lastTurn: { kind: 'page', n: 2, ok: true, at: 5 },
505
- ...patch,
506
- });
507
-
508
- it('names the page a confirm-each-page stop is waiting on', () => {
509
- expect(pageAwaitingCheck(waiting())).toBe(2);
510
- });
511
-
512
- it('says nothing when the pause is not a page check', () => {
513
- expect(pageAwaitingCheck(waiting({ paused: false }))).toBeNull();
514
- expect(
515
- pageAwaitingCheck(waiting({ prefs: { ...DEFAULT_PREFS, confirmEachPage: false } })),
516
- ).toBeNull();
517
- expect(
518
- pageAwaitingCheck(waiting({ lastTurn: { kind: 'page', n: 2, ok: false, at: 5 } })),
519
- ).toBeNull();
520
- expect(
521
- pageAwaitingCheck(waiting({ lastTurn: { kind: 'page', n: 4, ok: true, at: 5 } })),
522
- ).toBeNull();
523
- expect(pageAwaitingCheck(building({ paused: true }))).toBeNull();
524
- });
525
- });
526
-
527
- describe('driverAction', () => {
528
- const now = 1_000_000;
529
-
530
- it('sends only while building and idle', () => {
531
- expect(driverAction(outline, { running: false, now })).toBe('idle');
532
- expect(driverAction(building(), { running: true, now })).toBe('wait');
533
- expect(driverAction(building({ paused: true }), { running: false, now })).toBe('paused');
534
- expect(driverAction(building({ paused: true }), { running: true, now })).toBe('wait');
535
- });
536
-
537
- it('waits on a fresh dispatch and gives up after half a minute', () => {
538
- const step = { kind: 'page' as const, n: 1, at: now - 10_000 };
539
- expect(driverAction(building({ dispatched: step }), { running: false, now })).toBe('wait');
540
- const old = { kind: 'page' as const, n: 1, at: now - 31_000 };
541
- expect(driverAction(building({ dispatched: old }), { running: false, now })).toBe(
542
- 'interrupted',
543
- );
544
- });
545
-
546
- it('picks up where a turn ran cleanly', () => {
547
- expect(driverAction(building(), { running: false, now })).toBe('send');
548
- const fresh = { kind: 'page' as const, n: 1, ok: true, at: now - 60_000 };
549
- expect(driverAction(building({ lastTurn: fresh }), { running: false, now })).toBe('send');
550
- const stale = { kind: 'page' as const, n: 1, ok: true, at: now - 121_000 };
551
- expect(driverAction(building({ lastTurn: stale }), { running: false, now })).toBe(
552
- 'interrupted',
553
- );
554
- const failed = { kind: 'page' as const, n: 1, ok: false, at: now - 1_000 };
555
- expect(driverAction(building({ lastTurn: failed }), { running: false, now })).toBe(
556
- 'interrupted',
557
- );
558
- });
559
- });
560
-
561
- describe('teamState', () => {
562
- it('shows the page writer at work and the readers finished', () => {
563
- expect(
564
- teamState(building(), { running: true, waitingForAnswer: false, action: 'wait' }),
565
- ).toEqual({
566
- lead: 'working',
567
- research: 'done',
568
- outline: 'done',
569
- design: 'working',
570
- review: 'idle',
571
- });
572
- });
573
-
574
- it('greys out the reader when there are no files', () => {
575
- expect(
576
- teamState(building({ files: [] }), {
577
- running: false,
578
- waitingForAnswer: false,
579
- action: 'send',
580
- }).research,
581
- ).toBe('none');
582
- });
583
-
584
- it('asks the user when the review stops on a question', () => {
585
- const state = teamState(building({ built: 4, stage: 'review' }), {
586
- running: false,
587
- waitingForAnswer: true,
588
- action: 'wait',
589
- });
590
- expect(state.review).toBe('needs-you');
591
- expect(state.design).toBe('done');
592
- expect(state.lead).toBe('thinking');
593
- });
594
-
595
- it('marks everyone done once the deck is built', () => {
596
- const state = teamState(
597
- { ...building(), state: 'built', built: 4, stage: 'review' },
598
- {
599
- running: false,
600
- waitingForAnswer: false,
601
- action: 'idle',
602
- },
603
- );
604
- expect(state).toMatchObject({ lead: 'done', design: 'done', review: 'done' });
605
- });
606
-
607
- it('calls Lemon to the user when the build stopped', () => {
608
- const state = teamState(building(), {
609
- running: false,
610
- waitingForAnswer: false,
611
- action: 'interrupted',
612
- });
613
- expect(state.lead).toBe('needs-you');
614
- expect(
615
- teamState(building({ paused: true }), {
616
- running: false,
617
- waitingForAnswer: false,
618
- action: 'paused',
619
- }).lead,
620
- ).toBe('idle');
621
- });
622
- });
623
-
624
- describe('sanitizeDecisions', () => {
625
- it('keeps well-formed entries and trims the long ones', () => {
626
- const long = 'x'.repeat(200);
627
- const cleaned = sanitizeDecisions([
628
- { id: 'q1:0', question: long, answer: long },
629
- { id: '', question: 'no id', answer: 'x' },
630
- { question: 'no id either', answer: 'x' },
631
- 'nope',
632
- ]);
633
- expect(cleaned).toHaveLength(1);
634
- expect(cleaned[0].id).toBe('q1:0');
635
- expect(cleaned[0].question).toHaveLength(120);
636
- expect(cleaned[0].answer).toHaveLength(120);
637
- expect(sanitizeDecisions('none')).toEqual([]);
638
- });
639
-
640
- it('keeps at most twenty decisions', () => {
641
- const many = Array.from({ length: MAX_DECISIONS + 5 }, (_, i) => ({
642
- id: `q${i}:0`,
643
- question: `Q${i}`,
644
- answer: 'A',
645
- }));
646
- expect(sanitizeDecisions(many)).toHaveLength(MAX_DECISIONS);
647
- });
648
- });
649
-
650
- describe('collectDecisions', () => {
651
- const answered = (
652
- id: string,
653
- questions: { question: string; header?: string }[],
654
- answers: Record<string, string>,
655
- ) => ({ id, kind: 'question', status: 'answered', questions, answers });
656
-
657
- it('turns each answered question into a decision', () => {
658
- expect(
659
- collectDecisions(
660
- [],
661
- [
662
- answered('q1', [{ question: 'Use which number?', header: 'Numbers' }], {
663
- 'Use which number?': '41%',
664
- }),
665
- ],
666
- ),
667
- ).toEqual([{ id: 'q1:0', question: 'Numbers', answer: '41%' }]);
668
- });
669
-
670
- it('falls back to the question when it has no header', () => {
671
- expect(
672
- collectDecisions(
673
- [],
674
- [answered('q1', [{ question: 'Which page?' }], { 'Which page?': '2' })],
675
- )[0].question,
676
- ).toBe('Which page?');
677
- });
678
-
679
- it('does not add the same question twice when a resumed turn reports it again', () => {
680
- const first = collectDecisions(
681
- [],
682
- [answered('q1', [{ question: 'Which number?' }], { 'Which number?': '41%' })],
683
- );
684
- const again = collectDecisions(first, [
685
- answered('q1', [{ question: 'Which number?' }], { 'Which number?': '41%' }),
686
- ]);
687
- expect(again).toEqual(first);
688
- });
689
-
690
- it('ignores questions that are not answered and items that are not questions', () => {
691
- expect(
692
- collectDecisions(
693
- [],
694
- [
695
- {
696
- id: 'q1',
697
- kind: 'question',
698
- status: 'pending',
699
- questions: [{ question: 'Q' }],
700
- answers: null,
701
- },
702
- { id: 'a1', kind: 'assistant' },
703
- ],
704
- ),
705
- ).toEqual([]);
706
- });
707
-
708
- it('stops at twenty decisions', () => {
709
- const many = Array.from({ length: MAX_DECISIONS + 4 }, (_, i) =>
710
- answered(`q${i}`, [{ question: `Q${i}` }], { [`Q${i}`]: 'A' }),
711
- );
712
- expect(collectDecisions([], many)).toHaveLength(MAX_DECISIONS);
713
- });
714
- });
715
-
716
- describe('lastErrorText', () => {
717
- const user = (id: string): AgentItem => ({ kind: 'user', id, ts: '', text: 'go' });
718
- const error = (id: string, text: string): AgentItem => ({ kind: 'error', id, ts: '', text });
719
- const assistant = (id: string): AgentItem => ({
720
- kind: 'assistant',
721
- id,
722
- ts: '',
723
- text: 'ok',
724
- streaming: false,
725
- });
726
-
727
- it('returns the last error after the message the user sent', () => {
728
- const items = [user('u1'), error('e1', 'old'), assistant('a1'), error('e2', 'new')];
729
- expect(lastErrorText(items, 'u1')).toBe('new');
730
- });
731
-
732
- it('ignores errors that came before the user message', () => {
733
- const items = [error('e0', 'before'), user('u1'), assistant('a1'), user('u2')];
734
- expect(lastErrorText(items, 'u2')).toBeNull();
735
- });
736
-
737
- it('returns null when the turn carried no error', () => {
738
- expect(lastErrorText([user('u1'), assistant('a1')], 'u1')).toBeNull();
739
- expect(lastErrorText([], null)).toBeNull();
740
- });
741
-
742
- it('reads every item when the user message is unknown', () => {
743
- expect(lastErrorText([error('e1', 'one'), assistant('a1')], null)).toBe('one');
744
- expect(lastErrorText([user('u1'), error('e1', 'one')], 'missing')).toBe('one');
745
- });
746
-
747
- it('clips the text to 200 characters', () => {
748
- const long = 'x'.repeat(500);
749
- expect(lastErrorText([user('u1'), error('e1', long)], 'u1')).toHaveLength(200);
750
- });
751
- });