zcodeUiError.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { describe, expect, it } from "vitest";
  2. import { normalizeZCodeUiError, normalizeRestoredTaskFailureError } from "@/lib/zcodeUiError.js";
  3. describe("normalizeZCodeUiError", () => {
  4. it.each([
  5. (error: object) => error,
  6. (error: object) => ({ data: error }),
  7. (error: object) => ({ data: { error } }),
  8. (error: object) => ({ data: { zcode: { error } } }),
  9. ])("preserves structured underlying fields through RPC wrappers", (wrap) => {
  10. expect(
  11. normalizeZCodeUiError(
  12. wrap({
  13. message: "Model request failed",
  14. underlyingErrorMessage: "Upstream request failed",
  15. underlyingErrorDetail: "Upstream instance timed out after 10000ms.",
  16. }),
  17. ),
  18. ).toMatchObject({
  19. underlyingErrorMessage: "Upstream request failed",
  20. underlyingErrorDetail: "Upstream instance timed out after 10000ms.",
  21. });
  22. });
  23. it("外层 message 是泛化错误时优先展示 data.message", () => {
  24. const normalized = normalizeZCodeUiError({
  25. message: "Internal error",
  26. data: {
  27. message: "Provider quota exceeded",
  28. },
  29. });
  30. expect(normalized).toMatchObject({
  31. code: "UNKNOWN",
  32. message: "Provider quota exceeded",
  33. detail: undefined,
  34. });
  35. });
  36. it("message 缺失时会回退到 data.reason", () => {
  37. const normalized = normalizeZCodeUiError({
  38. data: {
  39. reason: "Credential expired",
  40. },
  41. });
  42. expect(normalized).toMatchObject({
  43. code: "UNKNOWN",
  44. message: "Credential expired",
  45. });
  46. });
  47. it("支持解析 JSON 字符串中的嵌套错误字段", () => {
  48. const normalized = normalizeZCodeUiError(
  49. '{"message":"Internal error","data":{"error":{"message":"token invalid"},"code":"AUTH_FAILED"}}',
  50. );
  51. expect(normalized).toMatchObject({
  52. code: "AUTH_FAILED",
  53. message: "token invalid",
  54. detail: undefined,
  55. });
  56. });
  57. it("支持上下文字段透传 traceId/taskId 与 fallbackCode", () => {
  58. const normalized = normalizeZCodeUiError(
  59. { data: { message: "Network timeout" } },
  60. {
  61. fallbackCode: "SEND_FAILED",
  62. traceId: "trace-123",
  63. taskId: "task-123",
  64. },
  65. );
  66. expect(normalized).toMatchObject({
  67. code: "SEND_FAILED",
  68. message: "Network timeout",
  69. traceId: "trace-123",
  70. taskId: "task-123",
  71. });
  72. });
  73. it("支持把顶层 detail 解析成可展开详情", () => {
  74. const normalized = normalizeZCodeUiError({
  75. message: "WebSocket protocol error: Handshake not finished",
  76. detail:
  77. "2026-04-10T02:39:27.704964Z ERROR codex_api::endpoint::responses_websocket: failed to connect to websocket",
  78. code: "STDERR_WS_HANDSHAKE_NOT_FINISHED",
  79. });
  80. expect(normalized).toMatchObject({
  81. code: "STDERR_WS_HANDSHAKE_NOT_FINISHED",
  82. message: "WebSocket protocol error: Handshake not finished",
  83. detail:
  84. "2026-04-10T02:39:27.704964Z ERROR codex_api::endpoint::responses_websocket: failed to connect to websocket",
  85. });
  86. });
  87. it("支持读取 zcode 结构化错误摘要", () => {
  88. const normalized = normalizeZCodeUiError({
  89. message: "Internal error",
  90. data: {
  91. zcode: {
  92. error: {
  93. code: "UNKNOWN_ERROR",
  94. message: "Network connection failed for the provider request.",
  95. detail: "Cannot connect to API: connect ECONNREFUSED 127.0.0.1:9091",
  96. traceId: "trace-network",
  97. },
  98. },
  99. },
  100. });
  101. expect(normalized).toMatchObject({
  102. code: "UNKNOWN_ERROR",
  103. message: "Network connection failed for the provider request.",
  104. detail: "Cannot connect to API: connect ECONNREFUSED 127.0.0.1:9091",
  105. traceId: "trace-network",
  106. });
  107. });
  108. it("保留错误 payload 中的结构化归因,避免新 schema 在 UI 归一化时丢失", () => {
  109. const normalized = normalizeZCodeUiError({
  110. code: "model_request_failed",
  111. message: "Provider request failed.",
  112. attribution: {
  113. source: "provider",
  114. reason: "server_error",
  115. providerId: "zai-api",
  116. modelId: "glm-5.2",
  117. statusCode: 503,
  118. retryable: true,
  119. },
  120. });
  121. expect(normalized.attribution).toEqual({
  122. source: "provider",
  123. reason: "server_error",
  124. providerId: "zai-api",
  125. modelId: "glm-5.2",
  126. statusCode: 503,
  127. retryable: true,
  128. });
  129. });
  130. });
  131. describe("normalizeRestoredTaskFailureError", () => {
  132. it("恢复 error snapshot 时优先使用 task meta 持久化的 lastError", () => {
  133. const normalized = normalizeRestoredTaskFailureError({
  134. taskId: "task-1",
  135. traceId: "trace-1",
  136. title: "",
  137. workspacePath: "/tmp/workspace",
  138. createdAt: 1,
  139. updatedAt: 2,
  140. mode: "default",
  141. status: "error",
  142. lastError: {
  143. code: "EMPTY_REPLY",
  144. message: "Agent 未产生任何回复,请重试。",
  145. traceId: "trace-empty",
  146. taskId: "task-1",
  147. },
  148. });
  149. expect(normalized).toMatchObject({
  150. code: "EMPTY_REPLY",
  151. message: "Agent 未产生任何回复,请重试。",
  152. traceId: "trace-empty",
  153. taskId: "task-1",
  154. });
  155. });
  156. it("旧 error snapshot 缺少 lastError 时不展示错误文案", () => {
  157. const normalized = normalizeRestoredTaskFailureError({
  158. taskId: "task-2",
  159. traceId: "trace-2",
  160. title: "",
  161. workspacePath: "/tmp/workspace",
  162. createdAt: 1,
  163. updatedAt: 2,
  164. mode: "default",
  165. status: "error",
  166. });
  167. expect(normalized).toBeNull();
  168. });
  169. });