|
| 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 | +}); |
0 commit comments