usePptxElementReferences.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // @vitest-environment jsdom
  2. import { act, renderHook } from "@testing-library/react";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. dispatchPptxElementReferenceAddToChat,
  6. type PptxElementReference,
  7. } from "@/lib/pptxElementReference.js";
  8. import { usePptxElementReferences } from "@/v4/composer/usePptxElementReferences.js";
  9. function makeReference(
  10. overrides: Partial<PptxElementReference> = {},
  11. ): PptxElementReference {
  12. return {
  13. bounds: { x: 0, y: 0, width: 100, height: 40 },
  14. capturedAt: 1,
  15. id: "reference-1",
  16. nodeId: "7",
  17. nodeName: "Title 1",
  18. nodeType: "shape",
  19. remoteSessionId: "remote-1",
  20. slideIndex: 0,
  21. slidePart: "ppt/slides/slide1.xml",
  22. sourceFingerprint: `sha256:${"a".repeat(64)}`,
  23. sourcePath: "/workspace/deck.pptx",
  24. sourceTitle: "deck.pptx",
  25. workspaceIdentity: "ssh:user@host:/workspace",
  26. workspacePath: "/workspace",
  27. zIndex: 0,
  28. ...overrides,
  29. };
  30. }
  31. describe("usePptxElementReferences", () => {
  32. it("只接收同 workspace/remote scope 的事件,并在 draft scope 切换时清空", () => {
  33. const hook = renderHook(
  34. ({ scopeId }) =>
  35. usePptxElementReferences({
  36. workspacePath: "/mounted/workspace",
  37. workspaceIdentity: "ssh:user@host:/workspace",
  38. remoteSessionId: "remote-1",
  39. scopeId,
  40. }),
  41. { initialProps: { scopeId: "draft-a" } },
  42. );
  43. act(() => {
  44. dispatchPptxElementReferenceAddToChat(
  45. makeReference({ remoteSessionId: "remote-2" }),
  46. );
  47. });
  48. expect(hook.result.current.references).toHaveLength(0);
  49. act(() => dispatchPptxElementReferenceAddToChat(makeReference()));
  50. expect(hook.result.current.references).toHaveLength(1);
  51. hook.rerender({ scopeId: "session-b" });
  52. expect(hook.result.current.references).toHaveLength(0);
  53. });
  54. it("禁用全局监听时不接收 Preview Pane 引用", () => {
  55. const hook = renderHook(() =>
  56. usePptxElementReferences({
  57. workspacePath: "/workspace",
  58. workspaceIdentity: "ssh:user@host:/workspace",
  59. remoteSessionId: "remote-1",
  60. listenAddToChatEvents: false,
  61. scopeId: "draft-a",
  62. }),
  63. );
  64. act(() => dispatchPptxElementReferenceAddToChat(makeReference()));
  65. expect(hook.result.current.references).toHaveLength(0);
  66. });
  67. });