#!/usr/bin/env node // scripts/ci/check-test-hygiene.mjs // 테스트 위생 게이트 — "의미 없는 RED" 를 커밋 단계에서 막는다. // // 정책 정본: docs/tdd-red/02-meaningful-vs-meaningless-red.md · 06-repo-application.md // 규칙은 테스트가 검증하는 행위를 흐리지 않는 것만 담는다 (스타일 취향 아님): // // focused-test .only / fit / fdescribe — 나머지 스위트를 조용히 건너뛴다 // empty-skip 인자 없는 .skip() — 이유 없는 영구 스킵 // trivial-assertion 항상 참인 단정 (expect(true).toBe(true) 류) // conditional-assertion isVisible() 를 조건으로 삼아 단정을 감싸는 형태 — 실패해도 초록 // machine-path 개발 머신의 스크린샷/임시 경로(Antigravity brain, AppData Temp) // fixed-wait Playwright 의 waitForTimeout — web-first assert / auto-wait 로 대체 // misnamed-e2e `.e2e.test.ts` — E2E 는 Playwright(`*.spec.ts`)만, 단위는 `*.test.ts` // spec-outside-e2e e2e/ 밖의 `*.spec.ts` — Vitest/Jest 는 `*.test.ts` 로 // assertion-free 단정이 한 줄도 없는 테스트 파일 — 렌더만 하고 아무것도 증명하지 않음 // // 사용: // node scripts/ci/check-test-hygiene.mjs // node scripts/ci/check-test-hygiene.mjs --self-test import { execFileSync } from 'node:child_process' import { readFileSync } from 'node:fs' const TEST_FILE = /(?:^|\/)(?:tests?|__tests__|e2e)\/.*\.(?:test|spec)\.[cm]?[jt]sx?$|\.(?:test|spec)\.[cm]?[jt]sx?$/ const EXCLUDED_PATH = /(?:^|\/)(?:node_modules|dist|build|out|coverage|playwright-report|test-results|scratch)(?:\/|$)/ const RULES = [ { name: 'focused-test', pattern: /\b(?:describe|it|test)\.only\s*\(|\bf(?:it|describe)\s*\(/, hint: '.only 를 지우고 필요한 테스트만 -g 로 실행하세요.', }, { name: 'empty-skip', pattern: /\b(?:describe|it|test)\.skip\(\s*\)/, hint: '스킵에는 조건과 이유를 인자로 넘기세요: test.skip(condition, reason).', }, { name: 'trivial-assertion', pattern: /expect\(\s*(?:true|false|[01])\s*\)\.(?:toBe|toEqual|toStrictEqual)\(\s*(?:true|false|[01])\s*\)|assert\.(?:ok|equal|strictEqual)\(\s*(?:true|false|[01])\s*(?:,\s*(?:true|false|[01])\s*)?\)/, hint: '항상 참인 단정은 아무것도 증명하지 않습니다. 관찰 가능한 결과를 단정하세요.', }, { name: 'conditional-assertion', pattern: /if\s*\(\s*await\s+[^\n]*\.isVisible\(\)\s*\)/, hint: 'isVisible() 로 단정을 감싸면 실패해도 초록입니다. expect(locator).toBeVisible() 로 바꾸세요.', }, { name: 'machine-path', pattern: /(?:antigravity|\.gemini)[\\/]|[A-Za-z]:[\\/]{1,2}Users[\\/][^\\/\n'"`]+[\\/]AppData[\\/]Local[\\/]Temp/i, hint: '개발 머신 경로 대신 os.tmpdir()/mkdtemp 와 Playwright trace 를 쓰세요.', }, { name: 'fixed-wait', pattern: /\.waitForTimeout\(\s*\d+\s*\)/, hint: 'Playwright auto-wait / web-first assertion 을 쓰세요. waitForTimeout 은 flaky 의 근원입니다.', }, { name: 'misnamed-e2e', pathPattern: /\.e2e\.test\.[cm]?[jt]sx?$/, pattern: /^/, hint: '브라우저/E2E 는 `*.spec.ts`(Playwright), 단위·통합은 `*.test.ts`(Vitest/Jest)로 나누세요.', }, { name: 'spec-outside-e2e', pathPattern: /\.spec\.[cm]?[jt]sx?$/, excludePathPattern: /(?:^|\/)e2e\//, pattern: /^/, hint: '`*.spec.ts` 는 Playwright E2E 전용입니다. Vitest/Jest 는 `*.test.ts` 로 바꾸세요.', }, ] const ASSERTION_TOKEN = /\bexpect\s*\(|\bassert(?:\.\w+|[A-Z]\w*)?\s*\(|\bt\.(?:is|truthy|falsy|deepEqual|throws)\s*\(|\.should\b/ function normalize(path) { return path.replaceAll('\\', '/') } function isCandidate(path) { const normalized = normalize(path) if (EXCLUDED_PATH.test(normalized)) return false return TEST_FILE.test(normalized) } export function scanSource(path, source) { const normalized = normalize(path) const findings = [] for (const rule of RULES) { if (rule.pathPattern && !rule.pathPattern.test(normalized)) continue if (rule.excludePathPattern?.test(normalized)) continue const match = rule.pattern.exec(source) if (!match) continue const line = source.slice(0, match.index).split('\n').length findings.push({ rule: rule.name, path: normalized, line, hint: rule.hint }) } if (!ASSERTION_TOKEN.test(source)) { findings.push({ rule: 'assertion-free', path: normalized, line: 1, hint: '단정이 없습니다. 관찰 가능한 결과를 expect/assert 로 고정하세요.', }) } return findings } function collectTestFiles() { const output = execFileSync('git', ['ls-files', '--cached', '--others', '--exclude-standard', '-z'], { encoding: 'utf8', }) return output.split('\0').filter(Boolean).filter(isCandidate) } function selfTest() { const unsafeCases = [ ['apps/desktop/tests/unit/a.test.ts', `it.only('x', () => {})`], ['apps/desktop/tests/unit/a.test.ts', `describe.skip()`], ['apps/desktop/tests/unit/a.test.ts', `expect(true).toBe(true)`], ['apps/web/e2e/a.spec.ts', `if (await page.getByText('x').isVisible()) { expect(await page.getByText('x').isVisible()).toBeTruthy() }`], ['apps/desktop/tests/e2e/a.spec.ts', `const dir = 'C:/Users/me/AppData/Local/Temp/playwright-run'`], ['apps/desktop/tests/e2e/a.spec.ts', `const dir = 'C:/Users/me/.gemini/antigravity/brain/x/screenshots'`], ['apps/desktop/tests/e2e/a.spec.ts', `await page.waitForTimeout(1500)`], ['apps/desktop/tests/red/ads/a.e2e.test.ts', `describe('x', () => {})`], ['apps/web/tests/payple.test.ts', `describe('x', () => { it('y', () => {}) })`], ['apps/desktop/tests/unit/a.spec.ts', `expect(1).toBe(1)`], ] const safeCases = [ [ 'apps/web/e2e/billing.spec.ts', `test.skip(!hasFixtureAccess, 'Local Supabase fixture credentials are required')\nexpect(page.url()).toContain('/login')`, ], ['packages/core/__tests__/url-policy.test.ts', `expect(isFileUrlWithinRoot('file:///C:/Users/me/app/index.html', ROOT)).toBe(true)`], ['apps/desktop/tests/main/windows/x.test.ts', `expect(w.fire('will-navigate', 'file:///C:/Users/me/Downloads/evil.html')).toHaveBeenCalled()`], ['apps/desktop/tests/unit/a.test.ts', `await expect(page.getByText('x')).toBeVisible()`], ['apps/desktop/tests/e2e/a.spec.ts', `await expect(page.getByTestId('nav-home')).toHaveAttribute('aria-current', 'page')`], ] const failures = [] for (const [path, source] of unsafeCases) { if (scanSource(path, source).length === 0) failures.push(`missed unsafe fixture: ${path} :: ${source}`) } for (const [path, source] of safeCases) { const found = scanSource(path, source) if (found.length > 0) failures.push(`flagged safe fixture: ${path} :: ${found.map((f) => f.rule).join(',')}`) } if (failures.length > 0) { for (const failure of failures) console.error(failure) process.exit(1) } console.log('Test hygiene scanner self-test passed.') process.exit(0) } if (process.argv.includes('--self-test')) { selfTest() } else { const findings = [] for (const path of collectTestFiles()) { let source try { source = readFileSync(path, 'utf8') } catch { continue } findings.push(...scanSource(path, source)) } if (findings.length > 0) { console.error('의미 없는 테스트 패턴이 발견되었습니다:') for (const finding of findings.sort((a, b) => a.path.localeCompare(b.path) || a.line - b.line)) { console.error(`- ${finding.path}:${finding.line} [${finding.rule}] ${finding.hint}`) } process.exit(1) } console.log(`Test hygiene gate passed (${collectTestFiles().length} test files).`) }