desktopCronSchedulerManualClaimRelease.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. releaseManualClaimForSettledRun,
  4. settleManualClaimForDispatchResult,
  5. } from "../src/scheduler/manualClaimRelease.js";
  6. describe("desktop cron scheduler manual claim release", () => {
  7. it("host ACK 保留 manual claim,派发失败才释放", async () => {
  8. const repo = {
  9. get: vi.fn(async () => ({ workspaceKey: "fallback-workspace" })),
  10. getRun: vi.fn(async () => ({ workspaceKey: "run-workspace" })),
  11. releaseManualClaim: vi.fn(async () => {}),
  12. };
  13. const params = {
  14. repo,
  15. automationId: "automation-1",
  16. runId: "automation-1:manual:recovered",
  17. workspaceKey: "run-workspace",
  18. logError: vi.fn(),
  19. };
  20. await settleManualClaimForDispatchResult({ ...params, ok: true });
  21. expect(repo.releaseManualClaim).not.toHaveBeenCalled();
  22. expect(repo.getRun).not.toHaveBeenCalled();
  23. expect(repo.get).not.toHaveBeenCalled();
  24. await settleManualClaimForDispatchResult({ ...params, ok: false });
  25. expect(repo.releaseManualClaim).toHaveBeenCalledTimes(1);
  26. expect(repo.releaseManualClaim).toHaveBeenCalledWith(
  27. "automation-1",
  28. "run-workspace",
  29. );
  30. });
  31. it("inFlight 丢失时从 run 台账恢复 workspaceKey 并释放 manual claim", async () => {
  32. const repo = {
  33. get: vi.fn(async () => ({ workspaceKey: "fallback-workspace" })),
  34. getRun: vi.fn(async () => ({ workspaceKey: "run-workspace" })),
  35. releaseManualClaim: vi.fn(async () => {}),
  36. };
  37. const logError = vi.fn();
  38. await releaseManualClaimForSettledRun({
  39. repo,
  40. automationId: "automation-1",
  41. runId: "automation-1:manual:late",
  42. logError,
  43. });
  44. expect(repo.getRun).toHaveBeenCalledWith("automation-1:manual:late");
  45. expect(repo.get).not.toHaveBeenCalled();
  46. expect(repo.releaseManualClaim).toHaveBeenCalledWith("automation-1", "run-workspace");
  47. expect(logError).not.toHaveBeenCalled();
  48. });
  49. it("run 台账缺失时回退 automation workspaceKey,仍失败则打 error", async () => {
  50. const repo = {
  51. get: vi.fn(async () => ({ workspaceKey: "automation-workspace" })),
  52. getRun: vi.fn(async () => null),
  53. releaseManualClaim: vi.fn(async () => {}),
  54. };
  55. const logError = vi.fn();
  56. await releaseManualClaimForSettledRun({
  57. repo,
  58. automationId: "automation-1",
  59. runId: "automation-1:manual:late",
  60. logError,
  61. });
  62. expect(repo.releaseManualClaim).toHaveBeenCalledWith(
  63. "automation-1",
  64. "automation-workspace",
  65. );
  66. expect(logError).not.toHaveBeenCalled();
  67. repo.get.mockResolvedValueOnce(null);
  68. await releaseManualClaimForSettledRun({
  69. repo,
  70. automationId: "automation-missing",
  71. runId: "automation-missing:manual:late",
  72. logError,
  73. });
  74. expect(logError).toHaveBeenCalledWith(
  75. "manual claim release skipped: workspaceKey missing automation=automation-missing runId=automation-missing:manual:late",
  76. );
  77. });
  78. });