conversation-session-formal-admission-ledger.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import {
  2. diagnostic,
  3. isRecord,
  4. } from "./conversation-session-formal-admission-utils.mjs";
  5. const REQUEST_METADATA_FIELDS = [
  6. "kind",
  7. "source",
  8. "timingPolicy",
  9. "syntheticReason",
  10. ];
  11. export function validateRequestLedger({
  12. caseFixtures,
  13. manifestSubjectPath,
  14. providerFiles,
  15. reasons,
  16. requests,
  17. specPath,
  18. }) {
  19. const allFixturesById = new Map();
  20. for (const providerFile of providerFiles) {
  21. for (const fixture of providerFile.fixtures) {
  22. if (allFixturesById.has(fixture.id)) {
  23. reasons.push(
  24. diagnostic({
  25. code: "DUPLICATE_PROVIDER_REQUEST_ID",
  26. message: `provider request id ${fixture.id} is declared by more than one provider fixture`,
  27. specPath,
  28. subjectPath: providerFile.subjectPath,
  29. }),
  30. );
  31. } else {
  32. allFixturesById.set(fixture.id, { fixture, providerFile });
  33. }
  34. }
  35. }
  36. const caseIds = caseFixtures.map((fixture) => fixture.id);
  37. const caseIdSet = new Set(caseIds);
  38. const allowedFixturesById = new Map(
  39. caseFixtures.map((fixture) => [fixture.id, fixture]),
  40. );
  41. for (const providerFile of providerFiles) {
  42. if (!providerFile.isCanonicalCommon) {
  43. continue;
  44. }
  45. for (const fixture of providerFile.fixtures) {
  46. if (fixture.metadata.kind === "common") {
  47. allowedFixturesById.set(fixture.id, fixture);
  48. }
  49. }
  50. }
  51. const requestIds = requests.map((request) => request.id);
  52. const manifestCaseIds = requestIds.filter((id) => caseIdSet.has(id));
  53. const inventedIds = requestIds.filter((id) => !allFixturesById.has(id));
  54. const unauthorizedIds = requestIds.filter(
  55. (id) => allFixturesById.has(id) && !allowedFixturesById.has(id),
  56. );
  57. const missingIds = caseIds.filter((id) => !requestIds.includes(id));
  58. if (inventedIds.length > 0 || missingIds.length > 0) {
  59. reasons.push(
  60. diagnostic({
  61. code: "REQUEST_LEDGER_ID_MISMATCH",
  62. message:
  63. `request ledger IDs do not match declared fixtures; ` +
  64. `invented=[${inventedIds.join(", ")}], missing case-local=[${missingIds.join(", ")}]`,
  65. specPath,
  66. subjectPath: manifestSubjectPath,
  67. }),
  68. );
  69. }
  70. if (unauthorizedIds.length > 0) {
  71. reasons.push(
  72. diagnostic({
  73. code: "UNAUTHORIZED_COMMON_REQUEST",
  74. message:
  75. "non-case requests must come from canonical common.json with metadata.kind=common; " +
  76. `unauthorized=[${unauthorizedIds.join(", ")}]`,
  77. specPath,
  78. subjectPath: manifestSubjectPath,
  79. }),
  80. );
  81. }
  82. if (
  83. inventedIds.length === 0 &&
  84. missingIds.length === 0 &&
  85. unauthorizedIds.length === 0 &&
  86. !arraysEqual(manifestCaseIds, caseIds)
  87. ) {
  88. reasons.push(
  89. diagnostic({
  90. code: "REQUEST_LEDGER_ORDER_MISMATCH",
  91. message:
  92. `case-local request order must be [${caseIds.join(", ")}]; ` +
  93. `received [${manifestCaseIds.join(", ")}]`,
  94. specPath,
  95. subjectPath: manifestSubjectPath,
  96. }),
  97. );
  98. }
  99. for (const request of requests) {
  100. const fixture = allowedFixturesById.get(request.id);
  101. if (!fixture) {
  102. continue;
  103. }
  104. for (const field of REQUEST_METADATA_FIELDS) {
  105. const fixtureValue = isRecord(fixture.metadata)
  106. ? fixture.metadata[field]
  107. : undefined;
  108. if (request[field] !== fixtureValue) {
  109. reasons.push(
  110. diagnostic({
  111. code: "REQUEST_LEDGER_METADATA_MISMATCH",
  112. message:
  113. `request ${request.id} metadata.${field} must match its provider fixture; ` +
  114. `manifest=${JSON.stringify(request[field])}, fixture=${JSON.stringify(fixtureValue)}`,
  115. specPath,
  116. subjectPath: manifestSubjectPath,
  117. }),
  118. );
  119. }
  120. }
  121. }
  122. }
  123. export function validateProviderFixture(
  124. value,
  125. subjectPath,
  126. specPath,
  127. reasons,
  128. { reportShapeError = true } = {},
  129. ) {
  130. if (value === undefined) {
  131. return null;
  132. }
  133. if (
  134. !isRecord(value) ||
  135. value.version !== 1 ||
  136. !Array.isArray(value.fixtures)
  137. ) {
  138. if (reportShapeError) {
  139. reasons.push(
  140. diagnostic({
  141. code: "INVALID_PROVIDER_FIXTURE_SHAPE",
  142. message: "provider fixture must have version 1 and a fixtures array",
  143. specPath,
  144. subjectPath,
  145. }),
  146. );
  147. }
  148. return null;
  149. }
  150. const valid = [];
  151. for (const fixture of value.fixtures) {
  152. if (
  153. !isRecord(fixture) ||
  154. typeof fixture.id !== "string" ||
  155. fixture.id.trim().length === 0 ||
  156. !isRecord(fixture.match) ||
  157. !isRecord(fixture.response) ||
  158. !isProviderMetadata(fixture.metadata)
  159. ) {
  160. reasons.push(
  161. diagnostic({
  162. code: "INVALID_PROVIDER_FIXTURE_SHAPE",
  163. message:
  164. "each provider fixture request requires a non-empty id and object match/response/metadata fields",
  165. specPath,
  166. subjectPath,
  167. }),
  168. );
  169. continue;
  170. }
  171. valid.push(fixture);
  172. }
  173. return valid;
  174. }
  175. export function validateRequestArray(value, subjectPath, specPath, reasons) {
  176. if (!Array.isArray(value)) {
  177. reasons.push(
  178. diagnostic({
  179. code: "INVALID_MANIFEST_SHAPE",
  180. message: "case manifest requests must be an array",
  181. specPath,
  182. subjectPath,
  183. }),
  184. );
  185. return null;
  186. }
  187. const requests = [];
  188. const seenIds = new Set();
  189. for (const request of value) {
  190. if (
  191. !isRecord(request) ||
  192. typeof request.id !== "string" ||
  193. request.id.trim().length === 0
  194. ) {
  195. reasons.push(
  196. diagnostic({
  197. code: "INVALID_MANIFEST_SHAPE",
  198. message: "each manifest request must have a string id",
  199. specPath,
  200. subjectPath,
  201. }),
  202. );
  203. continue;
  204. }
  205. if (seenIds.has(request.id)) {
  206. reasons.push(
  207. diagnostic({
  208. code: "DUPLICATE_MANIFEST_REQUEST_ID",
  209. message: `manifest request id ${request.id} is declared more than once`,
  210. specPath,
  211. subjectPath,
  212. }),
  213. );
  214. }
  215. seenIds.add(request.id);
  216. requests.push(request);
  217. }
  218. return requests.length === value.length ? requests : null;
  219. }
  220. function isProviderMetadata(value) {
  221. return (
  222. isRecord(value) &&
  223. typeof value.kind === "string" &&
  224. value.kind.trim().length > 0 &&
  225. typeof value.source === "string" &&
  226. value.source.trim().length > 0 &&
  227. typeof value.timingPolicy === "string" &&
  228. value.timingPolicy.trim().length > 0 &&
  229. (value.syntheticReason === undefined ||
  230. typeof value.syntheticReason === "string")
  231. );
  232. }
  233. export function validateSyntheticReasons(
  234. requests,
  235. subjectPath,
  236. specPath,
  237. reasons,
  238. ) {
  239. for (const request of requests) {
  240. if (
  241. (request.kind === "synthetic" || request.source === "synthetic") &&
  242. (typeof request.syntheticReason !== "string" ||
  243. request.syntheticReason.trim().length === 0)
  244. ) {
  245. reasons.push(
  246. diagnostic({
  247. code: "SYNTHETIC_REASON_MISSING",
  248. message: `synthetic request ${request.id ?? "<missing-id>"} requires a non-empty syntheticReason`,
  249. specPath,
  250. subjectPath,
  251. }),
  252. );
  253. }
  254. }
  255. }
  256. function arraysEqual(left, right) {
  257. return (
  258. left.length === right.length &&
  259. left.every((value, index) => value === right[index])
  260. );
  261. }