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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import assert from "node:assert/strict";
  2. import { spawn } from "node:child_process";
  3. import { mkdtemp, mkdir, 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. function runAudit(args, env) {
  143. return new Promise((resolveRun, reject) => {
  144. const child = spawn(process.execPath, [AUDIT_PATH, ...args], {
  145. cwd: REPO_ROOT,
  146. env,
  147. stdio: ["ignore", "pipe", "pipe"],
  148. });
  149. let stdout = "";
  150. let stderr = "";
  151. child.stdout.setEncoding("utf8");
  152. child.stderr.setEncoding("utf8");
  153. child.stdout.on("data", (chunk) => {
  154. stdout += chunk;
  155. });
  156. child.stderr.on("data", (chunk) => {
  157. stderr += chunk;
  158. });
  159. child.on("error", reject);
  160. child.on("close", (exitCode) => {
  161. resolveRun({ exitCode, stderr, stdout });
  162. });
  163. });
  164. }