conversation-session-v4-fork-edit-branch.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // catalog E09(forkUsesEditedActiveBranch)验收(case catalog 战役)。
  2. // 证据层 L3:edit 重跑替换分支 → fork 编辑后的 assistant → child 只含 active branch,
  3. // 不带被 rewind 掉的旧 user/assistant 分支。历史对账链路 = rowId→messageId 翻译 +
  4. // RewindTriggered 截断 + fork 复制 + child 冷订阅 transcript 合成 hydration(曾出过 bug 的面)。
  5. import { writeFile } from "node:fs/promises";
  6. import { join } from "node:path";
  7. import { clearAppData } from "#e2e/helpers/desktop-app";
  8. import {
  9. clickFirstV4Fork,
  10. editFirstV4UserQuery,
  11. getV4PaneSnapshot,
  12. prepareV4ConversationE2E,
  13. sendV4Prompt,
  14. waitForV4Edit,
  15. waitForV4Fork,
  16. waitForV4Pane,
  17. waitForV4TimelineContaining,
  18. } from "#e2e/helpers/v4-conversation";
  19. describe("v4 catalog E09:fork 使用编辑后的 active branch", () => {
  20. after(async () => {
  21. await browser.electron.restoreAllMocks();
  22. await clearAppData();
  23. });
  24. it("edit 重跑后 fork → child 只含编辑分支,不带旧分支", async () => {
  25. await prepareV4ConversationE2E();
  26. // 原始首轮
  27. await sendV4Prompt("E2E_V4_BRANCH_T1 原始问题");
  28. await waitForV4TimelineContaining("V4_BRANCH_T1_REPLY", 60000);
  29. const parent = await waitForV4Pane(
  30. (s) => s.sessionId !== "draft" && s.sessionId !== null && !s.canStop,
  31. "首轮没有完成",
  32. 45000,
  33. );
  34. const parentSessionId = parent.sessionId;
  35. // edit:行内 newText + Send → rewind 截断 + 重跑
  36. await waitForV4Edit();
  37. // D20:首轮 edit 的 branch cut 不能变成可绘制空帧;真实链路记录中间帧而非只看终态。
  38. await browser.execute(() => {
  39. const scope = window as typeof window & {
  40. __editFrames?: { at: number; hasRows: boolean; height: number }[];
  41. __editFrameRequest?: number;
  42. };
  43. scope.__editFrames = [];
  44. const sample = () => {
  45. const timeline = document.querySelector<HTMLElement>('[data-testid="v4-timeline"]');
  46. scope.__editFrames!.push({
  47. at: performance.now(),
  48. hasRows: Boolean(timeline?.querySelector('[data-testid^="v4-row-"]')),
  49. height: timeline?.scrollHeight ?? 0,
  50. });
  51. scope.__editFrameRequest = requestAnimationFrame(sample);
  52. };
  53. sample();
  54. });
  55. try {
  56. await editFirstV4UserQuery("E2E_V4_BRANCH_EDITED 编辑后的问题");
  57. await waitForV4TimelineContaining("E2E_V4_BRANCH_EDITED", 30000);
  58. await waitForV4TimelineContaining("V4_BRANCH_EDITED_REPLY", 60000);
  59. } finally {
  60. const frames = await browser.execute(() => {
  61. const scope = window as typeof window & {
  62. __editFrames?: { at: number; hasRows: boolean; height: number }[];
  63. __editFrameRequest?: number;
  64. };
  65. cancelAnimationFrame(scope.__editFrameRequest!);
  66. const result = scope.__editFrames ?? [];
  67. delete scope.__editFrames;
  68. delete scope.__editFrameRequest;
  69. return result;
  70. });
  71. await writeFile(
  72. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "edit-presentation-frames.json"),
  73. JSON.stringify(frames, null, 2),
  74. );
  75. expect(frames.length).toBeGreaterThan(0);
  76. expect(frames.filter((frame) => !frame.hasRows)).toEqual([]);
  77. }
  78. const afterEdit = await getV4PaneSnapshot();
  79. expect(afterEdit.timelineText).not.toContain("V4_BRANCH_T1_REPLY");
  80. expect(afterEdit.timelineText).not.toContain("E2E_V4_BRANCH_T1");
  81. // fork 编辑后的 assistant 行
  82. await waitForV4Pane((s) => !s.canStop, "edit 重跑没有收口", 45000);
  83. await waitForV4Fork();
  84. expect(await clickFirstV4Fork()).toBe(true);
  85. const child = await waitForV4Pane(
  86. (s) => s.sessionId !== null && s.sessionId !== "draft" && s.sessionId !== parentSessionId,
  87. "fork 后 pane 没有切到 child session",
  88. 60000,
  89. );
  90. expect(child.sessionId).not.toBe(parentSessionId);
  91. // child 历史 = 编辑后的 active branch;旧分支不得进入 child。
  92. // 注:rewind 合成 notice(rewoundPromptPreview)会引用被编辑掉的原 prompt 文本,
  93. // 这是 transcript 对 rewind 的记录、不是旧分支复活——强断言只看旧 assistant 回复
  94. // 与旧 user turn 是否作为真实轮次存在。
  95. await waitForV4TimelineContaining("V4_BRANCH_EDITED_REPLY", 30000);
  96. const childSnapshot = await getV4PaneSnapshot();
  97. expect(childSnapshot.timelineText).toContain("E2E_V4_BRANCH_EDITED");
  98. expect(childSnapshot.timelineText).not.toContain("V4_BRANCH_T1_REPLY");
  99. });
  100. });