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

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