|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { resetTaskRegistryForTests } from "../tasks/task-runtime.test-helpers.js"; |
| 3 | +import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js"; |
| 4 | +import { |
| 5 | + loadRunCronIsolatedAgentTurn, |
| 6 | + mockRunCronFallbackPassthrough, |
| 7 | + resetRunCronIsolatedAgentTurnHarness, |
| 8 | + resolveConfiguredModelRefMock, |
| 9 | + resolveCronPayloadOutcomeMock, |
| 10 | + runEmbeddedAgentMock, |
| 11 | +} from "./isolated-agent/run.test-harness.js"; |
| 12 | +import { CronService } from "./service.js"; |
| 13 | +import { createNoopLogger } from "./service.test-harness.js"; |
| 14 | +import { loadCronStore } from "./store.js"; |
| 15 | + |
| 16 | +const runCronIsolatedAgentTurn = await loadRunCronIsolatedAgentTurn(); |
| 17 | + |
| 18 | +type CronServiceParams = ConstructorParameters<typeof CronService>[0]; |
| 19 | +type SendCronFailureAlertParams = Parameters< |
| 20 | + NonNullable<CronServiceParams["sendCronFailureAlert"]> |
| 21 | +>[0]; |
| 22 | + |
| 23 | +describe.sequential("CronService silent failure alerts", () => { |
| 24 | + beforeEach(() => { |
| 25 | + resetRunCronIsolatedAgentTurnHarness(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("persists a tool failure and emits the configured operator alert", async () => { |
| 29 | + const { resolveCronPayloadOutcome } = await vi.importActual< |
| 30 | + typeof import("./isolated-agent/helpers.js") |
| 31 | + >("./isolated-agent/helpers.js"); |
| 32 | + resolveCronPayloadOutcomeMock.mockImplementation(resolveCronPayloadOutcome); |
| 33 | + const modelRef = { provider: "openai", model: "gpt-5.4" }; |
| 34 | + resolveConfiguredModelRefMock.mockReturnValue(modelRef); |
| 35 | + mockRunCronFallbackPassthrough(); |
| 36 | + runEmbeddedAgentMock.mockResolvedValue({ |
| 37 | + payloads: [{ text: "⚠️ 🛠️ Bash failed: mount unavailable", isError: true }], |
| 38 | + meta: { agentMeta: {}, finalAssistantVisibleText: "NO_REPLY" }, |
| 39 | + }); |
| 40 | + |
| 41 | + await withOpenClawTestState( |
| 42 | + { layout: "state-only", prefix: "openclaw-cron-silent-failure-" }, |
| 43 | + async (state) => { |
| 44 | + resetTaskRegistryForTests(); |
| 45 | + const sendCronFailureAlert = vi.fn( |
| 46 | + async (_params: SendCronFailureAlertParams) => undefined, |
| 47 | + ); |
| 48 | + const storePath = state.path("cron", "jobs.json"); |
| 49 | + const cron = new CronService({ |
| 50 | + storePath, |
| 51 | + cronEnabled: true, |
| 52 | + cronConfig: { |
| 53 | + triggers: { enabled: true }, |
| 54 | + failureAlert: { enabled: true, after: 1, cooldownMs: 0 }, |
| 55 | + }, |
| 56 | + log: createNoopLogger(), |
| 57 | + enqueueSystemEvent: vi.fn(), |
| 58 | + requestHeartbeat: vi.fn(), |
| 59 | + sendCronFailureAlert, |
| 60 | + runIsolatedAgentJob: async (runParams) => |
| 61 | + await runCronIsolatedAgentTurn({ |
| 62 | + ...runParams, |
| 63 | + cfg: { agents: { defaults: { model: `${modelRef.provider}/${modelRef.model}` } } }, |
| 64 | + deps: {}, |
| 65 | + sessionKey: `cron:${runParams.job.id}`, |
| 66 | + }), |
| 67 | + }); |
| 68 | + try { |
| 69 | + await cron.start(); |
| 70 | + const job = await cron.add({ |
| 71 | + name: "mount check", |
| 72 | + enabled: true, |
| 73 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 74 | + sessionTarget: "isolated", |
| 75 | + wakeMode: "next-heartbeat", |
| 76 | + payload: { |
| 77 | + kind: "agentTurn", |
| 78 | + message: "check mount", |
| 79 | + model: `${modelRef.provider}/${modelRef.model}`, |
| 80 | + }, |
| 81 | + delivery: { mode: "none" }, |
| 82 | + }); |
| 83 | + |
| 84 | + await expect(cron.run(job.id, "force")).resolves.toEqual({ ok: true, ran: true }); |
| 85 | + |
| 86 | + const persisted = (await loadCronStore(storePath)).jobs.find( |
| 87 | + (candidate) => candidate.id === job.id, |
| 88 | + ); |
| 89 | + expect(persisted?.state.lastRunStatus).toBe("error"); |
| 90 | + expect(persisted?.state.lastError).toContain("Bash failed"); |
| 91 | + expect(persisted?.state.consecutiveErrors).toBe(1); |
| 92 | + expect(persisted?.state.lastFailureAlertAtMs).toBeDefined(); |
| 93 | + expect(sendCronFailureAlert).toHaveBeenCalledOnce(); |
| 94 | + expect(sendCronFailureAlert.mock.calls[0]?.[0].payload.text).toContain( |
| 95 | + 'Automation "mount check" failed 1 times', |
| 96 | + ); |
| 97 | + } finally { |
| 98 | + cron.stop(); |
| 99 | + resetTaskRegistryForTests({ persist: false }); |
| 100 | + } |
| 101 | + }, |
| 102 | + ); |
| 103 | + }); |
| 104 | +}); |
0 commit comments