Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 389902e

Browse files
baozhoutaoclaude
andauthored
test(runner): pin the suite's timezone to UTC (#8486)
The date-face pin family asserts literal LOCAL-date faces (`Jul 4, 2024`, `7/4/2024 7:00 am`) built from fixed UTC instants through local date parts, and nothing pinned the runner's zone. Measured over the six files carrying the family at 76573a1: green at UTC, 7 failures at Europe/Paris, 7 at Asia/Shanghai, 31 at America/New_York, 33 at Etc/GMT+8 — green at exactly one offset, invisible on CI (which is UTC) and paid entirely by contributors elsewhere. `vitest.config.mts` now sets `process.env.TZ = 'UTC'` in module scope, which `apps/console/vitest.config.ts` inherits by importing it. The literal faces stay: they are deliberate anti-tautology anchors, and deriving them from the formatter under test would turn them into `formatDate(v) === formatDate(v)`. `scripts/__tests__/vitest-timezone-pin-8366.test.ts` spawns a real vitest under `TZ=Etc/GMT+8` and asserts the run still sees UTC, so deleting the pin reds CI rather than only a contributor's laptop. Claude-Session: https://claude.ai/code/session_01FhBNJcLRZLe8M87VcUgpKr Co-authored-by: Claude <noreply@anthropic.com>
1 parent 19d86fe commit 389902e

