tasksDatabaseRollback.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { DatabaseSync } from "node:sqlite";
  5. import { expect, it } from "vitest";
  6. import { AutomationRepo } from "../src/session/automationRepo.js";
  7. import { OffPeakTaskRepo } from "../src/session/offPeakTaskRepo.js";
  8. import { runTasksDatabaseMigrations } from "../src/session/tasksDatabase/migrations.js";
  9. import {
  10. rowToAutomation as oldAutomation,
  11. rowToTask as oldOffPeak,
  12. rowToRun as oldRun,
  13. } from "./fixtures/staging-backup-task-readers.mjs";
  14. it("DB109-01/09/10/13:八表内容保留,旧版读取新增/更新任务无需模型双写,再升级不补迁", async () => {
  15. const root = await mkdtemp(join(tmpdir(), "zcode-db109-rollback-"));
  16. const path = join(root, "tasks.sqlite");
  17. const automation = new AutomationRepo(path);
  18. const offPeak = new OffPeakTaskRepo(path);
  19. const db = new DatabaseSync(path);
  20. try {
  21. runTasksDatabaseMigrations(db);
  22. db.exec(`DELETE FROM tasks_schema_migration WHERE id='0002_provider_selection';
  23. INSERT INTO tasks(workspace_key,workspace_path,workspace_identity,task_id,title,created_at,updated_at,meta_json)
  24. VALUES('ssh:a:/same','/same','ssh:a:/same','session','旧会话标题',1,2,'{"unknown":"附件引用"}');
  25. INSERT INTO task_groups VALUES('group','分组','gray',1,2);
  26. INSERT INTO task_group_members VALUES('group','ssh:a:/same','/same','ssh:a:/same','session',1,1,1,2);
  27. INSERT INTO task_group_view_node_orders VALUES('task','session',1,1,2);
  28. INSERT INTO task_group_workspace_bootstraps VALUES('ssh:a:/same','group',1,2);
  29. INSERT INTO automations(automation_id,cron_expr,prompt,workspace_key,workspace_path,created_at,updated_at,model,provider,thought_level)
  30. VALUES('old','* * * * *','定时正文','ssh:a:/same','/same',1,2,'custom:proxy:old','zcode','high');
  31. INSERT INTO automation_runs(run_id,automation_id,workspace_key,created_at,updated_at,session_id) VALUES('run','old','ssh:a:/same',1,2,'session');
  32. INSERT INTO off_peak_tasks(off_peak_task_id,server_ticket_id,prompt,permission_mode,workspace_key,workspace_path,status,queued_at,created_at,updated_at,model,thought_level)
  33. VALUES('idle','ticket','闲时正文','default','ssh:a:/same','/same','queued',1,1,2,'old-model','high');`);
  34. const preservedTables = [
  35. "tasks",
  36. "task_groups",
  37. "task_group_members",
  38. "task_group_view_node_orders",
  39. "task_group_workspace_bootstraps",
  40. "automation_runs",
  41. "off_peak_tasks",
  42. ];
  43. const before = preservedTables.map((table) => db.prepare(`SELECT * FROM ${table}`).all());
  44. runTasksDatabaseMigrations(db);
  45. expect(preservedTables.map((table) => db.prepare(`SELECT * FROM ${table}`).all())).toEqual(
  46. before,
  47. );
  48. expect(
  49. oldAutomation(db.prepare("SELECT * FROM automations WHERE automation_id='old'").get()).prompt,
  50. ).toBe("定时正文");
  51. expect(oldRun(db.prepare("SELECT * FROM automation_runs").get()).sessionId).toBe("session");
  52. expect(oldOffPeak(db.prepare("SELECT * FROM off_peak_tasks").get())).toMatchObject({
  53. prompt: "闲时正文",
  54. serverTicketId: "ticket",
  55. model: "old-model",
  56. });
  57. const selection = { providerId: "proxy", modelId: "next", options: { reasoningLevel: "high" } };
  58. // 两个独立 Repo 逆序首次打开同一库,必须都在统一迁移门禁之后工作。
  59. const idle = await offPeak.create(
  60. {
  61. title: "新闲时",
  62. prompt: "新闲时正文",
  63. permissionMode: "default",
  64. workspacePath: "/new",
  65. modelSelection: selection,
  66. },
  67. { now: 20 },
  68. );
  69. const task = await automation.create(
  70. {
  71. title: "新任务",
  72. cronExpr: "* * * * *",
  73. prompt: "新定时正文",
  74. workspacePath: "/new",
  75. modelSelection: selection,
  76. },
  77. { nextRunAt: 50 },
  78. );
  79. const rawTask = db
  80. .prepare("SELECT * FROM automations WHERE automation_id=?")
  81. .get(task.automationId)!;
  82. const rawIdle = db
  83. .prepare("SELECT * FROM off_peak_tasks WHERE off_peak_task_id=?")
  84. .get(idle.offPeakTaskId)!;
  85. expect(rawTask.model).toBeNull();
  86. expect(rawIdle.model).toBeNull();
  87. expect(oldAutomation(rawTask).prompt).toBe("新定时正文");
  88. expect(oldOffPeak(rawIdle).prompt).toBe("新闲时正文");
  89. // 模拟回滚后的旧 Writer 更新,保留一次迁移账本;再升级不能拿旧选择恢复默认执行。
  90. automation.close();
  91. offPeak.close();
  92. const ledger = db.prepare("SELECT * FROM tasks_schema_migration").all();
  93. db.exec(
  94. "UPDATE automations SET model_selection=NULL,model='old-after-rollback',prompt='回滚后的正文' WHERE automation_id='old'",
  95. );
  96. expect((await automation.get("old"))?.prompt).toBe("回滚后的正文");
  97. await expect(automation.getModelSelectionForDispatch("old", "ssh:a:/same")).rejects.toThrow(
  98. "重新选择",
  99. );
  100. expect((await offPeak.get("idle"))?.serverTicketId).toBe("ticket");
  101. expect(db.prepare("SELECT * FROM tasks_schema_migration").all()).toEqual(ledger);
  102. } finally {
  103. automation.close();
  104. offPeak.close();
  105. db.close();
  106. await rm(root, { recursive: true, force: true });
  107. }
  108. });