assistantCodeCommentCards.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 type { AssistantCodeCommentCard } from "@/lib/assistantCodeComment.js";
  8. function card(index: number): AssistantCodeCommentCard {
  9. return {
  10. id: `comment-${index}`,
  11. title: `[P1] 风险 ${index}`,
  12. body: `需要修复 ${index}`,
  13. file: `src/file-${index}.ts`,
  14. path: `/workspace/src/file-${index}.ts`,
  15. displayPath: `src/file-${index}.ts`,
  16. priority: 1,
  17. startLine: index,
  18. endLine: index + 1,
  19. sourceStart: index,
  20. sourceEnd: index + 1,
  21. };
  22. }
  23. describe("AssistantCodeCommentCards", () => {
  24. afterEach(() => {
  25. window.getSelection()?.removeAllRanges();
  26. });
  27. it("默认折叠;每条只占一行,正文不展示 body,文字可选择且整行打开只读 Code Review", () => {
  28. const onOpenCodeViewer = vi.fn();
  29. render(
  30. createElement(
  31. ZCodeIntlProvider,
  32. { initialLocale: "zh-CN" },
  33. createElement(AssistantCodeCommentCards, {
  34. cards: Array.from({ length: 7 }, (_, index) => card(index + 1)),
  35. workspacePath: "/workspace",
  36. workspaceIdentity: "remote:ssh:demo:/workspace",
  37. workspaceRemoteSessionId: "remote-1",
  38. onOpenCodeViewer,
  39. }),
  40. ),
  41. );
  42. expect(screen.getByText("7 条评论")).toBeTruthy();
  43. expect(screen.queryByText("风险 1")).toBeNull();
  44. fireEvent.click(screen.getByRole("button", { name: /展开 7 条评论/ }));
  45. expect(screen.getByText("风险 1")).toBeTruthy();
  46. expect(screen.getByText("风险 6")).toBeTruthy();
  47. expect(screen.queryByText("需要修复 1")).toBeNull();
  48. const firstCard = screen.getByRole("button", { name: /风险 1/ });
  49. expect(screen.getByText("src/file-1.ts:1–2")).toBeTruthy();
  50. const selection = window.getSelection();
  51. const range = document.createRange();
  52. range.selectNodeContents(screen.getByText("风险 1"));
  53. selection?.removeAllRanges();
  54. selection?.addRange(range);
  55. fireEvent.click(firstCard);
  56. expect(onOpenCodeViewer).not.toHaveBeenCalled();
  57. selection?.removeAllRanges();
  58. fireEvent.click(firstCard);
  59. expect(onOpenCodeViewer).toHaveBeenCalledWith(
  60. expect.objectContaining({
  61. type: "code-review",
  62. title: "file-1.ts",
  63. path: "/workspace/src/file-1.ts",
  64. workspacePath: "/workspace",
  65. workspaceIdentity: "remote:ssh:demo:/workspace",
  66. workspaceRemoteSessionId: "remote-1",
  67. review: {
  68. requestId: expect.stringContaining("comment-1:"),
  69. title: "风险 1",
  70. body: "需要修复 1",
  71. priority: 1,
  72. startLine: 1,
  73. endLine: 2,
  74. },
  75. }),
  76. );
  77. fireEvent.keyDown(firstCard, { key: "Enter" });
  78. fireEvent.keyDown(firstCard, { key: " " });
  79. expect(onOpenCodeViewer).toHaveBeenCalledTimes(3);
  80. });
  81. });