assistantCodeCommentCards.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { AssistantCodeCommentCards } from "@/AssistantCodeCommentCards.js";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import enUS from "@/i18n/locales/en-US.js";
  8. import zhCN from "@/i18n/locales/zh-CN.js";
  9. import type { AssistantCodeCommentCard } from "@/lib/assistantCodeComment.js";
  10. function card(index: number): AssistantCodeCommentCard {
  11. return {
  12. id: `comment-${index}`,
  13. title: `[P1] 风险 ${index}`,
  14. body: `需要修复 ${index}`,
  15. file: `src/file-${index}.ts`,
  16. path: `/workspace/src/file-${index}.ts`,
  17. displayPath: `src/file-${index}.ts`,
  18. priority: 1,
  19. startLine: index,
  20. endLine: index + 1,
  21. sourceStart: index,
  22. sourceEnd: index + 1,
  23. };
  24. }
  25. describe("AssistantCodeCommentCards", () => {
  26. afterEach(() => {
  27. window.getSelection()?.removeAllRanges();
  28. });
  29. it("只保留当前卡片交互实际消费的本地化 key", () => {
  30. // Bug 原因:早期“标题 + 数量 + 展开其余五条”的卡片已改成整卡折叠,旧文案却未同步删除,
  31. // 容易让后续维护者误以为仍存在第二套展开交互。
  32. const expectedKeys = [
  33. "chat.codeCommentCards.collapseAll",
  34. "chat.codeCommentCards.expandAll",
  35. "chat.codeCommentCards.openReview",
  36. ];
  37. for (const messages of [enUS, zhCN]) {
  38. expect(
  39. Object.keys(messages)
  40. .filter((key) => key.startsWith("chat.codeCommentCards."))
  41. .sort(),
  42. ).toEqual(expectedKeys);
  43. }
  44. });
  45. it("默认折叠;每条只占一行,正文不展示 body,文字可选择且整行打开只读 Code Review", () => {
  46. const onOpenCodeViewer = vi.fn();
  47. render(
  48. createElement(
  49. ZCodeIntlProvider,
  50. { initialLocale: "zh-CN" },
  51. createElement(AssistantCodeCommentCards, {
  52. cards: Array.from({ length: 7 }, (_, index) => card(index + 1)),
  53. workspacePath: "/workspace",
  54. workspaceIdentity: "remote:ssh:demo:/workspace",
  55. workspaceRemoteSessionId: "remote-1",
  56. onOpenCodeViewer,
  57. }),
  58. ),
  59. );
  60. expect(screen.getByText("7 条评论")).toBeTruthy();
  61. const shell = screen.getByTestId("assistant-code-comment-cards");
  62. expect(shell.className).toContain("bg-card");
  63. expect(shell.className).toContain("border-border");
  64. expect(shell.className).toContain("shadow-none");
  65. const header = screen.getByTestId("assistant-code-comment-header");
  66. expect(header.className).toContain("h-10");
  67. expect(header.className).toContain("hover:bg-hover");
  68. expect(header.className).toContain("transition-colors");
  69. expect(header.className).not.toContain("h-8");
  70. expect(header.className).not.toContain("min-h-20");
  71. expect(screen.getByText("7 条评论").className).toContain("text-ui-base");
  72. const list = screen.getByTestId("assistant-code-comment-list");
  73. expect(list.className).not.toContain("divide-y");
  74. expect(list.className).not.toContain("border-t");
  75. const toggleIcon = screen.getByTestId("assistant-code-comment-toggle-icon");
  76. expect(toggleIcon.className).toContain("size-1.5");
  77. expect(toggleIcon.className).toContain("-rotate-45");
  78. expect(screen.queryByText("风险 1")).toBeNull();
  79. fireEvent.click(screen.getByRole("button", { name: /展开 7 条评论/ }));
  80. expect(list.className).toContain("border-t");
  81. expect(list.className).toContain("border-border");
  82. expect(screen.getByText("风险 1")).toBeTruthy();
  83. expect(screen.getByText("风险 6")).toBeTruthy();
  84. expect(screen.queryByText("需要修复 1")).toBeNull();
  85. expect(screen.getByTestId("assistant-code-comment-toggle-icon").className).toContain(
  86. "rotate-45",
  87. );
  88. const firstCard = screen.getByRole("button", { name: /风险 1/ });
  89. const firstCardSurface = firstCard.parentElement;
  90. expect(firstCardSurface).not.toBeNull();
  91. expect(firstCardSurface?.className).toContain("bg-background/50");
  92. expect(firstCard.className).toContain("select-text");
  93. expect(firstCard.className).toContain("whitespace-nowrap");
  94. expect(firstCard.className).toContain("min-h-10");
  95. expect(firstCard.className).toContain("py-2");
  96. expect(firstCard.className).not.toContain("bg-background/50");
  97. expect(firstCard.className).toContain("hover:bg-hover/30");
  98. expect(firstCard.className).not.toContain("hover:bg-surface-hover");
  99. expect(firstCard.querySelector("p")).toBeNull();
  100. expect(screen.getByText("src/file-1.ts:1–2")).toBeTruthy();
  101. expect(firstCard.querySelector("svg")).toBeNull();
  102. const selection = window.getSelection();
  103. const range = document.createRange();
  104. range.selectNodeContents(screen.getByText("风险 1"));
  105. selection?.removeAllRanges();
  106. selection?.addRange(range);
  107. fireEvent.click(firstCard);
  108. expect(onOpenCodeViewer).not.toHaveBeenCalled();
  109. selection?.removeAllRanges();
  110. fireEvent.click(firstCard);
  111. expect(onOpenCodeViewer).toHaveBeenCalledWith(
  112. expect.objectContaining({
  113. type: "code-review",
  114. title: "file-1.ts",
  115. path: "/workspace/src/file-1.ts",
  116. workspacePath: "/workspace",
  117. workspaceIdentity: "remote:ssh:demo:/workspace",
  118. workspaceRemoteSessionId: "remote-1",
  119. review: {
  120. requestId: expect.stringContaining("comment-1:"),
  121. title: "风险 1",
  122. body: "需要修复 1",
  123. priority: 1,
  124. startLine: 1,
  125. endLine: 2,
  126. },
  127. }),
  128. );
  129. fireEvent.keyDown(firstCard, { key: "Enter" });
  130. fireEvent.keyDown(firstCard, { key: " " });
  131. expect(onOpenCodeViewer).toHaveBeenCalledTimes(3);
  132. });
  133. });