conversation-session-v4-scroll.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // M5④ 门禁:长会话虚拟滚动(timeline 动态测高 + 底部锚定 + 回到底部)。
  2. // 证据层 L3:14 个 completed turn 构造稳定多 turn 历史,验证
  3. // ① 底部锚定跟随——完流后无需手动滚动即可见末行;
  4. // ② 顶部历史可达——scrollTop=0 后最早的行在 DOM 中可见(虚拟窗口正确回填);
  5. // ③ 上滚不被流式拉回——turn2 慢流期间用户停留在顶部,scrollTop 不被增量拽回底部;
  6. // ④ 回到底部按钮——解除跟随后出现,点按恢复跟随并贴底。
  7. // 锚定状态机语义的完整覆盖在 L1 单测(packages/ui/test/v4TimelineScrollAnchor.test.ts)。
  8. import { skipOccupationOnboardingIfPresent } from "../helpers/occupation-onboarding.js";
  9. import { clearAppData } from "../helpers/desktop-app.js";
  10. import {
  11. clickV4TimelineBackToBottom,
  12. getV4TimelineScrollState,
  13. prepareV4ConversationE2E,
  14. scrollV4TimelineToTop,
  15. selectV4TaskById,
  16. sendV4Prompt,
  17. startNewV4Draft,
  18. waitForV4Pane,
  19. waitForV4TimelineContaining,
  20. } from "../helpers/v4-conversation.js";
  21. describe("v4 M5④ 门禁:长会话虚拟滚动", () => {
  22. const HISTORY_TURNS = 14;
  23. after(async () => {
  24. await browser.electron.restoreAllMocks();
  25. await clearAppData();
  26. });
  27. it("50+ 行历史:底部锚定跟随 → 顶部可达 → 流式不拉回 → 回到底部", async () => {
  28. await skipOccupationOnboardingIfPresent();
  29. await prepareV4ConversationE2E();
  30. for (let index = 1; index <= HISTORY_TURNS; index += 1) {
  31. const marker = String(index).padStart(3, "0");
  32. await sendV4Prompt(`E2E_V4_SCROLL_HISTORY_${marker} 长会话历史`);
  33. await browser.waitUntil(
  34. async () => {
  35. const state = await getV4TimelineScrollState();
  36. // render unit 在 user row 到达时就会建立;若只等 unit 数量,最后一个
  37. // assistant/timeline row 尚未物化便会进入断言,默认 CI 串行负载下会读到 41/42。
  38. return state.renderUnitCount >= index && state.windowRowCount >= index * 3;
  39. },
  40. { timeout: 30000, timeoutMsg: `第 ${index} 个 scroll history turn 未完成` },
  41. );
  42. }
  43. const anchored = await getV4TimelineScrollState();
  44. const sessionA = await waitForV4Pane(
  45. (snapshot) => snapshot.sessionId !== null,
  46. "长会话没有绑定 session id",
  47. );
  48. if (!sessionA.sessionId) throw new Error("长会话 session id 缺失");
  49. // 修复原因:这个 case 验证的是 logical turn 虚拟化;provider 可把单个 logical
  50. // turn 投影为更多 raw rows,精确绑定 3 rows/turn 会把合法投影误报成滚动失败。
  51. expect(anchored.windowRowCount).toBeGreaterThanOrEqual(HISTORY_TURNS * 3);
  52. expect(anchored.renderUnitCount).toBe(HISTORY_TURNS);
  53. expect(anchored.following).toBe("true");
  54. // 虚拟化生效:DOM 中挂载的行数少于投影窗口行数(长会话不全量渲染)。
  55. expect(anchored.mountedRenderUnitCount).toBeLessThan(anchored.renderUnitCount);
  56. // 内容总高远超视口(可滚动历史确实存在)。
  57. expect(anchored.scrollHeight).toBeGreaterThan(anchored.clientHeight * 2);
  58. // ── 顶部历史可达:滚到顶后最早的行进入 DOM ──
  59. await scrollV4TimelineToTop();
  60. await waitForV4TimelineContaining("E2E_V4_SCROLL_HISTORY_001", 15000);
  61. await browser.waitUntil(
  62. async () => {
  63. const state = await getV4TimelineScrollState();
  64. return state.following === "false" && state.hasBackToBottom;
  65. },
  66. {
  67. timeout: 10000,
  68. timeoutMsg: "上滚离底后没有解除跟随/出现回到底部按钮",
  69. },
  70. );
  71. const savedHistoricalPosition = await getV4TimelineScrollState();
  72. await startNewV4Draft();
  73. await selectV4TaskById(sessionA.sessionId);
  74. await waitForV4Pane(
  75. (snapshot) => snapshot.sessionId === sessionA.sessionId,
  76. "切回长会话后 session 没有恢复",
  77. );
  78. await browser.waitUntil(
  79. async () => {
  80. const restored = await getV4TimelineScrollState();
  81. return (
  82. restored.following === "false" &&
  83. restored.hasBackToBottom &&
  84. Math.abs(restored.scrollTop - savedHistoricalPosition.scrollTop) <= 4
  85. );
  86. },
  87. {
  88. timeout: 15000,
  89. timeoutMsg: "切回长会话后没有恢复离底阅读位置",
  90. },
  91. );
  92. // ── turn2:慢流期间停在顶部,流式增量不得拉回 ──
  93. await clickV4TimelineBackToBottom();
  94. await sendV4Prompt("E2E_V4_SCROLL_TURN2 保持输出");
  95. await waitForV4TimelineContaining("V4_SCROLL_TURN2_STREAMING", 45000);
  96. await waitForV4Pane((s) => s.canStop, "turn2 streaming 没有出现 stop 按钮");
  97. await scrollV4TimelineToTop();
  98. await waitForV4TimelineContaining("E2E_V4_SCROLL_HISTORY_001", 15000);
  99. // 流式仍在进行的窗口内多次采样:scrollTop 必须停在顶部附近(不被增量拽回)。
  100. for (let sample = 0; sample < 4; sample += 1) {
  101. await browser.pause(700);
  102. const during = await getV4TimelineScrollState();
  103. expect(during.scrollTop).toBeLessThan(200);
  104. expect(during.following).toBe("false");
  105. }
  106. // 采样窗口结束时 turn2 仍在流式(证明上面的采样发生在流式期间)。
  107. const midStream = await waitForV4Pane(() => true, "读取流式中 pane 状态失败", 5000);
  108. expect(midStream.canStop).toBe(true);
  109. // ── 回到底部:恢复跟随,完流文本自动可见 ──
  110. await clickV4TimelineBackToBottom();
  111. const beforeBackgroundOutput = await waitForV4Pane(
  112. (snapshot) => snapshot.timelineText.includes("turn2 持续输出片段"),
  113. "切走前没有可比较的 turn2 流式片段",
  114. );
  115. const beforeBackgroundFragmentCount =
  116. beforeBackgroundOutput.timelineText.match(/turn2 持续输出片段/g)?.length ?? 0;
  117. await startNewV4Draft();
  118. await browser.pause(1200);
  119. await selectV4TaskById(sessionA.sessionId);
  120. const afterBackgroundOutput = await waitForV4Pane((snapshot) => {
  121. const fragmentCount = snapshot.timelineText.match(/turn2 持续输出片段/g)?.length ?? 0;
  122. // 修复原因:E2E 的 session projection keep-warm 只有 1 秒;切走 1.2 秒后
  123. // 切回时 data-session-id 会先于新 lease/snapshot 生效。只等待 session id 会
  124. // 在 timeline rows 仍为空的过渡帧返回 0,误判后台流式丢失。这里同时等待目标
  125. // timeline 恢复且片段确实增长,保持 SRM03 的后台输出 + 吸底恢复原始语义。
  126. return (
  127. snapshot.sessionId === sessionA.sessionId && fragmentCount > beforeBackgroundFragmentCount
  128. );
  129. }, "后台流式期间切回原 session 后投影没有恢复新增片段");
  130. const afterBackgroundFragmentCount =
  131. afterBackgroundOutput.timelineText.match(/turn2 持续输出片段/g)?.length ?? 0;
  132. expect(afterBackgroundFragmentCount).toBeGreaterThan(beforeBackgroundFragmentCount);
  133. await browser.waitUntil(
  134. async () => {
  135. const restored = await getV4TimelineScrollState();
  136. const distanceToBottom = Math.max(
  137. 0,
  138. restored.scrollHeight - restored.clientHeight - restored.scrollTop,
  139. );
  140. return restored.following === "true" && distanceToBottom <= 48;
  141. },
  142. {
  143. timeout: 15000,
  144. timeoutMsg: "吸底 session 切回后没有定位到当前最新底部",
  145. },
  146. );
  147. await waitForV4TimelineContaining("V4_SCROLL_TURN2_DONE", 60000);
  148. await waitForV4Pane((s) => !s.canStop, "turn2 没有回到空闲态", 45000);
  149. const final = await getV4TimelineScrollState();
  150. expect(final.following).toBe("true");
  151. expect(final.hasBackToBottom).toBe(false);
  152. });
  153. });