conversation-session-status-panel-inline-layout.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { setRendererViewport, clearRendererViewport } from "#e2e/helpers/renderer-viewport";
  2. import { TID_CHAT_SUMMARY_PANEL } from "@zcode/shared/test-ids";
  3. import { clearAppData } from "../helpers/desktop-app.js";
  4. import {
  5. E2E_REPLY_TOKEN,
  6. prepareV4ConversationE2E,
  7. sendV4Prompt,
  8. startNewV4Draft,
  9. waitForV4AssistantMessageContaining,
  10. waitForV4ConversationState,
  11. waitForV4ComposerText,
  12. waitForV4UserMessageContaining,
  13. } from "#e2e/helpers/v4-conversation";
  14. const WIDE_VIEWPORT = { width: 1800, height: 1000 };
  15. const NARROW_VIEWPORT = { width: 1100, height: 900 };
  16. const DRAFT_WIDTH_CLASS = "max-w-2xl";
  17. const NORMAL_WIDTH_CLASS =
  18. "w-full @min-[864px]/conversation:w-[calc(100%_-_6rem)] @min-[864px]/conversation:max-w-4xl @min-[1280px]/conversation:w-[calc(100%_-_24rem)] @min-[1280px]/conversation:max-w-6xl";
  19. const INLINE_WIDTH_CLASS = NORMAL_WIDTH_CLASS;
  20. const INLINE_OFFSET_CLASS = "@min-[1280px]/conversation:-translate-x-42";
  21. describe("会话区 status panel inline layout E2E", () => {
  22. after(async () => {
  23. await clearRendererViewport();
  24. await browser.electron.restoreAllMocks();
  25. await clearAppData();
  26. });
  27. it("status panel 只有出现 inline 偏移时才收敛消息列和输入区宽度", async function () {
  28. this.timeout(180000);
  29. await setRendererViewport(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  30. await prepareV4ConversationE2E();
  31. await setRendererViewport(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  32. const draftLayout = await waitForChatLayoutSnapshot(
  33. (snapshot) =>
  34. snapshot.panelState === null &&
  35. snapshot.dockClassName.includes(DRAFT_WIDTH_CLASS) &&
  36. !snapshot.dockClassName.includes(INLINE_OFFSET_CLASS),
  37. "新建草稿没有保持无 status panel 的窄 composer 布局",
  38. );
  39. expect(draftLayout.panelState).toBeNull();
  40. const runId = Date.now();
  41. const plainPrompt = `E2E_STATUS_PANEL_EMPTY_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  42. await sendV4Prompt(plainPrompt);
  43. await waitForV4ComposerText("", "普通首发后输入框没有清空");
  44. await waitForV4UserMessageContaining(plainPrompt);
  45. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  46. await waitForV4ConversationState(
  47. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  48. "普通首发完成后没有回到 idle",
  49. 90000,
  50. );
  51. const noPanelLayout = await waitForChatLayoutSnapshot(
  52. (snapshot) =>
  53. snapshot.panelState === null &&
  54. snapshot.contentClassName?.includes(NORMAL_WIDTH_CLASS) === true &&
  55. snapshot.dockClassName.includes(NORMAL_WIDTH_CLASS) &&
  56. !snapshot.hasContentInlineOffset &&
  57. !snapshot.hasDockInlineOffset,
  58. "无 status 内容的历史 task 没有保持普通消息列宽度",
  59. );
  60. expect(noPanelLayout.panelState).toBeNull();
  61. await startNewV4Draft();
  62. await setRendererViewport(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  63. const goalMarker = `E2E_STATUS_PANEL_GOAL_${runId}`;
  64. const goalPrompt = `/goal ${goalMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  65. await sendV4Prompt(goalPrompt);
  66. await waitForV4ComposerText("", "goal 首发后输入框没有清空");
  67. await waitForV4UserMessageContaining(goalMarker);
  68. await waitForV4ConversationState(
  69. (snapshot) =>
  70. snapshot.targetObjective?.includes(goalMarker) === true && Boolean(snapshot.taskId),
  71. "goal 首发没有形成 status panel 可展示的 target",
  72. 60000,
  73. );
  74. await expectInlinePanelLayout("宽屏自动展开 status panel 后没有同步收敛聊天宽度");
  75. await setRendererViewport(NARROW_VIEWPORT.width, NARROW_VIEWPORT.height);
  76. await expectMiniPanelLayout("窄屏自动收起 status panel 后没有恢复普通聊天宽度");
  77. await setRendererViewport(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  78. await expectInlinePanelLayout("重新放大后自动展开 status panel 没有恢复 inline 布局");
  79. await clickSummaryPanelButton(["Collapse to capsule", "收起为胶囊"]);
  80. await expectMiniPanelLayout("手动收起 status panel 后没有移除 inline 偏移");
  81. await clickSummaryPanelButton(["Expand status", "展开状态"]);
  82. await expectInlinePanelLayout("手动展开 status panel 后没有恢复 inline 偏移");
  83. });
  84. });
  85. async function expectInlinePanelLayout(timeoutMsg: string) {
  86. const snapshot = await waitForChatLayoutSnapshot(
  87. (current) =>
  88. current.panelState === "expanded" &&
  89. current.contentClassName?.includes(INLINE_WIDTH_CLASS) === true &&
  90. current.dockClassName.includes(INLINE_WIDTH_CLASS) &&
  91. current.hasContentInlineOffset &&
  92. current.hasDockInlineOffset &&
  93. transitionMatchesContainer(current.contentTransitionProperty, current.containerWidth) &&
  94. transitionMatchesContainer(current.dockTransitionProperty, current.containerWidth),
  95. timeoutMsg,
  96. );
  97. expect(snapshot.panelState).toBe("expanded");
  98. }
  99. async function expectMiniPanelLayout(timeoutMsg: string) {
  100. const snapshot = await waitForChatLayoutSnapshot(
  101. (current) =>
  102. current.panelState === "collapsed" &&
  103. current.contentClassName?.includes(NORMAL_WIDTH_CLASS) === true &&
  104. current.dockClassName.includes(NORMAL_WIDTH_CLASS) &&
  105. !current.hasContentInlineOffset &&
  106. !current.hasDockInlineOffset &&
  107. transitionMatchesContainer(current.contentTransitionProperty, current.containerWidth) &&
  108. transitionMatchesContainer(current.dockTransitionProperty, current.containerWidth),
  109. timeoutMsg,
  110. );
  111. expect(snapshot.panelState).toBe("collapsed");
  112. }
  113. function transitionMatchesContainer(value: string, containerWidth: number) {
  114. return value.includes("transform") && (containerWidth >= 1280 || value.includes("max-width"));
  115. }
  116. async function waitForChatLayoutSnapshot(
  117. predicate: (snapshot: ChatLayoutSnapshot) => boolean,
  118. timeoutMsg: string,
  119. timeout = 30000,
  120. ) {
  121. let latest: ChatLayoutSnapshot | null = null;
  122. try {
  123. await browser.waitUntil(
  124. async () => {
  125. latest = await getChatLayoutSnapshot();
  126. return predicate(latest);
  127. },
  128. {
  129. timeout,
  130. timeoutMsg,
  131. },
  132. );
  133. } catch (error) {
  134. latest = await getChatLayoutSnapshot().catch(() => latest);
  135. throw new Error(`${timeoutMsg}; latest=${JSON.stringify(latest)}`, {
  136. cause: error,
  137. });
  138. }
  139. return getChatLayoutSnapshot();
  140. }
  141. interface ChatLayoutSnapshot {
  142. containerWidth: number;
  143. contentClassName: string | null;
  144. contentTransitionProperty: string;
  145. dockClassName: string;
  146. dockTransitionProperty: string;
  147. hasContentInlineOffset: boolean;
  148. hasDockInlineOffset: boolean;
  149. panelState: string | null;
  150. }
  151. function getChatLayoutSnapshot(): Promise<ChatLayoutSnapshot> {
  152. return browser.execute((summaryPanelTestId) => {
  153. const panel = document.querySelector<HTMLElement>(`[data-testid="${summaryPanelTestId}"]`);
  154. // V4 的内容列和 dock 都有专用锚点;旧 chat-message 容器已不存在。
  155. const pane = document.querySelector('[data-testid="v4-session-pane-workspace-main"]');
  156. const dock = pane?.querySelector<HTMLElement>("[data-v4-composer-dock-content]") ?? null;
  157. const content = pane?.querySelector<HTMLElement>("[data-v4-timeline-virtual-history]") ?? null;
  158. const container = pane?.querySelector<HTMLElement>('[class*="@container/conversation"]');
  159. const fullPanelButton = panel?.querySelector<HTMLElement>(
  160. 'button[aria-label="Collapse to capsule"], button[aria-label="收起为胶囊"]',
  161. );
  162. const contentStyle = content ? window.getComputedStyle(content) : null;
  163. const dockStyle = dock ? window.getComputedStyle(dock) : null;
  164. return {
  165. containerWidth: container?.getBoundingClientRect().width ?? 0,
  166. contentClassName: content?.className ?? null,
  167. contentTransitionProperty: contentStyle?.transitionProperty ?? "",
  168. dockClassName: dock?.className ?? "",
  169. dockTransitionProperty: dockStyle?.transitionProperty ?? "",
  170. hasContentInlineOffset: Math.abs(Number.parseFloat(contentStyle?.translate ?? "0") + 168) < 1,
  171. hasDockInlineOffset: Math.abs(Number.parseFloat(dockStyle?.translate ?? "0") + 168) < 1,
  172. panelState: panel
  173. ? fullPanelButton && fullPanelButton.getBoundingClientRect().width > 0
  174. ? "expanded"
  175. : "collapsed"
  176. : null,
  177. };
  178. }, TID_CHAT_SUMMARY_PANEL);
  179. }
  180. async function clickSummaryPanelButton(labels: readonly string[]) {
  181. const clicked = await browser.execute(
  182. (summaryPanelTestId, candidateLabels) => {
  183. const panel = document.querySelector<HTMLElement>(`[data-testid="${summaryPanelTestId}"]`);
  184. const buttons = Array.from(panel?.querySelectorAll<HTMLButtonElement>("button") ?? []);
  185. const button = buttons.find((item) =>
  186. candidateLabels.includes(item.getAttribute("aria-label") ?? ""),
  187. );
  188. button?.click();
  189. return Boolean(button);
  190. },
  191. TID_CHAT_SUMMARY_PANEL,
  192. labels,
  193. );
  194. if (!clicked) {
  195. throw new Error(`没有找到 status panel 按钮: ${labels.join(" / ")}`);
  196. }
  197. }