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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "../helpers/desktop-app.js";
  18. import {
  19. prepareV4ConversationE2E,
  20. selectV4TaskById,
  21. sendV4Prompt,
  22. waitForV4AssistantMessageContaining,
  23. waitForV4Pane,
  24. waitForV4UserMessageContaining,
  25. } from "../helpers/v4-conversation.js";
  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"] > span`;
  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. // 占位文字显示「工作中 N 秒」,与流式消息里的运行时长同值同格式。
  130. await browser.waitUntil(
  131. () =>
  132. browser.execute(
  133. (inputSelector, labelSelector) => {
  134. const placeholder =
  135. document.querySelector(inputSelector)?.getAttribute("aria-placeholder") ?? "";
  136. const label = [...document.querySelectorAll(labelSelector)].at(-1)?.textContent ?? "";
  137. return /^(Working for|工作中) /.test(placeholder) && placeholder === label.trim();
  138. },
  139. FLOATING_INPUT,
  140. STREAM_WORKING_LABEL,
  141. ),
  142. { timeout: 3_000, timeoutMsg: "运行中占位文字没有与流式消息的工作时长一致" },
  143. );
  144. // 回放把问答响应延迟 4s:用户消息出现后立刻最小化,请求在最小化期间到达。
  145. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_MINIMIZE}"]`).click();
  146. await browser.waitUntil(
  147. async () =>
  148. (await $(HOST).getAttribute("data-floating")) === "collapsed" &&
  149. (await $(`${HOST} ${ELICITATION_BODY}`).isDisplayed()),
  150. { timeout: 60_000, timeoutMsg: "问答请求没有让最小化浮窗恢复并显示卡片" },
  151. );
  152. expect(await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_BUBBLE}"]`).isExisting()).toBe(false);
  153. const option = await $(`${HOST} ${ELICITATION_BODY}`).$("button=Use default");
  154. if (await option.isExisting()) {
  155. await option.click();
  156. }
  157. await submitElicitation();
  158. await waitForV4AssistantMessageContaining("spfv-ask-done");
  159. await browser.waitUntil(async () => !(await $(`${HOST} ${ELICITATION_BODY}`).isExisting()), {
  160. timeout: 30_000,
  161. timeoutMsg: "提交回答后问答卡片没有消失",
  162. });
  163. await $(FLOATING_INPUT).waitForDisplayed({ timeout: 10_000 });
  164. // 回答结束弹出新气泡;点 × 只清除气泡,浮窗保持收起。
  165. await waitForAnswerBubble("spfv-ask-done");
  166. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_ANSWER_BUBBLE_CLOSE}"]`).click();
  167. await $(ANSWER_BUBBLE).waitForExist({ reverse: true, timeout: 5_000 });
  168. expect(await $(HOST).getAttribute("data-floating")).toBe("collapsed");
  169. // 回答完成后对话空闲:操作栏可见,但不再显示 loading。
  170. await $(FLOATING_INPUT).click();
  171. await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HEADER}"]`).waitForExist({ timeout: 5_000 });
  172. await $(RUNNING).waitForExist({ reverse: true, timeout: 30_000 });
  173. });
  174. });
  175. async function waitForHostTransitionsSettled() {
  176. await browser.waitUntil(
  177. () =>
  178. browser.execute(
  179. (selector) =>
  180. (document.querySelector(selector)?.getAnimations() ?? []).every(
  181. (animation) => animation.playState !== "running",
  182. ),
  183. HOST,
  184. ),
  185. { timeout: 5_000, timeoutMsg: "完整视图宿主的几何过渡没有结束" },
  186. );
  187. }
  188. async function waitForAnswerBubble(text: string) {
  189. await browser.waitUntil(
  190. async () =>
  191. (await $(ANSWER_BUBBLE).isDisplayed()) && (await $(ANSWER_BUBBLE).getText()).includes(text),
  192. { timeout: 15_000, timeoutMsg: `回答气泡没有显示「${text}」` },
  193. );
  194. }
  195. async function saveThemeScreenshots(name: string) {
  196. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  197. if (!artifactDir) return;
  198. for (const theme of ["zai-light", "zai-dark"]) {
  199. await browser.execute((value) => {
  200. const actions = (
  201. window as typeof window & { __testActions?: { setTheme(theme: string): void } }
  202. ).__testActions;
  203. actions?.setTheme(value);
  204. }, theme);
  205. await browser.pause(300);
  206. await browser.saveScreenshot(join(artifactDir, `${name}-${theme}.png`));
  207. }
  208. }
  209. async function realKeys(keys: string[]) {
  210. const isMac = await browser.execute(() => /Mac|iPhone|iPad/.test(navigator.userAgent));
  211. const mapped = keys.map((key) => (key === "Control" && isMac ? "Command" : key));
  212. await browser.keys(mapped as never);
  213. }
  214. async function waitForHostMode(mode: "normal" | "conversation" | "floating") {
  215. await browser.waitUntil(
  216. async () => (await $(HOST).getAttribute("data-side-pane-full-view-host")) === mode,
  217. { timeout: 10_000, timeoutMsg: `完整视图宿主没有进入 ${mode}` },
  218. );
  219. }
  220. async function typeIntoFloatingComposer(text: string) {
  221. const input = await $(FLOATING_INPUT);
  222. await input.waitForDisplayed({ timeout: 10_000 });
  223. try {
  224. await input.click();
  225. } catch (error) {
  226. const geometry = await browser.execute(
  227. (hostSelector, inputSelector) => {
  228. const rect = (selector: string) => {
  229. const r = document.querySelector(selector)?.getBoundingClientRect();
  230. return r
  231. ? [Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height)]
  232. : null;
  233. };
  234. const host = document.querySelector<HTMLElement>(hostSelector);
  235. const inputRect = document.querySelector(inputSelector)?.getBoundingClientRect();
  236. const hit = inputRect
  237. ? document.elementFromPoint(
  238. inputRect.left + inputRect.width / 2,
  239. inputRect.top + inputRect.height / 2,
  240. )
  241. : null;
  242. return {
  243. host: rect(hostSelector),
  244. style: host?.getAttribute("style"),
  245. attrs: [host?.dataset.floating, host?.dataset.headerVisible],
  246. input: rect(inputSelector),
  247. hit: `${hit?.tagName}.${(hit?.className?.toString() ?? "").slice(0, 80)}`,
  248. dialogs: document.querySelectorAll('[role="dialog"]').length,
  249. };
  250. },
  251. HOST,
  252. FLOATING_INPUT,
  253. );
  254. throw new Error(`浮窗输入框不可点击: ${JSON.stringify(geometry)}`, { cause: error });
  255. }
  256. await browser.keys(text);
  257. await browser.waitUntil(async () => (await input.getText()).includes(text.slice(0, 20)), {
  258. timeout: 5_000,
  259. timeoutMsg: "浮窗输入框没有写入文字",
  260. });
  261. }
  262. async function submitElicitation() {
  263. const clicked = await browser.execute((hostSelector) => {
  264. const footer = document.querySelector<HTMLElement>(
  265. `${hostSelector} [data-elicitation-dialog-footer="true"]`,
  266. );
  267. const button = Array.from(footer?.querySelectorAll<HTMLButtonElement>("button") ?? []).find(
  268. (candidate) => ["Submit", "提交"].includes((candidate.textContent ?? "").trim()),
  269. );
  270. button?.click();
  271. return Boolean(button);
  272. }, HOST);
  273. expect(clicked).toBe(true);
  274. }
  275. async function ensureSidePaneExpanded() {
  276. const isExpanded = () =>
  277. browser.execute((frameSelector) => {
  278. const panel = document.querySelector(frameSelector)?.parentElement;
  279. return Boolean(
  280. panel &&
  281. panel.getBoundingClientRect().width > 100 &&
  282. getComputedStyle(panel).opacity === "1",
  283. );
  284. }, SIDE_FRAME);
  285. if (await isExpanded()) return;
  286. const toggle = await $(
  287. `[data-testid="${TID_WORKSPACE_HEADER}"] [data-testid="${TID_SIDE_PANE_TOGGLE}"]`,
  288. );
  289. await toggle.waitForClickable({ timeout: 10_000 });
  290. await toggle.click();
  291. await browser.waitUntil(isExpanded, { timeout: 10_000, timeoutMsg: "Side Pane 未实际展开" });
  292. }
  293. async function ensureTerminalTab() {
  294. if ((await $$(TAB).length) > 0) return;
  295. const launcher = await $('[data-side-pane-open-tab-item="terminal"]');
  296. await launcher.waitForClickable({ timeout: 10_000 });
  297. await launcher.click();
  298. await browser.waitUntil(async () => (await $$(TAB).length) === 1, {
  299. timeout: 10_000,
  300. timeoutMsg: "Terminal tab 没有打开",
  301. });
  302. }