mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:48:26 +00:00
refactor(test): dedupe cron legacy job setup
This commit is contained in:
@@ -57,6 +57,34 @@ function createCronEventHarness() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("CronService", () => {
|
describe("CronService", () => {
|
||||||
|
async function loadLegacyJobFromStore(rawJob: Record<string, unknown>) {
|
||||||
|
const store = await makeStorePath();
|
||||||
|
const enqueueSystemEvent = vi.fn();
|
||||||
|
const requestHeartbeatNow = vi.fn();
|
||||||
|
|
||||||
|
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
|
||||||
|
await fs.writeFile(
|
||||||
|
store.storePath,
|
||||||
|
JSON.stringify({ version: 1, jobs: [rawJob] }, null, 2),
|
||||||
|
"utf-8",
|
||||||
|
);
|
||||||
|
|
||||||
|
const cron = new CronService({
|
||||||
|
storePath: store.storePath,
|
||||||
|
cronEnabled: true,
|
||||||
|
log: noopLogger,
|
||||||
|
enqueueSystemEvent,
|
||||||
|
requestHeartbeatNow,
|
||||||
|
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })),
|
||||||
|
});
|
||||||
|
|
||||||
|
await cron.start();
|
||||||
|
const jobs = await cron.list({ includeDisabled: true });
|
||||||
|
const job = jobs.find((j) => j.id === rawJob.id);
|
||||||
|
|
||||||
|
return { cron, store, enqueueSystemEvent, requestHeartbeatNow, job };
|
||||||
|
}
|
||||||
|
|
||||||
it("runs a one-shot main job and disables it after success when requested", async () => {
|
it("runs a one-shot main job and disables it after success when requested", async () => {
|
||||||
const store = await makeStorePath();
|
const store = await makeStorePath();
|
||||||
const enqueueSystemEvent = vi.fn();
|
const enqueueSystemEvent = vi.fn();
|
||||||
@@ -409,10 +437,6 @@ describe("CronService", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("migrates legacy payload.provider to payload.channel on load", async () => {
|
it("migrates legacy payload.provider to payload.channel on load", async () => {
|
||||||
const store = await makeStorePath();
|
|
||||||
const enqueueSystemEvent = vi.fn();
|
|
||||||
const requestHeartbeatNow = vi.fn();
|
|
||||||
|
|
||||||
const rawJob = {
|
const rawJob = {
|
||||||
id: "legacy-1",
|
id: "legacy-1",
|
||||||
name: "legacy",
|
name: "legacy",
|
||||||
@@ -432,25 +456,7 @@ describe("CronService", () => {
|
|||||||
state: {},
|
state: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
|
const { cron, store, job } = await loadLegacyJobFromStore(rawJob);
|
||||||
await fs.writeFile(
|
|
||||||
store.storePath,
|
|
||||||
JSON.stringify({ version: 1, jobs: [rawJob] }, null, 2),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
|
|
||||||
const cron = new CronService({
|
|
||||||
storePath: store.storePath,
|
|
||||||
cronEnabled: true,
|
|
||||||
log: noopLogger,
|
|
||||||
enqueueSystemEvent,
|
|
||||||
requestHeartbeatNow,
|
|
||||||
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })),
|
|
||||||
});
|
|
||||||
|
|
||||||
await cron.start();
|
|
||||||
const jobs = await cron.list({ includeDisabled: true });
|
|
||||||
const job = jobs.find((j) => j.id === rawJob.id);
|
|
||||||
// Legacy delivery fields are migrated to the top-level delivery object
|
// Legacy delivery fields are migrated to the top-level delivery object
|
||||||
const delivery = job?.delivery as unknown as Record<string, unknown>;
|
const delivery = job?.delivery as unknown as Record<string, unknown>;
|
||||||
expect(delivery?.channel).toBe("telegram");
|
expect(delivery?.channel).toBe("telegram");
|
||||||
@@ -463,10 +469,6 @@ describe("CronService", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("canonicalizes payload.channel casing on load", async () => {
|
it("canonicalizes payload.channel casing on load", async () => {
|
||||||
const store = await makeStorePath();
|
|
||||||
const enqueueSystemEvent = vi.fn();
|
|
||||||
const requestHeartbeatNow = vi.fn();
|
|
||||||
|
|
||||||
const rawJob = {
|
const rawJob = {
|
||||||
id: "legacy-2",
|
id: "legacy-2",
|
||||||
name: "legacy",
|
name: "legacy",
|
||||||
@@ -486,25 +488,7 @@ describe("CronService", () => {
|
|||||||
state: {},
|
state: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
|
const { cron, store, job } = await loadLegacyJobFromStore(rawJob);
|
||||||
await fs.writeFile(
|
|
||||||
store.storePath,
|
|
||||||
JSON.stringify({ version: 1, jobs: [rawJob] }, null, 2),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
|
|
||||||
const cron = new CronService({
|
|
||||||
storePath: store.storePath,
|
|
||||||
cronEnabled: true,
|
|
||||||
log: noopLogger,
|
|
||||||
enqueueSystemEvent,
|
|
||||||
requestHeartbeatNow,
|
|
||||||
runIsolatedAgentJob: vi.fn(async () => ({ status: "ok" })),
|
|
||||||
});
|
|
||||||
|
|
||||||
await cron.start();
|
|
||||||
const jobs = await cron.list({ includeDisabled: true });
|
|
||||||
const job = jobs.find((j) => j.id === rawJob.id);
|
|
||||||
// Legacy delivery fields are migrated to the top-level delivery object
|
// Legacy delivery fields are migrated to the top-level delivery object
|
||||||
const delivery = job?.delivery as unknown as Record<string, unknown>;
|
const delivery = job?.delivery as unknown as Record<string, unknown>;
|
||||||
expect(delivery?.channel).toBe("telegram");
|
expect(delivery?.channel).toBe("telegram");
|
||||||
|
|||||||
Reference in New Issue
Block a user