conversationShareError.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. formatConversationShareErrorSummary,
  4. formatConversationShareAllowedArtifacts,
  5. formatConversationShareArtifactType,
  6. getConversationShareErrorDetails,
  7. resolveConversationShareFallbackIssueCode,
  8. resolveConversationShareIssueMessageId,
  9. resolveConversationSharePublishErrorMessageId,
  10. resolveConversationShareWarningMessageId,
  11. sanitizeConversationShareWarnings,
  12. } from "@/lib/conversationShareError.js";
  13. describe("conversation share error presentation", () => {
  14. it("keeps safe server rejection details through RPC and discards unsafe fields", () => {
  15. expect(
  16. getConversationShareErrorDetails({
  17. kind: "invalid_conversation",
  18. details: {
  19. rejection: {
  20. rule: "field_type",
  21. row_index: 0,
  22. field: "state",
  23. path: "/Users/secret",
  24. text: "secret",
  25. },
  26. },
  27. }),
  28. ).toMatchObject({ rejection: { rule: "field_type", row_index: 0, field: "state" } });
  29. expect(
  30. JSON.stringify(
  31. getConversationShareErrorDetails({
  32. details: { rejection: { rule: "field_type", path: "/Users/secret" } },
  33. }),
  34. ),
  35. ).not.toContain("/Users/secret");
  36. });
  37. it("用友好文件类别和支持类型替代内部 MIME 白名单文案", () => {
  38. const issue = {
  39. code: "artifact_type_not_allowed" as const,
  40. scope: "artifact" as const,
  41. artifactType: "mp4",
  42. extension: "mp4",
  43. mimeType: "video/mp4",
  44. allowedArtifacts: [
  45. { type: "pdf", extensions: ["pdf"], mimeTypes: ["application/pdf"] },
  46. { type: "text", extensions: ["txt"], mimeTypes: ["text/plain"] },
  47. ],
  48. };
  49. expect(formatConversationShareArtifactType(issue, "zh-CN")).toBe("MP4");
  50. expect(formatConversationShareAllowedArtifacts(issue, "zh-CN")).toBe("PDF、纯文本");
  51. expect(formatConversationShareAllowedArtifacts(issue, "en-US")).toBe("PDF, plain text");
  52. });
  53. it("uses the generic fallback for unknown values", () => {
  54. expect(resolveConversationSharePublishErrorMessageId(new Error("boom"))).toBe(
  55. "conversationShare.publishFailed",
  56. );
  57. });
  58. it("extracts only stable diagnostic fields and omits raw messages", () => {
  59. expect(
  60. getConversationShareErrorDetails({
  61. name: "ConversationShareClientError",
  62. kind: "artifact_not_allowed",
  63. status: 422,
  64. code: 3208,
  65. requestId: "server-request-123",
  66. message: "contains /private/path and https://signed.example/token",
  67. }),
  68. ).toEqual({
  69. name: "ConversationShareClientError",
  70. kind: "artifact_not_allowed",
  71. status: 422,
  72. code: 3208,
  73. requestId: "server-request-123",
  74. });
  75. });
  76. it("reads the RPC details envelope and rejects unsafe request ids", () => {
  77. expect(
  78. getConversationShareErrorDetails({
  79. name: "ConversationShareServiceError",
  80. kind: "artifact_not_allowed",
  81. details: {
  82. requestId: "server-request-123",
  83. issues: [{ code: "artifact_type_not_allowed", scope: "artifact" }],
  84. issueCount: 1,
  85. omittedIssueCount: 0,
  86. },
  87. }),
  88. ).toMatchObject({
  89. requestId: "server-request-123",
  90. issueCount: 1,
  91. issues: [{ code: "artifact_type_not_allowed", scope: "artifact" }],
  92. });
  93. expect(
  94. getConversationShareErrorDetails({
  95. kind: "network",
  96. details: { requestId: "https://signed.example/token" },
  97. }),
  98. ).not.toHaveProperty("requestId");
  99. });
  100. it("maps invalid conversation reasons to actionable guidance", () => {
  101. expect(
  102. resolveConversationSharePublishErrorMessageId({
  103. kind: "invalid_conversation",
  104. reasonCode: "input_attachment",
  105. }),
  106. ).toBe("conversationShare.error.inputAttachment");
  107. expect(
  108. resolveConversationSharePublishErrorMessageId({
  109. kind: "invalid_conversation",
  110. reasonCode: "running_turn",
  111. }),
  112. ).toBe("conversationShare.error.runningTurn");
  113. });
  114. it("keeps safe diagnostic metadata for logging and presentation", () => {
  115. expect(
  116. getConversationShareErrorDetails({
  117. kind: "invalid_conversation",
  118. reasonCode: "artifact_changed",
  119. diagnostics: { rowKind: "artifact", rowId: 7, artifactType: "pdf" },
  120. message: "private path must not be exposed",
  121. }),
  122. ).toMatchObject({
  123. reasonCode: "artifact_changed",
  124. diagnostics: { rowKind: "artifact", rowId: 7, artifactType: "pdf" },
  125. });
  126. });
  127. it("drops unsafe issue fields while keeping the stable error", () => {
  128. expect(
  129. getConversationShareErrorDetails({
  130. kind: "artifact_not_allowed",
  131. issues: [
  132. {
  133. code: "artifact_type_not_allowed",
  134. scope: "artifact",
  135. artifactDisplayName: "/private/report.pdf",
  136. mimeType: "https://signed.example/token",
  137. },
  138. ],
  139. }),
  140. ).toMatchObject({ kind: "artifact_not_allowed", issueCount: 1 });
  141. expect(
  142. getConversationShareErrorDetails({
  143. kind: "artifact_not_allowed",
  144. issues: [
  145. {
  146. code: "artifact_type_not_allowed",
  147. scope: "artifact",
  148. artifactDisplayName: "report.pdf",
  149. mimeType: "application/pdf",
  150. },
  151. ],
  152. }).issues,
  153. ).toEqual([
  154. expect.objectContaining({ artifactDisplayName: "report.pdf", mimeType: "application/pdf" }),
  155. ]);
  156. });
  157. it("formats a safe actionable summary from aggregated issues", () => {
  158. expect(
  159. formatConversationShareErrorSummary({
  160. kind: "artifact_not_allowed",
  161. issues: [
  162. {
  163. code: "artifact_type_not_allowed",
  164. scope: "artifact",
  165. turnOrdinal: 2,
  166. artifactDisplayName: "晨报.pdf",
  167. artifactType: "pdf",
  168. extension: "pdf",
  169. mimeType: "application/pdf",
  170. allowedFormats: ["PPTX", "DOCX"],
  171. },
  172. ],
  173. }),
  174. ).toEqual({
  175. titleId: "conversationShare.error.summary",
  176. issueCount: 1,
  177. turnOrdinals: [2],
  178. });
  179. });
  180. it("formats limit details without exposing paths or raw messages", () => {
  181. expect(
  182. formatConversationShareErrorSummary({
  183. kind: "limit_exceeded",
  184. issues: [
  185. {
  186. code: "payload_size_limit",
  187. scope: "conversation",
  188. actual: 4_800_000,
  189. limit: 4_000_000,
  190. },
  191. ],
  192. }),
  193. ).toMatchObject({ issueCount: 1, turnOrdinals: [] });
  194. });
  195. it("keeps a useful panel issue when the server returns only a stable kind", () => {
  196. expect(resolveConversationShareFallbackIssueCode({ kind: "invalid_conversation" })).toBe(
  197. "invalid_conversation",
  198. );
  199. expect(resolveConversationShareFallbackIssueCode({ kind: "network" })).toBe("network");
  200. });
  201. it.each([
  202. ["authentication_required", "conversationShare.error.authenticationRequired"],
  203. ["network", "conversationShare.error.network"],
  204. ["service_unavailable", "conversationShare.error.serviceUnavailable"],
  205. ["rate_limited", "conversationShare.error.rateLimited"],
  206. ["feature_disabled", "conversationShare.error.featureDisabled"],
  207. ["connection_unavailable", "conversationShare.error.connectionUnavailable"],
  208. ["invalid_contract", "conversationShare.issue.invalidServiceResponse"],
  209. ["disclosure_required", "conversationShare.error.disclosureRequired"],
  210. ])("预检保留 %s 的明确原因,不退化为 unknown", (kind, messageId) => {
  211. const code = resolveConversationShareFallbackIssueCode({ kind });
  212. expect(code).toBe(kind);
  213. expect(resolveConversationShareIssueMessageId({ code, scope: "transport" })).toBe(messageId);
  214. });
  215. it("limits rendered issues and reports the omitted count", () => {
  216. const details = getConversationShareErrorDetails({
  217. kind: "invalid_conversation",
  218. issueCount: 7,
  219. omittedIssueCount: 2,
  220. issues: Array.from({ length: 5 }, () => ({
  221. code: "invalid_conversation",
  222. scope: "conversation",
  223. })),
  224. });
  225. expect(details.issues).toHaveLength(5);
  226. expect(details.omittedIssueCount).toBe(2);
  227. });
  228. it("跳过提示用非阻断文案,未覆盖的 code 回落到 issue 文案", () => {
  229. expect(
  230. resolveConversationShareWarningMessageId({ code: "artifact_read_failed", scope: "artifact" }),
  231. ).toBe("conversationShare.warning.artifactSkipped");
  232. expect(
  233. resolveConversationShareWarningMessageId({ code: "input_attachment", scope: "turn" }),
  234. ).toBe("conversationShare.warning.inputAttachmentSkipped");
  235. expect(
  236. resolveConversationShareWarningMessageId({ code: "inline_tool_image", scope: "turn" }),
  237. ).toBe("conversationShare.issue.inlineToolImage");
  238. expect(
  239. resolveConversationShareWarningMessageId({ code: "unsupported_timeline", scope: "turn" }),
  240. ).toBe("conversationShare.issue.unsupportedTimeline");
  241. expect(
  242. resolveConversationShareWarningMessageId({ code: "artifact_changed", scope: "artifact" }),
  243. ).toBe("conversationShare.warning.artifactChangedSkipped");
  244. });
  245. it("清洗 progress 里的跳过提示并丢弃带路径的字段", () => {
  246. expect(
  247. sanitizeConversationShareWarnings([
  248. {
  249. code: "artifact_read_failed",
  250. scope: "artifact",
  251. turnOrdinal: 2,
  252. artifactDisplayName: "调研报告.pdf",
  253. },
  254. { code: "artifact_read_failed", scope: "artifact", artifactDisplayName: "a/b/leak.pdf" },
  255. { code: "inline_tool_image", scope: "turn", turnOrdinal: 1 },
  256. { code: "unsupported_timeline", scope: "turn", turnOrdinal: 1 },
  257. "not-an-issue",
  258. ]),
  259. ).toEqual([
  260. {
  261. code: "artifact_read_failed",
  262. scope: "artifact",
  263. turnOrdinal: 2,
  264. artifactDisplayName: "调研报告.pdf",
  265. },
  266. { code: "artifact_read_failed", scope: "artifact" },
  267. ]);
  268. expect(sanitizeConversationShareWarnings(undefined)).toEqual([]);
  269. });
  270. });
  271. it("405 接口异常含状态文案,不建议取消轮次", () => {
  272. expect(
  273. resolveConversationSharePublishErrorMessageId({ kind: "invalid_contract", status: 405 }),
  274. ).toBe("conversationShare.error.invalidContractHttp");
  275. expect(
  276. getConversationShareErrorDetails({
  277. kind: "network",
  278. details: { status: 200, clientRequestId: "client-1", requestId: "server-1" },
  279. }),
  280. ).toMatchObject({
  281. kind: "network",
  282. status: 200,
  283. clientRequestId: "client-1",
  284. requestId: "server-1",
  285. });
  286. expect(
  287. getConversationShareErrorDetails({ details: { clientRequestId: "https://private/token" } })
  288. .clientRequestId,
  289. ).toBeUndefined();
  290. });