git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/answers.test.ts
import {expect, test} from 'bun:test'
import {answerTo, answersOf} from '@/lib/answers'
import type {LogItem} from '@/lib/stream'
const item = (n: number, kind: LogItem['kind'], request: string): LogItem => ({n, at: 0, kind, request})
test('an answer from before a restart is not this request\'s', () => {
// Request 2 asked and answered; the agent restarts; it asks again as request 2.
const log = [item(10, 'permission', '2'), item(11, 'permission_answered', '2'), item(20, 'question', '2')]
expect(answerTo(log, log[0])?.n).toBe(11)
expect(answerTo(log, log[2])).toBeUndefined()
const answered = [...log, item(21, 'answered', '2')]
expect(answerTo(answered, answered[2])?.n).toBe(21)
})
test('the answers found in one pass are those found one by one', () => {
const log = [
item(1, 'permission', '1'), item(2, 'permission_answered', '1'),
item(3, 'question', '2'), item(4, 'permission', '2'), item(5, 'answered', '2'),
item(6, 'permission', '1'), item(7, 'question', '3'),
]
const all = answersOf(log)
for (const it of log.filter((x) => x.kind === 'permission' || x.kind === 'question')) expect(all.get(it)).toBe(answerTo(log, it))
})