TypeScript
test: ban wall-clock readings in tests (#22562)
Julius Marmingedd4506a1308b
diff --git a/apps/mobile/src/features/home/useThreadListActions.test.ts b/apps/mobile/src/features/home/useThreadListActions.test.tsindex 157ed016b4e..2e74d852c5b 100644--- a/apps/mobile/src/features/home/useThreadListActions.test.ts+++ b/apps/mobile/src/features/home/useThreadListActions.test.ts@@ -391,7 +391,8 @@ describe("thread action undo", () => {await useThreadListActions().unpinThread(thread);expect(readThreadUndoNotice()?.action).toBe("Unpinned");await readThreadUndoNotice()!.undo();- const restoresSnooze = snoozedUntil != null && Date.parse(snoozedUntil) > Date.now();+ // 2099 is the unexpired case; 2000 has long passed.+ const restoresSnooze = snoozedUntil === "2099-01-01T09:00:00.000Z";expect(state.requests.map(({ action }) => action)).toEqual(restoresSnooze ? ["unpin", "pin", "snooze"] : ["unpin", "pin"],);diff --git a/apps/server/src/serverRuntimeState.test.ts b/apps/server/src/serverRuntimeState.test.tsindex 21394819e25..59e681425d6 100644--- a/apps/server/src/serverRuntimeState.test.ts+++ b/apps/server/src/serverRuntimeState.test.ts@@ -206,8 +206,8 @@ describe("server ownership", () => {const state = yield* makeState;yield* ServerRuntimeState.persistServerRuntimeState({path: statePath,- // @effect-diagnostics-next-line globalDateInEffect:off - Legacy identity uses the real process start time, not TestClock.- state: { ...state, startedAt: new Date().toISOString() },+ // Any record time after this process started proves the PID was not reused.+ state: { ...state, startedAt: "2100-01-01T00:00:00.000Z" },});const refused = yield* Effect.scoped(ServerOwnership.acquireServerOwnership(statePath)).pipe(Effect.flip,diff --git a/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx b/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsxindex d7dd297ef47..069ac27df3c 100644--- a/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx+++ b/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx@@ -164,13 +164,16 @@ function setGrant(environmentId: EnvironmentId, allowed: boolean) {);}+// Default selection only picks recently active projects, so pin "now".+const NOW = Date.parse("2026-01-01T00:00:00.000Z");+function candidate(path: string): AgentSessionProjectCandidate {return {path,title: path.split("/").at(-1)!,sources: ["codex"],threadCount: 10,- lastActiveAt: new Date(Date.now() - 60_000).toISOString(),+ lastActiveAt: new Date(NOW - 60_000).toISOString(),alreadyImported: false,};}@@ -227,7 +230,9 @@ async function mountImport(remote = false) {}// The mocked modules read `state`, so every test starts by replacing all of it.+// Returns the clock spy so each test can `using` it; the spy would otherwise outlive the test.function setup() {+ const now = vi.spyOn(Date, "now").mockReturnValue(NOW);state.sessions.clear();for (const id of [primaryId, remoteId]) {state.sessions.set(@@ -253,10 +258,11 @@ function setup() {state.onDone.mockReset();state.scan.mockReset();state.refreshScan.mockReset();+ return now;}it("rechecks after project creation and can retry its history without recreating the project", async () => {- setup();+ using _now = setup();const creation = deferred<void>();state.createProject.mockImplementationOnce(async (input: CreateProjectInput) => {await creation.promise;@@ -283,7 +289,7 @@ it("rechecks after project creation and can retry its history without recreating});it("completes accepted imports when permission is revoked after the final server write", async () => {- setup();+ using _now = setup();state.candidates = state.candidates.slice(0, 1);const imported = deferred<{ importedCount: number; skippedCount: number }>();state.importThreads.mockImplementationOnce(async () =>diff --git a/apps/web/src/hooks/useThreadActions.undo.test.ts b/apps/web/src/hooks/useThreadActions.undo.test.tsindex b8e3edf310d..198c0eb9702 100644--- a/apps/web/src/hooks/useThreadActions.undo.test.ts+++ b/apps/web/src/hooks/useThreadActions.undo.test.ts@@ -265,7 +265,7 @@ describe("settle and snooze Undo", () => {it("wakes the thread from the snooze notice", async () => {const add = vi.spyOn(toastManager, "add").mockReturnValue("toast");const actions = useThreadActions();- await actions.snoozeThread(target, new Date(Date.now() + 60_000).toISOString());+ await actions.snoozeThread(target, "2100-01-01T00:00:00.000Z");expect(useThreadUndoNotice.getState().notice).toMatchObject({ action: "Snoozed", count: 1 });expect(add).not.toHaveBeenCalled();await currentUndo()();diff --git a/oxlint-plugin-t3code/index.ts b/oxlint-plugin-t3code/index.tsindex 77f88b71de3..2400bb0c189 100644--- a/oxlint-plugin-t3code/index.ts+++ b/oxlint-plugin-t3code/index.ts@@ -20,6 +20,7 @@ import noTestInLoop from "./rules/no-test-in-loop.ts";import noTestTimeoutOrUnreasonedSkip from "./rules/no-test-timeout-or-unreasoned-skip.ts";import noRpcPermissionBypass from "./rules/no-rpc-permission-bypass.ts";import noUnscopedHas from "./rules/no-unscoped-has.ts";+import noWallClockInTests from "./rules/no-wall-clock-in-tests.ts";import preferCatchTags from "./rules/prefer-catch-tags.ts";import preferEffectFn from "./rules/prefer-effect-fn.ts";import requireCenteredScrollGutter from "./rules/require-centered-scroll-gutter.ts";@@ -51,6 +52,7 @@ export default definePlugin({"no-test-timeout-or-unreasoned-skip": noTestTimeoutOrUnreasonedSkip,"no-rpc-permission-bypass": noRpcPermissionBypass,"no-unscoped-has": noUnscopedHas,+ "no-wall-clock-in-tests": noWallClockInTests,"prefer-catch-tags": preferCatchTags,"prefer-effect-fn": preferEffectFn,"require-centered-scroll-gutter": requireCenteredScrollGutter,diff --git a/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.test.ts b/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.test.tsnew file mode 100644index 00000000000..11ca2762ffe--- /dev/null+++ b/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.test.ts@@ -0,0 +1,94 @@+import { assert, describe } from "@effect/vitest";++import { createOxlintRuleHarness } from "../test/utils.ts";++const rule = createOxlintRuleHarness("t3code/no-wall-clock-in-tests", {+ filename: "fixture.test.ts",+});++describe("t3code/no-wall-clock-in-tests", () => {+ rule.valid(+ "ignores a computed member with a variable key",+ `+ import { it } from "vitest";+ it("parses", () => {+ const now = "parse";+ const parsed = Date[now]("2026-01-01");+ const literal = Date["parse"]("2026-01-01");+ });+ `,+ );++ rule.valid(+ "allows fixed dates and readings that seed mocks",+ `+ import { vi } from "vitest";++ const epoch = new Date("2026-01-01T00:00:00.000Z");+ export const later = new Date(epoch.getTime() + 1_000);+ vi.setSystemTime(new Date());+ vi.spyOn(Date, "now").mockReturnValue(Date.now() + 5_000);+ const clock = vi.fn(() => performance.now());+ clock.mockImplementation(() => Number(process.hrtime.bigint()));+ vi.mock("./clock", () => ({ now: () => Date.now() }));+ `,+ );++ rule.invalid(+ "reports elapsed-time measurement",+ `+ const startedAt = performance.now();+ const elapsed = performance.now() - startedAt;+ if (elapsed > 10_000) throw new Error("slow");+ `,+ (output) => {+ assert.match(output, /Wall-clock reading in a test/);+ assert.equal(output.match(/no-wall-clock-in-tests/g)?.length, 2);+ },+ );++ rule.invalid(+ "reports Date.now fixtures",+ `+ export const session = { expiresAt: Date.now() + 60_000 };+ `,+ );++ rule.invalid(+ "reports new Date() with no arguments",+ `+ export const createdAt = new Date().toISOString();+ `,+ );++ rule.invalid(+ "reports process.hrtime and process.hrtime.bigint",+ `+ export const a = process.hrtime();+ export const b = process.hrtime.bigint();+ `,+ (output) => {+ assert.equal(output.match(/no-wall-clock-in-tests/g)?.length, 2);+ },+ );++ rule.invalid(+ "reports a reading outside the mock's arguments",+ `+ import { vi } from "vitest";++ const mock = vi.fn();+ mock.mockReturnValue(1);+ export const now = Date.now();+ `,+ );+});++const productionRule = createOxlintRuleHarness("t3code/no-wall-clock-in-tests");++productionRule.valid(+ "ignores non-test files",+ `+ export const now = Date.now();+ `,+);diff --git a/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.ts b/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.tsnew file mode 100644index 00000000000..5e39ff678f3--- /dev/null+++ b/oxlint-plugin-t3code/rules/no-wall-clock-in-tests.ts@@ -0,0 +1,107 @@+import { defineRule, type ESTree } from "@oxlint/plugins";+import * as Option from "effect/Option";++import { getPropertyName, isIdentifier, unwrapExpression } from "../utils.ts";++const TEST_FILE_PATTERN = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;+const WALL_CLOCK_CALLS = new Set([+ "Date.now",+ "performance.now",+ "process.hrtime",+ "process.hrtime.bigint",+]);+const VI_MOCKING_FUNCTIONS = new Set(["fn", "mock", "setSystemTime", "spyOn"]);+const MOCK_METHODS = new Set([+ "mockImplementation",+ "mockImplementationOnce",+ "mockResolvedValue",+ "mockResolvedValueOnce",+ "mockReturnValue",+ "mockReturnValueOnce",+]);++const MESSAGE =+ "Wall-clock reading in a test; elapsed-time assertions pass or fail with runner load. Use TestClock/`Clock` under `it.effect`, `vi.setSystemTime`, or a fixed epoch. Benchmarks live behind an env gate.";++const isStringLiteral = (node: unknown) =>+ Option.exists(+ unwrapExpression(node),+ (expression) => expression.type === "Literal" && typeof expression.value === "string",+ );++const memberPath = (node: unknown): Option.Option<ReadonlyArray<string>> => {+ const expression = unwrapExpression(node);+ if (Option.isNone(expression)) return Option.none();+ if (expression.value.type === "Identifier") return Option.some([expression.value.name]);+ if (expression.value.type !== "MemberExpression") return Option.none();+ // `Date[now]` with a variable key is unknowable; only a literal key names a member.+ if (expression.value.computed && !isStringLiteral(expression.value.property))+ return Option.none();+ const property = getPropertyName(expression.value.property);+ if (Option.isNone(property)) return Option.none();+ return Option.map(memberPath(expression.value.object), (path) => [...path, property.value]);+};++const isWallClockCall = (callee: unknown): boolean =>+ Option.match(memberPath(callee), {+ onNone: () => false,+ onSome: (path) => WALL_CLOCK_CALLS.has(path.join(".")),+ });++const isMockingCall = (callee: unknown): boolean => {+ const expression = unwrapExpression(callee);+ if (Option.isNone(expression) || expression.value.type !== "MemberExpression") return false;+ const property = getPropertyName(expression.value.property);+ if (Option.isNone(property)) return false;+ if (MOCK_METHODS.has(property.value)) return true;+ return (+ VI_MOCKING_FUNCTIONS.has(property.value) &&+ isIdentifier(unwrapExpression(expression.value.object), "vi")+ );+};++// Readings that feed a mock (or live in a vi.mock factory) seed fake time+// rather than measure the runner, so they stay allowed.+const isMockArgument = (node: ESTree.Node): boolean => {+ let child: ESTree.Node = node;+ let current = node.parent;+ while (current) {+ if (+ current.type === "CallExpression" &&+ current.callee !== child &&+ isMockingCall(current.callee)+ ) {+ return true;+ }+ child = current;+ current = current.parent;+ }+ return false;+};++export default defineRule({+ meta: {+ type: "problem",+ docs: {+ description:+ "Disallow wall-clock readings (Date.now, new Date(), performance.now, process.hrtime) in tests.",+ },+ },+ create(context) {+ if (!TEST_FILE_PATTERN.test(context.filename)) return {};++ return {+ CallExpression(node) {+ if (!isWallClockCall(node.callee)) return;+ if (isMockArgument(node)) return;+ context.report({ node, message: MESSAGE });+ },+ NewExpression(node) {+ if (node.arguments.length > 0) return;+ if (!isIdentifier(unwrapExpression(node.callee), "Date")) return;+ if (isMockArgument(node)) return;+ context.report({ node, message: MESSAGE });+ },+ };+ },+});diff --git a/vite.config.ts b/vite.config.tsindex 787dbcf8ff9..29936cc7f53 100644--- a/vite.config.ts+++ b/vite.config.ts@@ -192,6 +192,7 @@ export default defineConfig({"t3code/no-test-timeout-or-unreasoned-skip": "error","t3code/no-rpc-permission-bypass": ["error", { allowRawClientAccess: true }],"t3code/no-unscoped-has": "error",+ "t3code/no-wall-clock-in-tests": "error","t3code/namespace-node-imports": "error","t3code/prefer-catch-tags": "error","t3code/prefer-effect-fn": "error",