| 16 | } |
| 17 | |
| 18 | function harness(answers: () => boolean = () => true) { |
| 19 | const posts: { payload: UiLogPayload; final: boolean }[] = []; |
| 20 | const timers: Timer[] = []; |
| 21 | const transport: UiLogTransport = { |
| 22 | send: async (payload, final) => { |
| 23 | posts.push({ payload, final }); |
| 24 | return answers(); |
| 25 | }, |
| 26 | }; |
| 27 | let tick = 0; |
| 28 | const buffer = new UiLogBuffer({ |
| 29 | page: '/?project=demo', |
| 30 | session: 'abc', |
| 31 | transport, |
| 32 | now: () => new Date(Date.UTC(2026, 8, 8, 10, 0, tick++)), |
| 33 | schedule: (fn, ms) => { |
| 34 | const timer: Timer = { fn, ms, cancelled: false }; |
| 35 | timers.push(timer); |
| 36 | return timer; |
| 37 | }, |
| 38 | cancel: (handle) => { |
| 39 | (handle as Timer).cancelled = true; |
| 40 | }, |
| 41 | bufferMax: 5, |
| 42 | batchMax: 3, |
| 43 | }); |
| 44 | /** Fire the newest live timer, as the browser would when its time comes. */ |
| 45 | const fire = async (): Promise<void> => { |
| 46 | const live = timers.filter((timer) => !timer.cancelled); |
| 47 | const timer = live[live.length - 1]; |
| 48 | if (timer === undefined) { |
| 49 | throw new Error('no timer armed'); |
| 50 | } |
| 51 | timer.cancelled = true; |
| 52 | timer.fn(); |
| 53 | await Promise.resolve(); |
| 54 | await Promise.resolve(); |
| 55 | }; |
| 56 | return { buffer, posts, timers, fire }; |
| 57 | } |
| 58 | |
| 59 | describe('UiLogBuffer', () => { |
| 60 | it('preserves the recorded project across project changes and failed batch retries', async () => { |