okami.mom/strata

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

Niko1221Merge f-020-int into f-020-rel3d9c21509

main
5.3 KiB68 linesraw
1/** Items 1, 2 and 5: the live status line, the reasoning row and the goal state, as the pure logic the page uses. */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { build } from 'esbuild';
5import { fileURLToPath } from 'node:url';
6
7async function load(file) {
8  const result = await build({ entryPoints: [fileURLToPath(new URL(`../src/lib/${file}`, import.meta.url))], bundle: true, format: 'esm', platform: 'node', write: false, alias: { react: fileURLToPath(new URL('./helpers/react-stub.mjs', import.meta.url)) }, logLevel: 'silent', loader: { '.json': 'json' } });
9  return import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
10}
11globalThis.window ??= globalThis; globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };
12globalThis.navigator ??= { language: 'en' };
13
14const status = await load('live-status.ts');
15const reasoning = await load('reasoning.ts');
16const goal = await load('goal.ts');
17
18test('item 1: only the newest status frame is kept, and idle, a missing phase or done clears it', () => {
19  const chat = 'c1';
20  assert.equal(status.applyStatusFrame(chat, { seq: 1, phase: 'thinking', code: 'thinking', text: 'Thinking…', tokens: 40, elapsedMs: 100 }, 1000), true);
21  assert.equal(status.statusOf(chat).tokens, 40);
22  assert.equal(status.applyStatusFrame(chat, { seq: 0, phase: 'tool', code: 'tool', text: 'old' }), false, 'an older frame never replaces a newer one');
23  assert.equal(status.statusOf(chat).phase, 'thinking');
24  status.applyStatusFrame(chat, { seq: 2, phase: 'preparing', code: 'preparing-tool', text: 'Writing pagoda.html…', args: { verb: 'Writing a file', target: 'pagoda.html' } });
25  assert.equal(status.statusOf(chat).phase, 'preparing');
26  status.applyStatusFrame(chat, { seq: 3, phase: 'idle', code: 'idle', text: '' });
27  assert.equal(status.statusOf(chat), null, 'idle removes the line');
28  status.applyStatusFrame(chat, { seq: 4, phase: 'goal', code: 'goal-step', text: 'Working on the goal…', args: { round: 1 } });
29  status.clearStatus(chat);
30  assert.equal(status.statusOf(chat), null, 'done and error clear it too');
31});
32
33test('item 1: playful words only while it thinks; a running tool always shows the real action', () => {
34  const frame = (phase, code, args) => ({ seq: 1, phase, code, text: `${phase}…`, args, elapsedMs: 0, receivedAt: 0 });
35  const words = Array.from({ length: 12 }, (_, i) => status.statusWords(frame('thinking', 'thinking'), i));
36  assert.equal(new Set(words).size, 12, 'a tasteful list of twelve');
37  assert.equal(words[0], 'Thinking…');
38  assert.ok(words.includes('Cooking up an answer…') && words.includes('Pondering…'));
39  assert.equal(status.statusWords(frame('thinking', 'thinking'), 13), words[1], 'it rotates round');
40  assert.equal(status.statusWords(frame('waiting', 'model-working'), 2), words[2]);
41  assert.equal(status.statusWords(frame('waiting', 'reading-prompt'), 5), 'Reading your message…', 'what is really happening is said plainly');
42  assert.equal(status.statusWords(frame('preparing', 'preparing-tool', { verb: 'Writing a file', target: 'pagoda.html' }), 5), 'Writing a file · pagoda.html…');
43  assert.equal(status.statusWords(frame('tool', 'tool', { verb: 'Searching the web' }), 9), 'Searching the web…');
44  assert.equal(status.statusWords(frame('goal', 'goal-step', { round: 2 }), 7), 'Working on the goal…');
45  assert.equal(status.isPlayful(frame('tool', 'tool')), false);
46  assert.equal(status.elapsedText(0, 14_000), '14s'); assert.equal(status.elapsedText(0, 64_000), '1m 04s');
47});
48
49test('item 2: the reasoning row shows what it is thinking about, then how long it took; it sticks to the bottom only while the person has not scrolled up', () => {
50  assert.equal(reasoning.lastReasoningLine('First idea.\n\n**Second** idea about `colours`\n'), 'Second idea about colours');
51  assert.equal(reasoning.lastReasoningLine(''), '');
52  assert.ok(reasoning.lastReasoningLine('x'.repeat(300)).length <= 110);
53  assert.equal(reasoning.thoughtDuration(38_400), '38s'); assert.equal(reasoning.thoughtDuration(64_000), '1m 04s');
54  assert.equal(reasoning.thoughtDuration(400), null); assert.equal(reasoning.thoughtDuration(undefined), null);
55  assert.equal(reasoning.atBottom({ scrollHeight: 500, scrollTop: 340, clientHeight: 160 }), true);
56  assert.equal(reasoning.atBottom({ scrollHeight: 500, scrollTop: 100, clientHeight: 160 }), false, 'scrolled up: stop following');
57});
58
59test('item 5: the goal object from a frame, an event or a route; anything else is "no goal"', () => {
60  assert.equal(goal.readGoal(null), null); assert.equal(goal.readGoal({ objective: '', phase: 'active' }), null); assert.equal(goal.readGoal({ objective: 'x', phase: 'weird' }), null);
61  const g = goal.readGoal({ objective: 'Build the page', phase: 'blocked', round: 2, maxRounds: 5, blocked: 'It needs a folder.' });
62  assert.deepEqual(g, { objective: 'Build the page', phase: 'blocked', round: 2, maxRounds: 5, blocked: 'It needs a folder.' });
63  goal.applyGoalFrame('chat-g', { objective: 'Ship it', phase: 'active', round: 1, maxRounds: 3 });
64  assert.equal(goal.goalOf('chat-g').objective, 'Ship it');
65  goal.applyGoalFrame('chat-g', null);
66  assert.equal(goal.goalOf('chat-g'), null, 'the host clearing the goal removes the bar');
67  assert.equal(goal.goalProgress, undefined, 'no step counter is shown for a goal');
68});