sidePaneFullViewModel.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. FLOATING_ANCHORS,
  4. FLOATING_MARGIN_PX,
  5. clampFloatingPosition,
  6. dismissFloatingAnswerBubble,
  7. parseFloatingAnchor,
  8. rebaselineFloatingAnswerBubbles,
  9. reduceFloatingAnswerBubbles,
  10. resolveFloatingAnswerPreview,
  11. resolveFloatingExpandedHeight,
  12. resolveFloatingPosition,
  13. resolveFloatingTransformOrigin,
  14. resolveFloatingWidth,
  15. resolveNearestFloatingAnchor,
  16. } from "@/side-pane-full-view/model.js";
  17. const SLOT = { left: 100, top: 60, width: 1200, height: 800 };
  18. const SIZE = { width: 560, height: 120 };
  19. describe("side pane full view floating geometry", () => {
  20. it("places every anchor inside the slot with a 16px margin", () => {
  21. const m = FLOATING_MARGIN_PX;
  22. expect(resolveFloatingPosition("top-left", SLOT, SIZE)).toEqual({ left: 116, top: 76 });
  23. expect(resolveFloatingPosition("top-right", SLOT, SIZE)).toEqual({
  24. left: 100 + 1200 - m - 560,
  25. top: 76,
  26. });
  27. expect(resolveFloatingPosition("left-center", SLOT, SIZE)).toEqual({
  28. left: 116,
  29. top: 60 + (800 - 120) / 2,
  30. });
  31. expect(resolveFloatingPosition("right-center", SLOT, SIZE)).toEqual({
  32. left: 100 + 1200 - m - 560,
  33. top: 60 + (800 - 120) / 2,
  34. });
  35. expect(resolveFloatingPosition("bottom-left", SLOT, SIZE)).toEqual({
  36. left: 116,
  37. top: 60 + 800 - m - 120,
  38. });
  39. expect(resolveFloatingPosition("bottom-center", SLOT, SIZE)).toEqual({
  40. left: 100 + (1200 - 560) / 2,
  41. top: 60 + 800 - m - 120,
  42. });
  43. expect(resolveFloatingPosition("bottom-right", SLOT, SIZE)).toEqual({
  44. left: 100 + 1200 - m - 560,
  45. top: 60 + 800 - m - 120,
  46. });
  47. });
  48. it("grows bottom anchors upward and top anchors downward", () => {
  49. const tall = { width: 560, height: 500 };
  50. expect(resolveFloatingPosition("bottom-right", SLOT, tall).top).toBe(60 + 800 - 16 - 500);
  51. expect(resolveFloatingPosition("top-right", SLOT, tall).top).toBe(76);
  52. });
  53. it("keeps an oversized window pinned to the slot origin", () => {
  54. const huge = { width: 2000, height: 2000 };
  55. expect(resolveFloatingPosition("bottom-right", SLOT, huge)).toEqual({ left: 100, top: 60 });
  56. });
  57. it("snaps to the anchor whose resting position is closest to the dropped window", () => {
  58. // 窗口中心拖到左上角附近
  59. expect(resolveNearestFloatingAnchor({ left: 130, top: 90 }, SLOT, SIZE)).toBe("top-left");
  60. // 拖到底部中间
  61. expect(resolveNearestFloatingAnchor({ left: 420, top: 640 }, SLOT, SIZE)).toBe("bottom-center");
  62. // 拖到右侧中部
  63. expect(resolveNearestFloatingAnchor({ left: 700, top: 400 }, SLOT, SIZE)).toBe("right-center");
  64. });
  65. it("clamps a dragged window so it never leaves the slot", () => {
  66. expect(clampFloatingPosition({ left: -50, top: 2000 }, SLOT, SIZE)).toEqual({
  67. left: 100,
  68. top: 60 + 800 - 120,
  69. });
  70. });
  71. it("caps width and expanded height to the slot", () => {
  72. expect(resolveFloatingWidth(1200)).toBe(560);
  73. expect(resolveFloatingWidth(400)).toBe(400 - 32);
  74. expect(resolveFloatingExpandedHeight(1000)).toBe(640);
  75. expect(resolveFloatingExpandedHeight(500)).toBe(500 - 32);
  76. });
  77. it("scales minimize / restore from the corner the bubble sits on", () => {
  78. expect(resolveFloatingTransformOrigin("bottom-right")).toBe("right bottom");
  79. expect(resolveFloatingTransformOrigin("top-left")).toBe("left top");
  80. expect(resolveFloatingTransformOrigin("left-center")).toBe("left center");
  81. expect(resolveFloatingTransformOrigin("bottom-center")).toBe("center bottom");
  82. });
  83. it("accepts only known anchors from persisted preferences", () => {
  84. for (const anchor of FLOATING_ANCHORS) {
  85. expect(parseFloatingAnchor(anchor)).toBe(anchor);
  86. }
  87. expect(parseFloatingAnchor("top-center")).toBeNull();
  88. expect(parseFloatingAnchor(null)).toBeNull();
  89. });
  90. });
  91. // 回答气泡(docs/ui/side-pane-full-view.md):基线之后出现的已结束回合才弹出,只有 × 或点击才清除。
  92. describe("side pane full view answer bubble", () => {
  93. const answer = (rowId: number, text = `answer ${rowId}`) => ({ rowId, text });
  94. const report = (
  95. ownerKey: string,
  96. turnKey: string,
  97. running: boolean,
  98. latestAnswer: ReturnType<typeof answer> | null,
  99. ) => ({ ownerKey, latestTurn: { turnKey, running, answer: latestAnswer } });
  100. it("treats the turn already finished on first sight as seen", () => {
  101. let bubbles = reduceFloatingAnswerBubbles(new Map(), report("task-a", "t1", false, answer(1)));
  102. expect(bubbles.get("task-a")).toEqual({ seenTurnKey: "t1", answer: null });
  103. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t1", false, answer(1)));
  104. expect(bubbles.get("task-a")?.answer ?? null).toBeNull();
  105. });
  106. it("pops a turn that finishes without ever being seen running", () => {
  107. // 回放或很快的回答:同一次渲染里就从 t1 变成已结束的 t2。
  108. let bubbles = reduceFloatingAnswerBubbles(new Map(), report("task-a", "t1", false, answer(1)));
  109. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t2", false, answer(2)));
  110. expect(bubbles.get("task-a")).toEqual({ seenTurnKey: "t2", answer: answer(2) });
  111. });
  112. it("pops the turn that was running on first sight once it completes", () => {
  113. let bubbles = reduceFloatingAnswerBubbles(new Map(), report("task-a", "t2", true, null));
  114. expect(bubbles.get("task-a")).toEqual({ seenTurnKey: null, answer: null });
  115. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t2", false, answer(2)));
  116. expect(bubbles.get("task-a")?.answer).toEqual(answer(2));
  117. });
  118. it("keeps the bubble while the next turn runs and replaces it with the new answer", () => {
  119. let bubbles = reduceFloatingAnswerBubbles(new Map(), report("task-a", "t1", false, answer(1)));
  120. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t2", false, answer(2)));
  121. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t3", true, null));
  122. expect(bubbles.get("task-a")?.answer).toEqual(answer(2));
  123. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t3", false, answer(3)));
  124. expect(bubbles.get("task-a")?.answer).toEqual(answer(3));
  125. });
  126. it("keeps owners apart and ignores reports without a turn", () => {
  127. let bubbles = reduceFloatingAnswerBubbles(new Map(), report("task-a", "t1", false, answer(1)));
  128. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-b", "t9", false, answer(9)));
  129. expect(bubbles.get("task-b")?.answer ?? null).toBeNull();
  130. expect(reduceFloatingAnswerBubbles(bubbles, { ownerKey: "task-a", latestTurn: null })).toBe(
  131. bubbles,
  132. );
  133. });
  134. it("clears only on dismiss and does not pop answers already seen outside the floating window", () => {
  135. let bubbles = reduceFloatingAnswerBubbles(new Map(), report("task-a", "t1", false, answer(1)));
  136. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t2", false, answer(2)));
  137. bubbles = dismissFloatingAnswerBubble(bubbles, "task-a");
  138. expect(bubbles.get("task-a")?.answer ?? null).toBeNull();
  139. // 离开浮窗期间 t3 在对话 tab 里结束,回到浮窗重新建立基线,不弹出。
  140. bubbles = rebaselineFloatingAnswerBubbles(bubbles);
  141. bubbles = reduceFloatingAnswerBubbles(bubbles, report("task-a", "t3", false, answer(3)));
  142. expect(bubbles.get("task-a")?.answer ?? null).toBeNull();
  143. });
  144. it("previews the answer as plain text", () => {
  145. expect(
  146. resolveFloatingAnswerPreview(
  147. "## 结论\n\n- **已完成** 修改,见 [文档](https://example.com)\n- 运行 `pnpm test`\n\n```ts\nconst a = 1;\n```\n保留 snake_case_name",
  148. ),
  149. ).toBe("结论 已完成 修改,见 文档 运行 pnpm test 保留 snake_case_name");
  150. });
  151. });