okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-029-urgent (bc694233b)99866a47a

main
2.0 KiB33 linesraw
1/** A streamed token changes one message. The others must not render again, or a long chat does work (and makes garbage) for every
2 *  token in proportion to its whole history: measured 0.7 s of main-thread time for a 10-turn reply on a new chat and 1.9 s after ten
3 *  replies, peak heap 95 MB instead of 50 MB (scripts/chat-growth.mjs). Source guards: the DOM is not available in this suite. */
4import test from 'node:test';
5import assert from 'node:assert/strict';
6import { readFileSync } from 'node:fs';
7import { resolve } from 'node:path';
8
9const chat = readFileSync(resolve(import.meta.dirname, '../src/screens/Chat.tsx'), 'utf8');
10
11test('the message row is a memoised component', () => {
12  assert.match(chat, /import \{[^}]*\bmemo\b[^}]*\} from 'react'/);
13  assert.match(chat, /const Message = memo\(MessageView\)/);
14});
15
16test('the list passes handlers whose identity never changes', () => {
17  assert.match(chat, /const messageHandlers = useMemo\(\(\) => \(\{[\s\S]*?\}\), \[\]\)/);
18  for (const name of ['onEditPrompt', 'onPreview', 'onOpenTimeSkip', 'onOpenLink'])
19    assert.match(chat, new RegExp(`${name}=\{messageHandlers\.${name}\}`), name);
20  assert.doesNotMatch(chat, /onEditPrompt=\{editPrompt\}/);
21});
22
23const store = readFileSync(resolve(import.meta.dirname, '../src/lib/store.ts'), 'utf8');
24
25test('streamed text and reasoning are applied in groups, and everything else applies what is waiting first', () => {
26  assert.match(store, /const queueText = \(kind: 'reasoning' \| 'delta', text: string\)/);
27  assert.match(store, /waitingTimer \?\?= setTimeout\(flushWaiting, 90\)/);
28  // the order of frames the person sees is the order written: a frame of any other kind flushes first
29  assert.match(store, /if \(type === 'delta' \|\| type === 'reasoning'\) \{ queueText\(type, String\(frame\.text \?\? ''\)\); return; \}\s*flushWaiting\(\);/);
30  // a failure or the end of the turn never leaves text behind
31  assert.match(store, /\} catch \(err\) \{\s*flushWaiting\(\);/);
32  assert.match(store, /\} finally \{\s*flushWaiting\(\);/);
33});