conversation-session-history-first.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { readFile, writeFile, rm } from "node:fs/promises";
  2. import { join, sep } from "node:path";
  3. import { DatabaseSync } from "node:sqlite";
  4. import {
  5. TID_V4_COMPOSER_INPUT,
  6. TID_V4_COMPOSER_SEND,
  7. TID_WORKSPACE_MORE_BUTTON,
  8. } from "@zcode/shared/test-ids";
  9. import {
  10. DEFAULT_WORKSPACE,
  11. clearAppData,
  12. waitForWorkspaceApp,
  13. setInputValueByTestIdDom,
  14. } from "#e2e/helpers/desktop-app";
  15. import {
  16. prepareV4ConversationE2E,
  17. selectV4TaskById,
  18. waitForV4TimelineContaining,
  19. scrollV4TimelineToTop,
  20. getV4TimelineScrollState,
  21. } from "#e2e/helpers/v4-conversation";
  22. import {
  23. HISTORY_FIRST_SESSION,
  24. HISTORY_FIRST_FAILED_SESSION,
  25. HISTORY_FIRST_TEXT,
  26. } from "#e2e/helpers/session-history-first-fixture";
  27. const root = join(DEFAULT_WORKSPACE, ".zcode", "history-first-e2e");
  28. const release = join(root, "release");
  29. const entered = join(root, "entered");
  30. const artifacts = process.env.ZCODE_E2E_ARTIFACT_DIR ?? join(process.cwd(), ".e2e-artifacts");
  31. describe("HF01:历史首帧早于 resume hook 完成", () => {
  32. afterEach(async function () {
  33. if (this.currentTest?.state !== "failed") return;
  34. await browser.saveScreenshot(join(artifacts, "history-first-failure.png"));
  35. console.info(
  36. "history-first UI failure",
  37. await browser.execute(() => ({
  38. text: document.body.innerText.slice(-6000),
  39. dialogs: [...document.querySelectorAll('[role="dialog"], [role="alertdialog"]')].map(
  40. (node) => node.textContent,
  41. ),
  42. trigger: document.querySelector('[data-testid="chat-model-select-trigger"]')?.outerHTML,
  43. })),
  44. );
  45. });
  46. after(async () => {
  47. await writeFile(release, "release").catch(() => undefined);
  48. await clearAppData();
  49. });
  50. it("可以先读历史和编辑草稿,释放初始化屏障后才可发送", async function () {
  51. this.timeout(180_000);
  52. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  53. await prepareV4ConversationE2E();
  54. const openedAt = performance.now();
  55. await selectV4TaskById(HISTORY_FIRST_SESSION);
  56. await waitForV4TimelineContaining(HISTORY_FIRST_TEXT, 30_000);
  57. const historyVisibleMs = Math.round(performance.now() - openedAt);
  58. await browser.waitUntil(
  59. async () => (await readFile(entered, "utf8").catch(() => "")) === "entered",
  60. { timeout: 30_000, timeoutMsg: "resume hook 未进入屏障" },
  61. );
  62. expect(await $(`[data-testid="v4-session-initialization"]`).isExisting()).toBe(false);
  63. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "E2E_HISTORY_FIRST 保留的草稿");
  64. expect(await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled()).toBe(false);
  65. await browser.saveScreenshot(join(artifacts, "history-first-initializing.png"));
  66. await writeFile(release, "release");
  67. await browser.waitUntil(
  68. async () => await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled(),
  69. { timeout: 30_000, timeoutMsg: "初始化未收敛" },
  70. );
  71. expect(await $(`[data-testid="${TID_V4_COMPOSER_INPUT}"]`).getText()).toContain(
  72. "E2E_HISTORY_FIRST 保留的草稿",
  73. );
  74. expect(await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled()).toBe(true);
  75. console.info("history-first timings", {
  76. historyVisibleMs,
  77. runtimeReadyMs: Math.round(performance.now() - openedAt),
  78. });
  79. await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).click();
  80. await waitForV4TimelineContaining("HISTORY_FIRST_OK", 30_000);
  81. await $(`[data-testid="${TID_WORKSPACE_MORE_BUTTON}"]`).click();
  82. await $('[data-testid="task-restart"]').click();
  83. await browser.waitUntil(
  84. async () => {
  85. const pane = $(`[data-session-id="${HISTORY_FIRST_SESSION}"]`);
  86. return (
  87. (await pane.getAttribute("data-session-lifecycle")) === "ready" &&
  88. Number(await pane.getAttribute("data-session-generation")) >= 2
  89. );
  90. },
  91. { timeout: 60_000, timeoutMsg: "旧路径会话未能在同一工作区重启" },
  92. );
  93. await waitForV4TimelineContaining("HISTORY_FIRST_OK", 30_000);
  94. const database = new DatabaseSync(
  95. join(DEFAULT_WORKSPACE, "..", ".zcode", "cli", "db", "db.sqlite"),
  96. );
  97. try {
  98. expect(
  99. database
  100. .prepare("SELECT directory, path FROM session WHERE id = ?")
  101. .get(HISTORY_FIRST_SESSION),
  102. ).toEqual({ directory: DEFAULT_WORKSPACE + sep, path: DEFAULT_WORKSPACE + sep });
  103. } finally {
  104. database.close();
  105. }
  106. });
  107. it("HF02:快照物化失败后保留历史、草稿和滚动,双击重试复用已就绪实例", async function () {
  108. this.timeout(120_000);
  109. await Promise.all([rm(release, { force: true }), rm(entered, { force: true })]);
  110. await selectV4TaskById(HISTORY_FIRST_FAILED_SESSION);
  111. await waitForV4TimelineContaining("历史段落", 30_000);
  112. await browser.waitUntil(
  113. async () => (await readFile(entered, "utf8").catch(() => "")) === "entered",
  114. { timeout: 30_000, timeoutMsg: "失败用例的 hook 未进入屏障" },
  115. );
  116. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "E2E_HISTORY_FIRST 重试保留草稿");
  117. await scrollV4TimelineToTop();
  118. const scrollBefore = (await getV4TimelineScrollState()).scrollTop;
  119. const database = new DatabaseSync(
  120. join(DEFAULT_WORKSPACE, "..", ".zcode", "cli", "db", "db.sqlite"),
  121. );
  122. try {
  123. // 在 M0 已展示、hook 尚未完成时损坏会话分支字段,触发真实最终物化读取错误。
  124. database
  125. .prepare("UPDATE session SET revert = '{' WHERE id = ?")
  126. .run(HISTORY_FIRST_FAILED_SESSION);
  127. await writeFile(release, "release");
  128. const retry = $('[data-testid="v4-session-initialization-retry"]');
  129. await retry.waitForDisplayed({ timeout: 30_000 });
  130. await browser.saveScreenshot(join(artifacts, "history-first-retry.png"));
  131. expect(await $(`[data-testid="${TID_V4_COMPOSER_INPUT}"]`).getText()).toContain(
  132. "重试保留草稿",
  133. );
  134. expect(Math.abs((await getV4TimelineScrollState()).scrollTop - scrollBefore)).toBeLessThan(2);
  135. await waitForV4TimelineContaining(HISTORY_FIRST_TEXT);
  136. database
  137. .prepare("UPDATE session SET revert = NULL WHERE id = ?")
  138. .run(HISTORY_FIRST_FAILED_SESSION);
  139. await Promise.all([rm(release, { force: true }), rm(entered, { force: true })]);
  140. const attemptsBefore = (await readFile(join(root, "attempts"), "utf8"))
  141. .trim()
  142. .split("\n").length;
  143. const pane = $(`[data-session-id="${HISTORY_FIRST_FAILED_SESSION}"]`);
  144. const generationBefore = await pane.getAttribute("data-session-generation");
  145. // revert 损坏发生在实例已就绪后的快照物化阶段;重试应修复投影,不能重跑 hook 或替换实例。
  146. await retry.doubleClick();
  147. await browser.waitUntil(
  148. async () => await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled(),
  149. { timeout: 30_000, timeoutMsg: "重试未就绪" },
  150. );
  151. expect((await readFile(join(root, "attempts"), "utf8")).trim().split("\n").length).toBe(
  152. attemptsBefore,
  153. );
  154. expect(await pane.getAttribute("data-session-generation")).toBe(generationBefore);
  155. expect(await retry.isExisting()).toBe(false);
  156. expect(await $(`[data-testid="${TID_V4_COMPOSER_INPUT}"]`).getText()).toContain(
  157. "重试保留草稿",
  158. );
  159. expect(await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled()).toBe(true);
  160. expect(Math.abs((await getV4TimelineScrollState()).scrollTop - scrollBefore)).toBeLessThan(2);
  161. } finally {
  162. database
  163. .prepare("UPDATE session SET revert = NULL WHERE id = ?")
  164. .run(HISTORY_FIRST_FAILED_SESSION);
  165. database.close();
  166. }
  167. });
  168. });