Skip to content

Commit d87f8cc

Browse files
committed
test: move cron validation off gateway server
1 parent 529577e commit d87f8cc

2 files changed

Lines changed: 207 additions & 156 deletions

File tree

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
});

‎src/gateway/server.cron.test.ts‎

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -721,54 +721,6 @@ describe("gateway server cron", () => {
721721
}
722722
});
723723

724-
test("rejects ambiguous announce delivery on add when multiple channels are configured", async () => {
725-
const { prevSkipCron } = await setupCronTestRun({
726-
tempPrefix: "openclaw-gw-cron-ambiguous-delivery-add-",
727-
cronEnabled: false,
728-
});
729-
730-
await writeCronConfig({
731-
session: {
732-
mainKey: "main",
733-
},
734-
channels: {
735-
telegram: {
736-
botToken: "telegram-token",
737-
},
738-
slack: {
739-
botToken: "xoxb-slack-token",
740-
appToken: "xapp-slack-token",
741-
},
742-
},
743-
plugins: {
744-
entries: {
745-
telegram: { enabled: true },
746-
slack: { enabled: true },
747-
},
748-
},
749-
});
750-
751-
const { server, ws } = await startServerWithClient();
752-
await connectOk(ws);
753-
754-
try {
755-
const addRes = await rpcReq(ws, "cron.add", {
756-
name: "ambiguous announce add",
757-
enabled: true,
758-
schedule: { kind: "every", everyMs: 60_000 },
759-
sessionTarget: "isolated",
760-
wakeMode: "next-heartbeat",
761-
payload: { kind: "agentTurn", message: "hello" },
762-
delivery: { mode: "announce" },
763-
});
764-
765-
expect(addRes.ok).toBe(false);
766-
expect(addRes.error?.message).toContain("delivery.channel is required");
767-
} finally {
768-
await cleanupCronTestRun({ ws, server, prevSkipCron });
769-
}
770-
});
771-
772724
test("ignores ambient disabled channel env when validating announce delivery", async () => {
773725
vi.stubEnv("SLACK_BOT_TOKEN", "xoxb-ambient");
774726
vi.stubEnv("TELEGRAM_BOT_TOKEN", "ambient-telegram");
@@ -806,114 +758,6 @@ describe("gateway server cron", () => {
806758
}
807759
});
808760

809-
test("rejects ambiguous announce delivery on update when multiple channels are configured", async () => {
810-
const { prevSkipCron } = await setupCronTestRun({
811-
tempPrefix: "openclaw-gw-cron-ambiguous-delivery-update-",
812-
cronEnabled: false,
813-
});
814-
815-
await writeCronConfig({
816-
session: {
817-
mainKey: "main",
818-
},
819-
channels: {
820-
telegram: {
821-
botToken: "telegram-token",
822-
},
823-
slack: {
824-
botToken: "xoxb-slack-token",
825-
appToken: "xapp-slack-token",
826-
},
827-
},
828-
plugins: {
829-
entries: {
830-
telegram: { enabled: true },
831-
slack: { enabled: true },
832-
},
833-
},
834-
});
835-
836-
const { server, ws } = await startServerWithClient();
837-
await connectOk(ws);
838-
839-
try {
840-
const addRes = await rpcReq(ws, "cron.add", {
841-
name: "ambiguous announce update",
842-
enabled: true,
843-
schedule: { kind: "every", everyMs: 60_000 },
844-
sessionTarget: "isolated",
845-
wakeMode: "next-heartbeat",
846-
payload: { kind: "agentTurn", message: "hello" },
847-
delivery: { mode: "none" },
848-
});
849-
expect(addRes.ok).toBe(true);
850-
const jobIdValue = (addRes.payload as { id?: unknown } | null)?.id;
851-
const jobId = typeof jobIdValue === "string" ? jobIdValue : "";
852-
expect(jobId.length > 0).toBe(true);
853-
854-
const updateRes = await rpcReq(ws, "cron.update", {
855-
id: jobId,
856-
patch: {
857-
delivery: { mode: "announce" },
858-
},
859-
});
860-
861-
expect(updateRes.ok).toBe(false);
862-
expect(updateRes.error?.message).toContain("delivery.channel is required");
863-
} finally {
864-
await cleanupCronTestRun({ ws, server, prevSkipCron });
865-
}
866-
});
867-
868-
test("rejects target ids mistakenly supplied as delivery.channel providers", async () => {
869-
const { prevSkipCron } = await setupCronTestRun({
870-
tempPrefix: "openclaw-gw-cron-invalid-delivery-provider-",
871-
cronEnabled: false,
872-
});
873-
874-
await writeCronConfig({
875-
session: {
876-
mainKey: "main",
877-
},
878-
channels: {
879-
slack: {
880-
botToken: "xoxb-slack-token",
881-
appToken: "xapp-slack-token",
882-
},
883-
},
884-
plugins: {
885-
entries: {
886-
slack: { enabled: true },
887-
},
888-
},
889-
});
890-
891-
const { server, ws } = await startServerWithClient();
892-
await connectOk(ws);
893-
894-
try {
895-
const addRes = await rpcReq(ws, "cron.add", {
896-
name: "invalid delivery provider",
897-
enabled: true,
898-
schedule: { kind: "every", everyMs: 60_000 },
899-
sessionTarget: "isolated",
900-
wakeMode: "next-heartbeat",
901-
payload: { kind: "agentTurn", message: "hello" },
902-
delivery: {
903-
mode: "announce",
904-
channel: "C0AT2Q238MQ",
905-
to: "C0AT2Q238MQ",
906-
},
907-
});
908-
909-
expect(addRes.ok).toBe(false);
910-
expect(addRes.error?.message).toContain("delivery.channel");
911-
expect(addRes.error?.message).toContain("slack");
912-
} finally {
913-
await cleanupCronTestRun({ ws, server, prevSkipCron });
914-
}
915-
});
916-
917761
test("writes cron run history and auto-runs due jobs", async () => {
918762
const { prevSkipCron } = await setupCronTestRun({
919763
tempPrefix: "openclaw-gw-cron-log-",

0 commit comments

Comments
 (0)