·StoryRoll

The Test That Only Failed at Midnight

On October 21, 2026, a test audit flagged something odd in convex/dm.test.ts: the file might fail depending on what time you ran it. Not on which machine. Not on which Node version. On the time.

Here is the setup. StoryRoll gives free players a daily cap on Game Master turns, and that cap resets on the UTC date. The tests that guard it are the money path: what happens when a player hits the cap, what the teaser says, what happens when they unlock more turns and the story picks back up. If those break, someone either gets free turns forever or gets locked out after paying.

Those tests use fake timers, so the Game Master's scheduled work (narration, voice, memory refresh) can drain inside a single test. Every one of the fake-timer blocks called vi.useFakeTimers() and then did nothing else.

That is the bug. A fake clock with no set time starts at the real wall clock. Then runAllTimers fast-forwards it. So a test run that begins at 23:59:30 UTC starts the clock thirty seconds before midnight, the timers push it past 00:00, and the code under test suddenly believes it is a new day. The cap resets mid-test. Assertions about a capped player fail because, as far as the code can tell, the player is no longer capped.

Nobody saw it because nobody was running the suite at that moment. We reproduced it on purpose, with a throwaway setup file that shifts Date.now() to a chosen start time:

  • Real clock: 74 passed.
  • Forced to 23:59:30 UTC: 7 failed, 67 passed. Six of them in the unlock tests, one in the roll payoff test for an already capped player.
  • Forced to 00:00:30 UTC: 74 passed.

That last row is the sneaky one. Start after midnight and everything is fine. The window is the minute or so before it, once a day.

The fix, and the audit's miscount

The fix is boring on purpose. Each block now calls vi.setSystemTime(FIXED_NOW) right after useFakeTimers(), with FIXED_NOW set to noon UTC on January 15, 2026. Noon is as far from a day boundary as a clock gets. No assertion changed. No runtime code changed. It is one test file.

The audit counted five affected blocks. A grep found eight. We pinned all eight, then reran the 23:59:30 case, plus 23:59:59.5 for good measure, and got 74 of 74 each time.

One test is supposed to cross midnight: the one checking that a payoff arriving after UTC midnight still lands. It still does, but now deliberately. It jumps from noon on the 15th to 00:05 on the 16th, because that is the behavior it exists to check, and the clock no longer decides for it.

We also searched convex/ for hard-coded date cutoffs that a January pin could quietly route around. Found none.

What this cost, and what it didn't

Cost: eight small blocks and a one-off harness we did not commit. Savings: a failure that would have shown up as a red build at a random hour for whoever happened to push near midnight UTC, with no code change to blame. Those are the failures that eat an afternoon, because they vanish when you rerun them.

What it did not give us is confidence about the rest. The fake-clock trap is easy to fall into and invisible when you do: the test reads fine, passes fine, and only misbehaves in a one-minute window. This file had eight of them.

We have 233 test files. This was the one we audited.

So what else in this repo is only passing because nobody runs it at 23:59?

S

Written by StoryRoll

Founder of StoryRoll. Building AI-powered tabletop RPGs.

Share:Share on X

Related Posts