archive-automations.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { join } from "node:path";
  2. import { DatabaseSync } from "node:sqlite";
  3. import { AutomationRepo } from "@zcode/services/node";
  4. import type { ZCodeAutomation, ZCodeAutomationCreateParams } from "@zcode/shared";
  5. /** 使用真实任务库建立关联;日程远于验收窗口,不让后台 tick 改写对照数据。 */
  6. export async function seedArchiveAutomations(target: { workspacePath?: string; taskId: string }) {
  7. const home = process.env.ZCODE_E2E_HOME_DIR;
  8. if (!home) throw new Error("archive automation fixture requires isolated E2E HOME");
  9. const database = new DatabaseSync(join(home, ".zcode/v2/tasks-index.sqlite"), { readOnly: true });
  10. let workspacePath = target.workspacePath;
  11. try {
  12. if (!workspacePath) {
  13. const rows = database
  14. .prepare("SELECT workspace_path FROM tasks WHERE task_id=?")
  15. .all(target.taskId);
  16. expect(rows).toHaveLength(1);
  17. workspacePath = String(rows[0]!.workspace_path);
  18. }
  19. } finally {
  20. database.close();
  21. }
  22. const repo = new AutomationRepo(join(home, ".zcode/v2/tasks-index.sqlite"));
  23. const records: ZCodeAutomation[] = [];
  24. try {
  25. for (const kind of [
  26. "first",
  27. "second",
  28. "paused",
  29. "completed",
  30. "failed",
  31. "unbound",
  32. "other-identity",
  33. ]) {
  34. const input: ZCodeAutomationCreateParams = {
  35. title: `E2E_ARCHIVE_${kind}`,
  36. workspacePath,
  37. targetTaskId: kind === "unbound" ? undefined : target.taskId,
  38. workspaceIdentity: kind === "other-identity" ? "e2e:other-workspace" : undefined,
  39. cronExpr: "0 9 * * *",
  40. prompt: "E2E_ARCHIVE_SCHEDULE",
  41. recurring: true,
  42. };
  43. const record = await repo.create(input, { nextRunAt: Date.now() + 86_400_000 });
  44. if (kind === "paused" || kind === "completed" || kind === "failed")
  45. await repo.update(record.automationId, {}, { lifecycleStatus: kind });
  46. records.push((await repo.get(record.automationId))!);
  47. }
  48. } finally {
  49. repo.close();
  50. }
  51. return {
  52. async assertPaused() {
  53. try {
  54. for (const [index, before] of records.entries()) {
  55. const after = await repo.get(before.automationId);
  56. expect(after).not.toBeNull();
  57. const { updatedAt: _beforeTime, ...expected } = before;
  58. const { updatedAt: _afterTime, ...actual } = after!;
  59. expect(actual).toEqual(
  60. index < 2 ? { ...expected, enabled: false, lifecycleStatus: "paused" } : expected,
  61. );
  62. }
  63. } finally {
  64. repo.close();
  65. }
  66. },
  67. };
  68. }