conversation-session-tool-groups-history-restore.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { restartIntoWorkspacePreservingProfile } from "#e2e/helpers/model-provider-restart";
  2. import { mkdir, rm, writeFile } from "node:fs/promises";
  3. import { join } from "node:path";
  4. import { clearAppData, seedSettings } from "#e2e/helpers/desktop-app";
  5. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  6. import { resolveE2ERuntimePath, resolveE2EToolPath } from "#e2e/helpers/e2e-runtime-paths";
  7. import {
  8. E2E_REPLY_TOKEN,
  9. getV4PaneSnapshot,
  10. prepareV4ConversationE2E,
  11. selectV4TaskById,
  12. sendV4Prompt,
  13. switchV4Mode,
  14. waitForV4AssistantMessageContaining,
  15. waitForV4Pane,
  16. } from "#e2e/helpers/v4-conversation";
  17. import { expandAssistantHistoriesWithContent } from "#e2e/helpers/conversation-session-tool-diagnostics";
  18. import { expandVisibleSummaryGroups } from "#e2e/helpers/conversation-session-tool-diagnostics";
  19. const CASE_NAME = "conversation-session-tool-groups-history-restore";
  20. const CASE_MARKER = "E2E_TOOL_GROUPS_HISTORY_RESTORE";
  21. const RUNTIME_ROOT = resolveE2ERuntimePath(CASE_NAME);
  22. const READ_PATH = resolveE2EToolPath(CASE_NAME, "read.txt");
  23. const WRITE_PATH = resolveE2EToolPath(CASE_NAME, "write.ts");
  24. describe("TGE05 individual tools history restore", () => {
  25. before(async function () {
  26. this.timeout(150000);
  27. await seedSettings({});
  28. await prepareV4ConversationE2E();
  29. await switchV4Mode("yolo");
  30. });
  31. afterEach(async () => {
  32. await rm(RUNTIME_ROOT, { recursive: true, force: true });
  33. });
  34. after(async () => {
  35. await browser.electron.restoreAllMocks();
  36. await clearAppData();
  37. });
  38. it("连续同类工具逐条显示,完整冷重启后保持内容与顺序", async function () {
  39. this.timeout(240000);
  40. await prepareFixture();
  41. await ensureToolCrossProductFullAccessMode();
  42. await sendV4Prompt(
  43. [
  44. `${CASE_MARKER}: Follow these tool steps in order.`,
  45. `1. Read ${READ_PATH} twice as two separate tool calls.`,
  46. `2. Run two separate Bash calls: node -e "console.log('TGE05_EXEC_A')" and node -e "console.log('TGE05_EXEC_B')"`,
  47. `3. Write ${WRITE_PATH} with exactly "export const restored = true;\\n", then Edit true to false.`,
  48. `4. Reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`,
  49. ].join(" "),
  50. );
  51. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN, 150000);
  52. await waitForV4Pane((snapshot) => !snapshot.canStop, "TGE05 工具轮没有结束", 150000);
  53. const history = await $("[data-testid^=chat-assistant-history-trigger-]");
  54. await expect(history).toBeDisplayed();
  55. await expect(history).toHaveAttribute("data-history-open", "false");
  56. await expandAssistantHistoriesWithContent();
  57. const liveGroups = await waitForIndividualTools();
  58. // 工具摘要字段用布局间距分隔,不能重新插入装饰性中点。
  59. for (const group of liveGroups) expect(group.summaryText).not.toContain("·");
  60. const labelWeights = await browser.execute(() =>
  61. Array.from(document.querySelectorAll(".tool-summary-kind-label")).map(
  62. (label) => getComputedStyle(label).fontWeight,
  63. ),
  64. );
  65. expect(labelWeights.length).toBeGreaterThan(0);
  66. expect(labelWeights.every((weight) => weight === "400")).toBe(true);
  67. // 外层收纳与内部工具分组独立:整段可收起,重新展开后工具列表仍可使用。
  68. await history.click();
  69. await expect(history).toHaveAttribute("data-history-open", "false");
  70. await expect($("[data-summary-group-trigger]")).not.toBeDisplayed();
  71. await history.click();
  72. await expect(history).toHaveAttribute("data-history-open", "true");
  73. await waitForIndividualTools();
  74. const sessionId = (await getV4PaneSnapshot()).sessionId;
  75. if (!sessionId || sessionId === "draft") throw new Error("TGE05 缺少 sessionId");
  76. await reloadAndRestoreSession(sessionId);
  77. await expandAssistantHistoriesWithContent();
  78. const restoredGroups = await waitForIndividualTools();
  79. for (const group of restoredGroups) expect(group.summaryText).not.toContain("·");
  80. expect(toStableToolEvidence(restoredGroups)).toEqual(toStableToolEvidence(liveGroups));
  81. expect((await getV4PaneSnapshot()).sessionId).toBe(sessionId);
  82. });
  83. });
  84. async function prepareFixture() {
  85. await rm(RUNTIME_ROOT, { recursive: true, force: true });
  86. await mkdir(RUNTIME_ROOT, { recursive: true });
  87. await writeFile(join(RUNTIME_ROOT, "read.txt"), "TGE05_READ\n", "utf8");
  88. }
  89. async function waitForIndividualTools() {
  90. const group = await $("[data-consecutive-summary-group]");
  91. // 外层收起动画会短暂保留隐藏 DOM;必须等重新展开可见后再读取内部摘要。
  92. await group.waitForDisplayed({ timeout: 60000 });
  93. await expect(group).toHaveAttribute("data-group-running", "false");
  94. await expect(group.$("[data-summary-group-statistics]")).toExist();
  95. // Explore / Run 混合统计不进入仅 Change 文件列表分支。
  96. await expect(group.$("[data-change-summary-files]")).not.toExist();
  97. expect(await group.$("[data-summary-group-statistics]").getAttribute("class")).not.toContain(
  98. "animated-gradient-text",
  99. );
  100. const trigger = await group.$("[data-summary-group-trigger]");
  101. await expect(group.$("[data-summary-group-icon] svg")).toExist();
  102. expect(await group.$("[data-summary-group-icon]").getText()).toBe("");
  103. await expect(
  104. group.$("[data-summary-group-icon] [role=button], [data-summary-group-icon] button"),
  105. ).not.toExist();
  106. await expect(trigger).toHaveText(
  107. /Explored 2 items, Ran 2 commands, Changed 1 file|已查阅 2 项,已执行 2 次,已修改 1 文件/u,
  108. );
  109. await expandVisibleSummaryGroups();
  110. await expect(trigger).toHaveAttribute("aria-expanded", "true");
  111. // 重试扫描只能展开收起组,不能把已展开组再次切回收起。
  112. await expandVisibleSummaryGroups();
  113. await expect(trigger).toHaveAttribute("aria-expanded", "true");
  114. type ToolEvidence = { toolCallId: string; toolName: string; status: string; summaryText: string };
  115. let latest: ToolEvidence[] = [];
  116. await browser.waitUntil(
  117. async () => {
  118. latest = await browser.execute(() =>
  119. Array.from(document.querySelectorAll<HTMLElement>("[data-tool-call-id][data-tool-name]"))
  120. .filter((block) => !block.parentElement?.closest("[data-tool-call-id]"))
  121. .map((block) => ({
  122. toolCallId: block.dataset.toolCallId ?? "",
  123. toolName: block.dataset.toolName ?? "",
  124. status: block.dataset.status ?? "",
  125. summaryText: block.innerText.trim(),
  126. })),
  127. );
  128. return latest.length === 6 && latest.every((tool) => tool.status === "completed");
  129. },
  130. { timeout: 60000, timeoutMsg: "连续工具没有逐条显示六个完成行" },
  131. );
  132. expect(latest.map((tool) => tool.toolName)).toEqual([
  133. "Read",
  134. "Read",
  135. "Bash",
  136. "Bash",
  137. "Write",
  138. "Edit",
  139. ]);
  140. // 分组子项完成后使用过去式,不能退回工具类型名;实时与冷恢复共用断言。
  141. for (const tool of latest) {
  142. if (tool.toolName === "Bash") expect(tool.summaryText).toMatch(/Ran|已执行/u);
  143. if (tool.toolName === "Write") expect(tool.summaryText).toMatch(/Wrote|已写入/u);
  144. if (tool.toolName === "Edit") expect(tool.summaryText).toMatch(/Edited|已编辑/u);
  145. }
  146. expect(new Set(latest.map((tool) => tool.toolCallId)).size).toBe(6);
  147. expect(latest.some((tool) => /^(explore|execute|changes|cua):/.test(tool.toolCallId))).toBe(
  148. false,
  149. );
  150. return latest;
  151. }
  152. function toStableToolEvidence(tools: Awaited<ReturnType<typeof waitForIndividualTools>>) {
  153. return tools.map(({ toolCallId, toolName, status, summaryText }) => ({
  154. toolCallId,
  155. toolName,
  156. status,
  157. summaryText,
  158. }));
  159. }
  160. async function reloadAndRestoreSession(sessionId: string) {
  161. // 冷恢复必须保留 Chromium profile;不把临时新 profile 当作用户重启。
  162. await restartIntoWorkspacePreservingProfile();
  163. await selectV4TaskById(sessionId);
  164. await waitForV4Pane(
  165. (snapshot) => snapshot.sessionId === sessionId,
  166. `TGE05 没有恢复 session ${sessionId}`,
  167. 60000,
  168. );
  169. await waitForRestoredSessionStable(sessionId, "TGE05 cold restore 没有稳定显示原会话");
  170. }
  171. async function waitForRestoredSessionStable(sessionId: string, failureMessage: string) {
  172. let stableSince = 0;
  173. await browser.waitUntil(
  174. async () => {
  175. const snapshot = await getV4PaneSnapshot();
  176. const matches =
  177. snapshot.sessionId === sessionId && snapshot.timelineText.includes(E2E_REPLY_TOKEN);
  178. if (!matches) {
  179. stableSince = 0;
  180. return false;
  181. }
  182. stableSince ||= Date.now();
  183. return Date.now() - stableSince >= 1000;
  184. },
  185. { timeout: 60000, timeoutMsg: failureMessage },
  186. );
  187. }