okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { readFile } from 'node:fs/promises' ; 4import { transform } from 'esbuild' ; 5 6const { code} = await transform ( await readFile ( new URL ( '../src/lib/time-skip.ts' , import . meta. url ), 'utf8' ), { loader :'ts' , format :'esm' }); 7const { timeSkipPrompt, timeSkipFrame, timeSkipTabId, timeSkipDocuments, timeSkipDocumentMarkdown, timeSkipResultState, withTimeSkipResult, TIME_SKIP_DURATION } = await import ( `data:text/javascript;base64, ${ Buffer . from ( code ). toString ( 'base64' )} ` ); 8 9test ( 'only an exact leading time-skip command starts a demo' , () => { 10assert . equal ( timeSkipPrompt ( '/time-skip Make something nice' ), 'Make something nice' ); 11assert . equal ( timeSkipPrompt ( ' /TIME-SKIP A plan\nwith two parts ' ), 'A plan\nwith two parts' ); 12assert . equal ( timeSkipPrompt ( '/time-skip' ), '' ); 13assert . equal ( timeSkipPrompt ( '/time-skip ' ), '' ); 14for ( const message of [ '/time-skipping hello' , '/time-skipper' , 'Explain /time-skip' , '/time-skip: hello' ]) assert . equal ( timeSkipPrompt ( message ), null ); 15}); 16 17test ( 'the candidate is checked before promotion and a regressed revision cannot replace it' , () => { 18const run = { id :'test' , prompt :'Make a plan' , startedAt :100000 }; 19let compared = false , promoted = false , rejected = false ; 20for ( let elapsed = 0 ; elapsed <= TIME_SKIP_DURATION ; elapsed += 250 ) { 21const frame = timeSkipFrame ( run , run . startedAt + elapsed ); 22if ( frame . status === 'Comparing' && frame . pass === 1 ) compared = true ; 23if ( frame . chosen === 'b' ) { assert . ok ( compared ); promoted = true ; } 24if ( promoted ) assert . equal ( frame . chosen , 'b' ); 25if ( frame . status === 'Keeping B' ) { assert . equal ( frame . pass , 2 ); rejected = true ; } 26if ( frame . done ) assert . ok ( rejected ); 27} 28assert . ok ( promoted && rejected ); 29}); 30 31test ( 'a restored or hidden run follows its original clock, and completion is stable' , () => { 32const run = { id :'one' , prompt :'Make a plan' , startedAt :100000 }; 33assert . equal ( timeSkipFrame ( run , 99999 ). index , 0 ); 34const saved = JSON . parse ( JSON . stringify ( run )); 35assert . equal ( timeSkipFrame ( saved , 118000 ). pass , 2 ); 36assert . equal ( timeSkipFrame ( saved , 118000 ). chosen , 'b' ); 37assert . equal ( timeSkipFrame ( saved , 128000 ). done , true ); 38assert . equal ( timeSkipFrame ({ ...saved , completedAt :128000 }, 100000 ). done , true ); 39assert . notEqual ( timeSkipTabId ( 'one' ), timeSkipTabId ( 'two' )); 40}); 41 42test ( 'original and kept outputs stay stable after the rejected later revision' , () => { 43for ( const prompt of [ 'Make a launch plan' , 'Write a counter app' ]) { 44const run = { id :'stable' , prompt, startedAt :100000 }; 45const documents = timeSkipDocuments ( prompt ); 46const baseline = JSON . stringify ( documents . original ); 47const update = timeSkipDocumentMarkdown ( documents . updated ); 48for ( const now of [ 109500 , 116000 , 124000 , 128000 ]) { 49timeSkipResultState ( run , now ); 50assert . equal ( JSON . stringify ( timeSkipDocuments ( prompt ). original ), baseline ); 51assert . equal ( timeSkipDocumentMarkdown ( timeSkipDocuments ( prompt ). updated ), update ); 52} 53assert . notEqual ( timeSkipDocumentMarkdown ( documents . original ), update ); 54assert . notEqual ( timeSkipDocumentMarkdown ( documents . revision ), update ); 55} 56}); 57 58test ( 'restore and undo preserve both artifacts and survive session serialization' , () => { 59const run = { id :'restore' , prompt :'Make a launch plan' , startedAt :100000 }; 60assert . equal ( withTimeSkipResult ( run , 'original' , 118000 ), run , 'cannot change the retained result during the demo' ); 61const restored = withTimeSkipResult ( run , 'original' , 128000 ); 62const saved = JSON . parse ( JSON . stringify ( restored )); 63assert . equal ( timeSkipResultState ( saved , 130000 ). kept , 'original' ); 64assert . equal ( timeSkipResultState ( saved , 130000 ). status , 'Original restored' ); 65assert . deepEqual ( timeSkipDocuments ( saved . prompt ), timeSkipDocuments ( run . prompt )); 66const undone = withTimeSkipResult ( saved , 'updated' , 130000 ); 67assert . equal ( timeSkipResultState ( undone , 130000 ). status , 'Updated result kept' ); 68assert . equal ( run . retainedVersion , undefined , 'the baseline run was not mutated' ); 69}); 70 71test ( 'unfinished updates are distinguished from checked and retained results' , () => { 72const run = { id :'progress' , prompt :'Make a launch plan' , startedAt :100000 }; 73assert . equal ( timeSkipResultState ( run , 106000 ). ready , false ); 74assert . equal ( timeSkipResultState ( run , 109500 ). status , 'Checking changes…' ); 75assert . equal ( timeSkipResultState ( run , 109500 ). kept , 'original' ); 76assert . equal ( timeSkipResultState ( run , 112500 ). kept , 'updated' ); 77assert . equal ( timeSkipResultState ( run , 120500 ). kept , 'updated' ); 78assert . equal ( timeSkipResultState ( run , 128000 ). status , 'Updated result kept' ); 79});