conversation-session-formal-admission-review.test.mjs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. import { inspectConversationSessionFormalAdmission as inspect } from "../lib/conversation-session-formal-admission.mjs";
  7. const temporaryRoots = [];
  8. after(async () => {
  9. await Promise.all(
  10. temporaryRoots.map((root) => rm(root, { force: true, recursive: true })),
  11. );
  12. });
  13. async function makeRoot() {
  14. const root = await mkdtemp(join(tmpdir(), "zcode-formal-review-"));
  15. temporaryRoots.push(root);
  16. return root;
  17. }
  18. async function writeText(root, relativePath, value) {
  19. const absolutePath = join(root, relativePath);
  20. await mkdir(dirname(absolutePath), { recursive: true });
  21. await writeFile(absolutePath, value, "utf8");
  22. }
  23. async function writeJson(root, relativePath, value) {
  24. await writeText(root, relativePath, `${JSON.stringify(value, null, 2)}\n`);
  25. }
  26. function request(id, { kind = "main", timingPolicy = "fast-text" } = {}) {
  27. return { id, kind, source: "capture", timingPolicy };
  28. }
  29. function providerEntry(manifestRequest) {
  30. const { id, ...metadata } = manifestRequest;
  31. return {
  32. id,
  33. match: { method: "POST", pathIncludes: "/messages" },
  34. metadata,
  35. response: { statusCode: 200, text: { text: "ok" } },
  36. };
  37. }
  38. async function addFormalCase(
  39. root,
  40. caseName,
  41. {
  42. commonFixture = { version: 1, fixtures: [] },
  43. specSource = "it('runs', async () => {});\n",
  44. } = {},
  45. ) {
  46. const spec = `./test/e2e/conversation-session/${caseName}.test.ts`;
  47. const caseFixturePath = `./test/e2e/fixtures/upstream/conversation-session/${caseName}.json`;
  48. const defaultRequest = request(`${caseName}-request`);
  49. const defaultManifest = {
  50. version: 1,
  51. caseName,
  52. spec,
  53. providerFixtures: [
  54. "./test/e2e/fixtures/upstream/common.json",
  55. caseFixturePath,
  56. ],
  57. fileFixtures: [],
  58. requests: [defaultRequest],
  59. };
  60. await writeText(
  61. root,
  62. `packages/desktop/test/e2e/conversation-session/${caseName}.test.ts`,
  63. specSource,
  64. );
  65. await writeJson(
  66. root,
  67. "packages/desktop/test/e2e/fixtures/upstream/common.json",
  68. commonFixture,
  69. );
  70. await writeJson(
  71. root,
  72. `packages/desktop/test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
  73. { version: 1, fixtures: [providerEntry(defaultRequest)] },
  74. );
  75. await writeJson(
  76. root,
  77. `packages/desktop/test/e2e/fixtures/cases/conversation-session/${caseName}.json`,
  78. defaultManifest,
  79. );
  80. return { defaultManifest, defaultRequest };
  81. }
  82. function codes(result, caseName) {
  83. return (
  84. result.formal.rejected.find((item) => item.caseName === caseName)
  85. ?.reasons ?? []
  86. ).map((reason) => reason.code);
  87. }
  88. test("uses the TypeScript AST for documented imports and template-expression calls", async () => {
  89. const root = await makeRoot();
  90. await addFormalCase(root, "documented-import", {
  91. specSource:
  92. "const note = `\nimport { prepareConversationE2E } from '../helpers/conversation-session.js';\n`;\n" +
  93. "it('documents migration', async () => note);\n",
  94. });
  95. await addFormalCase(root, "template-call", {
  96. specSource:
  97. "it('calls in interpolation', async () => `result: ${prepareConversationE2E()}`);\n",
  98. });
  99. const result = await inspect(root);
  100. assert.ok(
  101. result.formal.admitted.some(
  102. (item) => item.caseName === "documented-import",
  103. ),
  104. );
  105. assert.ok(codes(result, "template-call").includes("LEGACY_PREPARE_CALL"));
  106. });
  107. test("rejects absolute or traversal provider and static file fixture paths", async () => {
  108. const root = await makeRoot();
  109. const providerValues = await addFormalCase(root, "unsafe-provider");
  110. const absoluteProvider = join(root, "outside-provider.json");
  111. await writeJson(root, "outside-provider.json", { version: 1, fixtures: [] });
  112. await writeJson(
  113. root,
  114. "packages/desktop/test/e2e/fixtures/cases/conversation-session/unsafe-provider.json",
  115. {
  116. ...providerValues.defaultManifest,
  117. providerFixtures: [
  118. ...providerValues.defaultManifest.providerFixtures,
  119. absoluteProvider,
  120. ],
  121. },
  122. );
  123. const fileValues = await addFormalCase(root, "unsafe-file");
  124. const absoluteFile = join(root, "outside-static-file.txt");
  125. await writeText(root, "outside-static-file.txt", "outside\n");
  126. await writeJson(
  127. root,
  128. "packages/desktop/test/e2e/fixtures/cases/conversation-session/unsafe-file.json",
  129. {
  130. ...fileValues.defaultManifest,
  131. fileFixtures: [absoluteFile, "./test/e2e/fixtures/../../outside.txt"],
  132. },
  133. );
  134. const result = await inspect(root);
  135. assert.ok(
  136. codes(result, "unsafe-provider").includes("UNSAFE_PROVIDER_FIXTURE_PATH"),
  137. );
  138. assert.ok(codes(result, "unsafe-file").includes("UNSAFE_FILE_FIXTURE_PATH"));
  139. });
  140. test("only exempts complete runtime file descriptors with an allowed source", async () => {
  141. const root = await makeRoot();
  142. const missing = await addFormalCase(root, "descriptor-missing-source");
  143. await writeJson(
  144. root,
  145. "packages/desktop/test/e2e/fixtures/cases/conversation-session/descriptor-missing-source.json",
  146. {
  147. ...missing.defaultManifest,
  148. fileFixtures: [{ path: "/tmp/not-enough.txt" }],
  149. },
  150. );
  151. const invalid = await addFormalCase(root, "descriptor-invalid-source");
  152. await writeJson(
  153. root,
  154. "packages/desktop/test/e2e/fixtures/cases/conversation-session/descriptor-invalid-source.json",
  155. {
  156. ...invalid.defaultManifest,
  157. fileFixtures: [
  158. {
  159. description: "untrusted runtime producer",
  160. path: "/tmp/untrusted.txt",
  161. source: "downloaded-from-network",
  162. },
  163. ],
  164. },
  165. );
  166. const result = await inspect(root);
  167. for (const caseName of [
  168. "descriptor-missing-source",
  169. "descriptor-invalid-source",
  170. ]) {
  171. assert.ok(
  172. codes(result, caseName).includes(
  173. "INVALID_RUNTIME_FILE_FIXTURE_DESCRIPTOR",
  174. ),
  175. );
  176. }
  177. });
  178. test("rejects incomplete provider fixture entry shapes", async () => {
  179. const root = await makeRoot();
  180. const shapes = [
  181. ["empty-id", { ...providerEntry(request("valid")), id: "" }],
  182. ["missing-match", providerEntry(request("missing-match-request"))],
  183. ["missing-response", providerEntry(request("missing-response-request"))],
  184. [
  185. "invalid-metadata",
  186. { ...providerEntry(request("invalid-metadata-request")), metadata: [] },
  187. ],
  188. ];
  189. delete shapes[1][1].match;
  190. delete shapes[2][1].response;
  191. for (const [caseName, fixture] of shapes) {
  192. const values = await addFormalCase(root, caseName);
  193. await writeJson(
  194. root,
  195. `packages/desktop/test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
  196. { version: 1, fixtures: [fixture] },
  197. );
  198. await writeJson(
  199. root,
  200. `packages/desktop/test/e2e/fixtures/cases/conversation-session/${caseName}.json`,
  201. {
  202. ...values.defaultManifest,
  203. requests: [request(fixture.id || `${caseName}-request`)],
  204. },
  205. );
  206. }
  207. const result = await inspect(root);
  208. for (const [caseName] of shapes) {
  209. assert.ok(
  210. codes(result, caseName).includes("INVALID_PROVIDER_FIXTURE_SHAPE"),
  211. );
  212. }
  213. });
  214. test("only allows explicit common requests from canonical common.json", async () => {
  215. const root = await makeRoot();
  216. const commonRequest = request("canonical-common", { kind: "common" });
  217. const valid = await addFormalCase(root, "canonical-common-valid");
  218. await writeJson(
  219. root,
  220. "packages/desktop/test/e2e/fixtures/cases/conversation-session/canonical-common-valid.json",
  221. {
  222. ...valid.defaultManifest,
  223. requests: [commonRequest, valid.defaultRequest],
  224. },
  225. );
  226. const wrongKindRequest = request("canonical-common-wrong-kind");
  227. const wrongKind = await addFormalCase(root, "canonical-common-wrong-kind");
  228. await writeJson(
  229. root,
  230. "packages/desktop/test/e2e/fixtures/cases/conversation-session/canonical-common-wrong-kind.json",
  231. {
  232. ...wrongKind.defaultManifest,
  233. requests: [wrongKindRequest, wrongKind.defaultRequest],
  234. },
  235. );
  236. const thirdPartyRequest = request("third-party-common", { kind: "common" });
  237. const thirdParty = await addFormalCase(root, "third-party-common");
  238. const thirdPartyPath =
  239. "./test/e2e/fixtures/upstream/conversation-session/other-case.json";
  240. await writeJson(
  241. root,
  242. "packages/desktop/test/e2e/fixtures/upstream/conversation-session/other-case.json",
  243. { version: 1, fixtures: [providerEntry(thirdPartyRequest)] },
  244. );
  245. await writeJson(
  246. root,
  247. "packages/desktop/test/e2e/fixtures/cases/conversation-session/third-party-common.json",
  248. {
  249. ...thirdParty.defaultManifest,
  250. providerFixtures: [
  251. ...thirdParty.defaultManifest.providerFixtures,
  252. thirdPartyPath,
  253. ],
  254. requests: [thirdPartyRequest, thirdParty.defaultRequest],
  255. },
  256. );
  257. const duplicate = await addFormalCase(root, "duplicate-manifest-request");
  258. await writeJson(
  259. root,
  260. "packages/desktop/test/e2e/fixtures/cases/conversation-session/duplicate-manifest-request.json",
  261. {
  262. ...duplicate.defaultManifest,
  263. requests: [duplicate.defaultRequest, duplicate.defaultRequest],
  264. },
  265. );
  266. await writeJson(
  267. root,
  268. "packages/desktop/test/e2e/fixtures/upstream/common.json",
  269. {
  270. version: 1,
  271. fixtures: [providerEntry(commonRequest), providerEntry(wrongKindRequest)],
  272. },
  273. );
  274. const result = await inspect(root);
  275. assert.ok(
  276. result.formal.admitted.some(
  277. (item) => item.caseName === "canonical-common-valid",
  278. ),
  279. );
  280. assert.ok(
  281. codes(result, "canonical-common-wrong-kind").includes(
  282. "UNAUTHORIZED_COMMON_REQUEST",
  283. ),
  284. );
  285. assert.ok(
  286. codes(result, "third-party-common").includes("UNAUTHORIZED_COMMON_REQUEST"),
  287. );
  288. assert.ok(
  289. codes(result, "duplicate-manifest-request").includes(
  290. "DUPLICATE_MANIFEST_REQUEST_ID",
  291. ),
  292. );
  293. });