usageStatsErrorNotice.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // @vitest-environment jsdom
  2. // 修复原因:团队套餐业务错误(如"仅企业主账号可查询企业汇总数据"、
  3. // "您当前暂无有效的团队套餐授权记录,无法创建API Key")原先被 generic 文案
  4. // 掩盖,且后者含"授权"会被误判为 credential 错误走翻译文案。本文件锁定
  5. // 报错条参考 Plan Card teamUnavailable 样式原文展示业务错误的行为。
  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("原文展示企业主账号权限错误,参考 teamUnavailable 内联样式(InfoIcon + warning,无容器)", () => {
  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. // 参考 Plan Card teamUnavailable:内联展示,无边框无背景容器
  40. expect(html).not.toContain("rounded-lg");
  41. expect(html).not.toContain("border-warning/30");
  42. expect(html).not.toContain("bg-warning/10");
  43. // 业务错误不是凭据问题:不出现检查 API Key 入口
  44. expect(html).not.toContain("settings.usage.checkApiKey");
  45. expect(html).not.toContain("border-destructive");
  46. });
  47. it("原文展示团队 API Key 创建失败 msg,即使包含授权字样也不走 credential 翻译", () => {
  48. const html = renderNotice("您当前暂无有效的团队套餐授权记录,无法创建API Key");
  49. expect(html).toContain("您当前暂无有效的团队套餐授权记录,无法创建API Key");
  50. expect(html).not.toContain("usage.error.stats.credential");
  51. expect(html).not.toContain("settings.usage.checkApiKey");
  52. expect(html).toContain("lucide-info");
  53. expect(html).not.toContain("rounded-lg");
  54. });
  55. it("凭据错误保持翻译文案与检查 API Key 入口", () => {
  56. const html = renderNotice("token expired");
  57. expect(html).toContain("usage.error.stats.credential");
  58. expect(html).toContain("settings.usage.checkApiKey");
  59. expect(html).not.toContain("token expired");
  60. });
  61. it("其他错误保持 generic 文案", () => {
  62. const html = renderNotice("Request timed out after 15000ms");
  63. expect(html).toContain("usage.error.stats.generic");
  64. expect(html).not.toContain("Request timed out after 15000ms");
  65. });
  66. });