From e4e80752323b8140c73f2ba8b8fd2596dce1e7d3 Mon Sep 17 00:00:00 2001 From: HugoXK Date: Tue, 24 Nov 2026 12:38:12 +0000 Subject: [PATCH 1/2] test: stamp the repository-page fixture with one fixed instant (#1300) `makeTerminalJob` takes no instant and writes `new Date()`, so `renderRepo` stamped the job at call time and the whole-page equality compared two independently stamped pages. Build one store and one job with `createdAt` and the instant the helper should store, render both pages from that job, and assert the rendered line is the literal, not "now". Red on this tip: `test/clockFixtures.test.ts` fails because `makeTerminalJob` ignores the instant, and `tsc` refuses the third argument until the helper takes it. --- test/clockFixtures.test.ts | 44 +++++++++++ test/repoClockWarning.test.ts | 138 ++++++++++++++++++++-------------- 2 files changed, 124 insertions(+), 58 deletions(-) create mode 100644 test/clockFixtures.test.ts diff --git a/test/clockFixtures.test.ts b/test/clockFixtures.test.ts new file mode 100644 index 00000000..f1170fb6 --- /dev/null +++ b/test/clockFixtures.test.ts @@ -0,0 +1,44 @@ +import assert from 'node:assert/strict' +import test from 'node:test' +import { Store } from '../src/db.js' +import { makeTerminalJob, makeUser } from './helpers.js' + +// #1300: `makeTerminalJob` stamped `finished_at` with the wall clock and took no +// instant, so a page rendered from one of its rows could not be reproduced. The +// fixture now accepts the instant it should store; the default stays the wall +// clock so the existing call sites are untouched. +test('a fixture written for a fixed instant keeps that instant', () => { + const store = new Store(':memory:') + try { + const user = makeUser(store, { login: 'clock-fixture-user', status: 'approved' }) + const repo = store.createRepo({ + id: 'repo_clock_fixture', + userId: user.id, + owner: 'acme', + name: 'demo', + defaultBranch: 'main', + tokenEnc: 'x', + permission: 'admin', + repoRole: 'maintainer', + goal: '', + autoIterate: true, + intervalMinutes: 1440, + }) + const instant = '2026-11-23T21:25:43.500Z' + const job = makeTerminalJob(store, { + id: 'job_clock_fixture', + repoId: repo.id, + userId: user.id, + trigger: 'manual', + goal: 'g', + callbackHash: 'h', + platformTokenHash: 't', + kind: 'iterate', + baseBranch: 'main', + createdAt: instant, + }, { finishedAt: instant }) + assert.equal(job.finished_at, instant, 'the fixture stores the instant it was given instead of the wall clock') + } finally { + store.close() + } +}) diff --git a/test/repoClockWarning.test.ts b/test/repoClockWarning.test.ts index 8367166d..5b1570a4 100644 --- a/test/repoClockWarning.test.ts +++ b/test/repoClockWarning.test.ts @@ -25,47 +25,54 @@ const failing: ClockState = { thresholdSeconds: 120, } -function renderRepo(clock?: ClockState): string { +// #1300: one literal instant, used for both stamps and asserted on the page. +// The old fixture built a fresh store per render, so the job carried a +// wall-clock `created_at` read at call time and the two pages could differ by +// one second when a boundary fell between them. The page is now a function of +// the record: both renders below read one job that was built once with these +// stamps, and the rendered line is the literal below, not "now". +const FIXED_AT = '2026-11-23T21:25:43.500Z' +const FIXED_LABEL = '2026-11-23 21:25:43 UTC' + +function makeRepoFixture(): { store: Store; render: (clock?: ClockState) => string } { const config = testConfig() const store = new Store(':memory:') - try { - const user = makeUser(store, { login: 'clock-repo-user', status: 'approved' }) - const repo = store.createRepo({ - id: 'repo_clock', - userId: user.id, - owner: 'acme', - name: 'demo', - defaultBranch: 'main', - tokenEnc: 'x', - permission: 'admin', - repoRole: 'maintainer', - goal: '', - autoIterate: true, - intervalMinutes: 1440, - }) - const job = makeTerminalJob(store, { - id: 'job_clock', - repoId: repo.id, - userId: user.id, - trigger: 'manual', - goal: 'g', - callbackHash: 'h', - platformTokenHash: 't', - kind: 'iterate', - baseBranch: 'main', - }) - return repoPage({ - config, - locale: 'en', - viewer: { user: store.userById(user.id)!, csrfToken: 'csrf' }, - repo, - jobs: [job], - prJobs: [job], - ...(clock === undefined ? {} : { clock }), - }) - } finally { - store.close() - } + const user = makeUser(store, { login: 'clock-repo-user', status: 'approved' }) + const repo = store.createRepo({ + id: 'repo_clock', + userId: user.id, + owner: 'acme', + name: 'demo', + defaultBranch: 'main', + tokenEnc: 'x', + permission: 'admin', + repoRole: 'maintainer', + goal: '', + autoIterate: true, + intervalMinutes: 1440, + }) + const job = makeTerminalJob(store, { + id: 'job_clock', + repoId: repo.id, + userId: user.id, + trigger: 'manual', + goal: 'g', + callbackHash: 'h', + platformTokenHash: 't', + kind: 'iterate', + baseBranch: 'main', + createdAt: FIXED_AT, + }, { finishedAt: FIXED_AT }) + const render = (clock?: ClockState): string => repoPage({ + config, + locale: 'en', + viewer: { user: store.userById(user.id)!, csrfToken: 'csrf' }, + repo, + jobs: [job], + prJobs: [job], + ...(clock === undefined ? {} : { clock }), + }) + return { store, render } } function textOf(html: string): string { @@ -73,30 +80,44 @@ function textOf(html: string): string { } test('a failing clock paints the dashboard banner on the repository page, above the sections', () => { - const html = renderRepo(failing) - const banners = html.match(/id="clock-fail"/g) ?? [] - assert.equal(banners.length, 1, `expected exactly one clock banner on the repository page, rendering it ${banners.length} times`) - const opening = html.match(/