composerToolbarFit.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, render } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import {
  6. fitComposerToolbar,
  7. useComposerToolbarFit,
  8. } from "@/prompt-editor/useComposerToolbarFit.js";
  9. afterEach(() => {
  10. cleanup();
  11. vi.restoreAllMocks();
  12. vi.unstubAllGlobals();
  13. });
  14. it("does not clone a full toolbar that already fits on mount or resize", () => {
  15. let resized: ResizeObserverCallback | undefined;
  16. vi.stubGlobal(
  17. "ResizeObserver",
  18. class {
  19. constructor(callback: ResizeObserverCallback) {
  20. resized = callback;
  21. }
  22. observe() {}
  23. disconnect() {}
  24. },
  25. );
  26. vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function () {
  27. return { width: this.hasAttribute("data-composer-leading-content") ? 100 : 800 } as DOMRect;
  28. });
  29. const clone = vi.spyOn(Node.prototype, "cloneNode");
  30. function Toolbar() {
  31. const ref = useComposerToolbarFit();
  32. return createElement(
  33. "div",
  34. { ref },
  35. createElement(
  36. "div",
  37. { "data-composer-leading-actions": true },
  38. createElement("div", { "data-composer-leading-content": true }, "Mode"),
  39. ),
  40. );
  41. }
  42. render(createElement(Toolbar));
  43. resized?.([], {} as ResizeObserver);
  44. expect(clone).not.toHaveBeenCalled();
  45. });
  46. // jsdom 不做排版:只替代几何读数,保留真实 DOM、裁决和各阶段的空间变化。
  47. function fixture() {
  48. const root = document.createElement("div");
  49. root.style.columnGap = "12px";
  50. root.innerHTML = `<div data-composer-leading-actions><div data-composer-leading-content>
  51. <button data-composer-collapse-priority="1"></button>
  52. <button data-composer-collapse-priority="2"></button>
  53. <button data-composer-collapse-priority="0"></button>
  54. </div></div><div data-composer-trailing-actions>
  55. <button class="composer-model-trigger"><span class="composer-provider-prefix">Provider/</span>Model</button>
  56. <button data-composer-collapse-priority="3" data-composer-thought-control></button><button>Send</button>
  57. </div>`;
  58. const controls = [...root.querySelectorAll<HTMLElement>("[data-composer-collapse-priority]")];
  59. const leading = root.querySelector<HTMLElement>("[data-composer-leading-content]")!;
  60. const available = root.querySelector<HTMLElement>("[data-composer-leading-actions]")!;
  61. const trailing = root.querySelector<HTMLElement>("[data-composer-trailing-actions]")!;
  62. const model = root.querySelector<HTMLElement>(".composer-model-trigger")!;
  63. let width = 740;
  64. const rect = (value: number) => ({ width: value }) as DOMRect;
  65. const controlWidth = (index: number) => {
  66. if (!root.contains(controls[index])) return 0;
  67. if (!controls[index].dataset.composerCompact) return 100;
  68. return index === 3 && controls[index].dataset.composerCompact === "true" ? 40 : 28;
  69. };
  70. model.getBoundingClientRect = () =>
  71. rect(root.dataset.composerModelIcon ? 28 : root.dataset.composerProviderCompact ? 200 : 300);
  72. trailing.getBoundingClientRect = () =>
  73. rect(model.getBoundingClientRect().width + controlWidth(3) + 28);
  74. leading.getBoundingClientRect = () => rect(controlWidth(0) + controlWidth(1) + controlWidth(2));
  75. root.getBoundingClientRect = () => rect(width);
  76. available.getBoundingClientRect = () =>
  77. rect(Math.max(0, width - trailing.getBoundingClientRect().width - 12));
  78. return {
  79. root,
  80. controls,
  81. fit(nextWidth: number) {
  82. width = nextWidth;
  83. fitComposerToolbar(root);
  84. },
  85. compact() {
  86. return controls.map((control) => control.dataset.composerCompact ?? "full");
  87. },
  88. };
  89. }
  90. describe("composer toolbar fit", () => {
  91. it("七档独立压缩,think/供应商与末尾两个图标分别裁决,放宽逆序恢复", () => {
  92. const f = fixture();
  93. const expected: Record<number, string[]> = {
  94. 740: ["full", "full", "full", "full"],
  95. 739: ["full", "full", "true", "full"],
  96. 668: ["full", "full", "true", "full"],
  97. 667: ["true", "full", "true", "full"],
  98. 596: ["true", "full", "true", "full"],
  99. 595: ["true", "true", "true", "full"],
  100. 524: ["true", "true", "true", "full"],
  101. 523: ["true", "true", "true", "true"],
  102. 464: ["true", "true", "true", "true"],
  103. 463: ["true", "true", "true", "true"],
  104. 364: ["true", "true", "true", "true"],
  105. 363: ["true", "true", "true", "icon"],
  106. 352: ["true", "true", "true", "icon"],
  107. 351: ["true", "true", "true", "icon"],
  108. 180: ["true", "true", "true", "icon"],
  109. };
  110. for (const width of [
  111. 740, 739, 668, 667, 596, 595, 524, 523, 464, 463, 364, 363, 352, 351, 180, 352, 364, 464, 524,
  112. 596, 668, 740,
  113. ]) {
  114. f.fit(width);
  115. expect(f.compact(), `width=${width}`).toEqual(expected[width]);
  116. expect(f.root.dataset.composerProviderCompact === "true").toBe(width < 464);
  117. expect(f.root.dataset.composerModelIcon === "true").toBe(width < 352);
  118. }
  119. });
  120. it("手机缺少 Computer 时直接进入第二档,think 与模型最后分两档收起", () => {
  121. const f = fixture();
  122. f.controls[2].remove();
  123. f.fit(640);
  124. expect(f.compact()).toEqual(["full", "full", "full", "full"]);
  125. f.fit(639);
  126. expect(f.compact()).toEqual(["true", "full", "full", "full"]);
  127. expect(f.root.dataset.composerProviderCompact).toBeUndefined();
  128. f.fit(336);
  129. expect(f.root.dataset.composerProviderCompact).toBe("true");
  130. expect(f.controls[3].dataset.composerCompact).toBe("true");
  131. f.fit(335);
  132. expect(f.root.dataset.composerModelIcon).toBeUndefined();
  133. expect(f.controls[3].dataset.composerCompact).toBe("icon");
  134. f.fit(323);
  135. expect(f.root.dataset.composerModelIcon).toBe("true");
  136. });
  137. it("缺少可收起控件时仍处理供应商和模型,空间恢复时清除压缩状态", () => {
  138. const f = fixture();
  139. f.controls.forEach((control) => control.remove());
  140. f.fit(240);
  141. expect(f.root.dataset.composerProviderCompact).toBe("true");
  142. expect(f.root.dataset.composerModelIcon).toBeUndefined();
  143. f.fit(239);
  144. expect(f.root.dataset.composerModelIcon).toBe("true");
  145. f.fit(340);
  146. expect(f.root.dataset.composerProviderCompact).toBeUndefined();
  147. expect(f.root.dataset.composerModelIcon).toBeUndefined();
  148. });
  149. });
  150. // Side Pane 完整视图浮窗的单行输入框:工具栏宽度随内容变化,不能测量,直接收到最后一级。
  151. describe("icon-only composer toolbar", () => {
  152. it("宽度足够时也按同一顺序收到只留图标", () => {
  153. const f = fixture();
  154. f.root.getBoundingClientRect = () => ({ width: 2_000 }) as DOMRect;
  155. fitComposerToolbar(f.root, { iconOnly: true });
  156. expect(f.compact()).toEqual(["true", "true", "true", "icon"]);
  157. expect(f.root.dataset.composerProviderCompact).toBe("true");
  158. expect(f.root.dataset.composerModelIcon).toBe("true");
  159. });
  160. it("hook 不克隆测量;切回普通布局后按实际宽度恢复完整显示", () => {
  161. vi.stubGlobal(
  162. "ResizeObserver",
  163. class {
  164. observe() {}
  165. disconnect() {}
  166. },
  167. );
  168. vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function () {
  169. if (this.hasAttribute("data-composer-leading-content")) {
  170. return { width: this.querySelector("[data-composer-compact]") ? 28 : 100 } as DOMRect;
  171. }
  172. return { width: 800 } as DOMRect;
  173. });
  174. const clone = vi.spyOn(Node.prototype, "cloneNode");
  175. function Toolbar({ iconOnly }: { iconOnly: boolean }) {
  176. const ref = useComposerToolbarFit({ iconOnly });
  177. return createElement(
  178. "div",
  179. { ref, "data-testid": "toolbar" },
  180. createElement(
  181. "div",
  182. { "data-composer-leading-actions": true },
  183. createElement(
  184. "div",
  185. { "data-composer-leading-content": true },
  186. createElement("button", { "data-composer-collapse-priority": "1" }, "Mode"),
  187. ),
  188. ),
  189. );
  190. }
  191. const view = render(createElement(Toolbar, { iconOnly: true }));
  192. const button = view.container.querySelector<HTMLElement>("button")!;
  193. expect(button.dataset.composerCompact).toBe("true");
  194. expect(view.getByTestId("toolbar").dataset.composerModelIcon).toBe("true");
  195. expect(clone).not.toHaveBeenCalled();
  196. view.rerender(createElement(Toolbar, { iconOnly: false }));
  197. expect(button.dataset.composerCompact).toBeUndefined();
  198. expect(view.getByTestId("toolbar").dataset.composerModelIcon).toBeUndefined();
  199. });
  200. });
  201. // 合并入口只有完整、模型名+绿条、图标+绿条三态。
  202. describe("combined composer toolbar fit", () => {
  203. it("shrinks Computer, Mode, Plan, then thinking, then model; expands in reverse", () => {
  204. const f = fixture();
  205. const thought = f.controls[3];
  206. thought.remove();
  207. const model = f.root.querySelector<HTMLElement>(".composer-model-trigger")!;
  208. model.dataset.composerCombinedModel = "true";
  209. model.dataset.composerCollapsePriority = "3";
  210. model.querySelector(".composer-provider-prefix")?.remove();
  211. model.getBoundingClientRect = () =>
  212. ({
  213. width: f.root.dataset.composerModelIcon ? 40 : model.dataset.composerCompact ? 216 : 280,
  214. }) as DOMRect;
  215. const trailing = f.root.querySelector<HTMLElement>("[data-composer-trailing-actions]")!;
  216. trailing.getBoundingClientRect = () =>
  217. ({ width: model.getBoundingClientRect().width + 28 }) as DOMRect;
  218. for (const width of [620, 619, 547, 475, 403, 339, 180, 339, 403, 475, 547, 619, 620]) {
  219. f.fit(width);
  220. expect(f.controls[2].dataset.composerCompact === "true").toBe(width < 620);
  221. expect(f.controls[0].dataset.composerCompact === "true").toBe(width < 548);
  222. expect(f.controls[1].dataset.composerCompact === "true").toBe(width < 476);
  223. expect(model.dataset.composerCompact === "true").toBe(width < 404);
  224. expect(f.root.dataset.composerModelIcon === "true").toBe(width < 340);
  225. expect(f.root.dataset.composerProviderCompact).toBeUndefined();
  226. }
  227. });
  228. });