test(cron): dedupe migration and regression fixtures

This commit is contained in:
Peter Steinberger
2026-02-18 12:20:48 +00:00
parent 2fd211b705
commit eabf187fa5
2 changed files with 139 additions and 153 deletions

View File

@@ -24,6 +24,11 @@ async function makeStorePath() {
};
}
async function writeLegacyStore(storePath: string, legacyJob: Record<string, unknown>) {
await fs.mkdir(path.dirname(storePath), { recursive: true });
await fs.writeFile(storePath, JSON.stringify({ version: 1, jobs: [legacyJob] }, null, 2));
}
async function migrateAndLoadFirstJob(storePath: string): Promise<Record<string, unknown>> {
const cron = new CronService({
storePath,
@@ -41,6 +46,37 @@ async function migrateAndLoadFirstJob(storePath: string): Promise<Record<string,
return loaded.jobs[0] as Record<string, unknown>;
}
function makeLegacyJob(overrides: Record<string, unknown>): Record<string, unknown> {
return {
id: "job-legacy",
agentId: undefined,
name: "Legacy job",
description: null,
enabled: true,
deleteAfterRun: false,
createdAtMs: 1_700_000_000_000,
updatedAtMs: 1_700_000_000_000,
sessionTarget: "main",
wakeMode: "next-heartbeat",
payload: {
kind: "systemEvent",
text: "tick",
},
state: {},
...overrides,
};
}
async function migrateLegacyJob(legacyJob: Record<string, unknown>) {
const store = await makeStorePath();
try {
await writeLegacyStore(store.storePath, legacyJob);
return await migrateAndLoadFirstJob(store.storePath);
} finally {
await store.cleanup();
}
}
describe("cron store migration", () => {
beforeEach(() => {
noopLogger.debug.mockClear();
@@ -54,36 +90,24 @@ describe("cron store migration", () => {
});
it("migrates isolated jobs to announce delivery and drops isolation", async () => {
const store = await makeStorePath();
const atMs = 1_700_000_000_000;
const legacyJob = {
id: "job-1",
agentId: undefined,
sessionKey: " agent:main:discord:channel:ops ",
name: "Legacy job",
description: null,
enabled: true,
deleteAfterRun: false,
createdAtMs: 1_700_000_000_000,
updatedAtMs: 1_700_000_000_000,
schedule: { kind: "at", atMs },
sessionTarget: "isolated",
wakeMode: "next-heartbeat",
payload: {
kind: "agentTurn",
message: "hi",
deliver: true,
channel: "telegram",
to: "7200373102",
bestEffortDeliver: true,
},
isolation: { postToMainPrefix: "Cron" },
state: {},
};
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
await fs.writeFile(store.storePath, JSON.stringify({ version: 1, jobs: [legacyJob] }, null, 2));
const migrated = await migrateAndLoadFirstJob(store.storePath);
const migrated = await migrateLegacyJob(
makeLegacyJob({
id: "job-1",
sessionKey: " agent:main:discord:channel:ops ",
schedule: { kind: "at", atMs },
sessionTarget: "isolated",
payload: {
kind: "agentTurn",
message: "hi",
deliver: true,
channel: "telegram",
to: "7200373102",
bestEffortDeliver: true,
},
isolation: { postToMainPrefix: "Cron" },
}),
);
expect(migrated.sessionKey).toBe("agent:main:discord:channel:ops");
expect(migrated.delivery).toEqual({
mode: "announce",
@@ -102,103 +126,71 @@ describe("cron store migration", () => {
const schedule = migrated.schedule as Record<string, unknown>;
expect(schedule.kind).toBe("at");
expect(schedule.at).toBe(new Date(atMs).toISOString());
await store.cleanup();
});
it("adds anchorMs to legacy every schedules", async () => {
const store = await makeStorePath();
const createdAtMs = 1_700_000_000_000;
const legacyJob = {
id: "job-every-legacy",
agentId: undefined,
name: "Legacy every",
description: null,
enabled: true,
deleteAfterRun: false,
createdAtMs,
updatedAtMs: createdAtMs,
schedule: { kind: "every", everyMs: 120_000 },
sessionTarget: "main",
wakeMode: "next-heartbeat",
payload: {
kind: "systemEvent",
text: "tick",
},
state: {},
};
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
await fs.writeFile(store.storePath, JSON.stringify({ version: 1, jobs: [legacyJob] }, null, 2));
const migrated = await migrateAndLoadFirstJob(store.storePath);
const migrated = await migrateLegacyJob(
makeLegacyJob({
id: "job-every-legacy",
name: "Legacy every",
createdAtMs,
updatedAtMs: createdAtMs,
schedule: { kind: "every", everyMs: 120_000 },
}),
);
const schedule = migrated.schedule as Record<string, unknown>;
expect(schedule.kind).toBe("every");
expect(schedule.anchorMs).toBe(createdAtMs);
await store.cleanup();
});
it("adds default staggerMs to legacy recurring top-of-hour cron schedules", async () => {
const store = await makeStorePath();
const createdAtMs = 1_700_000_000_000;
const legacyJob = {
id: "job-cron-legacy",
agentId: undefined,
name: "Legacy cron",
description: null,
enabled: true,
deleteAfterRun: false,
createdAtMs,
updatedAtMs: createdAtMs,
schedule: { kind: "cron", expr: "0 */2 * * *", tz: "UTC" },
sessionTarget: "main",
wakeMode: "next-heartbeat",
payload: {
kind: "systemEvent",
text: "tick",
},
state: {},
};
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
await fs.writeFile(store.storePath, JSON.stringify({ version: 1, jobs: [legacyJob] }, null, 2));
const migrated = await migrateAndLoadFirstJob(store.storePath);
const migrated = await migrateLegacyJob(
makeLegacyJob({
id: "job-cron-legacy",
name: "Legacy cron",
createdAtMs,
updatedAtMs: createdAtMs,
schedule: { kind: "cron", expr: "0 */2 * * *", tz: "UTC" },
}),
);
const schedule = migrated.schedule as Record<string, unknown>;
expect(schedule.kind).toBe("cron");
expect(schedule.staggerMs).toBe(DEFAULT_TOP_OF_HOUR_STAGGER_MS);
await store.cleanup();
});
it("adds default staggerMs to legacy 6-field top-of-hour cron schedules", async () => {
const store = await makeStorePath();
const createdAtMs = 1_700_000_000_000;
const legacyJob = {
id: "job-cron-seconds-legacy",
agentId: undefined,
name: "Legacy cron seconds",
description: null,
enabled: true,
deleteAfterRun: false,
createdAtMs,
updatedAtMs: createdAtMs,
schedule: { kind: "cron", expr: "0 0 */3 * * *", tz: "UTC" },
sessionTarget: "main",
wakeMode: "next-heartbeat",
payload: {
kind: "systemEvent",
text: "tick",
},
state: {},
};
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
await fs.writeFile(store.storePath, JSON.stringify({ version: 1, jobs: [legacyJob] }, null, 2));
const migrated = await migrateAndLoadFirstJob(store.storePath);
const migrated = await migrateLegacyJob(
makeLegacyJob({
id: "job-cron-seconds-legacy",
name: "Legacy cron seconds",
createdAtMs,
updatedAtMs: createdAtMs,
schedule: { kind: "cron", expr: "0 0 */3 * * *", tz: "UTC" },
}),
);
const schedule = migrated.schedule as Record<string, unknown>;
expect(schedule.kind).toBe("cron");
expect(schedule.staggerMs).toBe(DEFAULT_TOP_OF_HOUR_STAGGER_MS);
});
await store.cleanup();
it("removes invalid legacy staggerMs from non top-of-hour cron schedules", async () => {
const migrated = await migrateLegacyJob(
makeLegacyJob({
id: "job-cron-minute-legacy",
name: "Legacy minute cron",
schedule: {
kind: "cron",
expr: "17 * * * *",
tz: "UTC",
staggerMs: "bogus",
},
}),
);
const schedule = migrated.schedule as Record<string, unknown>;
expect(schedule.kind).toBe("cron");
expect(schedule.staggerMs).toBeUndefined();
});
});