usageStatsErrorNotice.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // @vitest-environment jsdom
  2. // 修复原因:团队套餐业务错误(如"仅企业主账号可查询企业汇总数据"、
  3. // "您当前暂无有效的团队套餐授权记录,无法创建API Key")原先被 generic 文案
  4. // 掩盖,且后者含"授权"会被误判为 credential 错误走翻译文案。本文件锁定
  5. // 报错条原文展示业务错误,不将其误分类为凭据错误。
  6. import { createElement, type ComponentProps } from "react";
  7. import { renderToStaticMarkup } from "react-dom/server";
  8. import { afterEach, describe, expect, it, vi } from "vitest";
  9. import { cleanup } from "@testing-library/react";
  10. import { UsageStatsErrorNotice } from "../src/settings/usage-stats/UsageStatsErrorNotice.js";
  11. vi.mock("../src/i18n/IntlProvider.js", () => ({
  12. useZCodeIntl: () => ({
  13. locale: "en-US",
  14. intl: {
  15. formatMessage: ({ id }: { id: string }) => id,
  16. },
  17. }),
  18. }));
  19. vi.mock("../src/lib/settingsNavigation.js", () => ({
  20. setPendingSettingsSection: vi.fn(),
  21. }));
  22. afterEach(() => {
  23. cleanup();
  24. });
  25. function renderNotice(error: string): string {
  26. return renderToStaticMarkup(
  27. createElement(UsageStatsErrorNotice, {
  28. error,
  29. } satisfies ComponentProps<typeof UsageStatsErrorNotice>),
  30. );
  31. }
  32. describe("UsageStatsErrorNotice 团队套餐业务错误", () => {
  33. it("原文展示企业主账号权限错误并保留业务状态提示", () => {
  34. const html = renderNotice("仅企业主账号可查询企业汇总数据");
  35. expect(html).toContain("仅企业主账号可查询企业汇总数据");
  36. expect(html).not.toContain("usage.error.stats.generic");
  37. expect(html).toContain("lucide-info");
  38. expect(html).toContain("text-warning");
  39. // 业务错误不是凭据问题:不出现检查 API Key 入口
  40. expect(html).not.toContain("settings.usage.checkApiKey");
  41. });
  42. it("原文展示团队 API Key 创建失败 msg,即使包含授权字样也不走 credential 翻译", () => {
  43. const html = renderNotice("您当前暂无有效的团队套餐授权记录,无法创建API Key");
  44. expect(html).toContain("您当前暂无有效的团队套餐授权记录,无法创建API Key");
  45. expect(html).not.toContain("usage.error.stats.credential");
  46. expect(html).not.toContain("settings.usage.checkApiKey");
  47. expect(html).toContain("lucide-info");
  48. });
  49. it("凭据错误保持翻译文案与检查 API Key 入口", () => {
  50. const html = renderNotice("token expired");
  51. expect(html).toContain("usage.error.stats.credential");
  52. expect(html).toContain("settings.usage.checkApiKey");
  53. expect(html).not.toContain("token expired");
  54. });
  55. it("其他错误保持 generic 文案", () => {
  56. const html = renderNotice("Request timed out after 15000ms");
  57. expect(html).toContain("usage.error.stats.generic");
  58. expect(html).not.toContain("Request timed out after 15000ms");
  59. });
  60. });