composerToolbarFit.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @vitest-environment jsdom
  2. import { describe, expect, it } from "vitest";
  3. import { fitComposerToolbar } from "@/prompt-editor/useComposerToolbarFit.js";
  4. // jsdom 不做排版:只替代几何读数,保留真实 DOM、裁决和各阶段的空间变化。
  5. function fixture() {
  6. const root = document.createElement("div");
  7. root.style.columnGap = "12px";
  8. root.innerHTML = `<div data-composer-leading-actions><div data-composer-leading-content>
  9. <button data-composer-collapse-priority="1"></button>
  10. <button data-composer-collapse-priority="2"></button>
  11. <button data-composer-collapse-priority="0"></button>
  12. </div></div><div data-composer-trailing-actions>
  13. <button class="composer-model-trigger"><span class="composer-provider-prefix">Provider/</span>Model</button>
  14. <button data-composer-collapse-priority="3" data-composer-thought-control></button><button>Send</button>
  15. </div>`;
  16. const controls = [...root.querySelectorAll<HTMLElement>("[data-composer-collapse-priority]")];
  17. const leading = root.querySelector<HTMLElement>("[data-composer-leading-content]")!;
  18. const available = root.querySelector<HTMLElement>("[data-composer-leading-actions]")!;
  19. const trailing = root.querySelector<HTMLElement>("[data-composer-trailing-actions]")!;
  20. const model = root.querySelector<HTMLElement>(".composer-model-trigger")!;
  21. let width = 740;
  22. const rect = (value: number) => ({ width: value }) as DOMRect;
  23. const controlWidth = (index: number) => {
  24. if (!root.contains(controls[index])) return 0;
  25. if (!controls[index].dataset.composerCompact) return 100;
  26. return index === 3 && controls[index].dataset.composerCompact === "true" ? 40 : 28;
  27. };
  28. model.getBoundingClientRect = () =>
  29. rect(root.dataset.composerModelIcon ? 28 : root.dataset.composerProviderCompact ? 200 : 300);
  30. trailing.getBoundingClientRect = () =>
  31. rect(model.getBoundingClientRect().width + controlWidth(3) + 28);
  32. leading.getBoundingClientRect = () => rect(controlWidth(0) + controlWidth(1) + controlWidth(2));
  33. root.getBoundingClientRect = () => rect(width);
  34. available.getBoundingClientRect = () =>
  35. rect(Math.max(0, width - trailing.getBoundingClientRect().width - 12));
  36. return {
  37. root,
  38. controls,
  39. fit(nextWidth: number) {
  40. width = nextWidth;
  41. fitComposerToolbar(root);
  42. },
  43. compact() {
  44. return controls.map((control) => control.dataset.composerCompact ?? "full");
  45. },
  46. };
  47. }
  48. describe("composer toolbar fit", () => {
  49. it("七档独立压缩,think/供应商与末尾两个图标分别裁决,放宽逆序恢复", () => {
  50. const f = fixture();
  51. const expected: Record<number, string[]> = {
  52. 740: ["full", "full", "full", "full"],
  53. 739: ["full", "full", "true", "full"],
  54. 668: ["full", "full", "true", "full"],
  55. 667: ["true", "full", "true", "full"],
  56. 596: ["true", "full", "true", "full"],
  57. 595: ["true", "true", "true", "full"],
  58. 524: ["true", "true", "true", "full"],
  59. 523: ["true", "true", "true", "true"],
  60. 464: ["true", "true", "true", "true"],
  61. 463: ["true", "true", "true", "true"],
  62. 364: ["true", "true", "true", "true"],
  63. 363: ["true", "true", "true", "icon"],
  64. 352: ["true", "true", "true", "icon"],
  65. 351: ["true", "true", "true", "icon"],
  66. 180: ["true", "true", "true", "icon"],
  67. };
  68. for (const width of [
  69. 740, 739, 668, 667, 596, 595, 524, 523, 464, 463, 364, 363, 352, 351, 180, 352, 364, 464, 524,
  70. 596, 668, 740,
  71. ]) {
  72. f.fit(width);
  73. expect(f.compact(), `width=${width}`).toEqual(expected[width]);
  74. expect(f.root.dataset.composerProviderCompact === "true").toBe(width < 464);
  75. expect(f.root.dataset.composerModelIcon === "true").toBe(width < 352);
  76. }
  77. });
  78. it("手机缺少 Computer 时直接进入第二档,think 与模型最后分两档收起", () => {
  79. const f = fixture();
  80. f.controls[2].remove();
  81. f.fit(640);
  82. expect(f.compact()).toEqual(["full", "full", "full", "full"]);
  83. f.fit(639);
  84. expect(f.compact()).toEqual(["true", "full", "full", "full"]);
  85. expect(f.root.dataset.composerProviderCompact).toBeUndefined();
  86. f.fit(336);
  87. expect(f.root.dataset.composerProviderCompact).toBe("true");
  88. expect(f.controls[3].dataset.composerCompact).toBe("true");
  89. f.fit(335);
  90. expect(f.root.dataset.composerModelIcon).toBeUndefined();
  91. expect(f.controls[3].dataset.composerCompact).toBe("icon");
  92. f.fit(323);
  93. expect(f.root.dataset.composerModelIcon).toBe("true");
  94. });
  95. it("缺少可收起控件时仍处理供应商和模型,空间恢复时清除压缩状态", () => {
  96. const f = fixture();
  97. f.controls.forEach((control) => control.remove());
  98. f.fit(240);
  99. expect(f.root.dataset.composerProviderCompact).toBe("true");
  100. expect(f.root.dataset.composerModelIcon).toBeUndefined();
  101. f.fit(239);
  102. expect(f.root.dataset.composerModelIcon).toBe("true");
  103. f.fit(340);
  104. expect(f.root.dataset.composerProviderCompact).toBeUndefined();
  105. expect(f.root.dataset.composerModelIcon).toBeUndefined();
  106. });
  107. });