conversation-session-subagent-provider-retry-status.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. TID_CHAT_LOADING,
  3. TID_V4_SESSION_PANE,
  4. TID_V4_SUBAGENT_OPEN_SIDE_PANE,
  5. testId,
  6. } from "@zcode/shared/test-ids";
  7. import { clearAppData } from "#e2e/helpers/desktop-app";
  8. import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network";
  9. import { waitForToolCallBlockByToolCallId } from "#e2e/helpers/conversation-session-tool";
  10. import {
  11. prepareV4ConversationE2E,
  12. sendV4Prompt,
  13. waitForV4AssistantMessageContaining,
  14. waitForV4Pane,
  15. } from "#e2e/helpers/v4-conversation";
  16. const PARENT_MARKER = "E2E_SUBAGENT_PROVIDER_RETRY_PARENT";
  17. const PARENT_DONE = "E2E_SUBAGENT_PROVIDER_RETRY_PARENT_DONE";
  18. const CHILD_MARKER = "E2E_SUBAGENT_PROVIDER_RETRY_CHILD";
  19. const CHILD_DONE = "E2E_SUBAGENT_PROVIDER_RETRY_CHILD_DONE";
  20. const TOOL_CALL_ID = "toolu_e2e_subagent_provider_retry_agent";
  21. const MAIN_PANE_SELECTOR = `[data-testid="${testId(TID_V4_SESSION_PANE, "workspace-main")}"]`;
  22. const CHILD_PANE_SELECTOR = '[data-session-id^="sess_subagent_"]';
  23. describe("Main/Subagent provider retry 时间线状态 E2E", () => {
  24. after(async () => {
  25. await browser.electron.restoreAllMocks();
  26. await clearAppData();
  27. });
  28. it("SAT25: 前两次 retry 保持 loading,第三次才显示计数并随进展消失", async function () {
  29. this.timeout(240000);
  30. await prepareV4ConversationE2E();
  31. await sendV4Prompt(
  32. `${PARENT_MARKER}: launch one background Agent and finish after its completed task notification with ${PARENT_DONE}.`,
  33. );
  34. const mainEarlyRetryState = await waitForVisiblePane(
  35. MAIN_PANE_SELECTOR,
  36. (state) => state.hasGenericLoading && !hasRetryText(state.liveTailText),
  37. "Main 前两次 retry 没有按 loading 展示且隐藏重连文案",
  38. );
  39. expect(mainEarlyRetryState.hasGenericLoading).toBe(true);
  40. const mainRetryState = await waitForVisiblePane(
  41. MAIN_PANE_SELECTOR,
  42. (state) => hasRetryAttemptText(state.liveTailText, 3) && !state.hasGenericLoading,
  43. "Main 第三次 retry 没有在当前 turn 用 3/10 状态替换通用 loading",
  44. );
  45. expect(mainRetryState.hasGenericLoading).toBe(false);
  46. await waitForToolCallBlockByToolCallId(TOOL_CALL_ID, 60000);
  47. await waitForVisiblePane(
  48. MAIN_PANE_SELECTOR,
  49. (state) => !hasRetryText(state.text),
  50. "Main 收到有效工具进展后 retry 状态没有清除",
  51. );
  52. await waitForUpstreamRequest(
  53. { lastUserMessageIncludes: [CHILD_MARKER] },
  54. "subagent retry child 首个请求没有到达 replay server",
  55. 60000,
  56. );
  57. await openChildSidePane();
  58. const retryState = await waitForVisiblePane(
  59. CHILD_PANE_SELECTOR,
  60. (state) => state.hasGenericLoading && !hasRetryText(state.liveTailText),
  61. "child 前两次以内的 retry 没有按 loading 展示且隐藏重连文案",
  62. );
  63. expect(retryState.hasGenericLoading).toBe(true);
  64. await waitForVisiblePane(
  65. CHILD_PANE_SELECTOR,
  66. (state) => state.text.includes(CHILD_DONE) && !hasRetryText(state.text),
  67. "child 恢复正文进展后 retry 状态没有清除",
  68. 60000,
  69. );
  70. // Bug 原因:完成 token 曾同时出现在 user prompt 与 provider reply,聚合 timeline
  71. // 查询会被输入行提前满足;只观察 assistant row 才能作为父轮完成屏障。
  72. await waitForV4AssistantMessageContaining(PARENT_DONE, 90000);
  73. await waitForV4Pane(
  74. (snapshot) => !snapshot.canStop,
  75. "subagent retry case 完成后父会话没有回到 idle",
  76. 60000,
  77. );
  78. });
  79. });
  80. async function openChildSidePane() {
  81. const block = await waitForToolCallBlockByToolCallId(TOOL_CALL_ID, 30000);
  82. const action = await $(`[data-testid="${block.testId}"]`).$(
  83. `[data-testid^="${TID_V4_SUBAGENT_OPEN_SIDE_PANE}-"]`,
  84. );
  85. await action.waitForDisplayed({
  86. timeout: 15000,
  87. timeoutMsg: "running child 没有右侧会话入口",
  88. });
  89. await action.click();
  90. }
  91. interface VisiblePaneState {
  92. hasGenericLoading: boolean;
  93. liveTailText: string;
  94. text: string;
  95. }
  96. function hasRetryAttemptText(text: string, attempt: number): boolean {
  97. return (
  98. text.includes(`重新连接中... ${attempt}/10`) || text.includes(`Reconnecting... ${attempt}/10`)
  99. );
  100. }
  101. function hasRetryText(text: string): boolean {
  102. return text.includes("重新连接中...") || text.includes("Reconnecting...");
  103. }
  104. async function waitForVisiblePane(
  105. paneSelector: string,
  106. predicate: (state: VisiblePaneState) => boolean,
  107. timeoutMsg: string,
  108. timeout = 30000,
  109. ): Promise<VisiblePaneState> {
  110. let latest: VisiblePaneState = {
  111. hasGenericLoading: false,
  112. liveTailText: "",
  113. text: "",
  114. };
  115. await browser.waitUntil(
  116. async () => {
  117. latest = await browser.execute(
  118. (selector, loadingTestId) => {
  119. const pane = Array.from(document.querySelectorAll<HTMLElement>(selector)).find(
  120. (candidate) => candidate.getClientRects().length > 0,
  121. );
  122. const liveTail = pane?.querySelector<HTMLElement>('[data-v4-running-live-tail="true"]');
  123. return {
  124. hasGenericLoading: Boolean(liveTail?.querySelector(`[data-testid="${loadingTestId}"]`)),
  125. liveTailText: liveTail?.innerText ?? "",
  126. text: pane?.innerText ?? "",
  127. };
  128. },
  129. paneSelector,
  130. TID_CHAT_LOADING,
  131. );
  132. return predicate(latest);
  133. },
  134. { timeout, interval: 100, timeoutMsg },
  135. );
  136. return latest;
  137. }