#!/usr/bin/env node /** * SC 覆盖率三口径统一脚本(G-285-1 闭环资产) * ============================================================ * 背景:历史报告出现过三套互不相容的覆盖率数字(91.8% / 69.5% / 8.8%), * 分母不可复现,导致「覆盖是否达标」无法审计。本脚本固定三列口径, * 每轮只跑这一个命令即可得到可复现、可比的数字。 * * 三列口径(分母口径各不相同,禁止混用): * L1 台账三流 —— 分母 = sc-key-map.md 登记的全部 SC; * 分子 = 三流 TC(TC-x / TC-x-E / TC-x-P)在 * tests/test-cases/*.md 台账中同时存在的 SC 数。 * L2 执行资产引用 —— 分母同上; * 分子 = 至少被 1 个可执行资产(tests/e2e/**、tests/e2e-live/**、 * cfc-web/src/__tests__/**、cfc-frontend 测试目录)以 SC 编号 * 显式引用的 SC 数。注意:引用 ≠ 断言覆盖,仅表示有资产挂靠。 * L3 需求回链 —— 分母 = docs/需求分析/*.md + docs/superpowers/specs/*.md 文件数; * 分子 = 正文中出现任意 SC 编号的文件数。 * * 用法:node tests/scripts/sc-coverage.js [--json] * 只读,不写任何文件;退出码恒为 0(度量脚本不做门禁判定)。 */ const fs = require('fs'); const path = require('path'); const ROOT = path.resolve(__dirname, '../..'); const rd = (p) => fs.readFileSync(path.join(ROOT, p), 'utf8'); const exists = (p) => fs.existsSync(path.join(ROOT, p)); // 编号归一:SC-P-010 / SC-P-10 视为同一场景;TC 侧同规则(台账存在 TC-P-10 与 TC-P-010 两种写法) const NORM_ID = (s) => { const m = /^(SC|TC)-([A-Z]+)-0*(\d+)(.*)$/.exec(s); return m ? `${m[1]}-${m[2]}-${m[3]}${m[4]}` : s; }; const NORM = NORM_ID; function walk(dir, acc = []) { const abs = path.join(ROOT, dir); if (!fs.existsSync(abs)) return acc; for (const e of fs.readdirSync(abs, { withFileTypes: true })) { if (e.name === 'node_modules' || e.name.startsWith('.')) continue; const rel = path.posix.join(dir, e.name); if (e.isDirectory()) walk(rel, acc); else acc.push(rel); } return acc; } // ---------- 台账:sc-key-map.md ---------- const KM_PATH = 'docs/tdd/orchestration/sc-key-map.md'; if (!exists(KM_PATH)) { console.error(`FATAL 缺少场景台账 ${KM_PATH}`); process.exit(0); } const kmRaw = rd(KM_PATH).match(/\|\s*(SC-[A-Z]+-\d+)\s*\|/g) || []; const km = new Set(kmRaw.map((c) => NORM(c.replace(/\|/g, '').trim()))); // ---------- L1:三流 TC 台账 ---------- const tcFiles = (fs.existsSync(path.join(ROOT, 'tests/test-cases')) ? fs.readdirSync(path.join(ROOT, 'tests/test-cases')) .filter((f) => f.endsWith('.md')) .map((f) => `tests/test-cases/${f}`) : []); const tc = new Set(); for (const f of tcFiles) { for (const m of rd(f).matchAll(/^\|\s*(TC-[^\s|]+)\s*\|/gm)) tc.add(NORM_ID(m[1])); } const l1Hit = [...km].filter((s) => { const base = s.replace('SC-', 'TC-'); return tc.has(base) && tc.has(base + '-E') && tc.has(base + '-P'); }); // ---------- L2:执行资产引用 ---------- // 口径要点:执行资产 = 真正可运行的测试代码,含 // · tests/e2e/**(线上 API 级) // · tests/e2e-live/**(浏览器契约/UI) // · cfc-web/src/__tests__/**(管理端单测) // · cfc-frontend 测试目录(小程序单测) // · cfc-backend/src/test/java/**(后端契约/集成/单测,SC 的主要承载方之一) // ⚠️ 必须含后端 Java:历史把 L2 只按前端资产算(漏 cfc-backend/src/test/java), // 导致「后端契约已覆盖」的 SC 被误判为未覆盖,是 69.5% 与 100% 两套口径的成因之一。 const ASSET_DIRS = ['tests/e2e', 'tests/e2e-live', 'cfc-web/src/__tests__', 'cfc-frontend', 'cfc-backend/src/test/java']; const ASSET_SKIP = ['cfc-frontend/node_modules']; const assetFiles = ASSET_DIRS.flatMap((d) => walk(d)) .filter((f) => /\.(js|cjs|mjs|ts|vue|java)$/.test(f)) .filter((f) => !ASSET_SKIP.some((s) => f.startsWith(s))); const refMap = new Map(); // SC -> [files] for (const f of assetFiles) { const txt = rd(f).match(/\bSC-[A-Z]+-\d+\b/g) || []; for (const raw of txt) { const k = NORM(raw); if (!km.has(k)) continue; if (!refMap.has(k)) refMap.set(k, []); if (!refMap.get(k).includes(f)) refMap.get(k).push(f); } } const l2Hit = [...km].filter((s) => refMap.has(s)); // ---------- L2 诊断口径(用于解释历史 69.5% / 100% 的来源,禁止作为达标口径)---------- // 历史误用 1:把用例台账 md 当成「执行资产」→ 约 70%(69.5% 即此口径) // 历史误用 2:扫全 tests/ 目录(含台账)→ 恒 100%(246/246 即此口径) function scanRefs(dirs, filter) { const set = new Set(); for (const d of dirs) { for (const f of walk(d).filter(filter)) { for (const raw of rd(f).match(/\bSC-[A-Z]+-\d+\b/g) || []) { const k = NORM(raw); if (km.has(k)) set.add(k); } } } return set.size; } const isCode = (f) => /\.(js|cjs|mjs|ts|vue)$/.test(f); const isMd = (f) => f.endsWith('.md'); const diag = { withLedgerMd: scanRefs(ASSET_DIRS.concat(['tests/test-cases']), (f) => isCode(f) || isMd(f)), wholeTestsDir: scanRefs(['tests'], (f) => isCode(f) || isMd(f) || f.endsWith('.py')), }; // ---------- L3:需求回链 ---------- const reqDirs = ['docs/需求分析', 'docs/superpowers/specs']; const reqFiles = reqDirs.flatMap((d) => walk(d)).filter((f) => f.endsWith('.md')); const l3Hit = reqFiles.filter((f) => /\bSC-[A-Z]+-\d+\b/.test(rd(f))); const pct = (a, b) => (b === 0 ? '0.0' : ((a / b) * 100).toFixed(1)); const out = { generatedAt: new Date().toISOString(), caliber: { L1_ledger_three_stream: { hit: l1Hit.length, total: km.size, pct: pct(l1Hit.length, km.size) }, L2_executable_asset_ref: { hit: l2Hit.length, total: km.size, pct: pct(l2Hit.length, km.size) }, L3_requirement_backlink: { hit: l3Hit.length, total: reqFiles.length, pct: pct(l3Hit.length, reqFiles.length) }, }, inventory: { scInKeyMap: km.size, tcUnique: tc.size, tcFiles: tcFiles.length, assetFiles: assetFiles.length, requirementFiles: reqFiles.length, }, l1Miss: [...km].filter((s) => !l1Hit.includes(s)).sort(), l2Miss: [...km].filter((s) => !l2Hit.includes(s)).sort(), l2Diag: { note: '诊断用:历史 69.5% ≈ withLedgerMd,历史 246/246=100% ≈ wholeTestsDir;均非达标口径', withLedgerMd: { hit: diag.withLedgerMd, total: km.size, pct: pct(diag.withLedgerMd, km.size) }, wholeTestsDir: { hit: diag.wholeTestsDir, total: km.size, pct: pct(diag.wholeTestsDir, km.size) }, }, }; if (process.argv.includes('--json')) { console.log(JSON.stringify(out, null, 2)); } else { const { caliber: c, inventory: i } = out; console.log('=== SC 覆盖率三口径(单一脚本 · 可复现)==='); console.log(`L1 台账三流 ${c.L1_ledger_three_stream.hit}/${c.L1_ledger_three_stream.total} = ${c.L1_ledger_three_stream.pct}%`); console.log(`L2 执行资产引用 ${c.L2_executable_asset_ref.hit}/${c.L2_executable_asset_ref.total} = ${c.L2_executable_asset_ref.pct}%`); console.log(`L3 需求回链 ${c.L3_requirement_backlink.hit}/${c.L3_requirement_backlink.total} = ${c.L3_requirement_backlink.pct}%`); console.log(`资产盘点:SC ${i.scInKeyMap} / 唯一 TC ${i.tcUnique} / TC 台账 ${i.tcFiles} 文件 / 执行资产 ${i.assetFiles} 文件 / 需求文档 ${i.requirementFiles} 文件`); console.log(`L1 缺三流 ${out.l1Miss.length} 个:${out.l1Miss.join(' ') || '无'}`); console.log(`L2 无执行资产引用 ${out.l2Miss.length} 个:${out.l2Miss.slice(0, 40).join(' ') || '无'}${out.l2Miss.length > 40 ? ' ...' : ''}`); console.log(`[诊断] 含台账md ${out.l2Diag.withLedgerMd.hit}/${out.l2Diag.withLedgerMd.total}=${out.l2Diag.withLedgerMd.pct}%(历史 69.5% 来源);扫全 tests/ ${out.l2Diag.wholeTestsDir.hit}/${out.l2Diag.wholeTestsDir.total}=${out.l2Diag.wholeTestsDir.pct}%(历史 100% 来源)`); }