okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3c3d2881b
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { mkdtempSync , rmSync } from 'node:fs' ; 4import { tmpdir } from 'node:os' ; 5import { join } from 'node:path' ; 6import { stepSummary , parsePlan , parsePoints , stepObjective , stepLine , firstSentence , editedSteps , planAsPrompt , PlanStore , planPrompt } from '../server/agent/plan.mjs' ; 7 8test ( 'a plan is the last numbered list of the reply, with reasons after a dash and indented detail kept' , () => { 9const p = parsePlan ([ 10'I looked at the folder. It is empty.' , 11'' , 12'## Pagoda page' , 13'1. Look at what is already in the folder' , 14'2. **Write pagoda.html** with the night sky and the glow — one file, inline styles' , 15' - keep it under 10 KB' , 16'3. Open it and check it renders.' , 17'' , 18'Tell me if you want changes.' , 19]. join ( '\n' ), 'build a pagoda' ); 20assert . equal ( p . title , 'Pagoda page' ); 21assert . deepEqual ( p . steps . map ( s => s . text ), [ 'Look at what is already in the folder' , 'Write pagoda.html with the night sky and the glow' , 'Open it and check it renders' ]); 22assert . equal ( p . steps [ 1 ]. detail , 'one file, inline styles' ); 23assert . equal ( p . steps [ 2 ]. state , 'pending' ); 24assert . deepEqual ( p . steps . map ( s => s . id ), [ 's1' , 's2' , 's3' ]); 25}); 26 27test ( 'a reply with no list gives no plan, so the page shows the words as they are' , () => { 28assert . equal ( parsePlan ( 'I would just write the file.' , 'x' ), null ); 29assert . equal ( parsePlan ( '1. only one item' , 'x' ), null ); 30assert . equal ( parsePlan ( '3. a\n4. b' , 'x' ), null , 'a list must start at 1' ); 31}); 32 33test ( 'the last list wins, and a title falls back to the request' , () => { 34const p = parsePlan ( '1. old a\n2. old b\n\nBetter:\n1. new a\n2. new b\n3. new c' , 'Make a thing' ); 35assert . deepEqual ( p . steps . map ( s => s . text ), [ 'new a' , 'new b' , 'new c' ]); 36assert . equal ( p . title , 'Make a thing' ); 37}); 38 39test ( 'goal points are found in lines or in one sentence, and one point is not a list' , () => { 40const a = parsePoints ( 'Build the page: 1. draw the base 2. add the roof 3. add the glow' ); 41assert . equal ( a . title , 'Build the page' ); 42assert . deepEqual ( a . steps . map ( s => s . text ), [ 'draw the base' , 'add the roof' , 'add the glow' ]); 43const b = parsePoints ( 'Make it work\n1) first\n2) second' ); 44assert . equal ( b . steps . length , 2 ); 45assert . equal ( parsePoints ( 'Fix the 3. test and see' ), null ); 46assert . equal ( parsePoints ( '1. only' ), null ); 47assert . equal ( parsePoints ( 'make every test pass' ), null ); 48}); 49 50test ( 'a step goal names its step, what is done and what is not its job' , () => { 51const plan = { title :'Pagoda' , steps :[{ text :'Look' , detail :'' }, { text :'Write it' , detail :'one file' }, { text :'Check' , detail :'' }] }; 52const o = stepObjective ( plan , 1 ); 53assert . match ( o , / step 2 of 3: Write it \(one file\) / ); assert . match ( o , / Already done: 1\. Look / ); assert . match ( o , / do not start them / ); 54assert . ok ( ! /do not start / . test ( stepObjective ( plan , 2 ))); 55assert . equal ( stepLine ( 1 , 4 , 'Build the roof' , 'done' ), 'Build the roof — done' ); 56assert . equal ( stepLine ( 0 , 4 , 'x' , 'failed' ), "x — didn't finish" ); 57assert . equal ( firstSentence ( '## Done\n\nThe roof is built and tiled. Then I checked it.' ), 'The roof is built and tiled.' ); 58assert . equal ( firstSentence ( ' ' ), null ); 59}); 60 61test ( 'a step summary says what it changed, and is empty rather than wrong' , () => { 62assert . equal ( stepSummary ([{ verb :'Wrote a file' , target :'C:\\work\\pagoda.html' }, { verb :'Changed a file' , target :'style.css' }, { verb :'Read a file' , target :'a.txt' }, { verb :'Wrote a file' , target :'pagoda.html' }], 'Done.' ), 'Wrote pagoda.html, Changed style.css' ); 63assert . equal ( stepSummary ([], 'The roof is tiled and checked. Next I will glow.' ), 'The roof is tiled and checked.' ); 64assert . equal ( stepSummary ([], 'Not done yet (these are the remaining steps, which will be handled separately): the CSS.' ), null ); 65assert . equal ( stepSummary ([], '' ), null ); 66}); 67 68test ( 'an edit keeps order, drops empty lines and refuses an empty plan' , () => { 69const steps = editedSteps ([ ' one ' , '' , { text :'two' , detail :'why' }, { text :' ' }]); 70assert . deepEqual ( steps . map ( s => [ s . id , s . text , s . detail ]), [[ 's1' , 'one' , '' ], [ 's2' , 'two' , 'why' ]]); 71assert . throws (() => editedSteps ([]), / at least one step / ); assert . throws (() => editedSteps ( 'x' ), / list of steps / ); 72assert . match ( planAsPrompt ({ title :'T' , steps}), / 1\. one\n2\. two \(why\) / ); 73assert . match ( planPrompt ( 'do x' ), / Request: do x / ); 74}); 75 76test ( 'plans survive a restart' , () => { 77const dir = mkdtempSync ( join ( tmpdir (), 'strata-plans-' )); 78try { 79const file = join ( dir , 'plans.json' ); 80const a = new PlanStore ( file ); 81const plan = a . create ( 'chat-1' , { title :'T' , steps :editedSteps ([ 'a' , 'b' ]), request :'r' }); 82a . update ( 'chat-1' , p => { p . state = 'approved' ; }); 83const b = new PlanStore ( file ); 84assert . equal ( b . get ( 'chat-1' ). id , plan . id ); assert . equal ( b . get ( 'chat-1' ). state , 'approved' ); 85b . clear ( 'chat-1' ); assert . equal ( new PlanStore ( file ). get ( 'chat-1' ), null ); 86} finally { rmSync ( dir , { recursive :true , force :true }); } 87}); 88 89import { parseSources , researchPrompt , researchWritePrompt } from '../server/agent/plan.mjs' ; 90 91test ( 'research: the answer shape is asked for, and the sources are read back as [{n, title, url}]' , () => { 92assert . ok ( researchPrompt ( 'heat pumps' ). includes ( 'web_search' ) && researchPrompt ( 'heat pumps' ). includes ( 'web_fetch' )); 93assert . ok ( researchWritePrompt ( 'heat pumps' ). includes ( '## Sources' ) && researchWritePrompt ( 'heat pumps' ). includes ( '## Summary' )); 94const answer = [ 95'## Summary' , 'Heat pumps move heat.' , '## Key findings' , '- They are efficient [1].' , '- They cost more up front [2].' , 96'## Sources' , 97'1. Heat pump basics - https://energy.example.org/heat-pumps' , 98'2. [Costs explained](https://costs.example.com/page?x=1).' , 99'3. https://plain.example.net/a' , 100'4. Heat pump basics - https://energy.example.org/heat-pumps' , 101]. join ( '\n' ); 102assert . deepEqual ( parseSources ( answer ), [ 103{ n :1 , title :'Heat pump basics' , url :'https://energy.example.org/heat-pumps' }, 104{ n :2 , title :'Costs explained' , url :'https://costs.example.com/page?x=1' }, 105{ n :3 , title :'plain.example.net' , url :'https://plain.example.net/a' }, 106]); 107assert . deepEqual ( parseSources ( 'No sources section here, see https://x.example' ), [{ n :1 , title :'x.example' , url :'https://x.example' }], 'without a Sources section the linked pages are listed' ); 108assert . deepEqual ( parseSources ( 'Nothing to link.' ), []); 109});