cronRunLifecycle.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. recordCronRunOutcomeBestEffort,
  4. startManualClaimHeartbeat,
  5. settleCronRunTerminalOutcome,
  6. settleManualDispatchFailureBestEffort,
  7. } from "../src/host/cronRunLifecycle.js";
  8. function createRepo() {
  9. return {
  10. ensureRunClaimed: vi.fn(async () => {}),
  11. markRunOutcome: vi.fn(async () => {}),
  12. markRunDispatch: vi.fn(async () => {}),
  13. touchManualClaim: vi.fn(async () => {}),
  14. releaseManualClaim: vi.fn(async () => {}),
  15. };
  16. }
  17. const identity = {
  18. automationId: "automation-1",
  19. runId: "automation-1:manual:1",
  20. workspaceKey: "ssh://host/workspace",
  21. scheduledAt: 1,
  22. trigger: "manual" as const,
  23. };
  24. describe("cron run lifecycle", () => {
  25. it("running/queued 阶段只回写 outcome,不释放 manual claim", async () => {
  26. const repo = createRepo();
  27. await recordCronRunOutcomeBestEffort({
  28. ...identity,
  29. repo,
  30. outcome: "running",
  31. logWarn: vi.fn(),
  32. });
  33. expect(repo.markRunOutcome).toHaveBeenCalledWith(identity.runId, "running", undefined);
  34. expect(repo.releaseManualClaim).not.toHaveBeenCalled();
  35. });
  36. it("manual turn 终态后才按 workspaceKey 释放 claim", async () => {
  37. const repo = createRepo();
  38. await settleCronRunTerminalOutcome({
  39. ...identity,
  40. repo,
  41. outcome: "succeeded",
  42. logWarn: vi.fn(),
  43. });
  44. expect(repo.markRunOutcome).toHaveBeenCalledWith(identity.runId, "succeeded", undefined);
  45. expect(repo.releaseManualClaim).toHaveBeenCalledWith(
  46. identity.automationId,
  47. identity.workspaceKey,
  48. );
  49. expect(repo.markRunOutcome.mock.invocationCallOrder[0]).toBeLessThan(
  50. repo.releaseManualClaim.mock.invocationCallOrder[0]!,
  51. );
  52. });
  53. it("scheduled turn 不释放 manual claim,释放失败也不覆盖终态", async () => {
  54. const repo = createRepo();
  55. await settleCronRunTerminalOutcome({
  56. ...identity,
  57. trigger: "schedule",
  58. repo,
  59. outcome: "failed",
  60. error: "turn failed",
  61. logWarn: vi.fn(),
  62. });
  63. expect(repo.releaseManualClaim).not.toHaveBeenCalled();
  64. repo.releaseManualClaim.mockRejectedValueOnce(new Error("sqlite busy"));
  65. const logWarn = vi.fn();
  66. await expect(
  67. settleCronRunTerminalOutcome({
  68. ...identity,
  69. repo,
  70. outcome: "stopped",
  71. logWarn,
  72. }),
  73. ).resolves.toBeUndefined();
  74. expect(logWarn).toHaveBeenCalledWith(
  75. expect.stringContaining("释放 manual automation claim 失败"),
  76. expect.any(Error),
  77. );
  78. });
  79. it("派发失败清理异常不抛出,调用方可保留原始 dispatch error", async () => {
  80. const repo = createRepo();
  81. repo.markRunDispatch.mockRejectedValueOnce(new Error("write failed"));
  82. repo.releaseManualClaim.mockRejectedValueOnce(new Error("release failed"));
  83. const logWarn = vi.fn();
  84. await expect(
  85. settleManualDispatchFailureBestEffort({
  86. ...identity,
  87. repo,
  88. dispatchError: new Error("original dispatch failed"),
  89. logWarn,
  90. }),
  91. ).resolves.toBeUndefined();
  92. expect(repo.markRunDispatch).toHaveBeenCalledWith({
  93. runId: identity.runId,
  94. dispatchStatus: "failed_to_dispatch",
  95. error: "original dispatch failed",
  96. });
  97. expect(logWarn).toHaveBeenCalledTimes(2);
  98. });
  99. it("manual run 在 queue/running 期间续租,dispose 后停止", async () => {
  100. vi.useFakeTimers();
  101. const repo = createRepo();
  102. try {
  103. const heartbeat = startManualClaimHeartbeat({
  104. ...identity,
  105. repo,
  106. logWarn: vi.fn(),
  107. intervalMs: 100,
  108. });
  109. await vi.advanceTimersByTimeAsync(250);
  110. expect(repo.touchManualClaim).toHaveBeenCalledTimes(2);
  111. expect(repo.touchManualClaim).toHaveBeenCalledWith(
  112. identity.automationId,
  113. identity.workspaceKey,
  114. );
  115. heartbeat.dispose();
  116. await vi.advanceTimersByTimeAsync(200);
  117. expect(repo.touchManualClaim).toHaveBeenCalledTimes(2);
  118. } finally {
  119. vi.useRealTimers();
  120. }
  121. });
  122. });