A test with a fake clock, written by your agent
By the end of this lesson you will have tests/alert-clock.test.ts, written by your agent from cases you wrote first, pinning the job to New York's calendar: 01:00 UTC belongs to the previous trading day, a Saturday run fetches nothing, 17:30 maps correctly in both seasons, and retry pauses are checked without waiting. It runs in well under a second, with no network.
What you will see: npx vitest run tests/alert-clock.test.ts listing test names that read like the lines in your PLAN.md, all green; and, after you swap one time zone for UTC, the same list with one red line naming the 01:00 case.
The idea: the job's bugs live in the calendar
The job now reads the clock to decide the trading date, whether a bar is new, whether a key was sent today, and how long to pause before a retry. Each can be wrong in a way that passes a test run on the day you write it:
| Instant, UTC | In New York | Trading date to use |
|---|---|---|
| 2026-07-14 21:30 | Tuesday 17:30, summer time | 2026-07-14 |
| 2026-01-14 22:30 | Wednesday 17:30, winter time | 2026-01-14 |
| 2026-01-15 01:00 | Wednesday 20:00 | 2026-01-14, though UTC says the 15th |
| 2026-01-17 15:00 | Saturday 10:00 | none: nothing to fetch |
The third row is the bug that ships. new Date().toISOString().slice(0, 10) is the UTC date: right at 17:30 New York, a day ahead at lesson 4's 20:30 catch-up run. That run then expects a bar that cannot exist yet and fetches every symbol to find nothing.
Two kinds of fake clock
Pass the time in. Build a Dashboard already asked for checkAll(rules, fetchQuotes, now): the test hands over an instant from the table, with nothing to mock or restore. Prefer this.
Freeze the system clock for code that reads Date itself. vi.setSystemTime(date) makes Date answer with that instant, even without fake timers; it is not reset between tests, so restore the real clock with vi.useRealTimers() in afterEach, as Vitest's own date example does.
The retry pause needs neither: lesson 4 passed the sleep function in, so the test passes vi.fn() and checks it was asked for one second, then two. Nothing waits.
You write the cases, the agent writes the tests
An agent turns cases into tests well and picks them badly. So write the cases first, in PLAN.md:
## Clock cases
- 01:00 UTC on 2026-01-15 is trading date 2026-01-14.
- 15:00 UTC on 2026-01-17 has no trading date, and the run fetches nothing.
- A run with every symbol already processed today spends 0 calls.
- A 5xx is retried after 1 second and then 2; a 402 is not retried.
Add the two 17:30 rows from the table too. Done means the tests are green set the bar; here green is half of it, and the other half is seeing each test go red once.
The prompt
In plan mode (Shift+Tab until it is on), paste:
Read the cases under "Clock cases" in PLAN.md.
- src/lib/alerts/clock.ts: tradingDate(now: Date): string | null, the New York date on a weekday and null on a weekend, using Intl with time zone America/New_York. Nothing else in src/lib/alerts may call new Date() or Date.now(), retry.ts included: remove any default that reads the clock. The runner uses tradingDate instead of any date logic of its own, and scripts/alert.ts reads the clock once and passes it down.
- tests/alert-clock.test.ts: one test per case, named after it, each setting its own instant. Where code reads Date itself, use vi.setSystemTime and restore with vi.useRealTimers() in afterEach.
- For the retry case, pass a vi.fn() as the sleep and assert the pauses; nothing may actually wait.
- Add no cases I did not write. If one is missing, say so and stop.
Run the new file, then the whole suite.
What the agent should do. Lesson 4's prompt already asked for the trading-date rule, so the logic usually exists by now: expect it moved into clock.ts as the one function every caller uses, rather than written anew. The new work is the test file, written from your cases, and call sites changed to take now; the runner from lesson 4 may change too, and with it an older test, if it still worked a weekend out on its own. If it proposes a date library, ask what Intl lacks here; usually nothing.
Check it yourself
npx vitest run tests/alert-clock.test.tsis green, and every test name matches a case.git grep -n -e "new Date()" -e "Date.now()" -- src/lib/alerts scriptsfinds onlyscripts/alert.ts.- In
clock.ts, replaceAmerica/New_YorkwithUTCand run the file. The 01:00 case must fail; if nothing fails, the tests do not test the time zone. Revert. - Ask the agent which of your cases would pass if the code ignored time zones entirely. Each one it names proves nothing about time zones.
npm testis green for the whole suite.
Break it on purpose
Add a test that calls tradingDate(new Date()) and expects a value. It passes on a weekday and fails on Saturday morning for no fault of your code: a test with a schedule. Delete it, and keep the rule that every test involving time sets the time.
Compare with ours
git fetch upstream, then git diff upstream/ref/runs-without-you/5 -- src/lib/alerts tests.
Our version, on branch ref/runs-without-you/5
const NEW_YORK = new Intl.DateTimeFormat("en-US", {
timeZone: "America/New_York", year: "numeric", month: "2-digit", day: "2-digit", weekday: "short",
});
export function tradingDate(now: Date): string | null {
const p = Object.fromEntries(NEW_YORK.formatToParts(now).map((x) => [x.type, x.value]));
return p.weekday === "Sat" || p.weekday === "Sun" ? null : `${p.year}-${p.month}-${p.day}`;
}
Each test reads like its case: expect(tradingDate(new Date("2026-01-15T01:00:00Z"))).toBe("2026-01-14").
Try it now
Checkpoint: the calendar is now tested.
- Commit:
npm test
git status # the files you expect, and PLAN.md and the alert files, and no .env file
git add -A
git commit -m "test(alert): pin the job to the New York calendar with fixed instants"
- Add a case of your own that the table does not cover, and have the agent write its test.
- Take the lesson check. It shows you an instant in UTC and asks which trading day it is.