conversation-session-file-summary-cold-start.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // O16:完整重启 Electron / Host / CLI 后,已完成轮次的文件变更摘要仍需从持久数据冷物化。
  2. // 该 case 只覆盖 local desktop-continuous + 单次 Write;多文件、reverted、remote/replayable
  3. // 继续由 O10-O14 及对应 focused tests 覆盖,避免把不同恢复语义混进一个窗口级证据。
  4. import { readFile, rm } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { clearAppData, DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  7. import {
  8. getV4PaneSnapshot,
  9. prepareV4ConversationE2E,
  10. selectV4TaskById,
  11. sendV4Prompt,
  12. switchV4Mode,
  13. waitForV4Pane,
  14. waitForV4TimelineContaining,
  15. } from "#e2e/helpers/v4-conversation";
  16. const CASE_ROOT = join(DEFAULT_WORKSPACE, "file-summary-cold-start-e2e");
  17. const RELATIVE_PATH = "file-summary-cold-start-e2e/summary.txt";
  18. const FILE_PATH = join(DEFAULT_WORKSPACE, RELATIVE_PATH);
  19. const REQUEST_MARKER = "E2E_FILE_SUMMARY_COLD_START";
  20. const FINAL_MARKER = "E2E_FILE_SUMMARY_COLD_START_OK";
  21. interface FileSummarySnapshot {
  22. expanded: boolean;
  23. headerText: string;
  24. pathVisible: boolean;
  25. }
  26. describe("O16 cold start 文件变更摘要", () => {
  27. afterEach(async () => {
  28. await rm(CASE_ROOT, { force: true, recursive: true });
  29. });
  30. after(async () => {
  31. await browser.electron.restoreAllMocks();
  32. await clearAppData();
  33. });
  34. it("单次 Write 完成后完整冷启动仍显示相同摘要并可展开文件", async function () {
  35. this.timeout(240000);
  36. await rm(CASE_ROOT, { force: true, recursive: true });
  37. await prepareV4ConversationE2E();
  38. await switchV4Mode("yolo");
  39. const prompt = `${REQUEST_MARKER}: create ${RELATIVE_PATH} with the requested fixture content.`;
  40. await sendV4Prompt(prompt);
  41. await waitForV4TimelineContaining(FINAL_MARKER, 90000);
  42. await waitForFileContent("cold-summary-line-1\ncold-summary-line-2\n");
  43. await waitForV4Pane(
  44. (snapshot) => !snapshot.canStop,
  45. "文件摘要 cold-start case 完成后没有回到 idle",
  46. 60000,
  47. );
  48. const liveSummary = await waitForFileSummary();
  49. expect(liveSummary.headerText).toMatch(/1 (file changed|个文件已更改)/);
  50. expect(liveSummary.headerText).toContain("+2");
  51. expect(liveSummary.headerText).toContain("-0");
  52. const sessionId = (await getV4PaneSnapshot()).sessionId;
  53. if (!sessionId || sessionId === "draft") {
  54. throw new Error(`文件摘要 case 没有真实 sessionId: ${sessionId ?? "null"}`);
  55. }
  56. // 修复原因:renderer reload 仍可能复用 CLI 内存事件,无法复现本 bug;reloadSession
  57. // 会同时重启 Electron、Host 与 CLI,强制摘要只从 transcript + workspace checkpoint 恢复。
  58. await reloadAppAndRestoreV4Session(sessionId);
  59. await waitForV4TimelineContaining(FINAL_MARKER, 60000);
  60. const coldSummary = await waitForFileSummary();
  61. expect(coldSummary.headerText).toBe(liveSummary.headerText);
  62. expect(coldSummary.expanded).toBe(false);
  63. await expandFileSummary();
  64. const expandedSummary = await waitForFileSummary(true);
  65. expect(expandedSummary.pathVisible).toBe(true);
  66. expect(expandedSummary.headerText).toBe(liveSummary.headerText);
  67. expect((await getV4PaneSnapshot()).sessionId).toBe(sessionId);
  68. });
  69. });
  70. async function waitForFileContent(expected: string) {
  71. await browser.waitUntil(
  72. async () => (await readFile(FILE_PATH, "utf8").catch(() => "")) === expected,
  73. {
  74. timeout: 60000,
  75. timeoutMsg: `${FILE_PATH} 没有收敛到期望内容`,
  76. },
  77. );
  78. }
  79. async function waitForFileSummary(expectExpanded = false): Promise<FileSummarySnapshot> {
  80. let latest: FileSummarySnapshot | null = null;
  81. await browser.waitUntil(
  82. async () => {
  83. latest = await readFileSummary();
  84. return Boolean(
  85. latest && latest.expanded === expectExpanded && (!expectExpanded || latest.pathVisible),
  86. );
  87. },
  88. {
  89. timeout: 30000,
  90. timeoutMsg: expectExpanded
  91. ? `文件摘要展开后没有显示 ${RELATIVE_PATH}`
  92. : "没有找到已完成轮次的文件变更摘要卡片",
  93. },
  94. );
  95. if (!latest) throw new Error("文件变更摘要快照为空");
  96. return latest;
  97. }
  98. function readFileSummary(): Promise<FileSummarySnapshot | null> {
  99. return browser.execute((relativePath) => {
  100. const expandLabels = new Set(["Expand changed files", "展开已更改文件"]);
  101. const collapseLabels = new Set(["Collapse changed files", "收起已更改文件"]);
  102. const header = Array.from(document.querySelectorAll<HTMLButtonElement>("button")).find(
  103. (button) => {
  104. const label = button.getAttribute("aria-label") ?? "";
  105. return expandLabels.has(label) || collapseLabels.has(label);
  106. },
  107. );
  108. if (!header) return null;
  109. const card = header.parentElement?.parentElement;
  110. const label = header.getAttribute("aria-label") ?? "";
  111. return {
  112. expanded: collapseLabels.has(label),
  113. headerText: (header.innerText ?? "").replace(/\u00a0/g, " ").trim(),
  114. // FileDisplayInline 会把目录与文件名拆成两个文本节点,不能用连续 innerText
  115. // 匹配相对路径;详情行的 title 保留完整路径,正好能证明明细查询已返回目标文件。
  116. pathVisible: Boolean(
  117. card &&
  118. Array.from(card.querySelectorAll<HTMLElement>("[title]")).some((element) =>
  119. (element.getAttribute("title") ?? "").endsWith(relativePath),
  120. ),
  121. ),
  122. };
  123. }, RELATIVE_PATH);
  124. }
  125. async function expandFileSummary() {
  126. const clicked = await browser.execute(() => {
  127. const labels = new Set(["Expand changed files", "展开已更改文件"]);
  128. const header = Array.from(document.querySelectorAll<HTMLButtonElement>("button")).find(
  129. (button) => labels.has(button.getAttribute("aria-label") ?? ""),
  130. );
  131. header?.click();
  132. return Boolean(header);
  133. });
  134. expect(clicked).toBe(true);
  135. }
  136. async function reloadAppAndRestoreV4Session(sessionId: string) {
  137. await browser.reloadSession();
  138. await waitForRendererAfterReloadSession();
  139. // reloadSession 后 Electron service bridge 可能晚于 renderer DOM 恢复;这里的产品证据
  140. // 只依赖 renderer → Host → CLI 链路,直接等待任务项可点击可避免 bridge 重连假失败。
  141. await selectV4TaskById(sessionId);
  142. await waitForV4Pane(
  143. (snapshot) => snapshot.sessionId === sessionId,
  144. `CLI cold restart 后没有恢复 session ${sessionId}`,
  145. 60000,
  146. );
  147. }
  148. async function waitForRendererAfterReloadSession() {
  149. await browser.waitUntil(
  150. async () => {
  151. try {
  152. const puppeteer = await browser.getPuppeteer();
  153. const rendererTarget = puppeteer
  154. .targets()
  155. .filter((target) => isRendererUrl(target.url()))
  156. .at(-1);
  157. const targetId = rendererTarget
  158. ? ((rendererTarget as unknown as { _targetId?: string })._targetId ?? null)
  159. : null;
  160. if (!targetId) return false;
  161. await browser.switchToWindow(targetId);
  162. return true;
  163. } catch {
  164. // reloadSession 后旧 CDP websocket 会短暂断开,等待新 renderer target 即可。
  165. return false;
  166. }
  167. },
  168. {
  169. timeout: 30000,
  170. interval: 250,
  171. timeoutMsg: "CLI cold restart 后没有找到 renderer target",
  172. },
  173. );
  174. }
  175. function isRendererUrl(url: string) {
  176. try {
  177. return new URL(url).pathname.endsWith("/renderer/index.html");
  178. } catch {
  179. return false;
  180. }
  181. }