33 lines
2.1 KiB
TypeScript
33 lines
2.1 KiB
TypeScript
/** A goal reminder fires once through the real automation scheduler and injected clock. */
|
|
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
|
|
import {fixture} from './goal-questions-fixture'
|
|
import {it,assert,until} from './goal-questions-test-support'
|
|
it('fires one goal reminder through automations',async()=>{
|
|
let now=Date.now();const timers=new Set<any>()
|
|
const f=await fixture({now:()=>now,setTimer:(callback:any,delay:number)=>{
|
|
if(delay<1000){const id=setTimeout(callback,delay);return()=>clearTimeout(id)}
|
|
const timer={at:now+delay,callback};timers.add(timer);return()=>timers.delete(timer)
|
|
}})
|
|
try {
|
|
const id=await f.mainAgent();const {harness}=f.opened
|
|
const goal=await import(/* @vite-ignore */ `${f.base}/goal/server.ts`)
|
|
const {Automations}=await import(/* @vite-ignore */ `${f.base}/automations/server.ts`)
|
|
await goal.actions.start({text:'Ship the goal'},{host:f.host})
|
|
const get=()=>harness.snapshot(goal.Goals,f.opened.root.id,context)
|
|
await until(async()=>Boolean((await get()).goals[0].reminder))
|
|
const reminder=(await get()).goals[0].reminder
|
|
const ledger=()=>harness.snapshot(Automations,context)
|
|
await until(async()=>{const a=(await ledger()).automations[reminder];return (await harness.getTask(a.scheduler,context)).state.status==='sleeping'})
|
|
const deadline=(await ledger()).automations[reminder].nextAt
|
|
assert(deadline-(await ledger()).automations[reminder].createdAt===1800000,'The reminder waits 30 minutes')
|
|
await until(()=>[...timers].some(timer=>timer.at===deadline))
|
|
now=deadline
|
|
for(const timer of [...timers])if(timer.at<=now){timers.delete(timer);timer.callback()}
|
|
await until(async()=>(await ledger()).automations[reminder].fires===1)
|
|
await until(async()=>(await f.entries(id)).some((e:any)=>e.kind==='goal.note'&&e.model[0].content.includes('This is the goal reminder')))
|
|
assert((await ledger()).automations[reminder].fires===1,'One reminder fire')
|
|
await goal.actions.end(null,{host:f.host})
|
|
await until(async()=>!(await ledger()).automations[reminder])
|
|
} finally {await f.opened.close()}
|
|
})
|