chatErrorBannerI18n.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.each([
  23. [zhCN, "当前附件总量过大,请移除部分附件或压缩后重试。"],
  24. [
  25. enUS,
  26. "Current attachments are too large for one request. Remove or compress attachments and try again.",
  27. ],
  28. ] as const)("localizes the shared attachment budget error", (messages, expected) => {
  29. expect(
  30. resolveChatErrorBannerDisplayMessage(
  31. {
  32. code: "MEDIA_BUDGET_CURRENT_ATTACHMENT_TOO_LARGE",
  33. message: "Runtime fallback message",
  34. },
  35. createIntl(messages),
  36. ),
  37. ).toBe(expected);
  38. });
  39. it("localizes current media-budget attachment errors from stable runtime codes", () => {
  40. const zhIntl = createIntl(zhCN);
  41. const enIntl = createIntl(enUS);
  42. expect(
  43. resolveChatErrorBannerDisplayMessage(
  44. {
  45. code: MEDIA_BUDGET_CURRENT_VIDEO_TOO_LARGE_ERROR_CODE,
  46. message:
  47. "Current video attachments are too large to send. Remove or shrink videos and try again.",
  48. },
  49. zhIntl,
  50. ),
  51. ).toBe("当前视频附件过大,请移除部分视频或压缩后重试。");
  52. expect(
  53. resolveChatErrorBannerDisplayMessage(
  54. {
  55. code: MEDIA_BUDGET_CURRENT_IMAGE_TOO_LARGE_ERROR_CODE,
  56. message:
  57. "Current image attachments are too large to send. Remove or shrink images and try again.",
  58. },
  59. enIntl,
  60. ),
  61. ).toBe("Current image attachments are too large. Remove or compress images and try again.");
  62. });
  63. it("闲时票据不可用不展示上游原文(3102 与包装后的稳定标记都要本地化)", () => {
  64. const zhIntl = createIntl(zhCN);
  65. const enIntl = createIntl(enUS);
  66. expect(
  67. resolveChatErrorBannerDisplayMessage(
  68. { code: "3102", message: "off peak ticket is invaliad or expired" },
  69. zhIntl,
  70. ),
  71. ).toBe("已超过单次最长运行时间,请创建新的闲时任务继续。");
  72. // 外层 code 被压成包装码时靠适配层写入的稳定标记兜底。
  73. expect(
  74. resolveChatErrorBannerDisplayMessage(
  75. {
  76. code: "PROVIDER_BUSINESS_ERROR",
  77. message: `${OFF_PEAK_TICKET_EXPIRED_MARKER}: off peak ticket is invaliad or expired`,
  78. },
  79. enIntl,
  80. ),
  81. ).toBe(
  82. "This run exceeded the maximum single-run time. Create a new off-peak task to continue.",
  83. );
  84. });
  85. });