okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3d9c21509
main
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 ) { 8const 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' } }); 9return 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' , () => { 19const chat = 'c1' ; 20assert . equal ( status . applyStatusFrame ( chat , { seq :1 , phase :'thinking' , code :'thinking' , text :'Thinking…' , tokens :40 , elapsedMs :100 }, 1000 ), true ); 21assert . equal ( status . statusOf ( chat ). tokens , 40 ); 22assert . equal ( status . applyStatusFrame ( chat , { seq :0 , phase :'tool' , code :'tool' , text :'old' }), false , 'an older frame never replaces a newer one' ); 23assert . equal ( status . statusOf ( chat ). phase , 'thinking' ); 24status . applyStatusFrame ( chat , { seq :2 , phase :'preparing' , code :'preparing-tool' , text :'Writing pagoda.html…' , args :{ verb :'Writing a file' , target :'pagoda.html' } }); 25assert . equal ( status . statusOf ( chat ). phase , 'preparing' ); 26status . applyStatusFrame ( chat , { seq :3 , phase :'idle' , code :'idle' , text :'' }); 27assert . equal ( status . statusOf ( chat ), null , 'idle removes the line' ); 28status . applyStatusFrame ( chat , { seq :4 , phase :'goal' , code :'goal-step' , text :'Working on the goal…' , args :{ round :1 } }); 29status . clearStatus ( chat ); 30assert . 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' , () => { 34const frame = ( phase , code , args ) => ({ seq :1 , phase, code, text :` ${ phase } …` , args, elapsedMs :0 , receivedAt :0 }); 35const words = Array . from ({ length :12 }, ( _ , i ) => status . statusWords ( frame ( 'thinking' , 'thinking' ), i )); 36assert . equal ( new Set ( words ). size , 12 , 'a tasteful list of twelve' ); 37assert . equal ( words [ 0 ], 'Thinking…' ); 38assert . ok ( words . includes ( 'Cooking up an answer…' ) && words . includes ( 'Pondering…' )); 39assert . equal ( status . statusWords ( frame ( 'thinking' , 'thinking' ), 13 ), words [ 1 ], 'it rotates round' ); 40assert . equal ( status . statusWords ( frame ( 'waiting' , 'model-working' ), 2 ), words [ 2 ]); 41assert . equal ( status . statusWords ( frame ( 'waiting' , 'reading-prompt' ), 5 ), 'Reading your message…' , 'what is really happening is said plainly' ); 42assert . equal ( status . statusWords ( frame ( 'preparing' , 'preparing-tool' , { verb :'Writing a file' , target :'pagoda.html' }), 5 ), 'Writing a file · pagoda.html…' ); 43assert . equal ( status . statusWords ( frame ( 'tool' , 'tool' , { verb :'Searching the web' }), 9 ), 'Searching the web…' ); 44assert . equal ( status . statusWords ( frame ( 'goal' , 'goal-step' , { round :2 }), 7 ), 'Working on the goal…' ); 45assert . equal ( status . isPlayful ( frame ( 'tool' , 'tool' )), false ); 46assert . 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' , () => { 50assert . equal ( reasoning . lastReasoningLine ( 'First idea.\n\n**Second** idea about `colours`\n' ), 'Second idea about colours' ); 51assert . equal ( reasoning . lastReasoningLine ( '' ), '' ); 52assert . ok ( reasoning . lastReasoningLine ( 'x' . repeat ( 300 )). length <= 110 ); 53assert . equal ( reasoning . thoughtDuration ( 38_400 ), '38s' ); assert . equal ( reasoning . thoughtDuration ( 64_000 ), '1m 04s' ); 54assert . equal ( reasoning . thoughtDuration ( 400 ), null ); assert . equal ( reasoning . thoughtDuration ( undefined ), null ); 55assert . equal ( reasoning . atBottom ({ scrollHeight :500 , scrollTop :340 , clientHeight :160 }), true ); 56assert . 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"' , () => { 60assert . equal ( goal . readGoal ( null ), null ); assert . equal ( goal . readGoal ({ objective :'' , phase :'active' }), null ); assert . equal ( goal . readGoal ({ objective :'x' , phase :'weird' }), null ); 61const g = goal . readGoal ({ objective :'Build the page' , phase :'blocked' , round :2 , maxRounds :5 , blocked :'It needs a folder.' }); 62assert . deepEqual ( g , { objective :'Build the page' , phase :'blocked' , round :2 , maxRounds :5 , blocked :'It needs a folder.' }); 63goal . applyGoalFrame ( 'chat-g' , { objective :'Ship it' , phase :'active' , round :1 , maxRounds :3 }); 64assert . equal ( goal . goalOf ( 'chat-g' ). objective , 'Ship it' ); 65goal . applyGoalFrame ( 'chat-g' , null ); 66assert . equal ( goal . goalOf ( 'chat-g' ), null , 'the host clearing the goal removes the bar' ); 67assert . equal ( goal . goalProgress , undefined , 'no step counter is shown for a goal' ); 68});