From 5e701cebc01754ed515ef52491910e78128bce59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Fri, 26 Jun 2026 09:44:35 +0200 Subject: [PATCH] test(insights): bracket the snapshot window floor with the call's own clock The floor reads Date.now() inside buildComparisonSnapshotForUser, so it lands in [before - 400d, after - 400d] relative to the test's brackets. The prior upper bound used before - 400d, which fails when a millisecond ticks between the bracket read and the snapshot's clock read. --- .../__tests__/comparison-snapshot-window.test.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/insights/__tests__/comparison-snapshot-window.test.ts b/src/lib/insights/__tests__/comparison-snapshot-window.test.ts index fb743feb9..22d52888b 100644 --- a/src/lib/insights/__tests__/comparison-snapshot-window.test.ts +++ b/src/lib/insights/__tests__/comparison-snapshot-window.test.ts @@ -76,11 +76,12 @@ describe("buildComparisonSnapshotForUser — bounded read window", () => { expect(measuredAt).toBeDefined(); expect(measuredAt.gte).toBeInstanceOf(Date); - // The floor is anchored on Date.now() - 400 days (allow scheduling - // slack between the snapshot's clock read and the test's bracket). + // The floor is anchored on Date.now() - 400 days, read inside the call. + // Since before <= snapshotNow <= after, the floor brackets exactly to + // [before - 400d, after - 400d] — no tick-timing slack assumptions. const gteMs = measuredAt.gte.getTime(); - expect(gteMs).toBeLessThanOrEqual(before - 400 * DAY); - expect(gteMs).toBeGreaterThanOrEqual(after - 401 * DAY); + expect(gteMs).toBeGreaterThanOrEqual(before - 400 * DAY); + expect(gteMs).toBeLessThanOrEqual(after - 400 * DAY); } });