/** * Proves the engine-purity lint rule (eslint.config.mjs, CLAUDE.md ยง7) fires. * * A guard that has only ever been seen NOT firing has not been tested. Each * banned construct is linted as if it lived in packages/engine/src, and must be * reported. The same snippet placed outside the engine must not be, which proves * the rule is scoped and not a global ban someone will turn off in anger. */ import { ESLint } from 'eslint'; import { fileURLToPath } from 'node:url'; import { beforeAll, describe, expect, it } from 'vitest'; const repoRoot = fileURLToPath(new URL('../../..', import.meta.url)); const eslint = new ESLint({ cwd: repoRoot }); const ENGINE_FILE = 'packages/engine/src/__purity_probe__.ts'; const OUTSIDE_FILE = 'packages/ui/src/__purity_probe__.ts'; async function messages(code: string, filePath: string): Promise { const [result] = await eslint.lintText(code, { filePath }); if (result === undefined) throw new Error('ESLint returned no result'); return result.messages.map((m) => `${m.ruleId ?? 'fatal'}: ${m.message}`); } const IMPURE: readonly [what: string, code: string][] = [ ['Date.now()', 'export const t = Date.now();'], ['new Date() with no argument', 'export const d = new Date();'], ['Date() called as a function', 'export const s = Date();'], ['Math.random()', 'export const r = Math.random();'], ['fetch()', "export const p = fetch('/x');"], ['localStorage', "export const v = localStorage.getItem('x');"], ['indexedDB', "export const o = indexedDB.open('x');"], ['document', 'export const b = document.body;'], ['window', 'export const w = window.innerWidth;'], ['navigator', 'export const l = navigator.language;'], ['crypto', 'export const c = crypto.randomUUID();'], ['setTimeout', 'setTimeout(() => 0, 1);'], ['performance.now()', 'export const n = performance.now();'], ['the device time zone', 'export const z = Intl.DateTimeFormat().resolvedOptions().timeZone;'], ['a UTC day', 'export const day = new Date(0).toISOString().slice(0, 10);'], ['a Node built-in', "import { readFileSync } from 'node:fs'; export const f = readFileSync;"] ]; describe('engine purity lint rule', () => { // The first lint loads the config and typescript-eslint. On a busy machine that // alone took over vitest's 5 s, failing whichever case ran first. Pay it here. beforeAll(async () => { await messages('export {};', ENGINE_FILE); }, 60_000); it.each(IMPURE)('reports %s inside packages/engine/src', async (_what, code) => { const found = await messages(code, ENGINE_FILE); expect(found.filter((m) => m.startsWith('fatal')), 'snippet must parse').toEqual([]); expect(found.some((m) => /^no-restricted-/.test(m)), found.join('\n')).toBe(true); }); it.each(IMPURE)('does not report %s outside the engine', async (_what, code) => { const found = await messages(code, OUTSIDE_FILE); expect(found.filter((m) => /^no-restricted-/.test(m))).toEqual([]); }); it('allows a Date built from an explicit instant', async () => { expect(await messages('export const d = new Date(1761500000000);', ENGINE_FILE)).toEqual([]); }); it('the real engine sources are clean', async () => { const results = await eslint.lintFiles(['packages/engine/src/**/*.ts']); expect(results.length, 'the glob must match engine sources').toBeGreaterThan(0); const problems = results.flatMap((r) => r.messages.map((m) => `${r.filePath}:${m.line} ${m.message}`)); expect(problems).toEqual([]); }); });