okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3d9c21509
main
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 ) { 11const result = await build ({ entryPoints :[ src ( `lib/ ${ file } ` )], bundle :true , format :'esm' , platform :'node' , write :false , alias :{ react :stub }, logLevel :'silent' , loader :{ '.json' :'json' } }); 12return 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' , () => { 23const 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 ])); 24assert . equal ( p . steps . length , 3 ); 25assert . deepEqual ( p . steps . map ( s => s . state ), [ 'done' , 'running' , 'pending' ]); 26assert . equal ( p . steps [ 0 ]. summary , 'Found two files.' ); assert . equal ( p . steps [ 1 ]. detail , 'one file' ); 27assert . equal ( P . stepsDone ( p ), 1 ); assert . equal ( P . currentStep ( p ), 2 ); 28assert . equal ( P . readPlan ( plan ( 'weird' , [ step ( 's1' , 'x' )])), null ); 29assert . equal ( P . readPlan ( plan ( 'proposed' , [])), null ); 30assert . equal ( P . readPlan ( null ), null ); 31assert . 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' , () => { 35P . applyPlanFrame ( 'c' , plan ( 'proposed' , [ step ( 's1' , 'A' ), step ( 's2' , 'B' )])); 36assert . equal ( P . planOf ( 'c' ). state , 'proposed' ); 37P . applyPlanFrame ( 'c' , plan ( 'running' , [ step ( 's1' , 'A' , 'running' ), step ( 's2' , 'B' )])); 38assert . equal ( P . planOf ( 'c' ). steps [ 0 ]. state , 'running' ); 39assert . equal ( P . planOf ( 'other' ), null ); 40P . applyPlanFrame ( 'c' , null ); 41assert . 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' , () => { 45P . applyQueueFrame ( 'c' , [{ id :'q1' , text :'Add a moon' , deliverAt :'step' }, { id :'q2' , text :'Warmer glow' , deliverAt :'step' }, { id :'' , text :'x' }, { id :'q3' }]); 46const items = P . queueOf ( 'c' ); 47assert . deepEqual ( items . map ( q => q . id ), [ 'q1' , 'q2' ]); 48const p = P . readPlan ( plan ( 'running' , [ step ( 's1' , 'A' , 'done' ), step ( 's2' , 'B' , 'running' )])); 49assert . deepEqual ( P . queuedWhen ( items [ 0 ], 0 , p ), { id :'Queued — sends when step {0} finishes' , args :[ 2 ] }); 50assert . deepEqual ( P . queuedWhen ( items [ 1 ], 1 , p ), { id :'Queued — sends after that' }); 51assert . deepEqual ( P . queuedWhen ({ id :'e' , text :'x' , deliverAt :'end' }, 0 , null ), { id :'Queued — sends when the goal finishes' }); 52P . applyQueueFrame ( 'c' , []); 53assert . deepEqual ( P . queueOf ( 'c' ), []); 54}); 55 56test ( 'the goal bar reads the step the host reports' , () => { 57const g = G . readGoal ({ objective :'Build a small pagoda page' , phase :'active' , round :9 , maxRounds :50 , step :{ index :2 , total :3 , text :'Write it' } }); 58assert . deepEqual ( g . step , { index :2 , total :3 , text :'Write it' }); 59assert . equal ( G . goalProgress , undefined , 'no step counter is shown for a goal' ); 60assert . 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 () => { 64const [ 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' ))); 65assert . match ( card , / export function PlanCard / ); assert . match ( card , / export function PlanPanel / ); assert . match ( card , / export function QueuedMessages / ); 66assert . match ( card , / approvePlan\(chatId, plan\.id\) / ); assert . match ( card , / startPlanGoal\(plan\.id\) / ); assert . match ( card , / planReject\(chatId, plan\.id\) / ); 67assert . match ( chat , / <PlanPanel chatId / ); assert . match ( chat , / plan:\$\{session\.id\} / ); assert . match ( chat , / planCard \? <PlanCard / ); 68assert . match ( store , / type === 'plan' / ); assert . match ( store , / type === 'queued-message' / ); assert . match ( store , / type === 'queued-release' / ); assert . match ( store , / \/api\/plan\/start-goal / ); 69assert . match ( client , / 'plan', 'queue' / ); 70});