|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 3 | +import type { CronJob } from "../../cron/types.js"; |
| 4 | + |
| 5 | +const loadConfig = vi.hoisted(() => vi.fn<() => OpenClawConfig>(() => ({}) as OpenClawConfig)); |
| 6 | + |
| 7 | +vi.mock("../../config/config.js", async () => { |
| 8 | + const actual = |
| 9 | + await vi.importActual<typeof import("../../config/config.js")>("../../config/config.js"); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + loadConfig, |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +import { cronHandlers } from "./cron.js"; |
| 17 | + |
| 18 | +function createCronContext(currentJob?: CronJob) { |
| 19 | + return { |
| 20 | + cron: { |
| 21 | + add: vi.fn(async () => ({ id: "cron-1" })), |
| 22 | + update: vi.fn(async () => ({ id: "cron-1" })), |
| 23 | + getDefaultAgentId: vi.fn(() => "main"), |
| 24 | + getJob: vi.fn(() => currentJob), |
| 25 | + }, |
| 26 | + logGateway: { |
| 27 | + info: vi.fn(), |
| 28 | + }, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +async function invokeCronAdd(params: Record<string, unknown>) { |
| 33 | + const context = createCronContext(); |
| 34 | + const respond = vi.fn(); |
| 35 | + await cronHandlers["cron.add"]({ |
| 36 | + req: {} as never, |
| 37 | + params: params as never, |
| 38 | + respond: respond as never, |
| 39 | + context: context as never, |
| 40 | + client: null, |
| 41 | + isWebchatConnect: () => false, |
| 42 | + }); |
| 43 | + return { context, respond }; |
| 44 | +} |
| 45 | + |
| 46 | +async function invokeCronUpdate(params: Record<string, unknown>, currentJob: CronJob) { |
| 47 | + const context = createCronContext(currentJob); |
| 48 | + const respond = vi.fn(); |
| 49 | + await cronHandlers["cron.update"]({ |
| 50 | + req: {} as never, |
| 51 | + params: params as never, |
| 52 | + respond: respond as never, |
| 53 | + context: context as never, |
| 54 | + client: null, |
| 55 | + isWebchatConnect: () => false, |
| 56 | + }); |
| 57 | + return { context, respond }; |
| 58 | +} |
| 59 | + |
| 60 | +function createCronJob(overrides: Partial<CronJob> = {}): CronJob { |
| 61 | + return { |
| 62 | + id: "cron-1", |
| 63 | + name: "cron job", |
| 64 | + enabled: true, |
| 65 | + createdAtMs: 1, |
| 66 | + updatedAtMs: 1, |
| 67 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 68 | + sessionTarget: "isolated", |
| 69 | + wakeMode: "next-heartbeat", |
| 70 | + payload: { kind: "agentTurn", message: "hello" }, |
| 71 | + delivery: { mode: "none" }, |
| 72 | + state: {}, |
| 73 | + ...overrides, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +describe("cron method validation", () => { |
| 78 | + beforeEach(() => { |
| 79 | + loadConfig.mockReset().mockReturnValue({} as OpenClawConfig); |
| 80 | + }); |
| 81 | + |
| 82 | + it("rejects ambiguous announce delivery on add when multiple channels are configured", async () => { |
| 83 | + loadConfig.mockReturnValue({ |
| 84 | + session: { |
| 85 | + mainKey: "main", |
| 86 | + }, |
| 87 | + channels: { |
| 88 | + telegram: { |
| 89 | + botToken: "telegram-token", |
| 90 | + }, |
| 91 | + slack: { |
| 92 | + botToken: "xoxb-slack-token", |
| 93 | + appToken: "xapp-slack-token", |
| 94 | + }, |
| 95 | + }, |
| 96 | + plugins: { |
| 97 | + entries: { |
| 98 | + telegram: { enabled: true }, |
| 99 | + slack: { enabled: true }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } as OpenClawConfig); |
| 103 | + |
| 104 | + const { context, respond } = await invokeCronAdd({ |
| 105 | + name: "ambiguous announce add", |
| 106 | + enabled: true, |
| 107 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 108 | + sessionTarget: "isolated", |
| 109 | + wakeMode: "next-heartbeat", |
| 110 | + payload: { kind: "agentTurn", message: "hello" }, |
| 111 | + delivery: { mode: "announce" }, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(context.cron.add).not.toHaveBeenCalled(); |
| 115 | + expect(respond).toHaveBeenCalledWith( |
| 116 | + false, |
| 117 | + undefined, |
| 118 | + expect.objectContaining({ |
| 119 | + message: expect.stringContaining("delivery.channel is required"), |
| 120 | + }), |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it("rejects ambiguous announce delivery on update when multiple channels are configured", async () => { |
| 125 | + loadConfig.mockReturnValue({ |
| 126 | + session: { |
| 127 | + mainKey: "main", |
| 128 | + }, |
| 129 | + channels: { |
| 130 | + telegram: { |
| 131 | + botToken: "telegram-token", |
| 132 | + }, |
| 133 | + slack: { |
| 134 | + botToken: "xoxb-slack-token", |
| 135 | + appToken: "xapp-slack-token", |
| 136 | + }, |
| 137 | + }, |
| 138 | + plugins: { |
| 139 | + entries: { |
| 140 | + telegram: { enabled: true }, |
| 141 | + slack: { enabled: true }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + } as OpenClawConfig); |
| 145 | + |
| 146 | + const { context, respond } = await invokeCronUpdate( |
| 147 | + { |
| 148 | + id: "cron-1", |
| 149 | + patch: { |
| 150 | + delivery: { mode: "announce" }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + createCronJob(), |
| 154 | + ); |
| 155 | + |
| 156 | + expect(context.cron.update).not.toHaveBeenCalled(); |
| 157 | + expect(respond).toHaveBeenCalledWith( |
| 158 | + false, |
| 159 | + undefined, |
| 160 | + expect.objectContaining({ |
| 161 | + message: expect.stringContaining("delivery.channel is required"), |
| 162 | + }), |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + it("rejects target ids mistakenly supplied as delivery.channel providers", async () => { |
| 167 | + loadConfig.mockReturnValue({ |
| 168 | + session: { |
| 169 | + mainKey: "main", |
| 170 | + }, |
| 171 | + channels: { |
| 172 | + slack: { |
| 173 | + botToken: "xoxb-slack-token", |
| 174 | + appToken: "xapp-slack-token", |
| 175 | + }, |
| 176 | + }, |
| 177 | + plugins: { |
| 178 | + entries: { |
| 179 | + slack: { enabled: true }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + } as OpenClawConfig); |
| 183 | + |
| 184 | + const { context, respond } = await invokeCronAdd({ |
| 185 | + name: "invalid delivery provider", |
| 186 | + enabled: true, |
| 187 | + schedule: { kind: "every", everyMs: 60_000 }, |
| 188 | + sessionTarget: "isolated", |
| 189 | + wakeMode: "next-heartbeat", |
| 190 | + payload: { kind: "agentTurn", message: "hello" }, |
| 191 | + delivery: { |
| 192 | + mode: "announce", |
| 193 | + channel: "C0AT2Q238MQ", |
| 194 | + to: "C0AT2Q238MQ", |
| 195 | + }, |
| 196 | + }); |
| 197 | + |
| 198 | + expect(context.cron.add).not.toHaveBeenCalled(); |
| 199 | + expect(respond).toHaveBeenCalledWith( |
| 200 | + false, |
| 201 | + undefined, |
| 202 | + expect.objectContaining({ |
| 203 | + message: expect.stringContaining("delivery.channel must be one of: slack"), |
| 204 | + }), |
| 205 | + ); |
| 206 | + }); |
| 207 | +}); |
0 commit comments