okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8667a3dbf
main
1// /loop stops by itself: a count or a time said in words is read when the loop is created and enforced by the scheduler, and an 2// answer that says "done" / "stopped" ends it. The model is never left to keep replying "stopped" every minute. 3import test from 'node:test' ; 4import assert from 'node:assert/strict' ; 5import { build } from 'esbuild' ; 6 7const bundled = await build ({ stdin :{ contents :"export * from './src/lib/loop'; export * from './src/lib/loop-runner';" , resolveDir :process . cwd (), loader :'ts' }, bundle :true , write :false , format :'esm' , platform :'node' , loader :{ '.json' :'json' } }); 8const { parseLoop, readLimits, replySaysDone, judgeAnswer, withStopRule, LoopScheduler, LOOP_MAX_FAILURES , LOOP_MAX_RUNS } = await import ( `data:text/javascript;base64, ${ Buffer . from ( bundled . outputFiles [ 0 ]. text ). toString ( 'base64' )} ` ); 9 10const start = text => { const c = parseLoop ( text ); assert . equal ( c ?. kind , 'start' , text ); return c ; }; 11 12test ( 'a count in words or digits is read when the loop is created, and left out of the message' , () => { 13for ( const [ line , runs , prompt ] of [ 14[ '/loop 1m say hi, stop after 3 answers' , 3 , 'say hi' ], 15[ '/loop 1m say hi and stop after three replies' , 3 , 'say hi' ], 16[ '/loop 5m say hi. Stop the loop after 2 runs.' , 2 , 'say hi' ], 17[ '/loop 5m check the build, only 4 times' , 4 , 'check the build' ], 18[ '/loop 5m ping 3 times' , 3 , 'ping' ], 19[ '/loop every 2 minutes say hi for 5 answers' , 5 , 'say hi' ], 20[ '/loop 1m report the time, up to 6 times' , 6 , 'report the time' ], 21[ '/loop 1m say hi then stop after 1 answer' , 1 , 'say hi' ], 22[ '/loop 1m say hi, end after the 3rd run' , 3 , 'say hi' ], 23]) { 24const c = start ( line ); 25assert . equal ( c . maxRuns , runs , line ); 26assert . equal ( c . prompt , prompt , line ); 27} 28}); 29 30test ( 'a time limit and a stop condition in words are read too' , () => { 31let c = start ( '/loop 10m check the build for the next 2 hours' ); 32assert . equal ( c . maxMs , 2 * 3_600_000 ); assert . equal ( c . prompt , 'check the build' ); 33c = start ( '/loop 5m check the build, stop after 30 minutes' ); 34assert . equal ( c . maxMs , 30 * 60_000 ); assert . equal ( c . prompt , 'check the build' ); 35c = start ( '/loop 2m check the build, stop when it passes' ); 36assert . equal ( c . stopWhen , 'it passes' ); assert . equal ( c . prompt , 'check the build' ); assert . equal ( c . maxRuns , undefined ); 37c = start ( '/loop 2m look for the file until it exists' ); 38assert . equal ( c . stopWhen , 'it exists' ); assert . equal ( c . prompt , 'look for the file' ); 39}); 40 41test ( 'an ordinary message is not mistaken for a limit, and a limit with nothing to do is explained' , () => { 42for ( const line of [ '/loop 10m summarise my inbox notes' , '/loop 5m say hi' , '/loop 10m check whether the build passes' , '/loop 5m run the tests faster than last time' ]) { 43const c = start ( line ); 44assert . equal ( c . maxRuns , undefined , line ); assert . equal ( c . maxMs , undefined , line ); assert . equal ( c . stopWhen , undefined , line ); 45assert . equal ( c . prompt , line . replace ( / ^\/loop \S + / , '' )); 46} 47assert . equal ( parseLoop ( '/loop 5m stop after 3 answers' ). kind , 'help' ); 48assert . match ( parseLoop ( '/loop 5m stop after 3 answers' ). problem , / what to do / ); 49assert . equal ( readLimits ( 'say hi' ). prompt , 'say hi' ); 50assert . equal ( start ( '/loop 5m say hi, stop after 500 answers' ). maxRuns , LOOP_MAX_RUNS , 'the count is capped like the loop' ); 51}); 52 53test ( 'a reply that says done or stopped ends the loop, a normal reply does not' , () => { 54for ( const reply of [ 'stopped' , 'Stopped.' , 'Done' , 'DONE the build passes' , 'LOOP DONE' , 'The loop is stopped.' ]) assert . equal ( replySaysDone ( reply , true ), true , reply ); 55for ( const reply of [ 'stopped' , 'Done.' , 'DONE: it passes' , 'LOOP STOPPED' ]) assert . equal ( replySaysDone ( reply , false ), true , reply + ' (nothing stated)' ); 56for ( const reply of [ 'The build is done and the tests pass: 41 of 41.' , 'Hi! Nice to see you.' , 'I checked the folder and found nothing new.' , '' ]) assert . equal ( replySaysDone ( reply , true ), false , reply ); 57// Without a stated limit only the bare words count: "Done. The deploy finished" could be a normal answer. 58assert . equal ( replySaysDone ( 'Done. The build passes.' , false ), false ); 59assert . equal ( replySaysDone ( 'Done. The build passes.' , true ), true ); 60assert . match ( withStopRule ( 'check the build' , 'it passes' ), / begin your reply with the word DONE / ); 61assert . equal ( withStopRule ( 'check the build' ), 'check the build' ); 62}); 63 64test ( 'judgeAnswer: count, time, failures and the said-done rule, in that situation order' , () => { 65const p = ( answers , failures = 0 , startedAt = 0 ) => ({ answers, failures, startedAt}); 66assert . equal ( judgeAnswer ({ maxRuns :3 }, p ( 2 ), { text :'hi' , failed :false }, 0 ), null ); 67assert . equal ( judgeAnswer ({ maxRuns :3 }, p ( 3 ), { text :'hi' , failed :false }, 0 ), 'count' ); 68assert . equal ( judgeAnswer ({ maxMs :1000 }, p ( 1 ), { text :'hi' , failed :false }, 999 ), null ); 69assert . equal ( judgeAnswer ({ maxMs :1000 }, p ( 1 ), { text :'hi' , failed :false }, 1000 ), 'time' ); 70assert . equal ( judgeAnswer ({}, p ( 1 , LOOP_MAX_FAILURES - 1 ), { text :'' , failed :true }, 0 ), null ); 71assert . equal ( judgeAnswer ({}, p ( 1 , LOOP_MAX_FAILURES ), { text :'' , failed :true }, 0 ), 'failures' ); 72assert . equal ( judgeAnswer ({}, p ( 1 ), { text :'stopped' , failed :false }, 0 ), 'said-done' ); 73assert . equal ( judgeAnswer ({}, p ( LOOP_MAX_RUNS ), { text :'hi' , failed :false }, 0 ), 'cap' ); 74}); 75 76/** A scheduler on a fake clock with a scripted model. `reply(n, prompt)` is the model's n-th answer. */ 77function harness ( reply , { ready= () => true } = {}) { 78const sent = []; const notes = []; const timers = new Map (); let t = 0 , nextTimer = 1 ; 79const host = { 80async send ( _id , message ) { 81sent . push ( message ); 82const r = await reply ( sent . length , message . text ); 83return r === null ?{ answered :false , text :'' , failed :false } :typeof r === 'string' ?{ answered :true , text :r , failed :false } :{ answered :true , ...r }; 84}, 85note :( _id , text ) => notes . push ( text ), 86exists :() => true , 87 ready, 88now :() => t , 89setTimer :( fn , ms ) => { const id = nextTimer ++ ; timers . set ( id , { fn, at :t + ms }); return id ; }, 90clearTimer :id => timers . delete ( id ), 91}; 92const scheduler = new LoopScheduler ( host ); 93/** Move the clock to the next timer and run it; false when nothing is waiting. */ 94const advance = async () => { 95const next = [ ...timers . entries ()]. sort (( a , b ) => a [ 1 ]. at - b [ 1 ]. at )[ 0 ]; 96if ( ! next ) return false ; 97timers . delete ( next [ 0 ]); t = Math . max ( t , next [ 1 ]. at ); next [ 1 ]. fn (); 98for ( let i = 0 ; i < 20 ; i ++ ) await Promise . resolve (); 99await new Promise ( r => setImmediate ( r )); 100return true ; 101}; 102const drain = async ( max = 400 ) => { let n = 0 ; while ( n < max && await advance ()) n ++ ; return n ; }; 103return { scheduler, sent, notes, timers, advance, drain, clock :() => t }; 104} 105 106test ( '"stop after 3 answers" stops the scheduler after the third answer and nothing is sent again' , async () => { 107const h = harness (() => 'Hello!' ); 108const c = start ( '/loop 1m say hi, stop after 3 answers' ); 109await h . scheduler . start ( 'chat' , c ); 110await h . drain (); 111assert . equal ( h . sent . length , 3 ); 112assert . equal ( h . sent [ 0 ]. text , 'say hi' , 'the model is not told about the stop rule' ); 113assert . equal ( h . timers . size , 0 , 'no timer is left' ); 114assert . equal ( h . scheduler . isRunning ( 'chat' ), false ); 115assert . match ( h . notes . at ( - 1 ), / The loop is done: it ran 3 times / ); 116assert . equal ( h . notes . filter ( n => / stops by itself after 3 answers / . test ( n )). length , 1 , 'the start note says how it ends' ); 117}); 118 119test ( 'the owner case: a model that answers "stopped" every minute is cut off at once' , async () => { 120// With a count in the line the scheduler stops at 3 by itself, and a "stopped" answer before that also ends it. 121const h = harness ( n => ( n < 2 ?'Hi!' :'stopped' )); 122await h . scheduler . start ( 'chat' , start ( '/loop 1m say hi, stop after 3 answers' )); 123await h . drain (); 124assert . equal ( h . sent . length , 2 , 'the "stopped" answer ended the loop' ); 125assert . match ( h . notes . at ( - 1 ), / last answer said the work is done / ); 126// No limit in the line at all: a bare "Stopped." still ends it, instead of 100 minutes of "stopped". 127const g = harness ( n => ( n < 3 ?'Still going.' :'Stopped.' )); 128await g . scheduler . start ( 'chat' , start ( '/loop 1m keep an eye on the queue' )); 129await g . drain (); 130assert . equal ( g . sent . length , 3 ); assert . equal ( g . timers . size , 0 ); 131}); 132 133test ( 'a stop condition in words: the model is told how to say it is met, and its DONE ends the loop' , async () => { 134const h = harness ( n => ( n < 4 ?'Not yet: 2 tests fail.' :'DONE all tests pass.' )); 135await h . scheduler . start ( 'chat' , start ( '/loop 2m run the tests, stop when they all pass' )); 136await h . drain (); 137assert . equal ( h . sent . length , 4 ); 138assert . match ( h . sent [ 0 ]. text , / begin your reply with the word DONE / ); 139assert . equal ( h . sent [ 0 ]. shown , 'run the tests' , 'the chat shows the message without the rule' ); 140}); 141 142test ( 'a time limit ends the loop by the clock, between answers' , async () => { 143const h = harness (() => 'ok' ); 144await h . scheduler . start ( 'chat' , start ( '/loop 10m check the queue for the next 35 minutes' )); 145await h . drain (); 146// answers at 0, 10, 20, 30 minutes; the timer at 40 finds the 35 minutes over 147assert . equal ( h . sent . length , 4 ); 148assert . match ( h . notes . at ( - 1 ), / time you set is up / ); 149assert . equal ( h . timers . size , 0 ); 150}); 151 152test ( 'a busy chat waits, and does not use up the count' , async () => { 153let busy = true ; 154const h = harness (() => 'ok' , { ready :() => ! busy }); 155await h . scheduler . start ( 'chat' , start ( '/loop 1m ping, stop after 2 answers' )); 156assert . equal ( h . sent . length , 1 ); 157await h . advance (); // 1 minute: busy, retried in 15 s 158await h . advance (); await h . advance (); 159assert . equal ( h . sent . length , 1 , 'nothing is sent while the chat is busy' ); 160busy = false ; 161await h . drain (); 162assert . equal ( h . sent . length , 2 ); 163assert . equal ( h . scheduler . isRunning ( 'chat' ), false ); 164}); 165 166test ( 'three failed answers in a row end the loop, one failure does not' , async () => { 167const h = harness ( n => ( n === 2 ?{ text :'' , failed :true } :'ok' )); 168await h . scheduler . start ( 'chat' , start ( '/loop 1m check something, stop after 4 answers' )); 169await h . drain (); 170assert . equal ( h . sent . length , 5 , 'the failed run is not an answer, so 4 good answers took 5 sends' ); 171const bad = harness (() => ({ text :'' , failed :true })); 172await bad . scheduler . start ( 'chat' , start ( '/loop 1m check something' )); 173await bad . drain (); 174assert . equal ( bad . sent . length , LOOP_MAX_FAILURES ); 175assert . match ( bad . notes . at ( - 1 ), / did not work / ); 176}); 177 178test ( '/loop stop while an answer is coming means nothing more is sent' , async () => { 179let release ; 180const h = harness (() => new Promise ( resolve => { release = () => resolve ( 'ok' ); })); 181const started = h . scheduler . start ( 'chat' , start ( '/loop 1m ping' )); 182await Promise . resolve (); 183assert . equal ( h . scheduler . stop ( 'chat' ), true ); 184release (); await started ; 185assert . equal ( h . timers . size , 0 ); 186assert . equal ( h . scheduler . isRunning ( 'chat' ), false ); 187assert . equal ( await h . advance (), false ); 188assert . equal ( h . sent . length , 1 ); 189}); 190 191test ( 'a second /loop in the same chat is refused with one plain sentence; a loop with no limit still runs on' , async () => { 192const h = harness (() => 'ok' ); 193await h . scheduler . start ( 'chat' , start ( '/loop 1m ping' )); 194assert . equal ( await h . scheduler . start ( 'chat' , start ( '/loop 1m other' )), false ); 195assert . match ( h . notes . at ( - 1 ), / already has a loop / ); 196for ( let i = 0 ; i < 5 ; i ++ ) await h . advance (); 197assert . equal ( h . sent . length , 6 , 'no limit: it keeps going until the person stops it' ); 198assert . equal ( h . scheduler . stop ( 'chat' ), true ); 199});