|
| 1 | +/** |
| 2 | + * Test wiring gate (PROJECT §9.2 · DoD 1). |
| 3 | + * |
| 4 | + * Fails if a *.test.mjs / *.test.ps1 file exists in the repo but no package.json |
| 5 | + * script runs it, or if a wired script is not reachable from `test:unit`. |
| 6 | + * |
| 7 | + * Why: 2026-07-21 `c22dbc9` ("docs: GitHub identity README", a rebase replay) |
| 8 | + * silently dropped `test:state-io` + `test:fs-io` that `3f36e56` had added 80 |
| 9 | + * minutes earlier. Both unit tests kept passing on demand but never ran in |
| 10 | + * `npm test` or CI for weeks — a green gate that proved nothing about them. |
| 11 | + * An orphan test is worse than no test: it reads as coverage without being it. |
| 12 | + * |
| 13 | + * Contract packages own their own runner (`test:contracts` → vitest/node --test |
| 14 | + * inside the workspace package), so packages/contracts is out of scope here. |
| 15 | + * |
| 16 | + * Run: node scripts/check-test-wiring.mjs | npm run test:wiring |
| 17 | + */ |
| 18 | +import { readdir, readFile } from "node:fs/promises"; |
| 19 | +import { join, relative } from "node:path"; |
| 20 | +import { fileURLToPath } from "node:url"; |
| 21 | + |
| 22 | +const repoRoot = join(fileURLToPath(import.meta.url), "..", ".."); |
| 23 | + |
| 24 | +// Owned by `test:contracts` via the workspace filter, not by a root script. |
| 25 | +const SKIP_DIRS = new Set(["node_modules", "dist", "vendor", ".git", "versions"]); |
| 26 | +const SKIP_PREFIXES = ["packages/contracts/"]; |
| 27 | +// Live-only gates: need a running Codex / CDP / loopback control plane. |
| 28 | +// Documented as "不进 CI" in CLAUDE.md; wired but deliberately outside test:unit. |
| 29 | +const LIVE_ONLY_SCRIPTS = new Set(["test:control"]); |
| 30 | + |
| 31 | +async function walkTests(dir, acc = []) { |
| 32 | + let entries; |
| 33 | + try { |
| 34 | + entries = await readdir(dir, { withFileTypes: true }); |
| 35 | + } catch { |
| 36 | + return acc; |
| 37 | + } |
| 38 | + for (const e of entries) { |
| 39 | + const p = join(dir, e.name); |
| 40 | + if (e.isDirectory()) { |
| 41 | + if (SKIP_DIRS.has(e.name)) continue; |
| 42 | + await walkTests(p, acc); |
| 43 | + } else if (e.isFile() && /\.test\.(mjs|ps1)$/.test(e.name)) { |
| 44 | + acc.push(relative(repoRoot, p).replace(/\\/g, "/")); |
| 45 | + } |
| 46 | + } |
| 47 | + return acc; |
| 48 | +} |
| 49 | + |
| 50 | +const pkg = JSON.parse(await readFile(join(repoRoot, "package.json"), "utf8")); |
| 51 | +const scripts = pkg.scripts || {}; |
| 52 | + |
| 53 | +const testFiles = (await walkTests(repoRoot)).filter( |
| 54 | + (f) => !SKIP_PREFIXES.some((p) => f.startsWith(p)), |
| 55 | +); |
| 56 | + |
| 57 | +/** Scripts that directly invoke a given test file. */ |
| 58 | +function scriptsRunning(file) { |
| 59 | + return Object.entries(scripts) |
| 60 | + .filter(([, cmd]) => cmd.replace(/\\/g, "/").includes(file)) |
| 61 | + .map(([name]) => name); |
| 62 | +} |
| 63 | + |
| 64 | +/** Script names reachable from `test:unit` via `npm run <name>` chains. */ |
| 65 | +function reachableFromUnit() { |
| 66 | + const seen = new Set(); |
| 67 | + const queue = ["test:unit"]; |
| 68 | + while (queue.length) { |
| 69 | + const name = queue.shift(); |
| 70 | + if (seen.has(name)) continue; |
| 71 | + seen.add(name); |
| 72 | + const cmd = scripts[name]; |
| 73 | + if (!cmd) continue; |
| 74 | + for (const m of cmd.matchAll(/npm run ([\w:-]+)/g)) queue.push(m[1]); |
| 75 | + } |
| 76 | + return seen; |
| 77 | +} |
| 78 | + |
| 79 | +const reachable = reachableFromUnit(); |
| 80 | + |
| 81 | +let failed = 0; |
| 82 | +function ok(cond, msg) { |
| 83 | + if (!cond) { |
| 84 | + failed += 1; |
| 85 | + console.error("FAIL:", msg); |
| 86 | + } else { |
| 87 | + console.log("ok:", msg); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +ok(testFiles.length > 0, `discovered test files (${testFiles.length})`); |
| 92 | +ok(Boolean(scripts["test:unit"]), "test:unit script exists"); |
| 93 | + |
| 94 | +for (const file of testFiles) { |
| 95 | + const runners = scriptsRunning(file); |
| 96 | + if (runners.length === 0) { |
| 97 | + ok(false, `${file} — no package.json script runs it (orphan test)`); |
| 98 | + continue; |
| 99 | + } |
| 100 | + const live = runners.filter((r) => LIVE_ONLY_SCRIPTS.has(r)); |
| 101 | + if (live.length === runners.length) { |
| 102 | + console.log(`ok: ${file} — live-only via ${live.join(", ")} (outside test:unit by design)`); |
| 103 | + continue; |
| 104 | + } |
| 105 | + const inUnit = runners.filter((r) => reachable.has(r)); |
| 106 | + ok( |
| 107 | + inUnit.length > 0, |
| 108 | + `${file} — wired (${runners.join(", ")}) and reachable from test:unit`, |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +if (failed > 0) { |
| 113 | + console.error(`\ncheck-test-wiring: ${failed} failed`); |
| 114 | + process.exit(1); |
| 115 | +} |
| 116 | +console.log("\ncheck-test-wiring: all passed"); |
0 commit comments