test(session-projection-cache): drive interval deterministically

This commit is contained in:
imccyu
2026-08-26 14:52:54 +08:00
parent b8360cac53
commit 459919d21d
@@ -196,13 +196,16 @@ describe('SessionProjectionCache write policy', () => {
}) })
it('flushes on the configured interval when the count threshold is not reached', async () => { it('flushes on the configured interval when the count threshold is not reached', async () => {
const { ctx, root } = await harness({ config: { writeEveryEvents: 100, writeIntervalMs: 20 } }) const { ctx, cache } = await harness({ config: { writeEveryEvents: 100, writeIntervalMs: 20 } })
const write = vi.spyOn(cache, 'write').mockResolvedValue()
vi.useFakeTimers()
const session = ctx.sessions.create(SessionId('interval')) const session = ctx.sessions.create(SessionId('interval'))
write.mockClear()
mark(session, ['slow']) mark(session, ['slow'])
await new Promise(resolve => setTimeout(resolve, 10)) // before the interval await vi.advanceTimersByTimeAsync(19)
expect((await storedRows(root, session.id))?.['cache-test/marks']?.seq).toBe(-1) // still the creation cut expect(write).not.toHaveBeenCalled()
await settle() // past the interval; the fire-and-forget write lands await vi.advanceTimersByTimeAsync(1)
expect((await storedRows(root, session.id))?.['cache-test/marks']?.val).toEqual({ marks: ['slow'] }) expect(write).toHaveBeenCalledExactlyOnceWith(session)
}) })
it('write() on a never-dirty session checkpoints directly and rejects a non-JSON unit state', async () => { it('write() on a never-dirty session checkpoints directly and rejects a non-JSON unit state', async () => {