hardcodedCopyI18n.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // 回归:v4 及通用组件中的硬编码中文/英文文案必须跟随 locale(i18n)。
  2. // 覆盖 ConversationGoalBanner「目标」、code-block「Wrap lines/Copy code」、Spinner「Loading」。
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { describe, expect, it, vi } from "vitest";
  6. import type { GoalState } from "@zcode/shared/zcode-protocol-v4";
  7. import {
  8. CodeBlockCopyButton,
  9. CodeBlockWrapButton,
  10. } from "@/components/ai-elements/code-block.js";
  11. import { Spinner } from "@/components/ui/spinner.js";
  12. import { TooltipProvider } from "@/components/ui/tooltip.js";
  13. import { ConversationGoalBanner } from "@/v4/ConversationGoalBanner.js";
  14. const labels: Record<string, string> = {
  15. "chat.goalBanner.label": "Goal",
  16. "codeBlock.wrapLines": "Wrap lines",
  17. "codeBlock.copyCode": "Copy code",
  18. "common.loading": "Loading...",
  19. };
  20. vi.mock("@/i18n/IntlProvider.js", () => ({
  21. useZCodeIntl: () => ({
  22. intl: {
  23. formatMessage: (descriptor: { id: string }) => labels[descriptor.id] ?? descriptor.id,
  24. },
  25. locale: "en-US",
  26. }),
  27. }));
  28. function goal(overrides: Partial<GoalState> = {}): GoalState {
  29. return {
  30. status: "active",
  31. objective: "修好状态面板",
  32. iteration: 1,
  33. ...overrides,
  34. };
  35. }
  36. describe("硬编码文案 i18n 回归", () => {
  37. it("ConversationGoalBanner 的「目标」标签走 i18n", () => {
  38. const html = renderToStaticMarkup(
  39. createElement(ConversationGoalBanner, { goal: goal() }),
  40. );
  41. expect(html).toContain(">Goal<");
  42. expect(html).not.toContain("目标");
  43. });
  44. it("CodeBlockWrapButton / CodeBlockCopyButton 默认文案走 i18n", () => {
  45. const wrap = renderToStaticMarkup(
  46. createElement(TooltipProvider, null, createElement(CodeBlockWrapButton)),
  47. );
  48. expect(wrap).toContain('aria-label="Wrap lines"');
  49. const copy = renderToStaticMarkup(
  50. createElement(TooltipProvider, null, createElement(CodeBlockCopyButton)),
  51. );
  52. expect(copy).toContain('aria-label="Copy code"');
  53. });
  54. it("Spinner 的 aria-label 走 i18n", () => {
  55. const html = renderToStaticMarkup(createElement(Spinner));
  56. expect(html).toContain('aria-label="Loading..."');
  57. expect(html).not.toContain('aria-label="Loading"');
  58. });
  59. });