Skip to content

test(cases): keep the narrative staleness tests out of one millisecond - #102

Open
Jihair54 wants to merge 1 commit into
mainfrom
test/narrative-stale-ms-rounding
Open

Jihair54 wants to merge 1 commit into
mainfrom
test/narrative-stale-ms-rounding

Conversation

@Jihair54

Copy link
Copy Markdown
Contributor

tests/integration/cases-detail.test.ts › "does not mark the narrative stale when no detection changed after generation" fails about one run in three against a local PostgreSQL 15 (docker-compose.test.yml). I saw it while running the full suite for #101: it failed 1 run in 3 on main's code as well.

Cause. The test creates a detection, then a narrative with generatedAt: new Date(), and the API says stale when updatedAt > generatedAt.

  • Detection.updatedAt is written with sub-millisecond precision, and PostgreSQL rounds it to timestamp(3), up as often as down: select '2026-01-01 00:00:00.0006'::timestamp(3) gives .001.
  • new Date() truncates to the millisecond.

So when both fall in the same millisecond, updatedAt can come out 1 ms after a generatedAt taken later. A probe that created 20 detections saw the stored updatedAt 1 ms past a new Date() taken after the insert returned in 3 of them. The sibling test ("stale when a detection was attached after generation") can fail the other way, when the rounding goes down.

Change. Test only. Both staleness tests put generatedAt one second away from the detection writes, on the side each test means. The constant (NARRATIVE_CLOCK_MARGIN_MS) explains why. Production is unaffected: a narrative generated in the same millisecond as a detection write may at worst show as stale.

Tested. cases-detail.test.ts passed 10 runs out of 10 locally (9 tests each), against PostgreSQL 15. tsc and eslint report nothing on the file.

🤖 Generated with Claude Code

"does not mark the narrative stale when no detection changed after
generation" failed about one run in three against a local database:
the detection's updatedAt is stamped with sub-millisecond precision and
PostgreSQL rounds it to timestamp(3), up as often as down, while the
test's `generatedAt: new Date()` truncates. Taken in the same
millisecond, updatedAt came out 1 ms after it, so `stale` was true.

Both staleness tests now put generatedAt a second away from the
detection writes, on the side each one means. 10 runs out of 10 pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Jihair54 <[email protected]>
@Jihair54
Jihair54 requested a review from SoFrexs November 26, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant