|
| 1 | +import { join } from "node:path"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { validateCronAddParams } from "../../packages/gateway-protocol/src/index.js"; |
| 4 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 5 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 6 | +import { installClawCronJobs, readClawCronRefs } from "./cron.js"; |
| 7 | +import { buildClawAddPlan } from "./lifecycle.js"; |
| 8 | +import { parseClawManifest } from "./schema.js"; |
| 9 | +import type { ClawSourceIdentity } from "./types.js"; |
| 10 | + |
| 11 | +afterEach(() => closeOpenClawStateDatabaseForTest()); |
| 12 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 13 | + |
| 14 | +async function fixture() { |
| 15 | + const root = tempDirs.make("openclaw-claw-cron-"); |
| 16 | + const parsed = parseClawManifest({ |
| 17 | + schemaVersion: 1, |
| 18 | + agent: { id: "worker" }, |
| 19 | + cronJobs: [ |
| 20 | + { |
| 21 | + id: "daily-report", |
| 22 | + name: "Daily report", |
| 23 | + schedule: { cron: "0 9 * * *", timezone: "UTC" }, |
| 24 | + session: "main", |
| 25 | + message: "Prepare the report", |
| 26 | + delivery: { mode: "announce", channel: "last" }, |
| 27 | + }, |
| 28 | + ], |
| 29 | + }); |
| 30 | + if (!parsed.ok) { |
| 31 | + throw new Error(JSON.stringify(parsed.diagnostics)); |
| 32 | + } |
| 33 | + const source: ClawSourceIdentity = { |
| 34 | + kind: "package", |
| 35 | + name: "@acme/worker", |
| 36 | + version: "1.0.0", |
| 37 | + packageRoot: root, |
| 38 | + manifestPath: join(root, "openclaw.claw.json"), |
| 39 | + integrityKind: "artifact", |
| 40 | + integrity: "sha256:manifest", |
| 41 | + byteLength: 100, |
| 42 | + }; |
| 43 | + const plan = await buildClawAddPlan({ |
| 44 | + manifest: parsed.manifest, |
| 45 | + source, |
| 46 | + context: { workspace: join(root, "workspace"), agentId: "worker-two" }, |
| 47 | + }); |
| 48 | + return { root, plan, env: { OPENCLAW_STATE_DIR: join(root, "state") } }; |
| 49 | +} |
| 50 | + |
| 51 | +describe("installClawCronJobs", () => { |
| 52 | + it("pins declarations and execution to the final agent id", async () => { |
| 53 | + const current = await fixture(); |
| 54 | + const add = vi.fn().mockResolvedValue({ id: "scheduler-123" }); |
| 55 | + |
| 56 | + const refs = await installClawCronJobs(current.plan, { |
| 57 | + env: current.env, |
| 58 | + gateway: { add }, |
| 59 | + nowMs: 42, |
| 60 | + }); |
| 61 | + |
| 62 | + expect(add).toHaveBeenCalledWith({ |
| 63 | + name: "Daily report", |
| 64 | + declarationKey: "claw:worker-two:daily-report", |
| 65 | + displayName: "Daily report", |
| 66 | + owner: { agentId: "worker-two" }, |
| 67 | + enabled: true, |
| 68 | + agentId: "worker-two", |
| 69 | + schedule: { kind: "cron", expr: "0 9 * * *", tz: "UTC" }, |
| 70 | + sessionTarget: "session:agent:worker-two:main", |
| 71 | + wakeMode: "now", |
| 72 | + payload: { kind: "agentTurn", message: "Prepare the report" }, |
| 73 | + delivery: { mode: "announce", channel: "last" }, |
| 74 | + }); |
| 75 | + expect(validateCronAddParams(add.mock.calls[0]?.[0])).toBe(true); |
| 76 | + expect(refs).toMatchObject([ |
| 77 | + { |
| 78 | + schemaVersion: "openclaw.clawCronRef.v1", |
| 79 | + agentId: "worker-two", |
| 80 | + manifestId: "daily-report", |
| 81 | + schedulerJobId: "scheduler-123", |
| 82 | + status: "complete", |
| 83 | + }, |
| 84 | + ]); |
| 85 | + expect(readClawCronRefs("worker-two", { env: current.env })).toEqual(refs); |
| 86 | + }); |
| 87 | + |
| 88 | + it("accepts declaration convergence results from cron.add", async () => { |
| 89 | + const current = await fixture(); |
| 90 | + |
| 91 | + const refs = await installClawCronJobs(current.plan, { |
| 92 | + env: current.env, |
| 93 | + gateway: { add: vi.fn().mockResolvedValue({ created: false, job: { id: "existing-1" } }) }, |
| 94 | + }); |
| 95 | + |
| 96 | + expect(refs[0]).toMatchObject({ schedulerJobId: "existing-1", status: "complete" }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("preserves an ambiguous pending reference when cron.add fails", async () => { |
| 100 | + const current = await fixture(); |
| 101 | + |
| 102 | + await expect( |
| 103 | + installClawCronJobs(current.plan, { |
| 104 | + env: current.env, |
| 105 | + gateway: { add: vi.fn().mockRejectedValue(new Error("gateway unavailable")) }, |
| 106 | + }), |
| 107 | + ).rejects.toMatchObject({ |
| 108 | + code: "cron_install_failed", |
| 109 | + cronJobs: [{ manifestId: "daily-report", status: "pending", error: "gateway unavailable" }], |
| 110 | + }); |
| 111 | + expect(readClawCronRefs("worker-two", { env: current.env })).toMatchObject([ |
| 112 | + { manifestId: "daily-report", status: "pending", error: "gateway unavailable" }, |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("reconciles a response-lost retry by declaration key", async () => { |
| 117 | + const current = await fixture(); |
| 118 | + const add = vi.fn().mockRejectedValue(new Error("response lost")); |
| 119 | + |
| 120 | + await expect( |
| 121 | + installClawCronJobs(current.plan, { |
| 122 | + env: current.env, |
| 123 | + gateway: { add }, |
| 124 | + }), |
| 125 | + ).rejects.toMatchObject({ |
| 126 | + code: "cron_install_failed", |
| 127 | + cronJobs: [{ status: "pending" }], |
| 128 | + }); |
| 129 | + |
| 130 | + const refs = await installClawCronJobs(current.plan, { |
| 131 | + env: current.env, |
| 132 | + gateway: { |
| 133 | + add, |
| 134 | + list: vi.fn().mockResolvedValue({ |
| 135 | + jobs: [ |
| 136 | + { |
| 137 | + id: "scheduler-after-lost-response", |
| 138 | + declarationKey: "claw:worker-two:daily-report", |
| 139 | + }, |
| 140 | + ], |
| 141 | + }), |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + expect(add).toHaveBeenCalledTimes(1); |
| 146 | + expect(refs).toMatchObject([ |
| 147 | + { schedulerJobId: "scheduler-after-lost-response", status: "complete" }, |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("converges concurrent installs through the declaration key", async () => { |
| 152 | + const current = await fixture(); |
| 153 | + const jobs = new Map<string, { id: string; declarationKey: string }>(); |
| 154 | + const add = vi.fn(async (input: Record<string, unknown>) => { |
| 155 | + await Promise.resolve(); |
| 156 | + const declarationKey = String(input.declarationKey); |
| 157 | + const existing = jobs.get(declarationKey); |
| 158 | + if (existing) { |
| 159 | + return { created: false, job: existing }; |
| 160 | + } |
| 161 | + const created = { id: "scheduler-converged", declarationKey }; |
| 162 | + jobs.set(declarationKey, created); |
| 163 | + return { created: true, job: created }; |
| 164 | + }); |
| 165 | + |
| 166 | + const [first, second] = await Promise.all([ |
| 167 | + installClawCronJobs(current.plan, { env: current.env, gateway: { add } }), |
| 168 | + installClawCronJobs(current.plan, { env: current.env, gateway: { add } }), |
| 169 | + ]); |
| 170 | + |
| 171 | + expect(jobs.size).toBe(1); |
| 172 | + expect(first[0]).toMatchObject({ schedulerJobId: "scheduler-converged", status: "complete" }); |
| 173 | + expect(second[0]).toMatchObject({ schedulerJobId: "scheduler-converged", status: "complete" }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments