okami.mom/strata

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

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

main
4.9 KiB70 linesraw
1/** Owner round 7: /plan card, plan steps in the sidebar, queued messages, against agent6's contract (notes-split-agent6.md). */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { readFile } from 'node:fs/promises';
5import { build } from 'esbuild';
6import { fileURLToPath } from 'node:url';
7
8const src = f => fileURLToPath(new URL(`../src/${f}`, import.meta.url));
9const stub = fileURLToPath(new URL('./helpers/react-stub.mjs', import.meta.url));
10async function load(file) {
11  const result = await build({ entryPoints: [src(`lib/${file}`)], bundle: true, format: 'esm', platform: 'node', write: false, alias: { react: stub }, logLevel: 'silent', loader: { '.json': 'json' } });
12  return import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString('base64')}`);
13}
14globalThis.window ??= globalThis; globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };
15globalThis.navigator ??= { language: 'en' };
16const P = await load('plan.ts');
17const G = await load('goal.ts');
18
19const plan = (state, steps, extra = {}) => ({ id: 'p1', chatId: 'c', title: 'Pagoda', request: 'x', source: 'plan', state, goalStarted: false, steps, ...extra });
20const step = (id, text, state = 'pending', extra = {}) => ({ id, text, detail: null, state, summary: null, ...extra });
21
22test('a plan from the host is read with step states, detail and summary; bad parts are dropped', () => {
23  const p = P.readPlan(plan('running', [step('s1', 'Look', 'done', { summary: 'Found two files.' }), step('s2', 'Write', 'running', { detail: 'one file' }), step('s3', 'Check'), step('s4', '  '), 7]));
24  assert.equal(p.steps.length, 3);
25  assert.deepEqual(p.steps.map(s => s.state), ['done', 'running', 'pending']);
26  assert.equal(p.steps[0].summary, 'Found two files.'); assert.equal(p.steps[1].detail, 'one file');
27  assert.equal(P.stepsDone(p), 1); assert.equal(P.currentStep(p), 2);
28  assert.equal(P.readPlan(plan('weird', [step('s1', 'x')])), null);
29  assert.equal(P.readPlan(plan('proposed', [])), null);
30  assert.equal(P.readPlan(null), null);
31  assert.equal(P.readPlan(plan('paused', [step('s1', 'x', 'skipped')])).steps[0].state, 'skipped');
32});
33
34test('plan frames replace the plan; null clears it; the same plan changes nothing', () => {
35  P.applyPlanFrame('c', plan('proposed', [step('s1', 'A'), step('s2', 'B')]));
36  assert.equal(P.planOf('c').state, 'proposed');
37  P.applyPlanFrame('c', plan('running', [step('s1', 'A', 'running'), step('s2', 'B')]));
38  assert.equal(P.planOf('c').steps[0].state, 'running');
39  assert.equal(P.planOf('other'), null);
40  P.applyPlanFrame('c', null);
41  assert.equal(P.planOf('c'), null);
42});
43
44test('queued messages: the first waits for the current step, the next ones "after that", a goal without points waits for the end', () => {
45  P.applyQueueFrame('c', [{ id: 'q1', text: 'Add a moon', deliverAt: 'step' }, { id: 'q2', text: 'Warmer glow', deliverAt: 'step' }, { id: '', text: 'x' }, { id: 'q3' }]);
46  const items = P.queueOf('c');
47  assert.deepEqual(items.map(q => q.id), ['q1', 'q2']);
48  const p = P.readPlan(plan('running', [step('s1', 'A', 'done'), step('s2', 'B', 'running')]));
49  assert.deepEqual(P.queuedWhen(items[0], 0, p), { id: 'Queued — sends when step {0} finishes', args: [2] });
50  assert.deepEqual(P.queuedWhen(items[1], 1, p), { id: 'Queued — sends after that' });
51  assert.deepEqual(P.queuedWhen({ id: 'e', text: 'x', deliverAt: 'end' }, 0, null), { id: 'Queued — sends when the goal finishes' });
52  P.applyQueueFrame('c', []);
53  assert.deepEqual(P.queueOf('c'), []);
54});
55
56test('the goal bar reads the step the host reports', () => {
57  const g = G.readGoal({ objective: 'Build a small pagoda page', phase: 'active', round: 9, maxRounds: 50, step: { index: 2, total: 3, text: 'Write it' } });
58  assert.deepEqual(g.step, { index: 2, total: 3, text: 'Write it' });
59  assert.equal(G.goalProgress, undefined, 'no step counter is shown for a goal');
60  assert.equal(G.readGoal({ objective: 'x', phase: 'active', round: 1, maxRounds: 5 }).step, undefined);
61});
62
63test('the chat and the sidebar draw the plan from the same store, and the stream feeds it', async () => {
64  const [card, chat, store, client] = await Promise.all(['components/PlanCard.tsx', 'screens/Chat.tsx', 'lib/store.ts', 'lib/client.ts'].map(f => readFile(src(f), 'utf8')));
65  assert.match(card, /export function PlanCard/); assert.match(card, /export function PlanPanel/); assert.match(card, /export function QueuedMessages/);
66  assert.match(card, /approvePlan\(chatId, plan\.id\)/); assert.match(card, /startPlanGoal\(plan\.id\)/); assert.match(card, /planReject\(chatId, plan\.id\)/);
67  assert.match(chat, /<PlanPanel chatId/); assert.match(chat, /plan:\$\{session\.id\}/); assert.match(chat, /planCard \? <PlanCard/);
68  assert.match(store, /type === 'plan'/); assert.match(store, /type === 'queued-message'/); assert.match(store, /type === 'queued-release'/); assert.match(store, /\/api\/plan\/start-goal/);
69  assert.match(client, /'plan', 'queue'/);
70});