Repository navigation
Worker tests: hold D417 and D124 to the clock the code reads (#1342) - #1378
Merged
Merged
Conversation
entity_obligations_d417.test.ts fixed TODAY when the file loaded, while
legal_entities.ts reads new Date() on every request, so a run that crossed
midnight UTC got days_left off by one. Each test now freezes the JS Date at
the real instant it starts (node:test mock.timers, Date only), and today()
and plusDays() read that clock when called.
expiry_gate_datetime_d124.test.ts pins its spent row to SQLite's date when
the row is written; the gate reads datetime('now') when it runs. Across
midnight the row is on yesterday's date, where even the broken bare
comparison is right, so the behavioural tests stopped catching it. Each now
runs through onOneUtcDay, which re-reads SQLite's date after the last
assertion and runs a body that straddled midnight once more.
Test files only. No route change, no decision, no migration.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VCuRYkf1et4rs1gQ9QWFwR
|
Preview: https://studioos-pr-1378.guillaumelauzier.workers.dev (built from 4266a22) The pull request's SPA build on a Worker with no bindings: pages and deep links work, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Two Worker test files went wrong when a run crossed midnight UTC: D417 failed and D124 stopped catching the bug it guards. PR #1329's
test:drift(23:58 to 00:02 UTC) failed on D417. This PR holds each file to the clock its code reads. Test files only: no route change, no decision, no migration.Closes #1342
Implementation
entity_obligations_d417.test.ts.TODAYwas a module constant, fixed when the file loaded, whilelegal_entities.tsreadsnew Date()on every request.beforeEachnow freezes the JSDateat the real instant each test starts (mock.timers.enable({ apis: ['Date'] })), and anafterEachresets it.today()andplusDays()read that clock when called.today()agree, even if midnight passes during a test.Dateis mocked; timers and SQLite's clock are untouched. Every assertion is unchanged.expiry_gate_datetime_d124.test.ts.expiredIsopins the spent row to SQLite's date when the row is written; the gate readsdatetime('now')when it runs.onOneUtcDay. It records each expired fixture's day, re-reads SQLite's date after the last assertion, and runs a body that straddled midnight once more on the new day._timeFixture.mjsis not touched; it is outside this task's files.Files changed
cloudflare-worker/test/entity_obligations_d417.test.tscloudflare-worker/test/expiry_gate_datetime_d124.test.tsTesting
npm run build, thennpm run test:drift > drift.log 2>&1; echo EXIT=$?→ EXIT=0 (Worker 5,994, frontend 5,151, retention 127).faketime(libfaketime), which moves both the JS clock and SQLite's.D417, started at 23:59:50 to 23:59:59 UTC at ×10 speed, so midnight falls between the file's tests:
days_left,resolved_on,as_of);TZ=UTC faketime -f '@2026-10-07 23:59:55 x10' node --experimental-strip-types --no-warnings \ --import ./cloudflare-worker/test/_ts-loader.mjs --test cloudflare-worker/test/entity_obligations_d417.test.tsD124, started at 23:59:59 UTC. A preload shim holds the first expired-fixture read for 1.2 s, so the first test writes its spent row on 10-07 and runs the gate on 10-08 (the shim logs
fixture read 2026-10-07 23:59:59, now 2026-10-08 00:00:00).expires_at > datetime('now')in the magic-link gate)The shim
Run with
TZ=UTC faketime -f '@2026-10-07 23:59:59' node … --import ./midnight-shim.mjs --test …d124.test.ts.daysUntiloff by one: 5 tests fail, at the real time and under the ×10 shim at 23:59:55;today()reading yesterday: 4 tests fail.Risks
mock.timerswithDateis in Node 22 (the CI version). It mocksDateonly during each D417 test, and resets after.Dependencies
Agent
S10 · Claude Code
Review requested
Any agent.
🤖 Generated with Claude Code
https://claude.ai/code/session_01VCuRYkf1et4rs1gQ9QWFwR
Generated by Claude Code