conversation-session-formal-admission-contract.mjs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import { resolve } from "node:path";
  2. import {
  3. validateProviderFixture,
  4. validateRequestArray,
  5. validateRequestLedger,
  6. validateSyntheticReasons,
  7. } from "./conversation-session-formal-admission-ledger.mjs";
  8. import {
  9. diagnostic,
  10. isRecord,
  11. readJson,
  12. toRepoPath,
  13. } from "./conversation-session-formal-admission-utils.mjs";
  14. import {
  15. resolveProviderFixturePaths,
  16. validateFileFixtures,
  17. validateStaticFilePaths,
  18. } from "./conversation-session-formal-admission-paths.mjs";
  19. export async function validateFormalAdmissionManifest({
  20. absoluteSpecPath,
  21. caseFixture,
  22. caseFixturePath,
  23. caseName,
  24. manifest,
  25. manifestPath,
  26. reasons,
  27. root,
  28. specPath,
  29. }) {
  30. const manifestSubjectPath = toRepoPath(root, manifestPath);
  31. if (!isRecord(manifest)) {
  32. reasons.push(
  33. diagnostic({
  34. code: "INVALID_MANIFEST_SHAPE",
  35. message: "case manifest must be a JSON object",
  36. specPath,
  37. subjectPath: manifestSubjectPath,
  38. }),
  39. );
  40. return;
  41. }
  42. validateManifestIdentity({
  43. absoluteSpecPath,
  44. caseName,
  45. manifest,
  46. manifestSubjectPath,
  47. reasons,
  48. root,
  49. specPath,
  50. });
  51. validateCaseFixtureIdentity({
  52. absoluteSpecPath,
  53. caseFixture,
  54. caseFixturePath,
  55. caseName,
  56. reasons,
  57. root,
  58. specPath,
  59. });
  60. const providerFixtures = validateStringArray(
  61. manifest.providerFixtures,
  62. "providerFixtures",
  63. manifestSubjectPath,
  64. specPath,
  65. reasons,
  66. );
  67. const fileFixtures = validateFileFixtures(
  68. manifest.fileFixtures ?? [],
  69. manifestSubjectPath,
  70. specPath,
  71. reasons,
  72. );
  73. const requests = validateRequestArray(manifest.requests, manifestSubjectPath, specPath, reasons);
  74. if (!providerFixtures || !fileFixtures || !requests) {
  75. return;
  76. }
  77. const desktopRoot = resolve(root, "packages/desktop");
  78. const providerFixtureRoot = resolve(desktopRoot, "test/e2e/fixtures/upstream");
  79. const staticFileFixtureRoot = resolve(desktopRoot, "test/e2e/fixtures");
  80. const resolvedProviderFixtures = await resolveProviderFixturePaths({
  81. desktopRoot,
  82. providerFixtureRoot,
  83. providerFixtures,
  84. reasons,
  85. root,
  86. specPath,
  87. });
  88. if (!resolvedProviderFixtures.some((fixture) => fixture.absolutePath === caseFixturePath)) {
  89. reasons.push(
  90. diagnostic({
  91. code: "MANIFEST_OMITS_CASE_FIXTURE",
  92. message: "providerFixtures does not include the case-local provider fixture",
  93. specPath,
  94. subjectPath: manifestSubjectPath,
  95. }),
  96. );
  97. }
  98. const providerFiles = await readProviderFiles({
  99. canonicalCommonPath: resolve(providerFixtureRoot, "common.json"),
  100. reasons,
  101. resolvedProviderFixtures,
  102. root,
  103. specPath,
  104. });
  105. await validateStaticFilePaths({
  106. desktopRoot,
  107. fixtureRoot: staticFileFixtureRoot,
  108. reasons,
  109. root,
  110. specPath,
  111. staticPaths: fileFixtures.staticPaths,
  112. });
  113. validateProviderPolicy({
  114. manifest,
  115. manifestSubjectPath,
  116. reasons,
  117. requests,
  118. specPath,
  119. });
  120. validateSyntheticReasons(requests, manifestSubjectPath, specPath, reasons);
  121. for (const providerFile of providerFiles) {
  122. validateSyntheticReasons(
  123. providerFile.fixtures.map((fixture) => ({
  124. id: fixture.id,
  125. ...(isRecord(fixture.metadata) ? fixture.metadata : {}),
  126. })),
  127. providerFile.subjectPath,
  128. specPath,
  129. reasons,
  130. );
  131. }
  132. const declaredCaseFixture = providerFiles.find(
  133. (fixture) => fixture.absolutePath === caseFixturePath,
  134. );
  135. const fallbackCaseFixtures = validateProviderFixture(
  136. caseFixture,
  137. toRepoPath(root, caseFixturePath),
  138. specPath,
  139. reasons,
  140. { reportShapeError: false },
  141. );
  142. const caseFixtures = declaredCaseFixture?.fixtures ?? fallbackCaseFixtures ?? null;
  143. if (caseFixtures?.length === 0 && manifest.providerRequestPolicy !== "none") {
  144. reasons.push(
  145. diagnostic({
  146. code: "EMPTY_CASE_FIXTURE_WITHOUT_NONE",
  147. message: 'an empty case-local provider fixture requires providerRequestPolicy "none"',
  148. specPath,
  149. subjectPath: manifestSubjectPath,
  150. }),
  151. );
  152. }
  153. validateRequestLedger({
  154. caseFixtures: caseFixtures ?? [],
  155. manifestSubjectPath,
  156. providerFiles,
  157. reasons,
  158. requests,
  159. specPath,
  160. });
  161. }
  162. function validateCaseFixtureIdentity({
  163. absoluteSpecPath,
  164. caseFixture,
  165. caseFixturePath,
  166. caseName,
  167. reasons,
  168. root,
  169. specPath,
  170. }) {
  171. if (!isRecord(caseFixture)) {
  172. return;
  173. }
  174. const subjectPath = toRepoPath(root, caseFixturePath);
  175. if (caseFixture.caseName !== caseName) {
  176. reasons.push(
  177. diagnostic({
  178. code: "CASE_FIXTURE_CASE_NAME_MISMATCH",
  179. message: `case fixture caseName must be ${caseName}; received ${JSON.stringify(caseFixture.caseName)}`,
  180. specPath,
  181. subjectPath,
  182. }),
  183. );
  184. }
  185. if (
  186. typeof caseFixture.spec !== "string" ||
  187. resolve(root, "packages/desktop", caseFixture.spec) !== absoluteSpecPath
  188. ) {
  189. reasons.push(
  190. diagnostic({
  191. code: "CASE_FIXTURE_SPEC_PATH_MISMATCH",
  192. message: `case fixture spec must resolve to ${specPath}; received ${JSON.stringify(caseFixture.spec)}`,
  193. specPath,
  194. subjectPath,
  195. }),
  196. );
  197. }
  198. }
  199. function validateManifestIdentity({
  200. absoluteSpecPath,
  201. caseName,
  202. manifest,
  203. manifestSubjectPath,
  204. reasons,
  205. root,
  206. specPath,
  207. }) {
  208. if (manifest.version !== 1) {
  209. reasons.push(
  210. diagnostic({
  211. code: "INVALID_MANIFEST_VERSION",
  212. message: `case manifest version must be 1; received ${JSON.stringify(manifest.version)}`,
  213. specPath,
  214. subjectPath: manifestSubjectPath,
  215. }),
  216. );
  217. }
  218. if (manifest.caseName !== caseName) {
  219. reasons.push(
  220. diagnostic({
  221. code: "MANIFEST_CASE_NAME_MISMATCH",
  222. message: `case manifest caseName must be ${caseName}; received ${JSON.stringify(manifest.caseName)}`,
  223. specPath,
  224. subjectPath: manifestSubjectPath,
  225. }),
  226. );
  227. }
  228. const desktopRoot = resolve(root, "packages/desktop");
  229. if (
  230. typeof manifest.spec !== "string" ||
  231. resolve(desktopRoot, manifest.spec) !== absoluteSpecPath
  232. ) {
  233. reasons.push(
  234. diagnostic({
  235. code: "MANIFEST_SPEC_PATH_MISMATCH",
  236. message: `case manifest spec must resolve to ${specPath}; received ${JSON.stringify(manifest.spec)}`,
  237. specPath,
  238. subjectPath: manifestSubjectPath,
  239. }),
  240. );
  241. }
  242. }
  243. async function readProviderFiles({
  244. canonicalCommonPath,
  245. reasons,
  246. resolvedProviderFixtures,
  247. root,
  248. specPath,
  249. }) {
  250. const providerFiles = [];
  251. for (const fixture of resolvedProviderFixtures) {
  252. const result = await readJson(fixture.absolutePath);
  253. const subjectPath = toRepoPath(root, fixture.absolutePath);
  254. if (result.missing) {
  255. reasons.push(
  256. diagnostic({
  257. code: "MISSING_PROVIDER_FIXTURE",
  258. message: `declared provider fixture is missing: ${fixture.manifestPath}`,
  259. specPath,
  260. subjectPath,
  261. }),
  262. );
  263. continue;
  264. }
  265. if (result.error) {
  266. reasons.push(
  267. diagnostic({
  268. code: "INVALID_PROVIDER_FIXTURE_JSON",
  269. message: `declared provider fixture is not valid JSON: ${result.error.message}`,
  270. specPath,
  271. subjectPath,
  272. }),
  273. );
  274. continue;
  275. }
  276. const fixtures = validateProviderFixture(result.value, subjectPath, specPath, reasons);
  277. if (fixtures) {
  278. providerFiles.push({
  279. absolutePath: fixture.absolutePath,
  280. fixtures,
  281. isCanonicalCommon: fixture.absolutePath === canonicalCommonPath,
  282. subjectPath,
  283. });
  284. }
  285. }
  286. return providerFiles;
  287. }
  288. function validateProviderPolicy({ manifest, manifestSubjectPath, reasons, requests, specPath }) {
  289. if (manifest.providerRequestPolicy !== undefined && manifest.providerRequestPolicy !== "none") {
  290. reasons.push(
  291. diagnostic({
  292. code: "INVALID_PROVIDER_REQUEST_POLICY",
  293. message: 'providerRequestPolicy must be "none" when present',
  294. specPath,
  295. subjectPath: manifestSubjectPath,
  296. }),
  297. );
  298. }
  299. if (manifest.providerRequestPolicy === "none" && requests.length > 0) {
  300. reasons.push(
  301. diagnostic({
  302. code: "PROVIDER_REQUEST_POLICY_NONE_WITH_REQUESTS",
  303. message: 'providerRequestPolicy "none" requires an empty requests ledger',
  304. specPath,
  305. subjectPath: manifestSubjectPath,
  306. }),
  307. );
  308. }
  309. }
  310. function validateStringArray(value, field, subjectPath, specPath, reasons) {
  311. if (!Array.isArray(value) || value.some((item) => typeof item !== "string")) {
  312. reasons.push(
  313. diagnostic({
  314. code: "INVALID_MANIFEST_SHAPE",
  315. message: `case manifest ${field} must be an array of strings`,
  316. specPath,
  317. subjectPath,
  318. }),
  319. );
  320. return null;
  321. }
  322. return value;
  323. }