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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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(JSON.stringify(integration.json), /"formalRejected":1/);
  57. assert.match(integration.text.join("\n"), /formal admission: 0 admitted, 1 rejected/);
  58. assert.deepEqual(integration.errors, [
  59. "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)",
  60. ]);
  61. assert.equal(module.getCoverageAuditExitCode(true, integration.errors), 1);
  62. assert.equal(module.getCoverageAuditExitCode(false, integration.errors), 0);
  63. });
  64. test("projects pending fixture metadata rejection into audit errors", async () => {
  65. const module = await import(INTEGRATION_URL.href);
  66. const formalAdmission = {
  67. counts: {
  68. formalAdmitted: 0,
  69. formalRejected: 0,
  70. legacyHarness: 0,
  71. legacyHarnessFormal: 0,
  72. legacyHarnessPending: 0,
  73. missingFixture: 0,
  74. missingManifest: 0,
  75. pending: 1,
  76. pendingFixtureRejected: 1,
  77. },
  78. formal: { admitted: [], rejected: [] },
  79. pending: [
  80. {
  81. reasons: [
  82. {
  83. code: "PENDING_FIXTURE_FORMAL_TARGET_MISMATCH",
  84. message: "case manifest spec must resolve to the canonical formal target",
  85. subjectPath:
  86. "packages/desktop/test/e2e/fixtures/cases/conversation-session/pending.json",
  87. },
  88. ],
  89. specPath:
  90. "packages/desktop/test/e2e/conversation-session/manual-review/pending/pending.test.ts",
  91. },
  92. ],
  93. };
  94. const integration = module.buildFormalAdmissionAuditIntegration(formalAdmission);
  95. assert.match(integration.text.join("\n"), /pending fixture metadata: 1 rejected/);
  96. assert.deepEqual(integration.errors, [
  97. "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)",
  98. ]);
  99. });
  100. test("actual audit CLI reports overridden formal rejection in JSON and text", async () => {
  101. const root = await mkdtemp(join(tmpdir(), "zcode-formal-cli-"));
  102. const specPath = join(
  103. root,
  104. "packages/desktop/test/e2e/conversation-session/cli-rejected.test.ts",
  105. );
  106. await mkdir(dirname(specPath), { recursive: true });
  107. await writeFile(specPath, "it('has no fixture contract', async () => {});\n", "utf8");
  108. const env = {
  109. ...process.env,
  110. ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT: root,
  111. };
  112. try {
  113. const jsonRun = await runAudit(["--check", "--json"], env);
  114. assert.equal(jsonRun.exitCode, 1);
  115. const json = JSON.parse(jsonRun.stdout);
  116. assert.equal(json.formalAdmission.counts.formalRejected, 1);
  117. assert.match(jsonRun.stderr, /cli-rejected\.test\.ts/);
  118. assert.match(jsonRun.stderr, /MISSING_CASE_MANIFEST/);
  119. const textRun = await runAudit(["--check"], env);
  120. assert.equal(textRun.exitCode, 1);
  121. assert.match(textRun.stdout, /formal admission: 0 admitted, 1 rejected/);
  122. assert.match(textRun.stderr, /MISSING_CASE_FIXTURE/);
  123. } finally {
  124. await rm(root, { force: true, recursive: true });
  125. }
  126. });
  127. test("ordinary audit CLI rejects the formal root override as test-only", async () => {
  128. const root = await mkdtemp(join(tmpdir(), "zcode-formal-cli-untrusted-"));
  129. const env = {
  130. ...process.env,
  131. NODE_ENV: "test",
  132. ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT: root,
  133. };
  134. delete env.NODE_TEST_CONTEXT;
  135. try {
  136. const run = await runAudit(["--check"], env);
  137. assert.notEqual(run.exitCode, 0);
  138. assert.match(run.stderr, /ZCODE_CONVERSATION_SESSION_FORMAL_ADMISSION_ROOT is test-only/);
  139. } finally {
  140. await rm(root, { force: true, recursive: true });
  141. }
  142. });
  143. function runAudit(args, env) {
  144. return new Promise((resolveRun, reject) => {
  145. const child = spawn(process.execPath, [AUDIT_PATH, ...args], {
  146. cwd: REPO_ROOT,
  147. env,
  148. stdio: ["ignore", "pipe", "pipe"],
  149. });
  150. let stdout = "";
  151. let stderr = "";
  152. child.stdout.setEncoding("utf8");
  153. child.stderr.setEncoding("utf8");
  154. child.stdout.on("data", (chunk) => {
  155. stdout += chunk;
  156. });
  157. child.stderr.on("data", (chunk) => {
  158. stderr += chunk;
  159. });
  160. child.on("error", reject);
  161. child.on("close", (exitCode) => {
  162. resolveRun({ exitCode, stderr, stdout });
  163. });
  164. });
  165. }