task-list-row-authority-fixture.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { join } from "node:path";
  2. import { DatabaseSync } from "node:sqlite";
  3. import {
  4. PROVIDER_READINESS_HISTORY_SESSION_ID,
  5. seedProviderReadinessHistoryFixture,
  6. } from "./provider-readiness-history-fixture.js";
  7. export const TASK_INDEX_ROW_AUTHORITY_TOTAL = 25;
  8. export const TASK_INDEX_ROW_AUTHORITY_MISSING_SUMMARY_ID = "sess_e2e_task_index_only_24";
  9. export const TASK_INDEX_ROW_AUTHORITY_MISSING_SUMMARY_TITLE =
  10. "E2E_TASK_INDEX_ONLY_24 无摘要持久任务";
  11. export const TASK_INDEX_ROW_AUTHORITY_TASK_IDS = [
  12. PROVIDER_READINESS_HISTORY_SESSION_ID,
  13. ...Array.from(
  14. { length: TASK_INDEX_ROW_AUTHORITY_TOTAL - 1 },
  15. (_, index) => `sess_e2e_task_index_only_${String(index + 1).padStart(2, "0")}`,
  16. ),
  17. ];
  18. interface SeedTaskIndexRowAuthorityFixtureOptions {
  19. cliEntryPoint: string;
  20. e2eHomeDir: string;
  21. workspacePath: string;
  22. }
  23. /**
  24. * 预置 25 个 tasks-index 行,但只让其中 1 个拥有真实 CLI session summary。
  25. *
  26. * Bug 根因:旧列表把 sessions-index snapshot 当成左表,摘要缺失的持久 task 会在冷启动时
  27. * 消失。这里用真 SQLite 构造 1 个有摘要、24 个无摘要的等价类,验证行集合只由 tasks-index 决定。
  28. */
  29. export async function seedTaskIndexRowAuthorityFixture(
  30. options: SeedTaskIndexRowAuthorityFixtureOptions,
  31. ): Promise<void> {
  32. await seedProviderReadinessHistoryFixture(options);
  33. // Node 当前内置 SQLite 只提供 DatabaseSync;fixture 在 Electron/Host 启动前执行,
  34. // 没有与产品进程争用句柄,写完立即关闭。
  35. const database = new DatabaseSync(join(options.e2eHomeDir, ".zcode", "v2", "tasks-index.sqlite"));
  36. const insert = database.prepare(`
  37. INSERT INTO tasks (
  38. workspace_key, workspace_path, workspace_identity, task_id, title, task_status,
  39. provider, mode, model, migration_source, forked_from_task_id, cron_automation_id,
  40. created_at, updated_at, unread_at, pinned, archived, deleted, title_overridden,
  41. searchable_text, meta_json
  42. ) VALUES (?, ?, null, ?, ?, ?, 'glm', 'build', 'missing-model', null, null,
  43. null, ?, ?, null, 0, 0, 0, 1, ?, ?)
  44. `);
  45. const baseTimestamp = Date.now() - 120_000;
  46. database.exec("begin immediate");
  47. try {
  48. for (let index = 1; index < TASK_INDEX_ROW_AUTHORITY_TOTAL; index += 1) {
  49. const taskId = TASK_INDEX_ROW_AUTHORITY_TASK_IDS[index];
  50. if (!taskId) {
  51. throw new Error(`task row authority fixture 缺少第 ${index} 个 taskId`);
  52. }
  53. const isMissingSummarySentinel = taskId === TASK_INDEX_ROW_AUTHORITY_MISSING_SUMMARY_ID;
  54. const title = isMissingSummarySentinel
  55. ? TASK_INDEX_ROW_AUTHORITY_MISSING_SUMMARY_TITLE
  56. : `E2E_TASK_INDEX_ONLY_${String(index).padStart(2, "0")} 无摘要持久任务`;
  57. const status = isMissingSummarySentinel ? "running" : "completed";
  58. const createdAt = baseTimestamp - index * 1_000;
  59. const updatedAt = createdAt + 500;
  60. const meta = {
  61. taskId,
  62. traceId: `trace_${taskId}`,
  63. title,
  64. titleOverridden: true,
  65. workspacePath: options.workspacePath,
  66. createdAt,
  67. updatedAt,
  68. mode: "build",
  69. model: "missing-model",
  70. provider: "glm",
  71. status,
  72. };
  73. insert.run(
  74. options.workspacePath,
  75. options.workspacePath,
  76. taskId,
  77. title,
  78. status,
  79. createdAt,
  80. updatedAt,
  81. title,
  82. JSON.stringify(meta),
  83. );
  84. }
  85. database.exec("commit");
  86. } catch (error) {
  87. database.exec("rollback");
  88. throw error;
  89. } finally {
  90. database.close();
  91. }
  92. }