3 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
---
3+
4+
Pin the test runner's timezone to UTC (objectui#8366). The date-face pin family
5+
asserts literal LOCAL-date faces built from fixed UTC instants, so the suite was
6+
green at exactly one offset and red on a contributor's laptop anywhere else.
7+
Test infrastructure only; no package is released by this change.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { spawnSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
import { createRequire } from 'node:module';
6+
import { fileURLToPath } from 'node:url';
7+
8+
/**
9+
* objectui#8366 — the suite's ambient timezone is UTC, pinned.
10+
*
11+
* ## What this guards
12+
*
13+
* `vitest.config.mts` sets `process.env.TZ = 'UTC'` in its module scope. A
14+
* family of date pins asserts LITERAL local-date faces (`Jul 4, 2024`,
15+
* `7/4/2024 7:00 am`) built from fixed UTC instants through LOCAL date parts,
16+
* so without that line the face they render is a property of the
17+
* contributor's laptop. Measured on `76573a184`, over the six files that carry
18+
* the family, BEFORE the pin:
19+
*
20+
* TZ=UTC 184 passed TZ=Europe/Paris 7 failed
21+
* TZ=Asia/Shanghai 7 failed TZ=America/New_York 31 failed
22+
* TZ=Etc/GMT+8 33 failed
23+
*
24+
* Green at exactly one offset — not merely "west of somewhere". A `07:00 AM`
25+
* face pinned off an `07:00Z` instant is true at UTC+00:00 and nowhere else.
26+
*
27+
* ## Why a spawn and not just an in-process assertion
28+
*
29+
* CI runs in UTC. So `getTimezoneOffset() === 0`, asserted in this process, is
30+
* green on CI whether or not the config still pins anything — it would catch a
31+
* deleted pin only for the contributor it was written to protect, and stay
32+
* silent in the one place that gates merges. That is the exact shape of the
33+
* original defect, one level up.
34+
*
35+
* So the fact is measured where it can fail: a real vitest is spawned with
36+
* `TZ=Etc/GMT+8` in its environment and asked to run the ambient-zone case
37+
* below. It can only pass if the config overrode the inherited zone. Delete the
38+
* line in `vitest.config.mts` and this case reds on CI.
39+
*
40+
* `Etc/GMT+8` is UTC-08:00 — POSIX inverts the sign — chosen because it is the
41+
* zone the reporting contributor measured in.
42+
*
43+
* ## The live control
44+
*
45+
* A spawn-based pin has one silent failure: if `TZ` never reached the child at
46+
* all, the child would read UTC for a reason that has nothing to do with the
47+
* config, and the assertion would pass forever. `the child environment really
48+
* carries TZ` measures that hand-off against a plain `node -e`, so a green
49+
* spawn case is attributable.
50+
*
51+
* ## Recursion
52+
*
53+
* The child runs THIS file, filtered by `--testNamePattern` to the ambient-zone
54+
* case, so the spawn cases are skipped there. `OBJECTUI_TZ_PIN_CHILD` is a
55+
* second, independent belt: even an unfiltered child cannot spawn a grandchild.
56+
*/
57+
58+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
59+
const configPath = path.join(repoRoot, 'vitest.config.mts');
60+
const selfPath = path.relative(repoRoot, fileURLToPath(import.meta.url));
61+
62+
/** UTC-08:00. POSIX `Etc/GMT+N` zones invert the sign; this is deliberate. */
63+
const WEST = 'Etc/GMT+8';
64+
const WEST_OFFSET_MINUTES = 480;
65+
66+
/** The name the spawned child is filtered down to. Kept as one constant so the
67+
* filter and the `it()` title cannot drift apart. */
68+
const AMBIENT_CASE = 'the suite runs in UTC, whatever zone the contributor is in';
69+
70+
const IS_CHILD = process.env.OBJECTUI_TZ_PIN_CHILD === '1';
71+
72+
/** The vitest CLI entry, resolved rather than assumed at a `node_modules` path. */
73+
const vitestCli = (() => {
74+
const require = createRequire(path.join(repoRoot, 'noop.js'));
75+
const pkgPath = require.resolve('vitest/package.json');
76+
const bin = (JSON.parse(fs.readFileSync(pkgPath, 'utf8')).bin as { vitest: string }).vitest;
77+
return path.resolve(path.dirname(pkgPath), bin);
78+
})();
79+
80+
describe('objectui#8366 — the runner pins the timezone', () => {
81+
it(AMBIENT_CASE, () => {
82+
// Read two independent surfaces: `Date`'s own offset, and the zone ICU
83+
// resolves for a bare formatter — which is what every `toLocaleDateString`
84+
// in the date-face family actually consults.
85+
expect(new Date('2024-07-04T07:00:00.000Z').getTimezoneOffset()).toBe(0);
86+
expect(new Intl.DateTimeFormat().resolvedOptions().timeZone).toBe('UTC');
87+
});
88+
89+
it.skipIf(IS_CHILD)('control — the child environment really carries TZ', () => {
90+
// Non-vacuity for the case below. Without this, a `TZ` that never reached
91+
// the child would make the spawned run read UTC for a reason that has
92+
// nothing to do with `vitest.config.mts`, and the pin would be inert.
93+
const probe = spawnSync(
94+
process.execPath,
95+
['-e', 'process.stdout.write(String(new Date("2024-07-04T07:00:00.000Z").getTimezoneOffset()))'],
96+
{ cwd: repoRoot, encoding: 'utf8', env: { ...process.env, TZ: WEST }, timeout: 60_000 },
97+
);
98+
99+
expect(probe.status).toBe(0);
100+
expect(probe.stdout.trim()).toBe(String(WEST_OFFSET_MINUTES));
101+
});
102+
103+
it.skipIf(IS_CHILD)(
104+
'a real vitest spawned under a non-UTC TZ still runs in UTC',
105+
() => {
106+
const env: NodeJS.ProcessEnv = { ...process.env };
107+
// A fresh CLI, not a nested worker of this run.
108+
for (const key of Object.keys(env)) if (key.startsWith('VITEST')) delete env[key];
109+
env.TZ = WEST;
110+
env.OBJECTUI_TZ_PIN_CHILD = '1';
111+
112+
const child = spawnSync(
113+
process.execPath,
114+
[vitestCli, 'run', selfPath, '--testNamePattern', AMBIENT_CASE],
115+
{ cwd: repoRoot, encoding: 'utf8', env, timeout: 300_000 },
116+
);
117+
const output = `${child.stdout ?? ''}${child.stderr ?? ''}`;
118+
119+
expect(output).toContain('1 passed');
120+
expect(child.status).toBe(0);
121+
},
122+
360_000,
123+
);
124+
125+
it.skipIf(IS_CHILD)('the pin is where the spawn says it is', () => {
126+
// The cheap static half. It cannot replace the spawn — a line can be
127+
// present and no longer take effect, which is the direction vitest could
128+
// move under us — but it names the file and the spelling for whoever
129+
// arrives here from a red spawn.
130+
const source = fs.readFileSync(configPath, 'utf8');
131+
132+
expect(source).toContain("process.env.TZ = 'UTC'");
133+
// Not `??=` / `||=`: a contributor's zone usually comes from
134+
// `/etc/localtime`, not from `TZ`, so a conditional assignment would leave
135+
// the reported population unfixed. See the comment at the assignment.
136+
expect(source).not.toMatch(/process\.env\.TZ\s*(\?\?|\|\|)=/);
137+
});
138+
});

‎vitest.config.mts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,54 @@ import { assertCanonicalVitestInvocation, cliHasTestFilters } from './scripts/vi
77
const __filename = fileURLToPath(import.meta.url);
88
const __dirname = path.dirname(__filename);
99

10+
// ── The suite runs in UTC, always (objectui#8366) ───────────────────────────
11+
//
12+
// A family of pins asserts LITERAL local-date faces — `Jul 4, 2024`,
13+
// `Jul 4, '24`, `7/4/2024 7:00 am` — built from fixed UTC instants
14+
// (`2024-07-04T07:00:00.000Z` and friends) through LOCAL date parts. Nothing
15+
// pinned the runner's zone, so the face those pins render was a property of
16+
// the contributor's laptop. Measured on `76573a184` over the six files that
17+
// carry the family:
18+
//
19+
// TZ=UTC 6 files passed (184 tests, 0 failed)
20+
// TZ=Europe/Paris 3 failed | 3 passed (7 failed)
21+
// TZ=Asia/Shanghai 3 failed | 3 passed (7 failed)
22+
// TZ=America/New_York 4 failed | 2 passed (31 failed)
23+
// TZ=Etc/GMT+8 4 failed | 2 passed (33 failed)
24+
//
25+
// So the suite was green at EXACTLY ONE offset, not merely west of some
26+
// boundary: a `07:00 AM` face pinned off an `07:00Z` instant is true at
27+
// UTC+00:00 and nowhere else. CI is UTC, so the class was invisible there and
28+
// only ever cost a contributor — a wall of red that is not about the code.
29+
//
30+
// Why the zone and not the assertions: those literals are load-bearing ON
31+
// PURPOSE. `date-display.optionsStyle-7745.test.ts` says so in its own words —
32+
// its faces are "anchored by the two literals the card measured, so a redesign
33+
// that moved both sides together could not pass silently." Deriving the
34+
// expected face from the same formatter under test is what the two files that
35+
// ALREADY pass do (`dataset-format.date.test.ts`,
36+
// `DatasetWidget.dateMeasure.test.tsx`), and it is the right assertion where
37+
// the claim is "this surface shows what a list cell shows" — but on an anchor
38+
// it degrades to `formatDate(v) === formatDate(v)` and asserts nothing. Pinning
39+
// the offset the anchors were written against keeps them.
40+
//
41+
// This deletes no coverage: no test in this repo reads the ambient zone
42+
// (`process.env.TZ`, `getTimezoneOffset`, `resolvedOptions().timeZone` — zero
43+
// hits across every `*.test.ts`/`*.test.tsx`), and the one zone-aware surface
44+
// that exists takes its zone from METADATA, explicitly
45+
// (`GanttView.tsx`'s `tzOffsetMs(schema.timeZone, …)`). Non-UTC coverage, if
46+
// it is ever wanted, wants an explicit per-case zone — never the runner's
47+
// ambient one, which is the thing that made this silent.
48+
//
49+
// Unconditional on purpose: a contributor's zone usually comes from
50+
// `/etc/localtime` and not from `TZ`, so an `??=` would leave exactly the
51+
// reported population unfixed while pretending to fix it.
52+
//
53+
// Pinned by `scripts/__tests__/vitest-timezone-pin-8366.test.ts`, which spawns
54+
// a real vitest under a non-UTC `TZ` and asserts the run still sees UTC — so
55+
// deleting this line reds CI, where the ambient zone would otherwise hide it.
56+
process.env.TZ = 'UTC';
57+
1058
// Refuse the two invocations that pass while running none of the tests the
1159
// caller asked for — a package-cwd run (objectui#3378) and a path filter that
1260
// never reaches Vitest (objectui#3288).

0 commit comments

Comments
 (0)