conversation-session-history-first-migration.test.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { homedir } from "node:os";
  3. import { join } from "node:path";
  4. import { DatabaseSync } from "node:sqlite";
  5. import { TID_V4_COMPOSER_INPUT, TID_V4_COMPOSER_SEND } from "@zcode/shared/test-ids";
  6. import {
  7. DEFAULT_WORKSPACE,
  8. clearAppData,
  9. setInputValueByTestIdDom,
  10. waitForWorkspaceApp,
  11. } from "#e2e/helpers/desktop-app";
  12. import {
  13. selectV4TaskById,
  14. waitForV4TimelineContaining,
  15. waitForV4AssistantMessageContaining,
  16. } from "#e2e/helpers/v4-conversation";
  17. import {
  18. HISTORY_FIRST_SESSION,
  19. HISTORY_FIRST_TEXT,
  20. } from "#e2e/helpers/session-history-first-fixture";
  21. const root = join(DEFAULT_WORKSPACE, ".zcode", "history-first-e2e");
  22. describe("HF04:旧标题迁移与历史首帧边界", () => {
  23. after(async () => {
  24. await writeFile(join(root, "release"), "release").catch(() => undefined);
  25. await clearAppData();
  26. });
  27. it("先到的标题交接不阻塞历史,释放后只恢复一次并完成迁移", async function () {
  28. this.timeout(120_000);
  29. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 30_000);
  30. // 先观察迁移启动的 hook,再打开任务,确保不是订阅先到的普通历史场景。
  31. await browser.waitUntil(
  32. async () => (await readFile(join(root, "entered"), "utf8").catch(() => "")) === "entered",
  33. { timeout: 30_000, timeoutMsg: "旧标题交接没有启动冷恢复" },
  34. );
  35. const cli = new DatabaseSync(join(homedir(), ".zcode", "cli", "db", "db.sqlite"), {
  36. readOnly: true,
  37. });
  38. const host = new DatabaseSync(join(homedir(), ".zcode", "v2", "tasks-index.sqlite"), {
  39. readOnly: true,
  40. });
  41. try {
  42. expect(
  43. cli.prepare("SELECT title FROM session WHERE id = ?").get(HISTORY_FIRST_SESSION)?.title,
  44. ).toBe("E2E_BEFORE_TITLE_HANDOFF");
  45. expect(
  46. Number(
  47. host
  48. .prepare("SELECT handoff_pending FROM tasks WHERE task_id = ?")
  49. .get(HISTORY_FIRST_SESSION)?.handoff_pending,
  50. ) & 2,
  51. ).toBe(2);
  52. await selectV4TaskById(HISTORY_FIRST_SESSION);
  53. await waitForV4TimelineContaining(HISTORY_FIRST_TEXT, 30_000);
  54. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "E2E_HISTORY_FIRST_MIGRATION 保留草稿");
  55. expect(await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled()).toBe(false);
  56. await writeFile(join(root, "release"), "release");
  57. await browser.waitUntil(
  58. async () => await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).isEnabled(),
  59. { timeout: 30_000, timeoutMsg: "迁移场景初始化未完成" },
  60. );
  61. await browser.waitUntil(
  62. () =>
  63. cli.prepare("SELECT title FROM session WHERE id = ?").get(HISTORY_FIRST_SESSION)
  64. ?.title === "E2E_HISTORY_FIRST" &&
  65. (Number(
  66. host
  67. .prepare("SELECT handoff_pending FROM tasks WHERE task_id = ?")
  68. .get(HISTORY_FIRST_SESSION)?.handoff_pending,
  69. ) &
  70. 2) ===
  71. 0,
  72. { timeout: 30_000, timeoutMsg: "标题或旧意图位没有完成交接" },
  73. );
  74. expect(await $(`[data-testid="${TID_V4_COMPOSER_INPUT}"]`).getText()).toContain("保留草稿");
  75. expect((await readFile(join(root, "attempts"), "utf8")).trim().split("\n")).toHaveLength(1);
  76. await $(`[data-testid="${TID_V4_COMPOSER_SEND}"]`).click();
  77. await waitForV4AssistantMessageContaining("HISTORY_FIRST_MIGRATION_OK", 30_000);
  78. } finally {
  79. cli.close();
  80. host.close();
  81. }
  82. });
  83. });