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

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