retired-app-unit-evidence.mjs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { readFile, readdir } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. const repoRoot = fileURLToPath(new URL("../../", import.meta.url));
  5. const references = (text) =>
  6. [
  7. ...text.matchAll(
  8. /(?<![\w/:.-])(retired:)?((?:packages|apps|scripts)\/[^\s`|))]+\.(?:test|spec)\.[cm]?[jt]sx?)/gu,
  9. ),
  10. ].map((match) => ({ path: match[2], retired: Boolean(match[1]) }));
  11. const cells = (line) =>
  12. line.startsWith("|")
  13. ? line
  14. .split("|")
  15. .slice(1, -1)
  16. .map((cell) => cell.trim())
  17. : [];
  18. export function inspectRetiredAppUnitEvidence(markdown, { engineeringTests = new Set() } = {}) {
  19. const errors = [];
  20. const rows = markdown.split(/\r?\n/u);
  21. const aliases = new Map();
  22. const isAppUnit = (path) =>
  23. path.startsWith("packages/") &&
  24. !path.startsWith("packages/zcode-cua/") &&
  25. !/\/test\/(?:e2e|browser)\//u.test(path) &&
  26. !engineeringTests.has(path);
  27. for (const line of rows) {
  28. const row = cells(line);
  29. const id = (row[0] ?? "").replaceAll("`", "");
  30. const refs = references(row[1] ?? "");
  31. if (/^[A-Z]+$/u.test(id) && refs.length) aliases.set(id, refs);
  32. }
  33. for (const [index, line] of rows.entries()) {
  34. const refs = references(line);
  35. for (const ref of refs) {
  36. // 保留项不能套用 App 退役标签;避免再次把 CLI/CUA 或工程证据误报为已删。
  37. if (ref.retired && !isAppUnit(ref.path))
  38. errors.push(`line ${index + 1}: protected evidence mislabeled as retired: ${ref.path}`);
  39. // 顶部免责声明不会影响索引消费者;退役路径必须逐处标记,不能冒充现行测试。
  40. if (isAppUnit(ref.path) && !ref.retired)
  41. errors.push(`line ${index + 1}: unmarked retired App unit evidence: ${ref.path}`);
  42. }
  43. const row = cells(line);
  44. if (!/^(?:unit|covered(?:-unit)?|partial)$/u.test(row[1] ?? "")) continue;
  45. const evidence = row[2] ?? "";
  46. const evidenceRefs = references(evidence);
  47. for (const alias of evidence.replaceAll("`", "").split(/[、,\s+]+/u))
  48. evidenceRefs.push(...(aliases.get(alias) ?? []));
  49. if (evidenceRefs.length && evidenceRefs.every((ref) => ref.retired)) {
  50. errors.push(`line ${index + 1}: ${row[0]} is ${row[1]} with only retired evidence`);
  51. }
  52. }
  53. return errors;
  54. }
  55. export async function inspectCoverageMatrices(root = repoRoot) {
  56. const engineeringTests = new Set(
  57. JSON.parse(await readFile(resolve(root, "scripts/engineering-test-files.json"), "utf8")),
  58. );
  59. const directory = resolve(root, "docs/testing");
  60. const files = (await readdir(directory))
  61. .filter((file) => /coverage.*matrix.*\.md$/u.test(file))
  62. .sort();
  63. const results = await Promise.all(
  64. files.map(async (file) => {
  65. const errors = inspectRetiredAppUnitEvidence(
  66. await readFile(resolve(directory, file), "utf8"),
  67. { engineeringTests },
  68. );
  69. return errors.map((error) => `docs/testing/${file}: ${error}`);
  70. }),
  71. );
  72. return results.flat();
  73. }