conversation-session-activity-retention.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { BufferReader, BufferWriter, VSBuffer, deserialize, serialize } from "@zcode/rpc";
  2. import { join } from "node:path";
  3. import { selectTaskById } from "#e2e/helpers/conversation-session";
  4. import { clearAppData, pinE2ELocale } from "#e2e/helpers/desktop-app";
  5. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  6. import {
  7. prepareV4ConversationE2E,
  8. getV4PaneSnapshot,
  9. startNewV4Draft,
  10. sendV4Prompt,
  11. getV4Messages,
  12. sendV4PromptAndWaitAccepted,
  13. getLatestV4CommandAck,
  14. waitForV4ConversationState,
  15. } from "#e2e/helpers/v4-conversation";
  16. declare global {
  17. interface Window {
  18. __activitySendFault?: { bytes: number[]; port: MessagePort };
  19. }
  20. }
  21. const trigger = '[data-status-capsule="activity"]';
  22. const panel = '[data-status-capsule-content="activity"]';
  23. async function openActivity() {
  24. const button = await $(trigger);
  25. await button.waitForDisplayed({ timeout: 60000 });
  26. if (!(await $(panel).isDisplayed())) await button.click();
  27. await $(panel).waitForDisplayed({ timeout: 15000 });
  28. }
  29. // ACT01/05/06/09/10:工具实际执行,Provider 只决定工具输入,不注入投影。
  30. describe("Activity retention desktop candidate", () => {
  31. after(async () => {
  32. await browser.electron.restoreAllMocks();
  33. await clearAppData();
  34. });
  35. it("保留真实后台结果,下一轮收起明细,无目录时隐藏,刷新保持隐藏", async function () {
  36. this.timeout(240000);
  37. await prepareV4ConversationE2E();
  38. // 系统语言可能为日/韩/俄文;先固定真实界面语言,避免正确状态文案被中英文正则误判。
  39. await pinE2ELocale("en-US");
  40. await ensureToolCrossProductFullAccessMode();
  41. for (const [state, result] of [
  42. ["DONE", "completed"],
  43. ["STOP", "stopped"],
  44. ["FAIL", "failed"],
  45. ] as const) {
  46. const marker = `E2E_ACTIVITY_${state}`;
  47. await sendV4PromptAndWaitAccepted(marker, marker, "真实用户输入没有接纳");
  48. await openActivity();
  49. if (state === "STOP") {
  50. await browser.keys("Escape");
  51. const runningInput = "E2E_ACTIVITY_WHILE_RUNNING";
  52. await sendV4PromptAndWaitAccepted(runningInput, runningInput, "运行中消息未接纳");
  53. await openActivity();
  54. expect((await $$(`${panel} [data-background-task-kind="bash"]`)).length).toBe(1);
  55. const stop = await $(`${panel} [data-testid^="v4-background-work-cancel-"]`);
  56. await stop.waitForDisplayed({ timeout: 30000 });
  57. await stop.click();
  58. await browser.waitUntil(
  59. async () => (await getLatestV4CommandAck("cancelBackgroundWork"))?.status === "accepted",
  60. {
  61. timeout: 30000,
  62. timeoutMsg: "停止命令未被接纳",
  63. },
  64. );
  65. }
  66. const ended = `${panel} [data-activity-result="${result}"]`;
  67. await $(ended).waitForDisplayed({ timeout: 60000 });
  68. expect(await $(trigger).isDisplayed()).toBe(true);
  69. // 成功/失败的主标签表示状态,次要位只显示对应数量;停止沿用原摘要。
  70. expect(await $(`${trigger} [data-status-capsule-secondary]`).getText()).toBe(
  71. state === "STOP" ? "Stopped" : "1",
  72. );
  73. if (state !== "STOP") {
  74. expect(await $(trigger).getText()).toContain(state === "DONE" ? "Completed" : "Failed");
  75. }
  76. expect(await $(`${ended} [data-testid^="v4-background-work-cancel-"]`).isExisting()).toBe(
  77. false,
  78. );
  79. await waitForV4ConversationState((s) => s.state !== "streaming", "后台通知没有收口", 60000);
  80. const frozenText = await $(ended).getText();
  81. if (state === "DONE") expect(frozenText).toContain("E2E_ACTIVITY_REAL_OUTPUT");
  82. if (state === "FAIL") expect(frozenText).toContain("E2E_ACTIVITY_REAL_ERROR");
  83. await browser.refresh();
  84. await openActivity();
  85. await $(ended).waitForDisplayed({ timeout: 60000 });
  86. expect(await $(ended).getText()).toBe(frozenText);
  87. await browser.saveScreenshot(
  88. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, `activity-${state}.png`),
  89. );
  90. await browser.keys("Escape");
  91. if (state === "DONE") {
  92. const sessionId = (await getV4PaneSnapshot()).sessionId!;
  93. await startNewV4Draft();
  94. expect(await $(trigger).isExisting()).toBe(false);
  95. await selectTaskById(sessionId);
  96. await openActivity();
  97. await $(ended).waitForDisplayed({ timeout: 30000 });
  98. await browser.keys("Escape");
  99. // 仅在 RPC 发出前断开一次传输,不伪造 ACK、不写 store;Agent 不可能接纳此消息。
  100. await browser.execute(() => {
  101. const original = MessagePort.prototype.postMessage;
  102. MessagePort.prototype.postMessage = function (message: unknown, options?: unknown) {
  103. const text = message instanceof Uint8Array ? new TextDecoder().decode(message) : "";
  104. if (
  105. text.includes("sendConversationCommandV4") &&
  106. text.includes("E2E_ACTIVITY_SEND_REJECTED")
  107. ) {
  108. MessagePort.prototype.postMessage = original;
  109. window.__activitySendFault = { bytes: Array.from(message as Uint8Array), port: this };
  110. return;
  111. }
  112. return Reflect.apply(
  113. original,
  114. this,
  115. options === undefined ? [message] : [message, options],
  116. );
  117. };
  118. });
  119. await sendV4Prompt("E2E_ACTIVITY_SEND_REJECTED");
  120. const bytes = await browser.execute(() => window.__activitySendFault?.bytes);
  121. expect(bytes).toBeDefined();
  122. const header = deserialize(new BufferReader(VSBuffer.wrap(Uint8Array.from(bytes!)))) as [
  123. number,
  124. number,
  125. ];
  126. const writer = new BufferWriter();
  127. serialize(writer, [202, header[1]]);
  128. serialize(writer, {
  129. name: "Error",
  130. message: "E2E_ACTIVITY_SEND_REJECTED transport unavailable",
  131. stack: [],
  132. });
  133. await browser.execute((bytes) => {
  134. window.__activitySendFault!.port.dispatchEvent(
  135. new MessageEvent("message", { data: Uint8Array.from(bytes) }),
  136. );
  137. delete window.__activitySendFault;
  138. }, Array.from(writer.buffer.buffer));
  139. await browser.waitUntil(
  140. async () => (await getLatestV4CommandAck("sendText"))?.status === "transport-error",
  141. { timeout: 15000, timeoutMsg: "接纳前传输故障未触发" },
  142. );
  143. expect(
  144. (await getV4Messages("user")).some((row) =>
  145. row.text.includes("E2E_ACTIVITY_SEND_REJECTED"),
  146. ),
  147. ).toBe(false);
  148. const banner = $('[data-testid="chat-error-banner"]');
  149. await banner.waitForDisplayed({ timeout: 15000 });
  150. const buttons = await banner.$$("button");
  151. await buttons[(await buttons.length) - 1]!.click();
  152. await openActivity();
  153. await $(ended).waitForDisplayed({ timeout: 15000 });
  154. await browser.keys("Escape");
  155. }
  156. const next = `E2E_ACTIVITY_CLEAR_${state}`;
  157. await sendV4PromptAndWaitAccepted(next, next, "下一条消息未被接纳");
  158. await $(trigger).waitForExist({ reverse: true, timeout: 30000 });
  159. expect(await $(panel).isExisting()).toBe(false);
  160. await waitForV4ConversationState((s) => s.state !== "streaming", "清除消息未结束", 60000);
  161. await browser.refresh();
  162. await browser.waitUntil(
  163. async () => await $('[data-testid="v4-session-pane-workspace-main"]').isExisting(),
  164. { timeout: 30000 },
  165. );
  166. await waitForV4ConversationState((s) => s.state !== "streaming", "刷新快照未恢复", 30000);
  167. expect(await $(trigger).isExisting()).toBe(false);
  168. }
  169. });
  170. });