conversationSelectionGuard.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. guardConversationSelectionCandidate,
  4. hasExcludedConversationSelectionEndpoint,
  5. } from "../src/lib/conversationSelectionGuard.js";
  6. const VALID = {
  7. enabled: true,
  8. sameRow: true,
  9. insideTimeline: true,
  10. excluded: false,
  11. sameSelectableRegion: true,
  12. supportedContent: true,
  13. text: "selected text",
  14. hasLayout: true,
  15. } as const;
  16. describe("conversation selection guard", () => {
  17. it("接受单 row 正文选区并识别单条超限", () => {
  18. expect(guardConversationSelectionCandidate(VALID)).toBe("eligible");
  19. expect(guardConversationSelectionCandidate({ ...VALID, text: "x".repeat(8_001) })).toBe(
  20. "single-limit",
  21. );
  22. });
  23. it.each([
  24. { sameRow: false },
  25. { insideTimeline: false },
  26. { excluded: true },
  27. { sameSelectableRegion: false },
  28. { supportedContent: false },
  29. { enabled: false },
  30. { text: "" },
  31. { hasLayout: false },
  32. ])("拒绝跨行、控件、非正文、关闭门禁和空选区 %#", (patch) => {
  33. expect(guardConversationSelectionCandidate({ ...VALID, ...patch })).toBe("ineligible");
  34. });
  35. it("只按选区端点拒绝控件,避免表格工具栏夹在 Range 中时误伤正文", () => {
  36. const contentEndpoint = { closest: () => null } as unknown as Element;
  37. const controlEndpoint = { closest: () => ({}) as Element } as unknown as Element;
  38. expect(hasExcludedConversationSelectionEndpoint(contentEndpoint, contentEndpoint)).toBe(false);
  39. expect(hasExcludedConversationSelectionEndpoint(controlEndpoint, contentEndpoint)).toBe(true);
  40. expect(hasExcludedConversationSelectionEndpoint(contentEndpoint, controlEndpoint)).toBe(true);
  41. });
  42. });