conversation-session-formal-admission-audit.test.mjs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import assert from "node:assert/strict";
  2. import { spawn } from "node:child_process";
  3. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { dirname, join, resolve } from "node:path";
  6. import { test } from "node:test";
  7. import { fileURLToPath } from "node:url";
  8. const INTEGRATION_URL = new URL(
  9. "../lib/conversation-session-formal-admission-audit.mjs",
  10. import.meta.url,
  11. );
  12. const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
  13. const AUDIT_PATH = join(REPO_ROOT, "scripts/audit-conversation-session-case-coverage.mjs");
  14. test("projects formal rejection into audit errors, JSON, text, and nonzero check", async () => {
  15. let module;
  16. try {
  17. module = await import(INTEGRATION_URL.href);
  18. } catch (error) {
  19. assert.fail(`formal admission audit integration is unavailable: ${error.message}`);
  20. }
  21. const formalAdmission = {
  22. counts: {
  23. formalAdmitted: 0,
  24. formalRejected: 1,
  25. legacyHarness: 0,
  26. legacyHarnessFormal: 0,
  27. legacyHarnessPending: 0,
  28. missingFixture: 1,
  29. missingManifest: 0,
  30. pending: 0,
  31. pendingFixtureRejected: 0,
  32. },
  33. formal: {
  34. admitted: [],
  35. rejected: [
  36. {
  37. caseName: "rejected-case",
  38. legacyHarness: [],
  39. reasons: [
  40. {
  41. code: "MISSING_CASE_FIXTURE",
  42. message: "case-local provider fixture is missing",
  43. specPath: "packages/desktop/test/e2e/conversation-session/rejected-case.test.ts",
  44. subjectPath:
  45. "packages/desktop/test/e2e/fixtures/upstream/conversation-session/rejected-case.json",
  46. },
  47. ],
  48. specPath: "packages/desktop/test/e2e/conversation-session/rejected-case.test.ts",
  49. },
  50. ],
  51. },
  52. pending: [],
  53. };
  54. const integration = module.buildFormalAdmissionAuditIntegration(formalAdmission);
  55. assert.equal(integration.json, formalAdmission);
  56. assert.match(integration.text.join("\n"), /formal admission: 0 admitted, 1 rejected/);
  57. assert.deepEqual(integration.errors, [
  58. "formal admission rejected packages/desktop/test/e2e/conversation-session/rejected-case.test.ts: MISSING_CASE_FIXTURE: case-local provider fixture is missing (packages/desktop/test/e2e/fixtures/upstream/conversation-session/rejected-case.json)",
  59. ]);
  60. assert.equal(module.getCoverageAuditExitCode(true, integration.errors), 1);
  61. assert.equal(module.getCoverageAuditExitCode(false, integration.errors), 0);
  62. });
  63. test("projects pending fixture metadata rejection into audit errors", async () => {
  64. const module = await import(INTEGRATION_URL.href);
  65. const formalAdmission = {
  66. counts: {
  67. formalAdmitted: 0,
  68. formalRejected: 0,
  69. legacyHarness: 0,
  70. legacyHarnessFormal: 0,
  71. legacyHarnessPending: 0,
  72. missingFixture: 0,
  73. missingManifest: 0,
  74. pending: 1,
  75. pendingFixtureRejected: 1,
  76. },
  77. formal: { admitted: [], rejected: [] },
  78. pending: [
  79. {
  80. reasons: [
  81. {
  82. code: "PENDING_FIXTURE_FORMAL_TARGET_MISMATCH",
  83. message: "case manifest spec must resolve to the canonical formal target",
  84. subjectPath:
  85. "packages/desktop/test/e2e/fixtures/cases/conversation-session/pending.json",
  86. },
  87. ],
  88. specPath:
  89. "packages/desktop/test/e2e/conversation-session/manual-review/pending/pending.test.ts",
  90. },
  91. ],
  92. };
  93. const integration = module.buildFormalAdmissionAuditIntegration(formalAdmission);
  94. assert.match(integration.text.join("\n"), /pending fixture metadata: 1 rejected/);
  95. assert.deepEqual(integration.errors, [
  96. "pending fixture metadata rejected packages/desktop/test/e2e/conversation-session/manual-review/pending/pending.test.ts: PENDING_FIXTURE_FORMAL_TARGET_MISMATCH: case manifest spec must resolve to the canonical formal target (packages/desktop/test/e2e/fixtures/cases/conversation-session/pending.json)",
  97. ]);
  98. });
  99. test("actual audit CLI reports overridden formal rejection in JSON and text", async () => {
  100. const root = await mkdtemp(join(tmpdir(), "zcode-formal-cli-"));
  101. const specPath = join(
  102. root,
  103. "packages/desktop/test/e2e/conversation-session/cli-rejected.test.ts",
  104. );
  105. await mkdir(dirname(specPath), { recursive: true });
  106. await writeFile(specPath, "it('has no fixture contract', async () => {});\n", "utf8");
  107. const env = {
  108. ...process.env,
  109. ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT: root,
  110. };
  111. try {
  112. const jsonRun = await runAudit(["--check", "--json"], env);
  113. assert.equal(jsonRun.exitCode, 1);
  114. const json = JSON.parse(jsonRun.stdout);
  115. assert.equal(json.formalAdmission.counts.formalRejected, 1);
  116. assert.match(jsonRun.stderr, /cli-rejected\.test\.ts/);
  117. assert.match(jsonRun.stderr, /MISSING_CASE_MANIFEST/);
  118. const textRun = await runAudit(["--check"], env);
  119. assert.equal(textRun.exitCode, 1);
  120. assert.match(textRun.stdout, /formal admission: 0 admitted, 1 rejected/);
  121. assert.match(textRun.stderr, /MISSING_CASE_FIXTURE/);
  122. } finally {
  123. await rm(root, { force: true, recursive: true });
  124. }
  125. });
  126. test("ordinary audit CLI rejects the formal root override as test-only", async () => {
  127. const root = await mkdtemp(join(tmpdir(), "zcode-formal-cli-untrusted-"));
  128. const env = {
  129. ...process.env,
  130. NODE_ENV: "test",
  131. ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT: root,
  132. };
  133. delete env.NODE_TEST_CONTEXT;
  134. try {
  135. const run = await runAudit(["--check"], env);
  136. assert.notEqual(run.exitCode, 0);
  137. assert.match(run.stderr, /ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT is test-only/);
  138. } finally {
  139. await rm(root, { force: true, recursive: true });
  140. }
  141. });
  142. test("current coverage audit stays consistent after App unit evidence retirement", async () => {
  143. const env = { ...process.env };
  144. delete env.ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT;
  145. const run = await runAudit(["--check", "--json"], env);
  146. assert.equal(run.exitCode, 0, run.stderr);
  147. const report = JSON.parse(run.stdout);
  148. assert.deepEqual(report.errors, []);
  149. const matrix = await readFile(
  150. join(REPO_ROOT, "docs/testing/conversation-session-e2e-coverage-matrix.md"),
  151. "utf8",
  152. );
  153. // 修复原因:顶部退役说明不影响机器索引;按主路径审计的两字母缩写检查路径,避免已删业务单测仍被计数。
  154. const indexedSpecs = [...matrix.matchAll(/^\|\s*`[A-Z]{2}`\s*\|([^|]+)\|/gm)]
  155. .flatMap((row) => [
  156. ...row[1].matchAll(/`((?:packages|apps)\/[^`]+\.(?:test|spec)\.[cm]?[jt]sx?)`/g),
  157. ])
  158. .map((match) => match[1]);
  159. assert.ok(indexedSpecs.length > 0);
  160. for (const spec of indexedSpecs) {
  161. assert.ok(
  162. spec.startsWith("packages/desktop/test/e2e/") || spec.startsWith("apps/zcode-cli/"),
  163. `retired App unit evidence remains in the automation index: ${spec}`,
  164. );
  165. await readFile(join(REPO_ROOT, spec));
  166. }
  167. });
  168. test("VI03 keeps its retained CLI unit evidence with unambiguous full paths", async () => {
  169. const matrix = await readFile(
  170. join(REPO_ROOT, "docs/testing/conversation-session-e2e-coverage-matrix.md"),
  171. "utf8",
  172. );
  173. const row = matrix.split("\n").find((line) => /^\|\s*VI03\s*\|/.test(line));
  174. assert.ok(row, "VI03 is missing from the coverage matrix");
  175. assert.equal(row.split("|")[2].trim(), "covered-unit");
  176. for (const spec of [
  177. "apps/zcode-cli/packages/core/tests/read-tool-contract.test.ts",
  178. "apps/zcode-cli/packages/adapters/tests/fs-context.test.ts",
  179. ]) {
  180. assert.ok(row.includes(`\`${spec}\``), `VI03 must identify its CLI evidence: ${spec}`);
  181. await readFile(join(REPO_ROOT, spec));
  182. }
  183. });
  184. function runAudit(args, env) {
  185. return new Promise((resolveRun, reject) => {
  186. const child = spawn(process.execPath, [AUDIT_PATH, ...args], {
  187. cwd: REPO_ROOT,
  188. env,
  189. stdio: ["ignore", "pipe", "pipe"],
  190. });
  191. let stdout = "";
  192. let stderr = "";
  193. child.stdout.setEncoding("utf8");
  194. child.stderr.setEncoding("utf8");
  195. child.stdout.on("data", (chunk) => {
  196. stdout += chunk;
  197. });
  198. child.stderr.on("data", (chunk) => {
  199. stderr += chunk;
  200. });
  201. child.on("error", reject);
  202. child.on("close", (exitCode) => {
  203. resolveRun({ exitCode, stderr, stdout });
  204. });
  205. });
  206. }