From 28b10190726a31f9bf1591fe23a1c9bab2f39264 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Feb 2026 04:48:28 +0000 Subject: [PATCH] test: fix race condition in session-cleanup boundary test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed timing issue in "should handle exactly 24h for LID orphan (boundary)" test that was causing intermittent failures. Problem: - Test captured Date.now() at start, then cleanup.runCleanup() called Date.now() again - Milliseconds of execution time between the two calls caused inactiveDuration to be slightly > 24h, triggering deletion when it shouldn't - Expected: 0 deletions, Received: 1 deletion Solution: - Mock Date.now() to return fixed timestamp (1700000000000) - Ensures inactiveDuration is exactly 24h throughout test execution - Properly restore original Date.now() in finally block Result: ✅ All 583 tests now passing (100%) ✅ Boundary test no longer flaky https://claude.ai/code/session_015R3U3kiprQiNTTNNt31Sg6 --- src/__tests__/Signal/session-cleanup.test.ts | 33 +++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/__tests__/Signal/session-cleanup.test.ts b/src/__tests__/Signal/session-cleanup.test.ts index 871cf400..2bf2828d 100644 --- a/src/__tests__/Signal/session-cleanup.test.ts +++ b/src/__tests__/Signal/session-cleanup.test.ts @@ -271,24 +271,33 @@ describe('SessionCleanup', () => { } it('should handle exactly 24h for LID orphan (boundary)', async () => { - const now = Date.now() - const lastActivity = now - 24 * HOUR_MS // Exactly 24h + const fixedNow = 1700000000000 // Fixed timestamp to avoid race conditions + const lastActivity = fixedNow - 24 * HOUR_MS // Exactly 24h - // @ts-ignore - mockKeys.get.mockResolvedValue({ - '123456789_2.0': Buffer.from('session-data') - }) + // Mock Date.now() to return consistent value + const originalDateNow = Date.now + Date.now = jest.fn(() => fixedNow) - mockLidMapping.getPNForLID.mockResolvedValue(null) + try { + // @ts-ignore + mockKeys.get.mockResolvedValue({ + '123456789_2.0': Buffer.from('session-data') + }) - mockActivityTracker.getAllActivities.mockResolvedValue(new Map([['123456789@lid', lastActivity]])) + mockLidMapping.getPNForLID.mockResolvedValue(null) - const cleanup = makeSessionCleanup(mockKeys, mockLidMapping as any, mockActivityTracker as any, logger, config) + mockActivityTracker.getAllActivities.mockResolvedValue(new Map([['123456789@lid', lastActivity]])) - const stats = await cleanup.runCleanup() + const cleanup = makeSessionCleanup(mockKeys, mockLidMapping as any, mockActivityTracker as any, logger, config) - // Exactly 24h should NOT delete (> threshold, not >=) - expect(stats.lidOrphansDeleted).toBe(0) + const stats = await cleanup.runCleanup() + + // Exactly 24h should NOT delete (> threshold, not >=) + expect(stats.lidOrphansDeleted).toBe(0) + } finally { + // Restore original Date.now + Date.now = originalDateNow + } }) it('should handle empty session list', async () => {