conversation-session-delete-all-archived-tasks.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { basename, join } from "node:path";
  2. import { DatabaseSync } from "node:sqlite";
  3. import { DEFAULT_WORKSPACE, clearAppData, waitForWorkspaceApp } from "#e2e/helpers/desktop-app";
  4. import { restartIntoWorkspacePreservingProfile } from "#e2e/helpers/model-provider-restart-runtime";
  5. const home = process.env.ZCODE_E2E_HOME_DIR!;
  6. const prefix = "e2e-bulk-archive-";
  7. const control = `${prefix}active`;
  8. describe("TSL39:一键删除全部归档任务", () => {
  9. after(async () => {
  10. await clearAppData();
  11. });
  12. it("取消不写入,确认删除折叠组的全部 25 条并保留普通任务和 CLI 记录", async function () {
  13. this.timeout(120000);
  14. if (!home || !basename(home).startsWith(".e2e-home-"))
  15. throw new Error("isolated E2E HOME required");
  16. await waitForWorkspaceApp(DEFAULT_WORKSPACE, 45000);
  17. // 旧夹具依赖已移除的 runner 开关,且只有 Host 摘要,无法验证当前 CLI 删除命令。
  18. // 在进程退出后准备两库完整历史,冷启读取;执行阶段只经真实 UI 发送删除。
  19. await restartIntoWorkspacePreservingProfile(DEFAULT_WORKSPACE, {
  20. afterElectronProcessExit: async () => {
  21. const db = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"));
  22. const cli = new DatabaseSync(join(home, ".zcode/cli/db/db.sqlite"));
  23. try {
  24. const project = db
  25. .prepare("SELECT project_id FROM project_roots WHERE root_path=? LIMIT 1")
  26. .get(DEFAULT_WORKSPACE) as { project_id: string };
  27. const now = Date.now();
  28. for (let i = 0; i < 26; i++) {
  29. const id = i === 25 ? control : `${prefix}${i}`;
  30. const title = `E2E_BULK_ARCHIVE_${i}`,
  31. archived = i === 25 ? 0 : 1;
  32. cli
  33. .prepare(
  34. "INSERT INTO session(id,project_id,slug,directory,title,version,time_created,time_updated,title_source,last_outcome) VALUES(?,?,?,?,?,'1',?,?,'custom','completed')",
  35. )
  36. .run(id, "e2e-bulk-cli-scope", id, DEFAULT_WORKSPACE, title, now, now);
  37. db.prepare(
  38. "INSERT INTO tasks(workspace_key,workspace_path,task_id,title,provider,created_at,updated_at,host_id,root_path,project_id,archived,cli_title,cli_title_source,cli_created_at,cli_updated_at,cli_last_outcome,cli_deleted,meta_json) VALUES(?,?,?,?,'glm',?,?,'local',?,?,?,?, 'custom',?,?,'completed',0,?)",
  39. ).run(
  40. DEFAULT_WORKSPACE,
  41. DEFAULT_WORKSPACE,
  42. id,
  43. title,
  44. now,
  45. now,
  46. DEFAULT_WORKSPACE,
  47. project.project_id,
  48. archived,
  49. title,
  50. now,
  51. now,
  52. JSON.stringify({
  53. taskId: id,
  54. traceId: id,
  55. title,
  56. workspacePath: DEFAULT_WORKSPACE,
  57. createdAt: now,
  58. updatedAt: now,
  59. mode: "build",
  60. archived: Boolean(archived),
  61. }),
  62. );
  63. }
  64. } finally {
  65. db.close();
  66. cli.close();
  67. }
  68. },
  69. });
  70. expect(readCounts()).toEqual({
  71. archived: 25,
  72. deleted: 0,
  73. active: 1,
  74. cliRows: 26,
  75. cliDeleted: 0,
  76. });
  77. await $('[data-testid="task-settings-button"]').click();
  78. await $('[data-testid="settings-section-nav-archives"]').click();
  79. await $('[data-testid="archive-management"]').waitForDisplayed();
  80. // 分组收起没有行 DOM;批量目标必须来自完整权威集合,不能只删除已渲染行。
  81. expect(await $$('[data-testid="archive-task"]').length).toBe(0);
  82. const removeAll = $('[data-testid="archive-delete-all"]');
  83. await removeAll.click();
  84. const confirmation = $('[data-confirm-intent="archive-management-delete"]');
  85. await expect(confirmation).toHaveText(expect.stringContaining("25"));
  86. await browser.keys("Escape");
  87. await confirmation.waitForExist({ reverse: true });
  88. expect(readCounts()).toEqual({
  89. archived: 25,
  90. deleted: 0,
  91. active: 1,
  92. cliRows: 26,
  93. cliDeleted: 0,
  94. });
  95. await removeAll.click();
  96. await expect(confirmation).toHaveText(expect.stringContaining("25"));
  97. await $('[data-testid="confirm-dialog-confirm"]').click();
  98. await browser.waitUntil(() => readCounts().deleted === 25, { timeout: 30000 });
  99. expect(readCounts()).toEqual({
  100. archived: 0,
  101. deleted: 25,
  102. active: 1,
  103. cliRows: 26,
  104. cliDeleted: 25,
  105. });
  106. await expect(removeAll).toBeDisabled();
  107. expect(await $$('[data-testid="archive-group"]').length).toBe(0);
  108. await browser.saveScreenshot(
  109. join(process.env.ZCODE_E2E_ARTIFACT_DIR!, "delete-all-archived-tasks.png"),
  110. );
  111. });
  112. });
  113. function readCounts() {
  114. const db = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"), { readOnly: true });
  115. const cli = new DatabaseSync(join(home, ".zcode/cli/db/db.sqlite"), { readOnly: true });
  116. try {
  117. return {
  118. ...(db
  119. .prepare(
  120. "SELECT sum(archived=1 AND cli_deleted=0) AS archived, sum(cli_deleted=1) AS deleted, sum(task_id=? AND archived=0 AND cli_deleted=0) AS active FROM tasks WHERE task_id LIKE ?",
  121. )
  122. .get(control, `${prefix}%`) as { archived: number; deleted: number; active: number }),
  123. ...(cli
  124. .prepare(
  125. "SELECT count(*) AS cliRows, sum(time_deleted IS NOT NULL) AS cliDeleted FROM session WHERE id LIKE ?",
  126. )
  127. .get(`${prefix}%`) as { cliRows: number; cliDeleted: number }),
  128. };
  129. } finally {
  130. db.close();
  131. cli.close();
  132. }
  133. }