assistantPreviewCardValidationHook.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // @vitest-environment jsdom
  2. import { renderHook, waitFor } from "@testing-library/react";
  3. import { beforeEach, describe, expect, it, vi } from "vitest";
  4. import { useAssistantPreviewCardValidation } from "@/AssistantPreviewCards.js";
  5. import type {
  6. AssistantPreviewCard,
  7. AssistantPreviewCardFileStatService,
  8. } from "@/lib/assistantPreviewCards.js";
  9. const mocks = vi.hoisted(() => ({
  10. useWorkspaceServices: vi.fn(),
  11. }));
  12. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  13. useWorkspaceServices: mocks.useWorkspaceServices,
  14. }));
  15. function createPptxCard(path: string): AssistantPreviewCard {
  16. const title = path.split("/").at(-1) ?? path;
  17. return {
  18. id: `file:pptx:${path}`,
  19. type: "file",
  20. kind: "pptx",
  21. title,
  22. subtitleId: "chat.previewCards.pptx",
  23. path,
  24. };
  25. }
  26. function createFileService(): AssistantPreviewCardFileStatService & {
  27. checkFilesExist: ReturnType<typeof vi.fn>;
  28. } {
  29. return {
  30. checkFilesExist: vi.fn(async ({ paths }: { paths: string[] }) =>
  31. paths.map((path) => ({ path, exists: true })),
  32. ),
  33. };
  34. }
  35. describe("useAssistantPreviewCardValidation", () => {
  36. beforeEach(() => {
  37. mocks.useWorkspaceServices.mockReset();
  38. });
  39. it("语义相同但数组引用变化时复用已完成的存在性校验", async () => {
  40. const fileService = createFileService();
  41. mocks.useWorkspaceServices.mockReturnValue({ fileService });
  42. const firstCards = [createPptxCard("/workspace/deck.pptx")];
  43. const { result, rerender } = renderHook(
  44. ({ cards }: { cards: AssistantPreviewCard[] }) =>
  45. useAssistantPreviewCardValidation(cards, { workspacePath: "/workspace" }),
  46. { initialProps: { cards: firstCards } },
  47. );
  48. await waitFor(() => {
  49. expect(result.current.settled).toBe(true);
  50. });
  51. expect(fileService.checkFilesExist).toHaveBeenCalledTimes(1);
  52. rerender({ cards: firstCards.map((card) => ({ ...card })) });
  53. expect(result.current.settled).toBe(true);
  54. expect(result.current.visibleCards).toEqual(firstCards);
  55. expect(fileService.checkFilesExist).toHaveBeenCalledTimes(1);
  56. });
  57. it("路径签名或 workspace fileService 改变时重新校验", async () => {
  58. const firstFileService = createFileService();
  59. const secondFileService = createFileService();
  60. mocks.useWorkspaceServices.mockReturnValue({ fileService: firstFileService });
  61. const { result, rerender } = renderHook(
  62. ({ cards }: { cards: AssistantPreviewCard[] }) =>
  63. useAssistantPreviewCardValidation(cards, { workspacePath: "/workspace" }),
  64. { initialProps: { cards: [createPptxCard("/workspace/first.pptx")] } },
  65. );
  66. await waitFor(() => {
  67. expect(firstFileService.checkFilesExist).toHaveBeenCalledTimes(1);
  68. });
  69. const secondCards = [createPptxCard("/workspace/second.pptx")];
  70. rerender({ cards: secondCards });
  71. await waitFor(() => {
  72. expect(firstFileService.checkFilesExist).toHaveBeenCalledTimes(2);
  73. expect(result.current.visibleCards).toEqual(secondCards);
  74. });
  75. mocks.useWorkspaceServices.mockReturnValue({ fileService: secondFileService });
  76. rerender({ cards: secondCards.map((card) => ({ ...card })) });
  77. await waitFor(() => {
  78. expect(secondFileService.checkFilesExist).toHaveBeenCalledTimes(1);
  79. expect(result.current.visibleCards).toEqual(secondCards);
  80. });
  81. expect(firstFileService.checkFilesExist).toHaveBeenCalledTimes(2);
  82. });
  83. });