conversationSelectionGuard.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. guardConversationSelectionCandidate,
  6. hasExcludedConversationSelectionEndpoint,
  7. } from "../src/lib/conversationSelectionGuard.js";
  8. const VALID = {
  9. enabled: true,
  10. sameRow: true,
  11. insideTimeline: true,
  12. excluded: false,
  13. sameSelectableRegion: true,
  14. supportedContent: true,
  15. text: "selected text",
  16. hasLayout: true,
  17. } as const;
  18. describe("conversation selection guard", () => {
  19. it("接受单 row 正文选区并识别单条超限", () => {
  20. expect(guardConversationSelectionCandidate(VALID)).toBe("eligible");
  21. expect(guardConversationSelectionCandidate({ ...VALID, text: "x".repeat(8_001) })).toBe(
  22. "single-limit",
  23. );
  24. });
  25. it.each([
  26. { sameRow: false },
  27. { insideTimeline: false },
  28. { excluded: true },
  29. { sameSelectableRegion: false },
  30. { supportedContent: false },
  31. { enabled: false },
  32. { text: "" },
  33. { hasLayout: false },
  34. ])("拒绝跨行、控件、非正文、关闭门禁和空选区 %#", (patch) => {
  35. expect(guardConversationSelectionCandidate({ ...VALID, ...patch })).toBe("ineligible");
  36. });
  37. it("只按选区端点拒绝控件,避免表格工具栏夹在 Range 中时误伤正文", () => {
  38. const contentEndpoint = { closest: () => null } as unknown as Element;
  39. const controlEndpoint = { closest: () => ({}) as Element } as unknown as Element;
  40. expect(hasExcludedConversationSelectionEndpoint(contentEndpoint, contentEndpoint)).toBe(false);
  41. expect(hasExcludedConversationSelectionEndpoint(controlEndpoint, contentEndpoint)).toBe(true);
  42. expect(hasExcludedConversationSelectionEndpoint(contentEndpoint, controlEndpoint)).toBe(true);
  43. });
  44. it("使用紧凑 tooltip 密度并统一为辅助对话术语", () => {
  45. const source = readFileSync(
  46. resolve(process.cwd(), "packages/ui/src/v4/SelectionActionMenu.tsx"),
  47. "utf8",
  48. );
  49. const zh = readFileSync(
  50. resolve(process.cwd(), "packages/ui/src/i18n/locales/zh-CN.ts"),
  51. "utf8",
  52. );
  53. expect(source).toContain("rounded-lg");
  54. expect(source).toContain("text-ui-sm");
  55. expect(source).toContain("shadow-md");
  56. expect(source).toContain("px-2.5 py-1.5");
  57. expect(source).not.toContain("range.cloneContents().querySelector");
  58. expect(source).not.toContain("rounded-xl");
  59. expect(zh).toContain('"sidePane.selectionChat": "辅助对话"');
  60. expect(zh).toContain('"chat.selections.askInSideChat": "在辅助对话中提问"');
  61. });
  62. });