Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(test): enforce runner and per-test hygiene coverage
MagicLizi8111b30313f7
diff --git a/apps/desktop/src/main/device-link/__tests__/providerShareApprovalWindows.test.ts b/apps/desktop/src/main/device-link/__tests__/providerShareApprovalWindows.test.tsindex ae51ea2248..fe42044b59 100644--- a/apps/desktop/src/main/device-link/__tests__/providerShareApprovalWindows.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/providerShareApprovalWindows.test.ts@@ -2,7 +2,7 @@ import fs from 'node:fs';import os from 'node:os';import path from 'node:path';-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';const state = vi.hoisted(() => ({ scope: 'owner-a', boundaryPending: false, owned: [] as unknown[] }));const clipboard = vi.hoisted(() => ({ remember: vi.fn() }));@@ -22,6 +22,8 @@ vi.mock('../providerShareApi.js', () => ({type Host = typeof import('../providerShareHost');let dir: string;+let ownerA: string;+let ownerB: string;let host: Host;let windows: typeof import('../providerShareApprovalWindows');const events = () => ({ changed: vi.fn(), requested: vi.fn(), watchedRequested: vi.fn(), revoked: vi.fn(), reconcile: vi.fn() });@@ -36,22 +38,31 @@ const filePath = () => path.join(dir, state.scope, 'provider-share-approval-windconst assertCurrent = () => { if (state.boundaryPending) throw new Error('account boundary'); };async function loadProcess() {- // reset-modules: each case starts a fresh Electron main-process runtime so retained ownership cannot leak between accounts.+ // reset-modules: explicitly simulate a complete process restart in persistence regressions, not per-test preparation.vi.resetModules();windows = await import('../providerShareApprovalWindows');windows.configureProviderShareApprovalWindows({ filePath, scopeKey: () => state.scope });host = await import('../providerShareHost');}-beforeEach(async () => {+beforeAll(async () => {+ windows = await import('../providerShareApprovalWindows');+ host = await import('../providerShareHost');+});++beforeEach(() => {dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cindy-share-approval-window-'));- state.scope = 'owner-a';+ // Host teardown retains state by account for runtime reconnection. Distinct+ // fake accounts keep cases isolated without reloading the module graph.+ ownerA = path.basename(dir) + '-a';+ ownerB = path.basename(dir) + '-b';+ state.scope = ownerA;state.boundaryPending = false;state.owned = [];clipboard.remember.mockReset();vi.useFakeTimers({ toFake: ['Date'] });vi.setSystemTime(new Date('2026-10-15T08:00:00Z'));- await loadProcess();+ windows.configureProviderShareApprovalWindows({ filePath, scopeKey: () => state.scope });});afterEach(() => {@@ -114,7 +125,7 @@ describe('approval ownership across a complete application restart', () => {await loadProcess();const identified = { ...request, linkId: LINK_A };state.owned = [{ ...ownedShare, requests: [identified] }];- state.scope = 'owner-b';+ state.scope = ownerB;const other = events();const readWatchedShares = vi.fn(async () => [{ ...ownedShare, requests: [identified] }]);host.startProviderShareHost(other, { readWatchedShares });@@ -122,7 +133,7 @@ describe('approval ownership across a complete application restart', () => {expect(other.requested).toHaveBeenCalledWith(identified, expect.anything(), false);expect(readWatchedShares).not.toHaveBeenCalled();host.stopProviderShareHost();- state.scope = 'owner-a';+ state.scope = ownerA;vi.setSystemTime(Date.now() + windows.PROVIDER_SHARE_REMOTE_LINK_WINDOW_MS + 1);const expired = events();host.startProviderShareHost(expired, { readWatchedShares });@@ -203,14 +214,14 @@ describe('approval ownership across a complete application restart', () => {await host.quietProviderShareRequests('anthropic');host.stopProviderShareHost();await loadProcess();- state.scope = 'owner-b';+ state.scope = ownerB;state.owned = [ownedShare];const other = events();host.startProviderShareHost(other);await host.refreshProviderShareHost('other-account');expect(other.requested).toHaveBeenCalledWith(request, expect.anything(), false);host.stopProviderShareHost();- state.scope = 'owner-a';+ state.scope = ownerA;const original = events();host.startProviderShareHost(original);await host.refreshProviderShareHost('original-account');@@ -276,7 +287,7 @@ describe('approval ownership storage', () => {const originalFile = filePath();const until = Date.now() + windows.PROVIDER_SHARE_REMOTE_LINK_WINDOW_MS;const save = windows.updateProviderShareApprovalWindows((current) => ({ ...current, quietUntil: { anthropic: until } }), assertCurrent);- state.scope = 'owner-b';+ state.scope = ownerB;await expect(save).rejects.toThrow('Could not save provider share approval ownership');expect(fs.existsSync(originalFile)).toBe(false);expect(fs.existsSync(filePath())).toBe(false);diff --git a/scripts/__tests__/check-test-hygiene.test.mjs b/scripts/__tests__/check-test-hygiene.test.mjsindex b7848798bf..a781b1da62 100644--- a/scripts/__tests__/check-test-hygiene.test.mjs+++ b/scripts/__tests__/check-test-hygiene.test.mjs@@ -2,10 +2,14 @@// that flips one rule decision, so a detector regression cannot silently let// slow or source-literal unit tests back into CI.import assert from "node:assert/strict";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";import test from "node:test";import {compareWithBaseline,+ scanRepository,scanTestSource,TEST_FILE,} from "../check-test-hygiene.mjs";@@ -22,6 +26,31 @@ test("hygiene scans both test and spec files", () => {test("scanTestSource counts only unjustified hits for each rule", () => {const cases = [["per-test module reset", "beforeEach(() => { vi.resetModules(); });", { resetModules: 1 }],+ [+ "a reason cannot exempt a per-test hook",+ "beforeEach(() => {\n// reset-modules: cached state\nvi.resetModules();\n});",+ { resetModules: 1 },+ ],+ [+ "a reason cannot exempt a transitive per-test helper",+ "function reload() {\n// reset-modules: cached state\nvi.resetModules();\n}\nconst prepare = () => reload(); beforeEach(prepare);",+ { resetModules: 1 },+ ],+ [+ "same-named helpers in sibling suites keep their own lexical binding",+ "describe('reload', () => { function setup() {\n// reset-modules: cached state\nvi.resetModules();\n} beforeEach(setup); }); describe('other', () => { function setup() {} beforeEach(setup); });",+ { resetModules: 1 },+ ],+ [+ "registering a restart callback does not execute it in the hook",+ "let restart; beforeEach(() => { restart = () => {\n// reset-modules: persisted state after restart\nvi.resetModules();\n}; }); it('restart', () => restart());",+ { resetModules: 0 },+ ],+ [+ "explicit restart boundaries remain exempt",+ "function restart() {\n// reset-modules: verify persisted state after restart\nvi.resetModules();\n}\nit('restart', () => restart());",+ { resetModules: 0 },+ ],["justified module reset","// reset-modules: auto-sync prefs are module-scoped\nvi.resetModules();",@@ -124,6 +153,21 @@ test("scanTestSource counts only unjustified hits for each rule", () => {);});+test("repository scan covers runner performance without rejecting its source contracts", async () => {+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "cindy-hygiene-scan-"));+ try {+ fs.mkdirSync(path.join(root, "scripts", "__tests__"), { recursive: true });+ fs.writeFileSync(path.join(root, "scripts", "test-workspaces.config.mjs"), "export default { workspaces: [] };");+ fs.writeFileSync(path.join(root, "scripts", "__tests__", "runner.test.mjs"),+ "vi.resetModules(); setTimeout(done, 100); readFileSync(resolve(__dirname, '../runner.mjs'));");+ assert.deepEqual(await scanRepository(root), {+ "scripts/__tests__/runner.test.mjs": { resetModules: 1, realWaits: 1, sourceReads: 0 },+ });+ } finally {+ fs.rmSync(root, { recursive: true, force: true });+ }+});+test("compareWithBaseline only lets counts stay or shrink", () => {const baseline = { "a.test.ts": { resetModules: 2 } };const pass = compareWithBaseline(diff --git a/scripts/__tests__/test-workspaces.test.mjs b/scripts/__tests__/test-workspaces.test.mjsindex 5aa37132fa..889d10dd8d 100644--- a/scripts/__tests__/test-workspaces.test.mjs+++ b/scripts/__tests__/test-workspaces.test.mjs@@ -2303,6 +2303,7 @@ export const acquireTestGateLock = (options) => acquire({ ...options, ...${JSON.});let output = "";let failure;+ // real-wait: watchdog for a real CLI child; the close event clears it without waiting.const timer = setTimeout(() => {failure = new Error(`CLI did not finish: ${output}`);child.kill();diff --git a/scripts/check-test-hygiene.mjs b/scripts/check-test-hygiene.mjsindex 40c383fcde..f2d4003472 100644--- a/scripts/check-test-hygiene.mjs+++ b/scripts/check-test-hygiene.mjs@@ -8,6 +8,7 @@// reset-modules 每次 vi.resetModules() 都会让后续 import 重新求值整条模块链。// codexProxyHost.test.ts 逐用例重置时 285 条用例耗时 105s,不重置只要 7s。// 新增调用必须在上一行写 `// reset-modules: <被隔离的模块状态>`。+// beforeEach/afterEach 及其准备、清理 helper 不接受理由豁免。// real-wait 单测里 ≥100ms 的真实 setTimeout / sleep 既拖慢 CI 又制造时序抖动;// 改用 vi.useFakeTimers() 或等待真实事件。确需真实等待时上一行写// `// real-wait: <原因>`。已切换为 fake timers 的区间不计。@@ -26,15 +27,15 @@ import { fileURLToPath, pathToFileURL } from 'node:url';const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');const BASELINE_PATH = path.join(ROOT, 'scripts', 'test-hygiene-baseline.json');-const SCAN_ROOTS = ['apps', 'packages'];+const SCAN_ROOTS = ['apps', 'packages', 'scripts'];const SKIP_DIRS = new Set(['node_modules', 'dist', 'out', 'build', 'coverage', '.vite', '.expo', '.turbo']);export const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/;const NON_UNIT_TEST = /(?:^|[-.])(?:integration|e2e|git-integration)\.(?:test|spec)\.|\.bench\./;export const RULES = ['resetModules', 'realWaits', 'sourceReads'];const REAL_WAIT_MIN_MS = 100;const REAL_WAIT_PATTERN = /\b(?:sleep|delay|wait|waitMs|pause)\s*\(\s*(\d[\d_]*)\s*\)/g;-// Reuse Desktop's declared parser only for files that mix fake and real timers.-// No type checking or module loading is needed to locate test/hook callbacks.+// Reuse Desktop's declared parser to locate test/hook callbacks and local helpers.+// No type checking or module loading is needed.const requireDesktop = createRequire(new URL('../apps/desktop/package.json', import.meta.url));let typescript;@@ -89,13 +90,69 @@ function hasJustification(lines, line, marker) {return false;}-function countMatches(code, lines, pattern, marker, accept = () => true) {- let count = 0;- for (const match of code.matchAll(pattern)) {- if (!accept(match)) continue;- if (!hasJustification(lines, lineOf(code, match.index), marker)) count += 1;+/** A per-test hook cannot turn repeated module loading into an exemption. */+function countModuleResets(code, lines) {+ const resets = [...code.matchAll(/\bvi\.resetModules\s*\(/g)];+ if (resets.length === 0) return 0;+ const ts = typescript ??= requireDesktop('typescript');+ const source = ts.createSourceFile('test.tsx', code, ts.ScriptTarget.Latest, false, ts.ScriptKind.TSX);+ const nodeScopes = new WeakMap();+ const hooks = [];+ const isFunction = node => ts.isFunctionDeclaration(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node);+ function collect(node, scope) {+ if (ts.isFunctionDeclaration(node) && node.name) scope.bindings.set(node.name.text, node);+ if (ts.isSourceFile(node) || ts.isBlock(node) || isFunction(node)) {+ scope = { parent: scope, bindings: new Map() };+ }+ nodeScopes.set(node, scope);+ if (isFunction(node)) {+ if (node.name) scope.bindings.set(node.name.text, node);+ for (const parameter of node.parameters) {+ if (ts.isIdentifier(parameter.name)) scope.bindings.set(parameter.name.text, null);+ }+ }+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {+ scope.bindings.set(node.name.text, node.initializer && isFunction(node.initializer) ? node.initializer : null);+ }+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)+ && /^(?:beforeEach|afterEach)$/.test(node.expression.text)) hooks.push(...node.arguments);+ ts.forEachChild(node, child => collect(child, scope));}- return count;+ collect(source, null);+ function resolveHelper(identifier) {+ for (let scope = nodeScopes.get(identifier); scope; scope = scope.parent) {+ if (scope.bindings.has(identifier.text)) return scope.bindings.get(identifier.text);+ }+ return null;+ }+ const perTest = new Set();+ const visitedFunctions = new Set();+ function visitFunction(node) {+ if (!node || visitedFunctions.has(node)) return;+ visitedFunctions.add(node);+ if (node.body) visit(node.body);+ }+ function visit(node) {+ // Defining or storing a restart callback is not a per-test reload. Only+ // follow an actual local call, or the callback registered with a hook.+ if (isFunction(node)) return;+ if (ts.isCallExpression(node)) {+ const expression = node.expression;+ if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.expression)+ && expression.expression.text === 'vi' && expression.name.text === 'resetModules') {+ perTest.add(expression.getStart(source));+ }+ if (ts.isIdentifier(expression)) visitFunction(resolveHelper(expression));+ else if (isFunction(expression)) visitFunction(expression);+ }+ ts.forEachChild(node, visit);+ }+ for (const hook of hooks) {+ if (isFunction(hook)) visitFunction(hook);+ else if (ts.isIdentifier(hook)) visitFunction(resolveHelper(hook));+ }+ return resets.filter(match => perTest.has(match.index)+ || !hasJustification(lines, lineOf(code, match.index), 'reset-modules')).length;}/** Mask string contents while preserving line numbers and call-site punctuation. */@@ -336,7 +393,7 @@ export function scanTestSource(source, { isGuard = false } = {}) {const code = stripComments(source);const lines = source.split(/\r?\n/);const executableCode = maskStrings(code);- const resetModules = countMatches(executableCode, lines, /\bvi\.resetModules\s*\(/g, 'reset-modules');+ const resetModules = countModuleResets(executableCode, lines);const realWaits = countRealWaits(executableCode, lines);let sourceReads = 0;if (!isGuard && /\breadFileSync\b|\breadFile\s*\(|\?raw/.test(code)) {@@ -383,7 +440,9 @@ export async function scanRepository(root = ROOT) {const counts = {};for (const file of files.sort()) {const rel = path.relative(root, file).split(path.sep).join('/');- const result = scanTestSource(readFileSync(file, 'utf8'), { isGuard: guard.has(rel) });+ // Root runner tests are a dedicated infrastructure/contract tier. Their+ // source contracts remain valid; the performance rules still apply.+ const result = scanTestSource(readFileSync(file, 'utf8'), { isGuard: guard.has(rel) || rel.startsWith('scripts/') });if (RULES.some((rule) => result[rule] > 0)) counts[rel] = result;}return counts;@@ -408,7 +467,7 @@ export function compareWithBaseline(counts, baseline) {}const HINTS = {- resetModules: '只在被测的模块级状态确实需要重建的边界调用,上一行写 `// reset-modules: <原因>`;优先用生产代码的 dispose/reset 或依赖注入',+ resetModules: 'beforeEach/afterEach 及其准备、清理 helper 禁止重载模块,写理由也不豁免;显式重启边界上一行写 `// reset-modules: <原因>`;优先用生产代码的 dispose/reset 或依赖注入',realWaits: '改用 vi.useFakeTimers() 或等待真实事件;确需真实等待时上一行写 `// real-wait: <原因>`',sourceReads: '普通单测不得读取源码文本做字面量断言;改为经生产入口验证行为,纯跨文件契约移入对应包的 guard tier',};