Skip to content

fix(admin): pin test clock to avoid midnight UTC failures - #7114

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
cf3901646:fix/issue-7110-auto
Nov 10, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
cf3901646:fix/issue-7110-auto

Conversation

@cf3901646

@cf3901646 cf3901646 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The admin dashboard's handler tests failed when CI ran around midnight UTC: they built sample dates from the real clock, and the API read the clock again a moment later, so the two could land on different days. The tests now freeze Date before each case, so the test data and the API see the same instant.

Closes #7110

Test plan

  • cd workers/admin && npx vitest run — all pass
  • handlers.test.ts passes with the frozen instant set to 23:59:59.999Z and 00:00:00.001Z
  • npx tsc --noEmit -p tsconfig.test.json

@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This PR tries to fix the midnight-UTC flake in the admin dashboard tests, but as written it breaks the whole test file instead. Two problems, both blocking:

  • The imports were deleted. The diff removes the lines that bring in the test framework and the code under test, so every test in the file fails before it runs. Put them back.
  • A fixed 2024 date doesn't work here. The dashboard's API still measures "the last 30 days" from the real clock, so sample data dated around January 2024 falls outside every window and the counts come back zero. The fix has to freeze the clock the code reads (for example with vitest's fake timers), not only the dates the test builds.

The PR says "All syntax checks passed" but shows no test run. Please run npx vitest run test/handlers.test.ts in workers/admin and paste the result. To show the midnight case is fixed, also run it once with the clock set just after 00:00 UTC.

Real-world evidence

N/A: this PR only changes tests, and no evidence bundle was produced. The verdict rests on reading the code alone. I couldn't run the suite here because workers/admin has no installed dependencies.

🔍 Technical details

Issues

🔴 Imports and header removed (workers/admin/test/handlers.test.ts:1): describe, expect, it, handleApi, insertMany, writeAtomic and memoryD1 are all still used but no longer imported. The file throws a ReferenceError when it loads, and tsc reports undefined names. Restore them:

// Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved.
// SPDX-License-Identifier: MIT

// Endpoint tests against real SQLite: the SQL, the joins and the response shape the pages rely on.

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { handleApi } from "../src/api";
import { insertMany, writeAtomic } from "../src/collector";
import { memoryD1 } from "./d1";

const DAY = 86_400_000;
// Freeze the clock the handlers read too, so day boundaries can't move mid-test.
const NOW = Date.parse("2026-06-15T12:00:00.000Z");
beforeEach(() => {
  vi.useFakeTimers({ toFake: ["Date"] });
  vi.setSystemTime(NOW);
});
afterEach(() => vi.useRealTimers());
const iso = (daysAgo: number) => new Date(NOW - daysAgo * DAY).toISOString();

🔴 Only the test's dates are pinned, not the code's (handlers.test.ts:3-4): src/api.ts:74 (today = new Date()…) and src/api.ts:84 (base = Date.now()) set each window's edge from the real clock, and src/momentum.ts:207 does the same. With iso() anchored to 2024-01-01, every row lands about 1,000 days before base - range, so /api/momentum, /api/growth, /api/ci-trend and the rest return empty counts. The "windows end at the last collection" test is the exception, because it reads collect_log and passes either way. Faking Date as in the suggestion above pins both sides. Using noon UTC as the instant also keeps day() clear of the midnight boundary from #7110.

🟢 Comment wording: the new comment says what the line does. The one in the suggestion names the invariant instead, as CLAUDE.md asks.

Strengths

  • The PR targets the right cause: sample dates and window edges read the clock at different moments, and this change aims to give them one fixed source.
  • It stays in scope and touches only the test file.

# Conflicts:
#	workers/admin/test/handlers.test.ts
The handler tests built sample dates from the real clock while the API
read it again a moment later, so a run straddling midnight UTC could put
"today" on different days and fail. Freeze Date with vitest fake timers
before each test so both sides see the same instant.

Closes amd#7110
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Thanks for picking this up, @cf3901646! I pushed a follow-up so it can land: the file is back on main's current version with its header and imports, and the tests now freeze the clock the dashboard API reads as well as the test's own dates, so a run near midnight UTC can't split "today" anymore.

🔍 Technical details
  • Merged origin/main (resolved the conflict with ci(npu): never clean up the whole runner, and fail the repro when a Lemonade can't start #6655's rewrite onto main's file).
  • beforeEach uses vi.useFakeTimers({ toFake: ["Date"] }) + vi.setSystemTime(NOW); afterEach restores real timers. Sample rows stay relative to NOW, so they sit inside every 30-day window.
  • npx vitest run in workers/admin: 234/234 pass; handlers.test.ts also passes with NOW at 23:59:59.999Z and 00:00:00.001Z. tsc -p tsconfig.test.json clean.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 10, 2026
Merged via the queue into amd:main with commit c6535fc Nov 10, 2026
14 checks passed
@kovtcharov-amd kovtcharov-amd mentioned this pull request Nov 17, 2026
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.

test(admin): a momentum test fails around midnight UTC

2 participants