conversation-session-formal-admission.test.mjs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. import assert from "node:assert/strict";
  2. import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { dirname, join } from "node:path";
  5. import { after, test } from "node:test";
  6. const INSPECTOR_URL = new URL("../lib/conversation-session-formal-admission.mjs", import.meta.url);
  7. const temporaryRoots = [];
  8. after(async () => {
  9. await Promise.all(temporaryRoots.map((root) => rm(root, { force: true, recursive: true })));
  10. });
  11. async function inspect(root) {
  12. let module;
  13. try {
  14. module = await import(INSPECTOR_URL.href);
  15. } catch (error) {
  16. assert.fail(`formal admission inspector is unavailable: ${error.message}`);
  17. }
  18. return module.inspectConversationSessionFormalAdmission(root);
  19. }
  20. async function makeRoot() {
  21. const root = await mkdtemp(join(tmpdir(), "zcode-formal-admission-"));
  22. temporaryRoots.push(root);
  23. return root;
  24. }
  25. async function writeText(root, relativePath, value) {
  26. const absolutePath = join(root, relativePath);
  27. await mkdir(dirname(absolutePath), { recursive: true });
  28. await writeFile(absolutePath, value, "utf8");
  29. }
  30. async function writeJson(root, relativePath, value) {
  31. await writeText(root, relativePath, `${JSON.stringify(value, null, 2)}\n`);
  32. }
  33. function request(
  34. id,
  35. { kind = "main", source = "capture", syntheticReason, timingPolicy = "fast-text" } = {},
  36. ) {
  37. return {
  38. id,
  39. kind,
  40. source,
  41. timingPolicy,
  42. ...(syntheticReason === undefined ? {} : { syntheticReason }),
  43. };
  44. }
  45. function providerEntry(manifestRequest) {
  46. const { id, ...metadata } = manifestRequest;
  47. return {
  48. id,
  49. metadata,
  50. match: { method: "POST", pathIncludes: "/messages" },
  51. response: { statusCode: 200, text: { text: "ok" } },
  52. };
  53. }
  54. async function addFormalCase(
  55. root,
  56. caseName,
  57. {
  58. caseFixture,
  59. commonFixture = { version: 1, fixtures: [] },
  60. manifest,
  61. specSource = "it('runs', async () => {});\n",
  62. writeCaseFixture = true,
  63. writeCommonFixture = true,
  64. writeManifest = true,
  65. } = {},
  66. ) {
  67. const spec = `./test/e2e/conversation-session/${caseName}.test.ts`;
  68. const caseFixturePath = `./test/e2e/fixtures/upstream/conversation-session/${caseName}.json`;
  69. const defaultRequest = request(`${caseName}-request`);
  70. const defaultManifest = {
  71. version: 1,
  72. caseName,
  73. spec,
  74. providerFixtures: ["./test/e2e/fixtures/upstream/common.json", caseFixturePath],
  75. fileFixtures: [],
  76. requests: [defaultRequest],
  77. };
  78. const defaultCaseFixture = {
  79. version: 1,
  80. caseName,
  81. spec,
  82. fixtures: [providerEntry(defaultRequest)],
  83. };
  84. await writeText(
  85. root,
  86. `packages/desktop/test/e2e/conversation-session/${caseName}.test.ts`,
  87. specSource,
  88. );
  89. if (writeCommonFixture) {
  90. await writeJson(root, "packages/desktop/test/e2e/fixtures/upstream/common.json", commonFixture);
  91. }
  92. if (writeCaseFixture) {
  93. await writeJson(
  94. root,
  95. `packages/desktop/test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
  96. caseFixture ?? defaultCaseFixture,
  97. );
  98. }
  99. if (writeManifest) {
  100. const manifestValue = manifest ?? defaultManifest;
  101. const manifestPath = `packages/desktop/test/e2e/fixtures/cases/conversation-session/${caseName}.json`;
  102. if (typeof manifestValue === "string") {
  103. await writeText(root, manifestPath, manifestValue);
  104. } else {
  105. await writeJson(root, manifestPath, manifestValue);
  106. }
  107. }
  108. return { caseFixturePath, defaultManifest, defaultRequest, spec };
  109. }
  110. function codes(result, caseName) {
  111. return result.formal.rejected
  112. .find((item) => item.caseName === caseName)
  113. ?.reasons.map((reason) => reason.code);
  114. }
  115. test("admits a valid V4 formal case and permits compatible TID_CHAT_TOOL usage", async () => {
  116. const root = await makeRoot();
  117. await addFormalCase(root, "valid-v4", {
  118. specSource:
  119. "const selector = '[data-testid=\"TID_CHAT_TOOL_RESULT\"]';\n" +
  120. "it('runs', async () => selector);\n",
  121. });
  122. const result = await inspect(root);
  123. assert.deepEqual(result.counts, {
  124. formalAdmitted: 1,
  125. formalRejected: 0,
  126. legacyHarness: 0,
  127. legacyHarnessFormal: 0,
  128. legacyHarnessPending: 0,
  129. missingFixture: 0,
  130. missingManifest: 0,
  131. pending: 0,
  132. pendingFixtureRejected: 0,
  133. });
  134. assert.deepEqual(result.formal.admitted, [
  135. {
  136. caseName: "valid-v4",
  137. specPath: "packages/desktop/test/e2e/conversation-session/valid-v4.test.ts",
  138. },
  139. ]);
  140. });
  141. test("treats an omitted optional fileFixtures field as an empty list", async () => {
  142. const root = await makeRoot();
  143. const values = await addFormalCase(root, "no-file-fixtures");
  144. const { fileFixtures: _fileFixtures, ...manifest } = values.defaultManifest;
  145. await writeJson(
  146. root,
  147. "packages/desktop/test/e2e/fixtures/cases/conversation-session/no-file-fixtures.json",
  148. manifest,
  149. );
  150. const result = await inspect(root);
  151. assert.equal(result.counts.formalAdmitted, 1);
  152. assert.equal(result.counts.formalRejected, 0);
  153. });
  154. test("accepts a runtime-created file fixture descriptor without requiring it on disk", async () => {
  155. const root = await makeRoot();
  156. const values = await addFormalCase(root, "runtime-file-fixture");
  157. await writeJson(
  158. root,
  159. "packages/desktop/test/e2e/fixtures/cases/conversation-session/runtime-file-fixture.json",
  160. {
  161. ...values.defaultManifest,
  162. fileFixtures: [
  163. {
  164. description: "provider tool creates this file during the replay",
  165. path: "/tmp/generated-by-provider.txt",
  166. source: "created-by-provider-tool",
  167. },
  168. ],
  169. },
  170. );
  171. const result = await inspect(root);
  172. assert.equal(result.counts.formalAdmitted, 1);
  173. assert.equal(result.counts.formalRejected, 0);
  174. });
  175. test("rejects exact legacy helper imports and prepareConversationE2E calls", async () => {
  176. const root = await makeRoot();
  177. await addFormalCase(root, "legacy-import", {
  178. specSource:
  179. "import { helper } from '../helpers/conversation-session.js';\n" +
  180. "it('runs', async () => helper);\n",
  181. });
  182. await addFormalCase(root, "legacy-prepare", {
  183. specSource: "it('runs', async () => prepareConversationE2E());\n",
  184. });
  185. const result = await inspect(root);
  186. assert.equal(result.counts.formalRejected, 2);
  187. assert.equal(result.counts.legacyHarnessFormal, 2);
  188. assert.ok(codes(result, "legacy-import").includes("LEGACY_HELPER_IMPORT"));
  189. assert.ok(codes(result, "legacy-prepare").includes("LEGACY_PREPARE_CALL"));
  190. });
  191. test("rejects missing manifests and missing case-local fixtures", async () => {
  192. const root = await makeRoot();
  193. await addFormalCase(root, "missing-manifest", { writeManifest: false });
  194. await addFormalCase(root, "missing-fixture", { writeCaseFixture: false });
  195. const result = await inspect(root);
  196. assert.equal(result.counts.missingManifest, 1);
  197. assert.equal(result.counts.missingFixture, 1);
  198. assert.ok(codes(result, "missing-manifest").includes("MISSING_CASE_MANIFEST"));
  199. assert.ok(codes(result, "missing-fixture").includes("MISSING_CASE_FIXTURE"));
  200. });
  201. test("rejects invalid manifest JSON, version, case identity, and spec path", async () => {
  202. const root = await makeRoot();
  203. await addFormalCase(root, "invalid-json", { manifest: "{not-json\n" });
  204. const wrongVersion = await addFormalCase(root, "wrong-version");
  205. await writeJson(
  206. root,
  207. "packages/desktop/test/e2e/fixtures/cases/conversation-session/wrong-version.json",
  208. { ...wrongVersion.defaultManifest, version: 2 },
  209. );
  210. const wrongIdentity = await addFormalCase(root, "wrong-identity");
  211. await writeJson(
  212. root,
  213. "packages/desktop/test/e2e/fixtures/cases/conversation-session/wrong-identity.json",
  214. { ...wrongIdentity.defaultManifest, caseName: "someone-else" },
  215. );
  216. const wrongSpec = await addFormalCase(root, "wrong-spec");
  217. await writeJson(
  218. root,
  219. "packages/desktop/test/e2e/fixtures/cases/conversation-session/wrong-spec.json",
  220. { ...wrongSpec.defaultManifest, spec: "./test/e2e/other.test.ts" },
  221. );
  222. const wrongFixtureSpec = await addFormalCase(root, "wrong-fixture-spec");
  223. await writeJson(
  224. root,
  225. "packages/desktop/test/e2e/fixtures/upstream/conversation-session/wrong-fixture-spec.json",
  226. {
  227. version: 1,
  228. caseName: "wrong-fixture-spec",
  229. spec: "./test/e2e/other.test.ts",
  230. fixtures: [providerEntry(wrongFixtureSpec.defaultRequest)],
  231. },
  232. );
  233. const result = await inspect(root);
  234. assert.ok(codes(result, "invalid-json").includes("INVALID_MANIFEST_JSON"));
  235. assert.ok(codes(result, "wrong-version").includes("INVALID_MANIFEST_VERSION"));
  236. assert.ok(codes(result, "wrong-identity").includes("MANIFEST_CASE_NAME_MISMATCH"));
  237. assert.ok(codes(result, "wrong-spec").includes("MANIFEST_SPEC_PATH_MISMATCH"));
  238. assert.ok(codes(result, "wrong-fixture-spec").includes("CASE_FIXTURE_SPEC_PATH_MISMATCH"));
  239. });
  240. test("rejects providerRequestPolicy none when requests are declared", async () => {
  241. const root = await makeRoot();
  242. const values = await addFormalCase(root, "policy-none");
  243. await writeJson(
  244. root,
  245. "packages/desktop/test/e2e/fixtures/cases/conversation-session/policy-none.json",
  246. { ...values.defaultManifest, providerRequestPolicy: "none" },
  247. );
  248. const result = await inspect(root);
  249. assert.ok(codes(result, "policy-none").includes("PROVIDER_REQUEST_POLICY_NONE_WITH_REQUESTS"));
  250. });
  251. test("rejects synthetic requests without a non-empty synthetic reason", async () => {
  252. const root = await makeRoot();
  253. const values = await addFormalCase(root, "synthetic-no-reason");
  254. const syntheticRequest = request("synthetic-request", {
  255. source: "synthetic",
  256. });
  257. await writeJson(
  258. root,
  259. "packages/desktop/test/e2e/fixtures/cases/conversation-session/synthetic-no-reason.json",
  260. { ...values.defaultManifest, requests: [syntheticRequest] },
  261. );
  262. await writeJson(
  263. root,
  264. "packages/desktop/test/e2e/fixtures/upstream/conversation-session/synthetic-no-reason.json",
  265. { version: 1, fixtures: [providerEntry(syntheticRequest)] },
  266. );
  267. const result = await inspect(root);
  268. assert.ok(codes(result, "synthetic-no-reason").includes("SYNTHETIC_REASON_MISSING"));
  269. });
  270. test("rejects case-local request ledger ID, order, and metadata mismatches", async () => {
  271. const root = await makeRoot();
  272. const idValues = await addFormalCase(root, "ledger-id");
  273. await writeJson(
  274. root,
  275. "packages/desktop/test/e2e/fixtures/cases/conversation-session/ledger-id.json",
  276. { ...idValues.defaultManifest, requests: [request("invented-id")] },
  277. );
  278. const orderValues = await addFormalCase(root, "ledger-order");
  279. const first = request("first");
  280. const second = request("second");
  281. await writeJson(
  282. root,
  283. "packages/desktop/test/e2e/fixtures/cases/conversation-session/ledger-order.json",
  284. { ...orderValues.defaultManifest, requests: [second, first] },
  285. );
  286. await writeJson(
  287. root,
  288. "packages/desktop/test/e2e/fixtures/upstream/conversation-session/ledger-order.json",
  289. { version: 1, fixtures: [providerEntry(first), providerEntry(second)] },
  290. );
  291. const metadataValues = await addFormalCase(root, "ledger-metadata");
  292. await writeJson(
  293. root,
  294. "packages/desktop/test/e2e/fixtures/cases/conversation-session/ledger-metadata.json",
  295. {
  296. ...metadataValues.defaultManifest,
  297. requests: [request("ledger-metadata-request", { timingPolicy: "fault-stream" })],
  298. },
  299. );
  300. const result = await inspect(root);
  301. assert.ok(codes(result, "ledger-id").includes("REQUEST_LEDGER_ID_MISMATCH"));
  302. assert.ok(codes(result, "ledger-order").includes("REQUEST_LEDGER_ORDER_MISMATCH"));
  303. assert.ok(codes(result, "ledger-metadata").includes("REQUEST_LEDGER_METADATA_MISMATCH"));
  304. });
  305. test("rejects omitted case fixtures and missing declared provider or file paths", async () => {
  306. const root = await makeRoot();
  307. const omitted = await addFormalCase(root, "omitted-case-fixture");
  308. await writeJson(
  309. root,
  310. "packages/desktop/test/e2e/fixtures/cases/conversation-session/omitted-case-fixture.json",
  311. {
  312. ...omitted.defaultManifest,
  313. providerFixtures: ["./test/e2e/fixtures/upstream/common.json"],
  314. },
  315. );
  316. const missingProvider = await addFormalCase(root, "missing-provider-path");
  317. await writeJson(
  318. root,
  319. "packages/desktop/test/e2e/fixtures/cases/conversation-session/missing-provider-path.json",
  320. {
  321. ...missingProvider.defaultManifest,
  322. providerFixtures: [
  323. ...missingProvider.defaultManifest.providerFixtures,
  324. "./test/e2e/fixtures/upstream/not-there.json",
  325. ],
  326. },
  327. );
  328. const missingFile = await addFormalCase(root, "missing-file-path");
  329. await writeJson(
  330. root,
  331. "packages/desktop/test/e2e/fixtures/cases/conversation-session/missing-file-path.json",
  332. {
  333. ...missingFile.defaultManifest,
  334. fileFixtures: ["./test/e2e/fixtures/fs/not-there/input.txt"],
  335. },
  336. );
  337. const result = await inspect(root);
  338. assert.ok(codes(result, "omitted-case-fixture").includes("MANIFEST_OMITS_CASE_FIXTURE"));
  339. assert.ok(codes(result, "missing-provider-path").includes("MISSING_PROVIDER_FIXTURE"));
  340. assert.ok(codes(result, "missing-file-path").includes("MISSING_FILE_FIXTURE"));
  341. });
  342. test("counts a pending legacy spec without rejecting formal admission", async () => {
  343. const root = await makeRoot();
  344. await writeText(
  345. root,
  346. "packages/desktop/test/e2e/conversation-session/manual-review/pending/legacy.test.ts",
  347. "import { prepareConversationE2E } from '../../../helpers/conversation-session.js';\n" +
  348. "it('runs', async () => prepareConversationE2E());\n",
  349. );
  350. const result = await inspect(root);
  351. assert.equal(result.counts.formalAdmitted, 0);
  352. assert.equal(result.counts.formalRejected, 0);
  353. assert.equal(result.counts.pending, 1);
  354. assert.equal(result.counts.pendingFixtureRejected, 0);
  355. assert.equal(result.counts.legacyHarness, 1);
  356. assert.equal(result.counts.legacyHarnessPending, 1);
  357. assert.deepEqual(result.formal.rejected, []);
  358. });
  359. test("checks pending fixture metadata against the canonical formal target", async () => {
  360. const root = await makeRoot();
  361. const caseName = "pending-contract";
  362. const pendingSpec = `./test/e2e/conversation-session/manual-review/pending/${caseName}.test.ts`;
  363. const canonicalSpec = `./test/e2e/conversation-session/${caseName}.test.ts`;
  364. await writeText(
  365. root,
  366. `packages/desktop/test/e2e/conversation-session/manual-review/pending/${caseName}.test.ts`,
  367. "it('runs', async () => {});\n",
  368. );
  369. await writeJson(
  370. root,
  371. `packages/desktop/test/e2e/fixtures/cases/conversation-session/${caseName}.json`,
  372. { version: 1, caseName, spec: canonicalSpec },
  373. );
  374. await writeJson(
  375. root,
  376. `packages/desktop/test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
  377. { version: 1, caseName, spec: pendingSpec, fixtures: [] },
  378. );
  379. const result = await inspect(root);
  380. assert.equal(result.counts.pendingFixtureRejected, 1);
  381. assert.deepEqual(
  382. result.pending[0].reasons.map((reason) => reason.code),
  383. ["PENDING_FIXTURE_FORMAL_TARGET_MISMATCH"],
  384. );
  385. assert.equal(result.pending[0].canonicalSpecPath.endsWith(`${caseName}.test.ts`), true);
  386. });