chatErrorBannerI18n.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. MEDIA_BUDGET_CURRENT_IMAGE_TOO_LARGE_ERROR_CODE,
  4. MEDIA_BUDGET_CURRENT_VIDEO_TOO_LARGE_ERROR_CODE,
  5. OFF_PEAK_TICKET_EXPIRED_MARKER,
  6. } from "@zcode/shared";
  7. import { resolveChatErrorBannerDisplayMessage } from "@/ChatErrorBanner.js";
  8. import type { IntlInstance } from "@/i18n/IntlProvider.js";
  9. import enUS from "@/i18n/locales/en-US.js";
  10. import zhCN from "@/i18n/locales/zh-CN.js";
  11. function createIntl(messages: Record<string, string>): IntlInstance {
  12. return {
  13. formatMessage({ id }, values = {}) {
  14. const template = messages[id] ?? id;
  15. return template.replace(/\{([A-Za-z0-9_]+)\}/g, (match, key) =>
  16. Object.prototype.hasOwnProperty.call(values, key) ? String(values[key]) : match,
  17. );
  18. },
  19. };
  20. }
  21. describe("ChatErrorBanner i18n", () => {
  22. it("uses the remote wording for an empty remote workspace instead of local plan copy", () => {
  23. const zhIntl = createIntl(zhCN);
  24. const enIntl = createIntl(enUS);
  25. const error = {
  26. code: "model_config_missing",
  27. message: "Model config is missing",
  28. };
  29. const zhRemote = resolveChatErrorBannerDisplayMessage(error, zhIntl, "remote");
  30. const enRemote = resolveChatErrorBannerDisplayMessage(error, enIntl, "remote");
  31. expect(zhRemote).toBe(zhCN["chat.error.remoteNoAvailableModel"]);
  32. expect(zhRemote).not.toBe("chat.error.remoteNoAvailableModel");
  33. expect(zhRemote).not.toBe(zhCN["chat.error.noAvailableModel"]);
  34. expect(enRemote).toBe(enUS["chat.error.remoteNoAvailableModel"]);
  35. expect(enRemote).not.toBe("chat.error.remoteNoAvailableModel");
  36. expect(enRemote).not.toBe(enUS["chat.error.noAvailableModel"]);
  37. expect(resolveChatErrorBannerDisplayMessage(error, zhIntl, "local")).toBe(
  38. zhCN["chat.error.noAvailableModel"],
  39. );
  40. });
  41. it.each([
  42. [zhCN, "当前附件总量过大,请移除部分附件或压缩后重试。"],
  43. [
  44. enUS,
  45. "Current attachments are too large for one request. Remove or compress attachments and try again.",
  46. ],
  47. ] as const)("localizes the shared attachment budget error", (messages, expected) => {
  48. expect(
  49. resolveChatErrorBannerDisplayMessage(
  50. {
  51. code: "MEDIA_BUDGET_CURRENT_ATTACHMENT_TOO_LARGE",
  52. message: "Runtime fallback message",
  53. },
  54. createIntl(messages),
  55. ),
  56. ).toBe(expected);
  57. });
  58. it("localizes current media-budget attachment errors from stable runtime codes", () => {
  59. const zhIntl = createIntl(zhCN);
  60. const enIntl = createIntl(enUS);
  61. expect(
  62. resolveChatErrorBannerDisplayMessage(
  63. {
  64. code: MEDIA_BUDGET_CURRENT_VIDEO_TOO_LARGE_ERROR_CODE,
  65. message:
  66. "Current video attachments are too large to send. Remove or shrink videos and try again.",
  67. },
  68. zhIntl,
  69. ),
  70. ).toBe("当前视频附件过大,请移除部分视频或压缩后重试。");
  71. expect(
  72. resolveChatErrorBannerDisplayMessage(
  73. {
  74. code: MEDIA_BUDGET_CURRENT_IMAGE_TOO_LARGE_ERROR_CODE,
  75. message:
  76. "Current image attachments are too large to send. Remove or shrink images and try again.",
  77. },
  78. enIntl,
  79. ),
  80. ).toBe("Current image attachments are too large. Remove or compress images and try again.");
  81. });
  82. it("闲时票据不可用不展示上游原文(3102 与包装后的稳定标记都要本地化)", () => {
  83. const zhIntl = createIntl(zhCN);
  84. const enIntl = createIntl(enUS);
  85. expect(
  86. resolveChatErrorBannerDisplayMessage(
  87. { code: "3102", message: "off peak ticket is invaliad or expired" },
  88. zhIntl,
  89. ),
  90. ).toBe("已超过单次最长运行时间,请创建新的闲时任务继续。");
  91. // 外层 code 被压成包装码时靠适配层写入的稳定标记兜底。
  92. expect(
  93. resolveChatErrorBannerDisplayMessage(
  94. {
  95. code: "PROVIDER_BUSINESS_ERROR",
  96. message: `${OFF_PEAK_TICKET_EXPIRED_MARKER}: off peak ticket is invaliad or expired`,
  97. },
  98. enIntl,
  99. ),
  100. ).toBe(
  101. "This run exceeded the maximum single-run time. Create a new off-peak task to continue.",
  102. );
  103. });
  104. });