refactor(test): dedupe cron legacy job setup

This commit is contained in:
Peter Steinberger
2026-02-15 15:46:00 +00:00
parent e687ad15ac
commit 75d22b2164

View File

@@ -57,6 +57,34 @@ function createCronEventHarness() {
}
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 () => {
const store = await makeStorePath();
const enqueueSystemEvent = vi.fn();
@@ -409,10 +437,6 @@ describe("CronService", () => {
});
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 = {
id: "legacy-1",
name: "legacy",
@@ -432,25 +456,7 @@ describe("CronService", () => {
state: {},
};
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);
const { cron, store, job } = await loadLegacyJobFromStore(rawJob);
// Legacy delivery fields are migrated to the top-level delivery object
const delivery = job?.delivery as unknown as Record<string, unknown>;
expect(delivery?.channel).toBe("telegram");
@@ -463,10 +469,6 @@ describe("CronService", () => {
});
it("canonicalizes payload.channel casing on load", async () => {
const store = await makeStorePath();
const enqueueSystemEvent = vi.fn();
const requestHeartbeatNow = vi.fn();
const rawJob = {
id: "legacy-2",
name: "legacy",
@@ -486,25 +488,7 @@ describe("CronService", () => {
state: {},
};
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);
const { cron, store, job } = await loadLegacyJobFromStore(rawJob);
// Legacy delivery fields are migrated to the top-level delivery object
const delivery = job?.delivery as unknown as Record<string, unknown>;
expect(delivery?.channel).toBe("telegram");