test(compaction): control the manual duration clock

This commit is contained in:
_Kerman committed 2026-10-23 17:59:33 +08:00
1 parent db2b8122f2
commit 7ee39638b2
1 file changed
+15 -9
@@ -880,17 +880,23 @@ describe('compactNow transaction and failure classification', () => {
const { compact } = detachedService()
const session = closedConversation(2)
const agent = fakeAgent(session, () => () => undefined)
compact.gate = new Promise<undefined>((resolve) => {
setTimeout(() => { resolve(undefined) }, 5)
})
const openedAt = Date.now()
const originalNow = Date.now
const clock = vi.spyOn(Date, 'now').mockReturnValue(openedAt)
try {
compact.duringSummary = () => { clock.mockReturnValue(openedAt + 25) }
await compact.compactNow(agent, SIGNAL)
await compact.compactNow(agent, SIGNAL)
const start = session.snapshotEvents().findLast(event => event.type === 'compaction/start')
const end = session.snapshotEvents().findLast(event => event.type === 'compaction/end')
expect(start).toBeDefined()
expect(end).toBeDefined()
expect(end!.time - start!.time).toBeGreaterThan(0)
const start = session.snapshotEvents().findLast(event => event.type === 'compaction/start')
const end = session.snapshotEvents().findLast(event => event.type === 'compaction/end')
expect(start?.time).toBe(openedAt)
expect(end?.time).toBe(openedAt + 25)
expect(end!.time - start!.time).toBe(25)
} finally {
clock.mockRestore()
}
expect(Date.now).toBe(originalNow)
})
it('excludes concurrent automatic and manual compaction of one session', async () => {