From d3fd7f144d6be28c30001ea5e140feb65b851dab Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Mon, 5 May 2025 15:10:06 +0200 Subject: [PATCH] test: compare timestamps numerically in ensureUserExists last_seen test --- tests/unit/db.test.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tests/unit/db.test.ts b/tests/unit/db.test.ts index 009ac6e..45fff44 100644 --- a/tests/unit/db.test.ts +++ b/tests/unit/db.test.ts @@ -179,19 +179,32 @@ describe('Database', () => { // First call creates the user ensureUserExists(rawUserId, testDb); const initialUser = testDb.query("SELECT * FROM users WHERE id = ?").get(normalizedId); - const initialLastSeen = initialUser.last_seen; + const initialLastSeenStr = initialUser.last_seen; + const initialLastSeenMs = Date.parse(initialLastSeenStr); // Convert to milliseconds - // Wait a bit to ensure timestamp changes - await sleep(50); + // Wait a bit to ensure timestamp potentially changes + // Increase sleep slightly to make change more likely even if DB is fast + await sleep(100); // Second call should update last_seen const resultId = ensureUserExists(rawUserId, testDb); expect(resultId).toBe(normalizedId); const updatedUser = testDb.query("SELECT * FROM users WHERE id = ?").get(normalizedId); - expect(updatedUser.last_seen).toBeDefined(); - expect(updatedUser.last_seen).not.toBe(initialLastSeen); // last_seen should be updated + const updatedLastSeenStr = updatedUser.last_seen; + const updatedLastSeenMs = Date.parse(updatedLastSeenStr); // Convert to milliseconds + + expect(updatedLastSeenStr).toBeDefined(); expect(updatedUser.first_seen).toBe(initialUser.first_seen); // first_seen should NOT be updated + + // Compare timestamps numerically (milliseconds since epoch) + // It should be greater than or equal to the initial one. + // Using '>' is safer if the sleep guarantees a change. + expect(updatedLastSeenMs).toBeGreaterThan(initialLastSeenMs); + + // Keep the string check just in case, but the numerical one is more reliable here + // This might still fail occasionally if the calls land in the exact same second. + // expect(updatedLastSeenStr).not.toBe(initialLastSeenStr); }); test('should return the normalized ID on success', () => { @@ -200,6 +213,7 @@ describe('Database', () => { }); test('should return null for invalid raw user ID', () => { + // Suppress console.error during this specific test if needed, or just check output const resultId = ensureUserExists('invalid-id!', testDb); expect(resultId).toBeNull(); const userCount = testDb.query("SELECT COUNT(*) as count FROM users").get();