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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {
  2. TID_CHAT_MESSAGES,
  3. TID_CHAT_SUMMARY_PANEL,
  4. } from "@zcode/shared";
  5. import { clearAppData } from "../../../helpers/desktop-app.js";
  6. import {
  7. E2E_REPLY_TOKEN,
  8. prepareConversationE2E,
  9. sendPrompt,
  10. startNewTask,
  11. waitForAssistantMessageContaining,
  12. waitForChatState,
  13. waitForComposerText,
  14. waitForUserMessageContaining,
  15. } from "../../../helpers/conversation-session.js";
  16. const WIDE_VIEWPORT = { width: 1800, height: 1000 };
  17. const NARROW_VIEWPORT = { width: 1100, height: 900 };
  18. const DRAFT_WIDTH_CLASS = "max-w-2xl";
  19. const NORMAL_WIDTH_CLASS =
  20. "max-w-4xl @min-[1152px]:max-w-5xl @min-[1280px]:max-w-6xl @min-[1536px]:max-w-[1280px] @min-[1792px]:max-w-[1536px] @min-[2048px]:max-w-[1792px]";
  21. const INLINE_WIDTH_CLASS =
  22. "max-w-3xl @min-[1152px]:max-w-3xl @min-[1280px]:max-w-4xl @min-[1536px]:max-w-6xl @min-[1792px]:max-w-7xl @min-[2048px]:max-w-[1536px]";
  23. const INLINE_OFFSET_CLASS = "-translate-x-[184px]";
  24. describe("会话区 status panel inline layout E2E", () => {
  25. after(async () => {
  26. await browser.electron.restoreAllMocks();
  27. await clearAppData();
  28. });
  29. it("status panel 只有出现 inline 偏移时才收敛消息列和输入区宽度", async function () {
  30. this.timeout(180000);
  31. await setElectronWindowSize(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  32. await prepareConversationE2E();
  33. await setElectronWindowSize(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  34. const draftLayout = await waitForChatLayoutSnapshot(
  35. (snapshot) =>
  36. snapshot.panelState === null &&
  37. snapshot.dockClassName.includes(DRAFT_WIDTH_CLASS) &&
  38. !snapshot.dockClassName.includes(INLINE_OFFSET_CLASS),
  39. "新建草稿没有保持无 status panel 的窄 composer 布局",
  40. );
  41. expect(draftLayout.panelState).toBeNull();
  42. const runId = Date.now();
  43. const plainPrompt =
  44. `E2E_STATUS_PANEL_EMPTY_${runId}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  45. await sendPrompt(plainPrompt);
  46. await waitForComposerText("", "普通首发后输入框没有清空");
  47. await waitForUserMessageContaining(plainPrompt);
  48. await waitForAssistantMessageContaining(E2E_REPLY_TOKEN);
  49. await waitForChatState(
  50. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  51. "普通首发完成后没有回到 idle",
  52. 90000,
  53. );
  54. const noPanelLayout = await waitForChatLayoutSnapshot(
  55. (snapshot) =>
  56. snapshot.panelState === null &&
  57. snapshot.contentClassName?.includes(NORMAL_WIDTH_CLASS) === true &&
  58. snapshot.dockClassName.includes(NORMAL_WIDTH_CLASS) &&
  59. !snapshot.hasContentInlineOffset &&
  60. !snapshot.hasDockInlineOffset,
  61. "无 status 内容的历史 task 没有保持普通消息列宽度",
  62. );
  63. expect(noPanelLayout.panelState).toBeNull();
  64. await startNewTask();
  65. await setElectronWindowSize(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  66. const goalMarker = `E2E_STATUS_PANEL_GOAL_${runId}`;
  67. const goalPrompt =
  68. `/goal ${goalMarker}: Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  69. await sendPrompt(goalPrompt);
  70. await waitForComposerText("", "goal 首发后输入框没有清空");
  71. await waitForUserMessageContaining(goalMarker);
  72. await waitForChatState(
  73. (snapshot) =>
  74. snapshot.targetObjective?.includes(goalMarker) === true &&
  75. Boolean(snapshot.taskId),
  76. "goal 首发没有形成 status panel 可展示的 target",
  77. 60000,
  78. );
  79. await expectInlinePanelLayout("宽屏自动展开 status panel 后没有同步收敛聊天宽度");
  80. await setElectronWindowSize(NARROW_VIEWPORT.width, NARROW_VIEWPORT.height);
  81. await expectMiniPanelLayout("窄屏自动收起 status panel 后没有恢复普通聊天宽度");
  82. await setElectronWindowSize(WIDE_VIEWPORT.width, WIDE_VIEWPORT.height);
  83. await expectInlinePanelLayout("重新放大后自动展开 status panel 没有恢复 inline 布局");
  84. await clickSummaryPanelButton(["Collapse to capsule", "收起为胶囊"]);
  85. await expectMiniPanelLayout("手动收起 status panel 后没有移除 inline 偏移");
  86. await clickSummaryPanelButton(["Expand status", "展开状态"]);
  87. await expectInlinePanelLayout("手动展开 status panel 后没有恢复 inline 偏移");
  88. });
  89. });
  90. async function expectInlinePanelLayout(timeoutMsg: string) {
  91. const snapshot = await waitForChatLayoutSnapshot(
  92. (current) =>
  93. current.panelState === "expanded" &&
  94. current.contentClassName?.includes(INLINE_WIDTH_CLASS) === true &&
  95. current.dockClassName.includes(INLINE_WIDTH_CLASS) &&
  96. current.hasContentInlineOffset &&
  97. current.hasDockInlineOffset &&
  98. transitionContainsWidthAndTransform(current.contentTransitionProperty) &&
  99. transitionContainsWidthAndTransform(current.dockTransitionProperty),
  100. timeoutMsg,
  101. );
  102. expect(snapshot.panelState).toBe("expanded");
  103. }
  104. async function expectMiniPanelLayout(timeoutMsg: string) {
  105. const snapshot = await waitForChatLayoutSnapshot(
  106. (current) =>
  107. current.panelState === "collapsed" &&
  108. current.contentClassName?.includes(NORMAL_WIDTH_CLASS) === true &&
  109. current.dockClassName.includes(NORMAL_WIDTH_CLASS) &&
  110. !current.hasContentInlineOffset &&
  111. !current.hasDockInlineOffset &&
  112. transitionContainsWidthAndTransform(current.contentTransitionProperty) &&
  113. transitionContainsWidthAndTransform(current.dockTransitionProperty),
  114. timeoutMsg,
  115. );
  116. expect(snapshot.panelState).toBe("collapsed");
  117. }
  118. function transitionContainsWidthAndTransform(value: string) {
  119. return value.includes("max-width") && value.includes("transform");
  120. }
  121. async function waitForChatLayoutSnapshot(
  122. predicate: (snapshot: ChatLayoutSnapshot) => boolean,
  123. timeoutMsg: string,
  124. timeout = 30000,
  125. ) {
  126. let latest: ChatLayoutSnapshot | null = null;
  127. try {
  128. await browser.waitUntil(
  129. async () => {
  130. latest = await getChatLayoutSnapshot();
  131. return predicate(latest);
  132. },
  133. {
  134. timeout,
  135. timeoutMsg,
  136. },
  137. );
  138. } catch (error) {
  139. latest = await getChatLayoutSnapshot().catch(() => latest);
  140. throw new Error(`${timeoutMsg}; latest=${JSON.stringify(latest)}`, {
  141. cause: error,
  142. });
  143. }
  144. return getChatLayoutSnapshot();
  145. }
  146. interface ChatLayoutSnapshot {
  147. contentClassName: string | null;
  148. contentTransitionProperty: string;
  149. dockClassName: string;
  150. dockTransitionProperty: string;
  151. hasContentInlineOffset: boolean;
  152. hasDockInlineOffset: boolean;
  153. panelState: string | null;
  154. }
  155. function getChatLayoutSnapshot(): Promise<ChatLayoutSnapshot> {
  156. return browser.execute(
  157. (messagesTestId, summaryPanelTestId, inlineOffsetClass) => {
  158. const panel = document.querySelector<HTMLElement>(
  159. `[data-testid="${summaryPanelTestId}"]`,
  160. );
  161. const dock = document.querySelector<HTMLElement>(
  162. '[data-chat-composer-region="true"] > div',
  163. );
  164. const firstTurnShell = document.querySelector<HTMLElement>(
  165. `[data-testid="${messagesTestId}"] [data-chat-turn-group-shell="true"]`,
  166. );
  167. const content = firstTurnShell?.parentElement ?? null;
  168. const contentStyle = content ? window.getComputedStyle(content) : null;
  169. const dockStyle = dock ? window.getComputedStyle(dock) : null;
  170. return {
  171. contentClassName: content?.className ?? null,
  172. contentTransitionProperty: contentStyle?.transitionProperty ?? "",
  173. dockClassName: dock?.className ?? "",
  174. dockTransitionProperty: dockStyle?.transitionProperty ?? "",
  175. hasContentInlineOffset:
  176. content?.className.includes(inlineOffsetClass) ?? false,
  177. hasDockInlineOffset:
  178. dock?.className.includes(inlineOffsetClass) ?? false,
  179. panelState: panel?.dataset.state ?? null,
  180. };
  181. },
  182. TID_CHAT_MESSAGES,
  183. TID_CHAT_SUMMARY_PANEL,
  184. INLINE_OFFSET_CLASS,
  185. );
  186. }
  187. async function clickSummaryPanelButton(labels: readonly string[]) {
  188. const clicked = await browser.execute(
  189. (summaryPanelTestId, candidateLabels) => {
  190. const panel = document.querySelector<HTMLElement>(
  191. `[data-testid="${summaryPanelTestId}"]`,
  192. );
  193. const buttons = Array.from(
  194. panel?.querySelectorAll<HTMLButtonElement>("button") ?? [],
  195. );
  196. const button = buttons.find((item) =>
  197. candidateLabels.includes(item.getAttribute("aria-label") ?? ""),
  198. );
  199. button?.click();
  200. return Boolean(button);
  201. },
  202. TID_CHAT_SUMMARY_PANEL,
  203. labels,
  204. );
  205. if (!clicked) {
  206. throw new Error(`没有找到 status panel 按钮: ${labels.join(" / ")}`);
  207. }
  208. }
  209. async function setElectronWindowSize(width: number, height: number) {
  210. const resized = await browser.electron.execute(
  211. (electron, nextWidth, nextHeight) => {
  212. const window = electron.BrowserWindow.getAllWindows().find(
  213. (candidate) => !candidate.isDestroyed() && candidate.isVisible(),
  214. );
  215. if (!window) {
  216. return false;
  217. }
  218. const bounds = window.getBounds();
  219. window.setBounds({
  220. ...bounds,
  221. height: nextHeight,
  222. width: nextWidth,
  223. });
  224. window.focus();
  225. return true;
  226. },
  227. width,
  228. height,
  229. );
  230. if (!resized) {
  231. throw new Error("没有找到可调整尺寸的 Electron 窗口");
  232. }
  233. await browser.waitUntil(
  234. async () =>
  235. browser.execute(
  236. (expectedWidth, expectedHeight) =>
  237. Math.abs(window.innerWidth - expectedWidth) < 80 &&
  238. Math.abs(window.innerHeight - expectedHeight) < 120,
  239. width,
  240. height,
  241. ),
  242. {
  243. timeout: 10000,
  244. timeoutMsg: `Electron 窗口尺寸没有收敛到 ${width}x${height}`,
  245. },
  246. );
  247. }