conversation-session-project-upgrade-order.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { join } from "node:path";
  2. import { DatabaseSync } from "node:sqlite";
  3. import { waitForWorkspaceApp, DEFAULT_WORKSPACE } from "#e2e/helpers/desktop-app";
  4. import { loginWithApiKey } from "#e2e/helpers/conversation-session";
  5. import {
  6. SAVED_PROJECT_NAMES,
  7. MIGRATED_REMOTE_NAME,
  8. LEGACY_PINNED_TASK_IDS,
  9. CURRENT_IMPORTED_TASK_ID,
  10. } from "#e2e/helpers/project-upgrade-order-fixture";
  11. describe("虚拟项目升级顺序", () => {
  12. it("VP28:仅迁移旧设置保留的项目,历史任务不建项目,刷新保留", async function () {
  13. this.timeout(90_000);
  14. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45_000);
  15. const expected = ["ZCodeProject", MIGRATED_REMOTE_NAME, ...SAVED_PROJECT_NAMES];
  16. const readNames = () =>
  17. browser.execute(() =>
  18. [...document.querySelectorAll('[data-testid="project-section"] [data-project-name]')].map(
  19. (node) => node.getAttribute("data-project-name"),
  20. ),
  21. );
  22. await $('[data-project-name="empty-saved"]').waitForExist({ timeout: 30_000 });
  23. expect(await readNames()).toEqual(expected);
  24. for (const id of LEGACY_PINNED_TASK_IDS)
  25. expect(await $(`[data-testid="task-item-${id}"]`).isExisting()).toBe(false);
  26. await expect($(`[data-testid="task-item-${CURRENT_IMPORTED_TASK_ID}"]`)).toHaveText(
  27. expect.stringContaining("E2E_CURRENT_IMPORTED"),
  28. );
  29. const home = process.env.ZCODE_E2E_HOME_DIR;
  30. if (!home) throw new Error("isolated E2E HOME required");
  31. const db = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"), { readOnly: true });
  32. try {
  33. expect(db.prepare("SELECT id FROM schema_migration").all()).toEqual([
  34. { id: "0005_projects" },
  35. { id: "0006_default_workspace_tasks" },
  36. { id: "0007_initial_workspace_order" },
  37. { id: "0008_remote_entry_deleted_cache" },
  38. // 失败原因:新增 tasks_index 迁移(0009、两个 0010)后,这份完整清单没有同步。
  39. { id: "0009_session_creation_commands" },
  40. { id: "0010_remote_task_ownership" },
  41. { id: "0010_task_worktree_root" },
  42. ]);
  43. expect(
  44. db
  45. .prepare(
  46. "SELECT project_id,workspace_path,archived FROM tasks WHERE task_id='e2e-system-default'",
  47. )
  48. .get(),
  49. ).toEqual({
  50. project_id: null,
  51. workspace_path: join(home, ".zcode/workspace/default"),
  52. archived: 0,
  53. });
  54. expect(
  55. await $(
  56. '[data-testid="conversation-section"] [data-testid="task-item-e2e-system-default"]',
  57. ).getText(),
  58. ).toContain("E2E_SYSTEM_DEFAULT");
  59. for (const [name, archived, deleted] of [
  60. ["a-history", 0, 0],
  61. ["archived-history", 1, 0],
  62. ["deleted-history", 1, 1],
  63. ] as const) {
  64. expect(
  65. db
  66. .prepare(
  67. "SELECT project_id,workspace_path,title,archived,deleted FROM tasks WHERE task_id=?",
  68. )
  69. .get(`e2e-order-${name}`),
  70. ).toEqual({
  71. project_id: null,
  72. workspace_path: join(home, "ZCodeProject", ".zcode-e2e", "project-upgrade-order", name),
  73. title: `E2E_ORDER_${name}`,
  74. archived,
  75. deleted,
  76. });
  77. }
  78. await expect(
  79. $('[data-testid="conversation-section"] [data-testid="task-item-e2e-order-a-history"]'),
  80. ).toHaveText(expect.stringContaining("E2E_ORDER_a-history"));
  81. expect(await $('[data-testid="task-item-e2e-order-archived-history"]').isExisting()).toBe(
  82. false,
  83. );
  84. expect(await $('[data-testid="task-item-e2e-order-deleted-history"]').isExisting()).toBe(
  85. false,
  86. );
  87. const upstream = db
  88. .prepare("SELECT id,time_applied FROM tasks_schema_migration ORDER BY id")
  89. .all();
  90. expect(upstream.map((row) => row.id)).toEqual([
  91. "0001_adopt_task_schema",
  92. "0002_provider_selection",
  93. "0003_official_glm_selection",
  94. "0004_project_execution_scopes",
  95. "0006_task_group_admissions",
  96. // 同上:tasks 库新增 0007 迁移后清单没有同步。
  97. "0007_task_permanent_deletion",
  98. ]);
  99. const projectAppliedAt = db
  100. .prepare("SELECT applied_at FROM schema_migration WHERE id='0005_projects'")
  101. .get()!.applied_at;
  102. expect(upstream.every((row) => Number(row.time_applied) <= Number(projectAppliedAt))).toBe(
  103. true,
  104. );
  105. expect(
  106. db
  107. .prepare("SELECT name FROM projects WHERE deleted=0 ORDER BY position")
  108. .all()
  109. .map((row) => row.name),
  110. ).toEqual(expected.filter((name) => name !== MIGRATED_REMOTE_NAME));
  111. expect(
  112. db.prepare("SELECT cached_name,initial_position FROM remote_project_entries").all(),
  113. ).toEqual([{ cached_name: MIGRATED_REMOTE_NAME, initial_position: 1 }]);
  114. } finally {
  115. db.close();
  116. }
  117. await browser.saveScreenshot(
  118. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-upgrade-order.png"),
  119. );
  120. await browser.refresh();
  121. try {
  122. // 隔离夹具无账号登录态;刷新可能回欢迎页,复用真实登录步骤后再检查顺序。
  123. const login = $('[data-testid="login-use-api-key-button"]');
  124. await browser.waitUntil(
  125. async () =>
  126. (await login.isExisting()) || (await $('[data-project-name="empty-saved"]').isExisting()),
  127. { timeout: 45_000 },
  128. );
  129. if (await login.isExisting()) await loginWithApiKey();
  130. await $('[data-project-name="empty-saved"]').waitForExist({ timeout: 45_000 });
  131. } catch (error) {
  132. await browser.saveScreenshot(
  133. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-upgrade-refresh-failed.png"),
  134. );
  135. process.stderr.write(
  136. `VP28 refresh state: ${JSON.stringify(await browser.execute(() => ({ text: document.body.innerText.slice(0, 2000), url: location.href })))}\n`,
  137. );
  138. throw error;
  139. }
  140. expect(await readNames()).toEqual(expected);
  141. // 项目节点先出现不代表任务行已可见,刷新后等待用户能读到的标题。
  142. await expect(
  143. $('[data-testid="conversation-section"] [data-testid="task-item-e2e-order-a-history"]'),
  144. ).toHaveText(expect.stringContaining("E2E_ORDER_a-history"));
  145. await browser.saveScreenshot(
  146. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "project-upgrade-refreshed.png"),
  147. );
  148. });
  149. it("VP28:旧后端置顶行保持隐藏,当前后端导入会话仍可取消置顶和归档", async () => {
  150. for (const id of LEGACY_PINNED_TASK_IDS)
  151. expect(await $(`[data-testid="task-item-${id}"]`).isExisting()).toBe(false);
  152. const row = $(`[data-testid="task-item-${CURRENT_IMPORTED_TASK_ID}"]`);
  153. await row.$('button[aria-label="取消置顶任务"], button[aria-label="Unpin task"]').click();
  154. const unpinned = $(
  155. `[data-testid="conversation-section"] [data-testid="task-item-${CURRENT_IMPORTED_TASK_ID}"]`,
  156. );
  157. await unpinned.waitForExist();
  158. // 取消置顶会把行移到任务区,需跟随新行 hover 才展示桌面归档操作。
  159. await unpinned.moveTo();
  160. await unpinned.$('button[data-testid^="task-archive-"]').click();
  161. await $(`[data-archive-confirming-task-id="${CURRENT_IMPORTED_TASK_ID}"]`).waitForExist();
  162. await unpinned.$('button[data-testid^="task-archive-"]').click();
  163. await unpinned.waitForExist({ reverse: true });
  164. const db = new DatabaseSync(
  165. join(process.env.ZCODE_E2E_HOME_DIR!, ".zcode/v2/tasks-index.sqlite"),
  166. { readOnly: true },
  167. );
  168. try {
  169. for (const id of LEGACY_PINNED_TASK_IDS)
  170. expect(
  171. db
  172. .prepare(
  173. "SELECT provider,pinned,archived,created_at,cli_created_at FROM tasks WHERE task_id=?",
  174. )
  175. .get(id),
  176. ).toEqual({
  177. provider: "claude",
  178. pinned: 1,
  179. archived: 0,
  180. created_at: 10,
  181. cli_created_at: 10,
  182. });
  183. expect(
  184. db
  185. .prepare("SELECT provider,migration_source,pinned,archived FROM tasks WHERE task_id=?")
  186. .get(CURRENT_IMPORTED_TASK_ID),
  187. ).toEqual({ provider: "glm", migration_source: "claudeCode", pinned: 0, archived: 1 });
  188. } finally {
  189. db.close();
  190. }
  191. });
  192. });