conversationShareError.test.ts 11 KB

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