conversation-session-subagent-side-tabs.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { openStatusCapsule } from "#e2e/helpers/status-capsule";
  2. import { TID_V4_PANE_SHELL, TID_V4_SUBAGENT_OPEN_SIDE_PANE } from "@zcode/shared/test-ids";
  3. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  4. import { ensureReadonlyToolFixtureFile } from "#e2e/helpers/conversation-session-readonly-tool";
  5. import { clearAppData } from "#e2e/helpers/desktop-app";
  6. import {
  7. prepareV4ConversationE2E,
  8. sendV4Prompt,
  9. waitForV4AssistantMessageContaining,
  10. waitForV4ComposerText,
  11. } from "#e2e/helpers/v4-conversation";
  12. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  13. const PARENT_MARKER = "E2E_SUBAGENT_SIDE_TABS_PARENT";
  14. const PARENT_DONE = "E2E_SUBAGENT_SIDE_TABS_PARENT_DONE";
  15. const CHILD_COUNT = 6;
  16. const BACKGROUND_PARENT_MARKER = "E2E_RUNNING_SUBAGENT_SIDE_TAB_PARENT";
  17. const BACKGROUND_LAUNCHED = "E2E_RUNNING_SUBAGENT_SIDE_TAB_LAUNCHED";
  18. const BACKGROUND_PARENT_DONE = "E2E_RUNNING_SUBAGENT_SIDE_TAB_PARENT_DONE";
  19. describe("子智能体右侧 tabs manual review", () => {
  20. after(async () => {
  21. await browser.electron.restoreAllMocks();
  22. await clearAppData();
  23. });
  24. it("SAT12: Running subagent 摘要行复用 child 右侧 tab", async function () {
  25. this.timeout(240000);
  26. await prepareV4ConversationE2E();
  27. await ensureToolCrossProductFullAccessMode();
  28. await ensureReadonlyToolFixtureFile();
  29. const prompt = [
  30. `${BACKGROUND_PARENT_MARKER}: launch exactly one Agent with run_in_background true.`,
  31. `The child must read the readonly fixture and reply with exactly E2E_SIDE_TAB_BACKGROUND_CHILD_DONE.`,
  32. `After the background launch tool returns, reply with exactly ${BACKGROUND_LAUNCHED} without waiting.`,
  33. `After its task notification arrives, reply with exactly ${BACKGROUND_PARENT_DONE}.`,
  34. ].join(" ");
  35. await sendV4Prompt(prompt);
  36. await waitForV4ComposerText("", "Running subagent side-tab 首发后输入框没有清空");
  37. await waitForV4AssistantMessageContaining(`${BACKGROUND_LAUNCHED}`);
  38. await waitForRunningSubagentTrigger(90000);
  39. await clickRunningSubagentTrigger();
  40. await waitForVisibleSubagentTabCount(1, 30000);
  41. await clickRunningSubagentTrigger();
  42. expect(await countVisibleSubagentTabs()).toBe(1);
  43. // 先观察运行中 tab 的去重,再释放上游流,避免依赖固定 sleep 的抢时序。
  44. await releaseControlledReplay("side-tab-child-background");
  45. await waitForV4AssistantMessageContaining(BACKGROUND_PARENT_DONE);
  46. });
  47. it("SAT01-SAT03: 6 个 running child 使用右侧 tabs 且打开后持续更新", async function () {
  48. this.timeout(240000);
  49. await prepareV4ConversationE2E();
  50. await ensureToolCrossProductFullAccessMode();
  51. await ensureReadonlyToolFixtureFile();
  52. const prompt = [
  53. `${PARENT_MARKER}: In one assistant turn launch exactly ${CHILD_COUNT} foreground Agent tool calls in parallel.`,
  54. `Use subagent_type "general-purpose" for every child.`,
  55. `Give child N this exact instruction: E2E_SIDE_TAB_CHILD_N: read the readonly fixture, then reply with E2E_SIDE_TAB_CHILD_N_DONE.`,
  56. `Do not finish the parent response until every child completes; then reply with exactly ${PARENT_DONE}.`,
  57. ].join(" ");
  58. await sendV4Prompt(prompt);
  59. await waitForV4ComposerText("", "subagent side tabs 首发后输入框没有清空");
  60. await waitForSubagentSummaryActions(CHILD_COUNT, 90000);
  61. await expectSubagentSummariesToOpenSidePaneDirectly();
  62. await openAllSubagentTabs();
  63. // 流式增量更新同一 row;隐藏 tab 的虚拟列表也可能卸载正文,不能要求所有 DOM 同时可见。
  64. // 先验证保活 pane 的投影,再逐个激活读取正文,覆盖 SAT03 的“切回直接显示最新内容”。
  65. await browser.waitUntil(
  66. async () => {
  67. const children = await readChildTimelineStates();
  68. return children.length === CHILD_COUNT && children.every((child) => child.seq > 0);
  69. },
  70. { timeout: 30000, timeoutMsg: "六个 child tabs 没有保留初始投影" },
  71. );
  72. const initialChildren: Awaited<ReturnType<typeof readChildTimelineStates>> = [];
  73. for (const child of await readChildTimelineStates()) {
  74. initialChildren.push(
  75. await readVisibleChildTimelineState(child, /^E2E_SIDE_TAB_CHILD_\d+_$/u),
  76. );
  77. }
  78. expect(new Set(initialChildren.map((child) => child.sessionId)).size).toBe(CHILD_COUNT);
  79. expect(
  80. initialChildren
  81. .map((child) => child.assistantText.match(/E2E_SIDE_TAB_CHILD_\d+_/u)?.[0])
  82. .sort(),
  83. ).toEqual(
  84. Array.from({ length: CHILD_COUNT }, (_, index) => `E2E_SIDE_TAB_CHILD_${index + 1}_`).sort(),
  85. );
  86. await waitForVisibleSubagentTabCount(CHILD_COUNT, 30000);
  87. expect(await countWorkbenchPanes()).toBe(1);
  88. await clickFirstSubagentSummaryAction();
  89. expect(await countVisibleSubagentTabs()).toBe(CHILD_COUNT);
  90. for (let index = 1; index <= CHILD_COUNT; index++) {
  91. await releaseControlledReplay(`side-tab-child-${index}`);
  92. }
  93. await browser.waitUntil(
  94. async () => {
  95. const current = await readChildTimelineStates();
  96. return (
  97. current.length === CHILD_COUNT &&
  98. current.every((child) => {
  99. const initial = initialChildren.find((item) => item.sessionId === child.sessionId);
  100. return Boolean(initial && child.seq > initial.seq);
  101. })
  102. );
  103. },
  104. {
  105. timeout: 120000,
  106. timeoutMsg: "已打开的 child tabs 没有持续更新 projection sequence",
  107. },
  108. );
  109. for (const initial of initialChildren) {
  110. const current = await readVisibleChildTimelineState(
  111. initial,
  112. new RegExp(`^${initial.assistantText}DONE$`, "u"),
  113. );
  114. expect(current.seq).toBeGreaterThan(initial.seq);
  115. }
  116. expect((await readChildTimelineStates()).map((child) => child.sessionId).sort()).toEqual(
  117. initialChildren.map((child) => child.sessionId).sort(),
  118. );
  119. await waitForV4AssistantMessageContaining(`${PARENT_DONE}`);
  120. });
  121. });
  122. async function waitForSubagentSummaryActions(expected: number, timeout: number): Promise<void> {
  123. await browser.waitUntil(
  124. async () =>
  125. browser.execute(
  126. (prefix, count) => document.querySelectorAll(`[data-testid^="${prefix}-"]`).length >= count,
  127. TID_V4_SUBAGENT_OPEN_SIDE_PANE,
  128. expected,
  129. ),
  130. {
  131. timeout,
  132. timeoutMsg: `没有等到 ${expected} 个 subagent 摘要直开入口`,
  133. },
  134. );
  135. }
  136. async function waitForRunningSubagentTrigger(timeout: number): Promise<void> {
  137. await openStatusCapsule("activity", timeout);
  138. await $(
  139. '[data-status-capsule-content="activity"] [data-running-subagent-session-trigger="true"]',
  140. ).waitForDisplayed({ timeout });
  141. }
  142. async function clickRunningSubagentTrigger(): Promise<void> {
  143. await browser.execute(() => {
  144. document
  145. .querySelector<HTMLButtonElement>('[data-running-subagent-session-trigger="true"]')
  146. ?.click();
  147. });
  148. }
  149. async function expectSubagentSummariesToOpenSidePaneDirectly(): Promise<void> {
  150. const summaries = await browser.execute(
  151. (prefix) =>
  152. Array.from(document.querySelectorAll<HTMLElement>(`[data-testid^="${prefix}-"]`)).map(
  153. (summary) => ({
  154. ariaExpanded: summary.getAttribute("aria-expanded"),
  155. hasChevron: Boolean(summary.querySelector(".lucide-chevron-right")),
  156. hasInlineDetails: Boolean(
  157. summary.parentElement?.querySelector('[data-slot="collapsible-content"]'),
  158. ),
  159. }),
  160. ),
  161. TID_V4_SUBAGENT_OPEN_SIDE_PANE,
  162. );
  163. expect(summaries).toHaveLength(CHILD_COUNT);
  164. expect(summaries.every((summary) => summary.ariaExpanded === null)).toBe(true);
  165. expect(summaries.every((summary) => !summary.hasChevron)).toBe(true);
  166. expect(summaries.every((summary) => !summary.hasInlineDetails)).toBe(true);
  167. }
  168. async function openAllSubagentTabs(): Promise<void> {
  169. await browser.execute(
  170. (prefix, expected) => {
  171. const summaries = Array.from(
  172. document.querySelectorAll<HTMLElement>(`[data-testid^="${prefix}-"]`),
  173. ).slice(0, expected);
  174. for (const summary of summaries) summary.click();
  175. },
  176. TID_V4_SUBAGENT_OPEN_SIDE_PANE,
  177. CHILD_COUNT,
  178. );
  179. await waitForVisibleSubagentTabCount(CHILD_COUNT, 30000);
  180. }
  181. async function clickFirstSubagentSummaryAction(): Promise<void> {
  182. await browser.execute((prefix) => {
  183. document.querySelector<HTMLElement>(`[data-testid^="${prefix}-"]`)?.click();
  184. }, TID_V4_SUBAGENT_OPEN_SIDE_PANE);
  185. }
  186. async function waitForVisibleSubagentTabCount(expected: number, timeout: number): Promise<void> {
  187. await browser.waitUntil(async () => (await countVisibleSubagentTabs()) === expected, {
  188. timeout,
  189. timeoutMsg: `右侧没有出现 ${expected} 个 subagent tabs`,
  190. });
  191. }
  192. async function countVisibleSubagentTabs(): Promise<number> {
  193. return browser.execute(
  194. () => document.querySelectorAll('[data-side-pane-tab-id^="subagent-session:"]').length,
  195. );
  196. }
  197. async function countWorkbenchPanes(): Promise<number> {
  198. return browser.execute(
  199. (testId) => document.querySelectorAll(`[data-testid^="${testId}"]`).length,
  200. TID_V4_PANE_SHELL,
  201. );
  202. }
  203. async function readChildTimelineStates() {
  204. return browser.execute(() => {
  205. // 切换任务后旧 pane 仍可能保活;只统计当前 tabs 对应的 child,不能混入前一个 case。
  206. const tabs = Array.from(
  207. document.querySelectorAll<HTMLElement>('[data-side-pane-tab-id^="subagent-session:"]'),
  208. );
  209. return tabs.map((tab) => {
  210. const pane = document.querySelector<HTMLElement>(
  211. `[data-testid=${JSON.stringify(`v4-session-pane-${tab.dataset.sidePaneTabId}`)}]`,
  212. );
  213. const timeline = pane?.querySelector<HTMLElement>('[data-testid="v4-timeline"]');
  214. return {
  215. tabId: tab.dataset.sidePaneTabId ?? null,
  216. sessionId: pane?.dataset.sessionId ?? null,
  217. seq: Number(pane?.dataset.projectionSeq ?? 0),
  218. // 不读 user prompt,避免指令中已有 DONE marker 导致完成态假阳性。
  219. assistantText: Array.from(timeline?.querySelectorAll<HTMLElement>("[data-row-id]") ?? [])
  220. .filter((row) => row.classList.contains("group/assistant-row"))
  221. .map((row) => row.textContent ?? "")
  222. .join("\n"),
  223. };
  224. });
  225. });
  226. }
  227. async function readVisibleChildTimelineState(
  228. child: Awaited<ReturnType<typeof readChildTimelineStates>>[number],
  229. expectedText: RegExp,
  230. ) {
  231. const { sessionId, tabId } = child;
  232. if (!sessionId || !tabId) throw new Error("child tab 没有 session identity");
  233. await browser.execute((id) => {
  234. // tab ID 还包含 root/Host 作用域,必须读取真实 DOM 身份,不能只用 child ID 拼接。
  235. const tab = document.querySelector<HTMLElement>(
  236. `[data-side-pane-tab-id=${JSON.stringify(id)}]`,
  237. );
  238. if (!tab) throw new Error(`child tab 已丢失: ${id}`);
  239. tab.click();
  240. }, tabId);
  241. let state: Awaited<ReturnType<typeof readChildTimelineStates>>[number] | undefined;
  242. await browser.waitUntil(
  243. async () => {
  244. state = (await readChildTimelineStates()).find((child) => child.sessionId === sessionId);
  245. return Boolean(state && expectedText.test(state.assistantText));
  246. },
  247. { timeout: 30000, timeoutMsg: `切回 ${sessionId} 后没有显示预期的 assistant 正文` },
  248. );
  249. if (!state) throw new Error(`child pane 已丢失: ${sessionId}`);
  250. return state;
  251. }