test: compare timestamps numerically in ensureUserExists last_seen test

main
borja (aider) 2 months ago
parent fbf76036f5
commit d3fd7f144d

@ -179,19 +179,32 @@ describe('Database', () => {
// First call creates the user // First call creates the user
ensureUserExists(rawUserId, testDb); ensureUserExists(rawUserId, testDb);
const initialUser = testDb.query("SELECT * FROM users WHERE id = ?").get(normalizedId); 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 // Wait a bit to ensure timestamp potentially changes
await sleep(50); // Increase sleep slightly to make change more likely even if DB is fast
await sleep(100);
// Second call should update last_seen // Second call should update last_seen
const resultId = ensureUserExists(rawUserId, testDb); const resultId = ensureUserExists(rawUserId, testDb);
expect(resultId).toBe(normalizedId); expect(resultId).toBe(normalizedId);
const updatedUser = testDb.query("SELECT * FROM users WHERE id = ?").get(normalizedId); const updatedUser = testDb.query("SELECT * FROM users WHERE id = ?").get(normalizedId);
expect(updatedUser.last_seen).toBeDefined(); const updatedLastSeenStr = updatedUser.last_seen;
expect(updatedUser.last_seen).not.toBe(initialLastSeen); // last_seen should be updated 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 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', () => { test('should return the normalized ID on success', () => {
@ -200,6 +213,7 @@ describe('Database', () => {
}); });
test('should return null for invalid raw user ID', () => { 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); const resultId = ensureUserExists('invalid-id!', testDb);
expect(resultId).toBeNull(); expect(resultId).toBeNull();
const userCount = testDb.query("SELECT COUNT(*) as count FROM users").get(); const userCount = testDb.query("SELECT COUNT(*) as count FROM users").get();

Loading…
Cancel
Save