conversation-session-side-pane-full-view.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import { join } from "node:path";
  2. import {
  3. TID_SIDE_PANE_FULL_VIEW_ANSWER_BUBBLE,
  4. TID_SIDE_PANE_FULL_VIEW_ANSWER_BUBBLE_CLOSE,
  5. TID_SIDE_PANE_FULL_VIEW_BUBBLE,
  6. TID_SIDE_PANE_FULL_VIEW_EXPAND,
  7. TID_SIDE_PANE_FULL_VIEW_HEADER,
  8. TID_SIDE_PANE_FULL_VIEW_HOST,
  9. TID_SIDE_PANE_FULL_VIEW_MINIMIZE,
  10. TID_SIDE_PANE_FULL_VIEW_RUNNING,
  11. TID_SIDE_PANE_FULL_VIEW_TOGGLE,
  12. TID_SIDE_PANE_TOGGLE,
  13. TID_V4_COMPOSER_INPUT,
  14. TID_V4_COMPOSER_SEND,
  15. TID_WORKSPACE_HEADER,
  16. } from "@zcode/shared/test-ids";
  17. import { clearAppData } from "#e2e/helpers/desktop-app";
  18. import {
  19. prepareV4ConversationE2E,
  20. selectV4TaskById,
  21. sendV4Prompt,
  22. waitForV4AssistantMessageContaining,
  23. waitForV4Pane,
  24. waitForV4UserMessageContaining,
  25. } from "#e2e/helpers/v4-conversation";
  26. // spec:docs/ui/side-pane-full-view.md;用例:docs/testing/side-pane-tabs-e2e-case-catalog.md(SPFV-007/008)
  27. const SIDE_FRAME = '[data-workspace-side-frame="true"]';
  28. const HOST = `[data-testid="${TID_SIDE_PANE_FULL_VIEW_HOST}"]`;
  29. const FLOATING_INPUT = `${HOST}[data-side-pane-full-view-host="floating"] [data-testid="${TID_V4_COMPOSER_INPUT}"]`;
  30. const TAB = "[data-side-pane-tab-id]";
  31. const ELICITATION_BODY = '[data-elicitation-dialog-body="true"]';
  32. const RUNNING = `[data-testid="${TID_SIDE_PANE_FULL_VIEW_RUNNING}"]`;
  33. const ANSWER_BUBBLE = `${HOST} [data-testid="${TID_SIDE_PANE_FULL_VIEW_ANSWER_BUBBLE}"]`;
  34. const ANSWER_BUBBLE_OPEN = `${ANSWER_BUBBLE} > button:first-child`;
  35. const STREAM_WORKING_LABEL = `${HOST} [data-testid^="chat-assistant-history-trigger"]`;
  36. describe("Side Pane 完整视图浮窗(SPFV provider)", () => {
  37. after(async () => {
  38. await browser.electron.restoreAllMocks();
  39. await clearAppData();
  40. });
  41. it("SPFV-E2E-007 浮窗发送消息,并按 session 记忆完整视图", async function () {
  42. this.timeout(180_000);
  43. await prepareV4ConversationE2E();
  44. const runId = Date.now();
  45. await sendV4Prompt(`E2E_SPFV_FIRST_${runId}: reply briefly.`);
  46. await waitForV4AssistantMessageContaining("spfv-first-ok");
  47. const sessionA = (
  48. await waitForV4Pane(
  49. (snapshot) => snapshot.sessionId !== null && snapshot.sessionId !== "draft",
  50. "首条消息后没有绑定真实会话",
  51. )
  52. ).sessionId!;
  53. await ensureSidePaneExpanded();
  54. await ensureTerminalTab();
  55. await $(`${SIDE_FRAME} [data-testid="${TID_SIDE_PANE_FULL_VIEW_TOGGLE}"]`).click();
  56. await waitForHostMode("floating");
  57. const floatingPrompt = `E2E_SPFV_FLOATING_SEND_${runId}: reply briefly.`;
  58. await typeIntoFloatingComposer(floatingPrompt);
  59. await browser.keys("Enter");
  60. await waitForV4UserMessageContaining(`E2E_SPFV_FLOATING_SEND_${runId}`);
  61. await waitForV4AssistantMessageContaining("spfv-floating-ok");
  62. // 回答结束:浮窗上方弹出气泡,只有回答正文,没有图标与标题。
  63. await waitForAnswerBubble("spfv-floating-ok");
  64. expect(await $(`${ANSWER_BUBBLE_OPEN} svg`).isExisting()).toBe(false);
  65. expect((await $(ANSWER_BUBBLE_OPEN).getText()).trim()).toBe("spfv-floating-ok");
  66. // 默认吸附右下:气泡在浮窗上方。
  67. const bubbleBottom = await browser.execute(
  68. (selector) => document.querySelector(selector)?.getBoundingClientRect().bottom ?? -1,
  69. ANSWER_BUBBLE,
  70. );
  71. const hostTop = await browser.execute(
  72. (selector) => document.querySelector(selector)?.getBoundingClientRect().top ?? -1,
  73. HOST,
  74. );
  75. expect(bubbleBottom).toBeLessThanOrEqual(hostTop);
  76. await saveThemeScreenshots("spfv-answer-bubble");
  77. // 完整视图隐藏左侧任务列表;切走会话走命令面板(⌘K → 新建任务),不依赖原生菜单 accelerator。
  78. await realKeys(["Control", "k"]);
  79. await $('[role="dialog"] [cmdk-input]').waitForDisplayed({ timeout: 10_000 });
  80. await browser.keys("New task");
  81. await browser.keys("Enter");
  82. await waitForV4Pane((snapshot) => snapshot.sessionId === "draft", "命令面板新建任务失败");
  83. await waitForHostMode("normal");
  84. expect(await $(`[data-testid="${TID_WORKSPACE_HEADER}"]`).isExisting()).toBe(true);
  85. await selectV4TaskById(sessionA);
  86. await waitForHostMode("floating");
  87. // 气泡只有 × 或点击才消失:切走再切回仍在;点气泡展开消息流并清除气泡。
  88. await waitForAnswerBubble("spfv-floating-ok");
  89. await $(ANSWER_BUBBLE_OPEN).click();
  90. await browser.waitUntil(
  91. async () =>
  92. (await $(HOST).getAttribute("data-floating")) === "expanded" &&
  93. !(await $(ANSWER_BUBBLE).isExisting()),
  94. { timeout: 5_000, timeoutMsg: "点击气泡后没有展开消息流并清除气泡" },
  95. );
  96. // 展开有高度过渡,按钮仍在移动时点击会落到消息正文上;等宿主过渡结束再点。
  97. await waitForHostTransitionsSettled();
  98. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_EXPAND}"]`).click();
  99. await browser.waitUntil(
  100. async () => (await $(HOST).getAttribute("data-floating")) === "collapsed",
  101. {
  102. timeout: 5_000,
  103. timeoutMsg: "收起消息流失败",
  104. },
  105. );
  106. expect(await $(ANSWER_BUBBLE).isExisting()).toBe(false);
  107. });
  108. it("SPFV-E2E-008 最小化时出现 AskUserQuestion 会自动恢复浮窗并显示卡片", async function () {
  109. this.timeout(180_000);
  110. await waitForHostMode("floating");
  111. const runId = Date.now();
  112. await typeIntoFloatingComposer(`E2E_SPFV_ASK_${runId}: ask one question before continuing.`);
  113. await browser.waitUntil(
  114. () =>
  115. browser.execute(
  116. (hostSelector, sendTestId) =>
  117. document.querySelector<HTMLButtonElement>(
  118. `${hostSelector} [data-testid="${sendTestId}"]`,
  119. )?.disabled === false,
  120. HOST,
  121. TID_V4_COMPOSER_SEND,
  122. ),
  123. { timeout: 30_000, timeoutMsg: "切回会话后浮窗发送按钮没有可用" },
  124. );
  125. await browser.keys("Enter");
  126. await waitForV4UserMessageContaining(`E2E_SPFV_ASK_${runId}`);
  127. // 运行中(问答请求还没到):焦点仍在浮窗,操作栏「⌃ / ⌄」右侧显示 loading。
  128. await $(RUNNING).waitForDisplayed({ timeout: 10_000 });
  129. // 浮窗占位保留整数时长;运行时顶部不再渲染折叠入口。
  130. await browser.waitUntil(
  131. () =>
  132. browser.execute(
  133. (inputSelector, labelSelector) => {
  134. const placeholder =
  135. document.querySelector(inputSelector)?.getAttribute("aria-placeholder") ?? "";
  136. return (
  137. /^(?:Working for [0-9]+[smhd]|工作中 [0-9]+ [秒分时天])/.test(placeholder) &&
  138. !Array.from(document.querySelectorAll(labelSelector)).some((element) =>
  139. /^(?:Working for|工作中)/.test(element.textContent?.trim() ?? ""),
  140. )
  141. );
  142. },
  143. FLOATING_INPUT,
  144. STREAM_WORKING_LABEL,
  145. ),
  146. { timeout: 3_000, timeoutMsg: "运行中浮窗未显示整数时长,或顶部仍存在折叠入口" },
  147. );
  148. // 回放把问答响应延迟 4s:用户消息出现后立刻最小化,请求在最小化期间到达。
  149. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_MINIMIZE}"]`).click();
  150. await browser.waitUntil(
  151. async () =>
  152. (await $(HOST).getAttribute("data-floating")) === "collapsed" &&
  153. (await $(`${HOST} ${ELICITATION_BODY}`).isDisplayed()),
  154. { timeout: 60_000, timeoutMsg: "问答请求没有让最小化浮窗恢复并显示卡片" },
  155. );
  156. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`).isExisting()).toBe(false);
  157. const option = await $(`${HOST} ${ELICITATION_BODY}`).$("button=Use default");
  158. if (await option.isExisting()) {
  159. await option.click();
  160. }
  161. await submitElicitation();
  162. await waitForV4AssistantMessageContaining("spfv-ask-done");
  163. await browser.waitUntil(async () => !(await $(`${HOST} ${ELICITATION_BODY}`).isExisting()), {
  164. timeout: 30_000,
  165. timeoutMsg: "提交回答后问答卡片没有消失",
  166. });
  167. await $(FLOATING_INPUT).waitForDisplayed({ timeout: 10_000 });
  168. // 回答结束弹出新气泡;点 × 只清除气泡,浮窗保持收起。
  169. await waitForAnswerBubble("spfv-ask-done");
  170. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_ANSWER_BUBBLE_CLOSE}"]`).click();
  171. await $(ANSWER_BUBBLE).waitForExist({ reverse: true, timeout: 5_000 });
  172. expect(await $(HOST).getAttribute("data-floating")).toBe("collapsed");
  173. // 回答完成后对话空闲:操作栏可见,但不再显示 loading。
  174. await $(FLOATING_INPUT).click();
  175. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"]`).waitForExist({ timeout: 5_000 });
  176. await $(RUNNING).waitForExist({ reverse: true, timeout: 30_000 });
  177. });
  178. });
  179. async function waitForHostTransitionsSettled() {
  180. await browser.waitUntil(
  181. () =>
  182. browser.execute(
  183. (selector) =>
  184. (document.querySelector(selector)?.getAnimations() ?? []).every(
  185. (animation) => animation.playState !== "running",
  186. ),
  187. HOST,
  188. ),
  189. { timeout: 5_000, timeoutMsg: "完整视图宿主的几何过渡没有结束" },
  190. );
  191. }
  192. async function waitForAnswerBubble(text: string) {
  193. await browser.waitUntil(
  194. async () =>
  195. (await $(ANSWER_BUBBLE).isDisplayed()) && (await $(ANSWER_BUBBLE).getText()).includes(text),
  196. { timeout: 15_000, timeoutMsg: `回答气泡没有显示「${text}」` },
  197. );
  198. }
  199. async function saveThemeScreenshots(name: string) {
  200. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  201. if (!artifactDir) return;
  202. for (const theme of ["zai-light", "zai-dark"]) {
  203. await browser.execute((value) => {
  204. const actions = (
  205. window as typeof window & { __testActions?: { setTheme(theme: string): void } }
  206. ).__testActions;
  207. actions?.setTheme(value);
  208. }, theme);
  209. await browser.pause(300);
  210. await browser.saveScreenshot(join(artifactDir, `${name}-${theme}.png`));
  211. }
  212. }
  213. async function realKeys(keys: string[]) {
  214. const isMac = await browser.execute(() => /Mac|iPhone|iPad/.test(navigator.userAgent));
  215. const mapped = keys.map((key) => (key === "Control" && isMac ? "Command" : key));
  216. await browser.keys(mapped as never);
  217. }
  218. async function waitForHostMode(mode: "normal" | "conversation" | "floating") {
  219. await browser.waitUntil(
  220. async () => (await $(HOST).getAttribute("data-side-pane-full-view-host")) === mode,
  221. { timeout: 10_000, timeoutMsg: `完整视图宿主没有进入 ${mode}` },
  222. );
  223. }
  224. async function typeIntoFloatingComposer(text: string) {
  225. const input = await $(FLOATING_INPUT);
  226. await input.waitForDisplayed({ timeout: 10_000 });
  227. try {
  228. await input.click();
  229. } catch (error) {
  230. const geometry = await browser.execute(
  231. (hostSelector, inputSelector) => {
  232. const rect = (selector: string) => {
  233. const r = document.querySelector(selector)?.getBoundingClientRect();
  234. return r
  235. ? [Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height)]
  236. : null;
  237. };
  238. const host = document.querySelector<HTMLElement>(hostSelector);
  239. const inputRect = document.querySelector(inputSelector)?.getBoundingClientRect();
  240. const hit = inputRect
  241. ? document.elementFromPoint(
  242. inputRect.left + inputRect.width / 2,
  243. inputRect.top + inputRect.height / 2,
  244. )
  245. : null;
  246. return {
  247. host: rect(hostSelector),
  248. style: host?.getAttribute("style"),
  249. attrs: [host?.dataset.floating, host?.dataset.headerVisible],
  250. input: rect(inputSelector),
  251. hit: `${hit?.tagName}.${(hit?.className?.toString() ?? "").slice(0, 80)}`,
  252. dialogs: document.querySelectorAll('[role="dialog"]').length,
  253. };
  254. },
  255. HOST,
  256. FLOATING_INPUT,
  257. );
  258. throw new Error(`浮窗输入框不可点击: ${JSON.stringify(geometry)}`, { cause: error });
  259. }
  260. await browser.keys(text);
  261. await browser.waitUntil(async () => (await input.getText()).includes(text.slice(0, 20)), {
  262. timeout: 5_000,
  263. timeoutMsg: "浮窗输入框没有写入文字",
  264. });
  265. }
  266. async function submitElicitation() {
  267. const clicked = await browser.execute((hostSelector) => {
  268. const footer = document.querySelector<HTMLElement>(
  269. `${hostSelector} [data-elicitation-dialog-footer="true"]`,
  270. );
  271. const button = Array.from(footer?.querySelectorAll<HTMLButtonElement>("button") ?? []).find(
  272. (candidate) => ["Submit", "提交"].includes((candidate.textContent ?? "").trim()),
  273. );
  274. button?.click();
  275. return Boolean(button);
  276. }, HOST);
  277. expect(clicked).toBe(true);
  278. }
  279. async function ensureSidePaneExpanded() {
  280. const isExpanded = () =>
  281. browser.execute((frameSelector) => {
  282. const panel = document.querySelector(frameSelector)?.parentElement;
  283. return Boolean(
  284. panel &&
  285. panel.getBoundingClientRect().width > 100 &&
  286. getComputedStyle(panel).opacity === "1",
  287. );
  288. }, SIDE_FRAME);
  289. if (await isExpanded()) return;
  290. const toggle = await $(
  291. `[data-testid="${TID_WORKSPACE_HEADER}"] [data-testid="${TID_SIDE_PANE_TOGGLE}"]`,
  292. );
  293. await toggle.waitForClickable({ timeout: 10_000 });
  294. await toggle.click();
  295. await browser.waitUntil(isExpanded, { timeout: 10_000, timeoutMsg: "Side Pane 未实际展开" });
  296. }
  297. async function ensureTerminalTab() {
  298. if ((await $$(TAB).length) > 0) return;
  299. const launcher = await $('[data-side-pane-open-tab-item="terminal"]');
  300. await launcher.waitForClickable({ timeout: 10_000 });
  301. await launcher.click();
  302. await browser.waitUntil(async () => (await $$(TAB).length) === 1, {
  303. timeout: 10_000,
  304. timeoutMsg: "Terminal tab 没有打开",
  305. });
  306. }