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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // 修复原因:abb6b25588 把中文命令计数文案从「已执行 N 次」改为「已执行 N 命令」,
  107. // 与英文 "Ran N commands" 对齐;断言随产品文案更新。
  108. await expect(trigger).toHaveText(
  109. /Explored 2 items, Ran 2 commands, Changed 1 file|已查阅 2 项,已执行 2 命令,已修改 1 文件/u,
  110. );
  111. await expandVisibleSummaryGroups();
  112. await expect(trigger).toHaveAttribute("aria-expanded", "true");
  113. // 重试扫描只能展开收起组,不能把已展开组再次切回收起。
  114. await expandVisibleSummaryGroups();
  115. await expect(trigger).toHaveAttribute("aria-expanded", "true");
  116. type ToolEvidence = { toolCallId: string; toolName: string; status: string; summaryText: string };
  117. let latest: ToolEvidence[] = [];
  118. await browser.waitUntil(
  119. async () => {
  120. latest = await browser.execute(() =>
  121. Array.from(document.querySelectorAll<HTMLElement>("[data-tool-call-id][data-tool-name]"))
  122. .filter((block) => !block.parentElement?.closest("[data-tool-call-id]"))
  123. .map((block) => ({
  124. toolCallId: block.dataset.toolCallId ?? "",
  125. toolName: block.dataset.toolName ?? "",
  126. status: block.dataset.status ?? "",
  127. summaryText: block.innerText.trim(),
  128. })),
  129. );
  130. return latest.length === 6 && latest.every((tool) => tool.status === "completed");
  131. },
  132. { timeout: 60000, timeoutMsg: "连续工具没有逐条显示六个完成行" },
  133. );
  134. expect(latest.map((tool) => tool.toolName)).toEqual([
  135. "Read",
  136. "Read",
  137. "Bash",
  138. "Bash",
  139. "Write",
  140. "Edit",
  141. ]);
  142. // 分组子项完成后使用过去式,不能退回工具类型名;实时与冷恢复共用断言。
  143. for (const tool of latest) {
  144. if (tool.toolName === "Bash") expect(tool.summaryText).toMatch(/Ran|已执行/u);
  145. if (tool.toolName === "Write") expect(tool.summaryText).toMatch(/Wrote|已写入/u);
  146. if (tool.toolName === "Edit") expect(tool.summaryText).toMatch(/Edited|已编辑/u);
  147. }
  148. expect(new Set(latest.map((tool) => tool.toolCallId)).size).toBe(6);
  149. expect(latest.some((tool) => /^(explore|execute|changes|cua):/.test(tool.toolCallId))).toBe(
  150. false,
  151. );
  152. return latest;
  153. }
  154. function toStableToolEvidence(tools: Awaited<ReturnType<typeof waitForIndividualTools>>) {
  155. return tools.map(({ toolCallId, toolName, status, summaryText }) => ({
  156. toolCallId,
  157. toolName,
  158. status,
  159. summaryText,
  160. }));
  161. }
  162. async function reloadAndRestoreSession(sessionId: string) {
  163. // 冷恢复必须保留 Chromium profile;不把临时新 profile 当作用户重启。
  164. await restartIntoWorkspacePreservingProfile();
  165. await selectV4TaskById(sessionId);
  166. await waitForV4Pane(
  167. (snapshot) => snapshot.sessionId === sessionId,
  168. `TGE05 没有恢复 session ${sessionId}`,
  169. 60000,
  170. );
  171. await waitForRestoredSessionStable(sessionId, "TGE05 cold restore 没有稳定显示原会话");
  172. }
  173. async function waitForRestoredSessionStable(sessionId: string, failureMessage: string) {
  174. let stableSince = 0;
  175. await browser.waitUntil(
  176. async () => {
  177. const snapshot = await getV4PaneSnapshot();
  178. const matches =
  179. snapshot.sessionId === sessionId && snapshot.timelineText.includes(E2E_REPLY_TOKEN);
  180. if (!matches) {
  181. stableSince = 0;
  182. return false;
  183. }
  184. stableSince ||= Date.now();
  185. return Date.now() - stableSince >= 1000;
  186. },
  187. { timeout: 60000, timeoutMsg: failureMessage },
  188. );
  189. }