mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-25 23:47:20 +00:00
test(cron): improve fire-and-forget harness coverage
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { CronService } from "./service.js";
|
||||
import {
|
||||
@@ -7,6 +5,7 @@ import {
|
||||
createCronStoreHarness,
|
||||
createNoopLogger,
|
||||
installCronTestHooks,
|
||||
writeCronStoreSnapshot,
|
||||
} from "./service.test-harness.js";
|
||||
|
||||
const noopLogger = createNoopLogger();
|
||||
@@ -120,44 +119,35 @@ describe("CronService interval/cron jobs fire on time", () => {
|
||||
const requestHeartbeatNow = vi.fn();
|
||||
const nowMs = Date.parse("2025-12-13T00:00:00.000Z");
|
||||
|
||||
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
store.storePath,
|
||||
JSON.stringify(
|
||||
await writeCronStoreSnapshot({
|
||||
storePath: store.storePath,
|
||||
jobs: [
|
||||
{
|
||||
version: 1,
|
||||
jobs: [
|
||||
{
|
||||
id: "legacy-every",
|
||||
name: "legacy every",
|
||||
enabled: true,
|
||||
createdAtMs: nowMs,
|
||||
updatedAtMs: nowMs,
|
||||
schedule: { kind: "every", everyMs: 120_000 },
|
||||
sessionTarget: "main",
|
||||
wakeMode: "now",
|
||||
payload: { kind: "systemEvent", text: "sf-tick" },
|
||||
state: { nextRunAtMs: nowMs + 120_000 },
|
||||
},
|
||||
{
|
||||
id: "minute-cron",
|
||||
name: "minute cron",
|
||||
enabled: true,
|
||||
createdAtMs: nowMs,
|
||||
updatedAtMs: nowMs,
|
||||
schedule: { kind: "cron", expr: "* * * * *", tz: "UTC" },
|
||||
sessionTarget: "main",
|
||||
wakeMode: "now",
|
||||
payload: { kind: "systemEvent", text: "minute-tick" },
|
||||
state: { nextRunAtMs: nowMs + 60_000 },
|
||||
},
|
||||
],
|
||||
id: "legacy-every",
|
||||
name: "legacy every",
|
||||
enabled: true,
|
||||
createdAtMs: nowMs,
|
||||
updatedAtMs: nowMs,
|
||||
schedule: { kind: "every", everyMs: 120_000 },
|
||||
sessionTarget: "main",
|
||||
wakeMode: "now",
|
||||
payload: { kind: "systemEvent", text: "sf-tick" },
|
||||
state: { nextRunAtMs: nowMs + 120_000 },
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
{
|
||||
id: "minute-cron",
|
||||
name: "minute cron",
|
||||
enabled: true,
|
||||
createdAtMs: nowMs,
|
||||
updatedAtMs: nowMs,
|
||||
schedule: { kind: "cron", expr: "* * * * *", tz: "UTC" },
|
||||
sessionTarget: "main",
|
||||
wakeMode: "now",
|
||||
payload: { kind: "systemEvent", text: "minute-tick" },
|
||||
state: { nextRunAtMs: nowMs + 60_000 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const cron = new CronService({
|
||||
storePath: store.storePath,
|
||||
|
||||
@@ -3,6 +3,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { CronService } from "./service.js";
|
||||
import { writeCronStoreSnapshot } from "./service.test-harness.js";
|
||||
|
||||
const noopLogger = {
|
||||
debug: vi.fn(),
|
||||
@@ -167,29 +168,24 @@ describe("CronService read ops while job is running", () => {
|
||||
const requestHeartbeatNow = vi.fn();
|
||||
const nowMs = Date.parse("2025-12-13T00:00:00.000Z");
|
||||
|
||||
await fs.mkdir(path.dirname(store.storePath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
store.storePath,
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
jobs: [
|
||||
{
|
||||
id: "startup-catchup",
|
||||
name: "startup catch-up",
|
||||
enabled: true,
|
||||
createdAtMs: nowMs - 86_400_000,
|
||||
updatedAtMs: nowMs - 86_400_000,
|
||||
schedule: { kind: "at", at: new Date(nowMs - 60_000).toISOString() },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { kind: "agentTurn", message: "startup replay" },
|
||||
delivery: { mode: "none" },
|
||||
state: { nextRunAtMs: nowMs - 60_000 },
|
||||
},
|
||||
],
|
||||
}),
|
||||
"utf-8",
|
||||
);
|
||||
await writeCronStoreSnapshot({
|
||||
storePath: store.storePath,
|
||||
jobs: [
|
||||
{
|
||||
id: "startup-catchup",
|
||||
name: "startup catch-up",
|
||||
enabled: true,
|
||||
createdAtMs: nowMs - 86_400_000,
|
||||
updatedAtMs: nowMs - 86_400_000,
|
||||
schedule: { kind: "at", at: new Date(nowMs - 60_000).toISOString() },
|
||||
sessionTarget: "isolated",
|
||||
wakeMode: "next-heartbeat",
|
||||
payload: { kind: "agentTurn", message: "startup replay" },
|
||||
delivery: { mode: "none" },
|
||||
state: { nextRunAtMs: nowMs - 60_000 },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const isolatedRun = createDeferredIsolatedRun();
|
||||
|
||||
|
||||
@@ -51,6 +51,22 @@ export function createCronStoreHarness(options?: { prefix?: string }) {
|
||||
return { makeStorePath };
|
||||
}
|
||||
|
||||
export async function writeCronStoreSnapshot(params: { storePath: string; jobs: CronJob[] }) {
|
||||
await fs.mkdir(path.dirname(params.storePath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
params.storePath,
|
||||
JSON.stringify(
|
||||
{
|
||||
version: 1,
|
||||
jobs: params.jobs,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
"utf-8",
|
||||
);
|
||||
}
|
||||
|
||||
export function installCronTestHooks(options: {
|
||||
logger: ReturnType<typeof createNoopLogger>;
|
||||
baseTimeIso?: string;
|
||||
|
||||
Reference in New Issue
Block a user