conversation-session-side-chat-escape.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {
  2. TID_SIDE_PANE_TOGGLE,
  3. TID_V4_COMPOSER_INPUT,
  4. TID_V4_COMPOSER_SEND,
  5. TID_V4_SESSION_PANE,
  6. TID_V4_STOP,
  7. testId,
  8. } from "@zcode/shared/test-ids";
  9. import { clearAppData } from "#e2e/helpers/desktop-app";
  10. import {
  11. getV4PaneSnapshot,
  12. prepareV4ConversationE2E,
  13. sendV4Prompt,
  14. waitForV4Pane,
  15. } from "#e2e/helpers/v4-conversation";
  16. const MAIN_PANE = "workspace-main";
  17. const SEED = "E2E_SSC_ESCAPE_SEED";
  18. const RUNNING = "E2E_SSC_ESCAPE_RUNNING";
  19. const paneSelector = (paneId: string) => `[data-testid="${testId(TID_V4_SESSION_PANE, paneId)}"]`;
  20. const inputSelector = (paneId: string) =>
  21. `${paneSelector(paneId)} [data-testid="${TID_V4_COMPOSER_INPUT}"]`;
  22. async function send(paneId: string, marker: string): Promise<void> {
  23. const input = $(inputSelector(paneId));
  24. await input.waitForDisplayed();
  25. await browser.waitUntil(async () => (await input.getAttribute("contenteditable")) === "true");
  26. await input.click();
  27. await browser.keys(marker);
  28. await browser.waitUntil(async () => (await input.getText()).includes(marker));
  29. await $(`${paneSelector(paneId)} [data-testid="${TID_V4_COMPOSER_SEND}"]`).click();
  30. await waitForV4Pane(
  31. (pane) => pane.canStop && currentTurnText(pane.timelineText, marker).includes("ssc-live-1"),
  32. `${marker} 未开始流式输出`,
  33. 30000,
  34. paneId,
  35. );
  36. }
  37. function currentTurnText(timeline: string, marker: string): string {
  38. const index = timeline.lastIndexOf(marker);
  39. return index < 0 ? "" : timeline.slice(index + marker.length);
  40. }
  41. async function focusInput(paneId: string): Promise<void> {
  42. const input = $(inputSelector(paneId));
  43. await input.click();
  44. await expect(input).toBeFocused();
  45. }
  46. async function focusOutsidePanes(): Promise<void> {
  47. // 面板开关的 Tooltip 会消费 Esc;移开指针并失焦,验证无浮层的页面空白焦点。
  48. await browser.action("pointer").move({ x: 5, y: 5 }).perform();
  49. await browser.execute(() => (document.activeElement as HTMLElement | null)?.blur());
  50. await browser.waitUntil(() =>
  51. browser.execute(
  52. () =>
  53. document.activeElement === document.body &&
  54. !document.querySelector('[data-slot="tooltip-content"]'),
  55. ),
  56. );
  57. }
  58. describe("Side chat Esc focus ownership", () => {
  59. let childPaneId = "";
  60. beforeEach(async function () {
  61. this.timeout(120000);
  62. await prepareV4ConversationE2E({ resetDraftBeforeProvider: true });
  63. await sendV4Prompt(SEED);
  64. await waitForV4Pane(
  65. (pane) => !pane.canStop && pane.timelineText.includes("ssc-seed-ready"),
  66. "父会话未完成初始化",
  67. );
  68. const previousTabIds = await browser.execute(() =>
  69. Array.from(
  70. document.querySelectorAll<HTMLElement>('[data-side-pane-tab-id^="selection-side-chat:"]'),
  71. (tab) => tab.dataset.sidePaneTabId,
  72. ),
  73. );
  74. await focusInput(MAIN_PANE);
  75. await browser.keys("/btw");
  76. const option = $('[data-option-id="app-slash:btw"]');
  77. await option.waitForDisplayed();
  78. await option.click();
  79. await browser.waitUntil(async () => {
  80. childPaneId = await browser.execute(
  81. (previous) =>
  82. Array.from(
  83. document.querySelectorAll<HTMLElement>(
  84. '[data-side-pane-tab-id^="selection-side-chat:"]',
  85. ),
  86. ).find((tab) => !previous.includes(tab.dataset.sidePaneTabId))?.dataset.sidePaneTabId ??
  87. "",
  88. previousTabIds,
  89. );
  90. return Boolean(childPaneId);
  91. });
  92. await waitForV4Pane((pane) => Boolean(pane.sessionId), "辅助对话未就绪", 30000, childPaneId);
  93. });
  94. afterEach(async () => {
  95. if (childPaneId && !(await $(inputSelector(childPaneId)).isDisplayed())) {
  96. await $(`[data-testid="${TID_SIDE_PANE_TOGGLE}"]`).click();
  97. await $(inputSelector(childPaneId)).waitForDisplayed();
  98. }
  99. for (const paneId of [MAIN_PANE, childPaneId].filter(Boolean)) {
  100. if ((await getV4PaneSnapshot(paneId)).canStop) {
  101. await $(`${paneSelector(paneId)} [data-testid="${TID_V4_STOP}"]`).click();
  102. await waitForV4Pane((pane) => !pane.canStop, "清理运行中的对话失败", 30000, paneId);
  103. }
  104. }
  105. });
  106. after(async () => {
  107. await browser.electron.restoreAllMocks();
  108. await clearAppData();
  109. });
  110. it("SSC36: 收起右侧面板后 Esc 默认停止主对话,隐藏辅助对话继续运行", async function () {
  111. this.timeout(90000);
  112. const childMarker = `${RUNNING}_SSC36_CHILD`;
  113. const mainMarker = `${RUNNING}_SSC36_MAIN`;
  114. await send(childPaneId, childMarker);
  115. await send(MAIN_PANE, mainMarker);
  116. const toggle = $(`[data-testid="${TID_SIDE_PANE_TOGGLE}"]`);
  117. await focusOutsidePanes();
  118. await browser.keys("Escape");
  119. const beforeOpen = currentTurnText(
  120. (await getV4PaneSnapshot(MAIN_PANE)).timelineText,
  121. mainMarker,
  122. );
  123. await waitForV4Pane(
  124. (pane) =>
  125. pane.canStop &&
  126. currentTurnText(pane.timelineText, mainMarker).length > beforeOpen.length + 20,
  127. "面板打开时对话外 Esc 误停主对话",
  128. );
  129. expect((await getV4PaneSnapshot(childPaneId)).canStop).toBe(true);
  130. await toggle.click();
  131. await $(inputSelector(childPaneId)).waitForDisplayed({ reverse: true });
  132. await focusOutsidePanes();
  133. await browser.keys("Escape");
  134. await waitForV4Pane((pane) => !pane.canStop, "右侧面板关闭后 Esc 未停止主对话", 15000);
  135. await toggle.click();
  136. await $(inputSelector(childPaneId)).waitForDisplayed();
  137. const beforeClosed = currentTurnText(
  138. (await getV4PaneSnapshot(childPaneId)).timelineText,
  139. childMarker,
  140. );
  141. await waitForV4Pane(
  142. (pane) =>
  143. pane.canStop &&
  144. currentTurnText(pane.timelineText, childMarker).length > beforeClosed.length + 20,
  145. "隐藏的辅助对话被误停",
  146. 15000,
  147. childPaneId,
  148. );
  149. });
  150. for (const scenario of [
  151. { id: "SSC34", stopChild: true },
  152. { id: "SSC35", stopChild: false },
  153. ]) {
  154. it(`${scenario.id}: Esc 只停止键盘焦点所属对话,重复按键不穿透`, async function () {
  155. this.timeout(90000);
  156. const target = scenario.stopChild ? childPaneId : MAIN_PANE;
  157. const other = scenario.stopChild ? MAIN_PANE : childPaneId;
  158. const otherMarker = `${RUNNING}_${scenario.id}_OTHER`;
  159. // 先启动非目标对话,确保旧 window 监听器会先抢到 Esc。
  160. await send(other, otherMarker);
  161. await send(target, `${RUNNING}_${scenario.id}_TARGET`);
  162. await focusInput(target);
  163. await browser.keys("Escape");
  164. await browser.waitUntil(
  165. async () =>
  166. !(await getV4PaneSnapshot(target)).canStop || !(await getV4PaneSnapshot(other)).canStop,
  167. { timeout: 15000, timeoutMsg: "Esc 未停止任何对话" },
  168. );
  169. expect((await getV4PaneSnapshot(target)).canStop).toBe(false);
  170. expect((await getV4PaneSnapshot(other)).canStop).toBe(true);
  171. // 焦点仍在已停止的对话;第二次 Esc 不能落到另一运行中的监听器。
  172. await expect($(inputSelector(target))).toBeFocused();
  173. await browser.keys("Escape");
  174. const before = currentTurnText((await getV4PaneSnapshot(other)).timelineText, otherMarker);
  175. await waitForV4Pane(
  176. (pane) =>
  177. pane.canStop &&
  178. currentTurnText(pane.timelineText, otherMarker).length > before.length + 20,
  179. "另一对话被误停或不再收到流式正文",
  180. 15000,
  181. other,
  182. );
  183. await focusInput(other);
  184. await browser.keys("Escape");
  185. await waitForV4Pane((pane) => !pane.canStop, "切换焦点后 Esc 未停止目标对话", 15000, other);
  186. expect((await getV4PaneSnapshot(target)).canStop).toBe(false);
  187. });
  188. }
  189. });