test(cron): share timed-out registry setup

This commit is contained in:
Vincent Koc
2026-04-12 05:07:21 +01:00
parent f2f98d5613
commit 393877e4fa

View File

@@ -12,6 +12,35 @@ const { logger, makeStorePath } = setupCronServiceSuite({
prefix: "cron-service-ops-seam", prefix: "cron-service-ops-seam",
}); });
function withStateDirForStorePath(storePath: string) {
const stateRoot = path.dirname(path.dirname(storePath));
const originalStateDir = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = stateRoot;
resetTaskRegistryForTests();
return () => {
if (originalStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalStateDir;
}
resetTaskRegistryForTests();
};
}
function createTimedOutIsolatedCronState(params: { storePath: string; now: number }) {
return createCronServiceState({
storePath: params.storePath,
cronEnabled: true,
log: logger,
nowMs: () => params.now,
enqueueSystemEvent: vi.fn(),
requestHeartbeatNow: vi.fn(),
runIsolatedAgentJob: vi.fn(async () => {
throw new Error("cron: job execution timed out");
}),
});
}
function createInterruptedMainJob(now: number): CronJob { function createInterruptedMainJob(now: number): CronJob {
return { return {
id: "startup-interrupted", id: "startup-interrupted",
@@ -118,27 +147,17 @@ describe("cron service ops seam coverage", () => {
it("records timed out manual runs as timed_out in the shared task registry", async () => { it("records timed out manual runs as timed_out in the shared task registry", async () => {
const { storePath } = await makeStorePath(); const { storePath } = await makeStorePath();
const stateRoot = path.dirname(path.dirname(storePath));
const now = Date.parse("2026-03-23T12:00:00.000Z"); const now = Date.parse("2026-03-23T12:00:00.000Z");
const originalStateDir = process.env.OPENCLAW_STATE_DIR; const restoreStateDir = withStateDirForStorePath(storePath);
process.env.OPENCLAW_STATE_DIR = stateRoot;
resetTaskRegistryForTests();
await writeCronStoreSnapshot({ await writeCronStoreSnapshot({
storePath, storePath,
jobs: [createDueIsolatedJob(now)], jobs: [createDueIsolatedJob(now)],
}); });
const state = createCronServiceState({ const state = createTimedOutIsolatedCronState({
storePath, storePath,
cronEnabled: true, now,
log: logger,
nowMs: () => now,
enqueueSystemEvent: vi.fn(),
requestHeartbeatNow: vi.fn(),
runIsolatedAgentJob: vi.fn(async () => {
throw new Error("cron: job execution timed out");
}),
}); });
await run(state, "isolated-timeout"); await run(state, "isolated-timeout");
@@ -149,12 +168,7 @@ describe("cron service ops seam coverage", () => {
sourceId: "isolated-timeout", sourceId: "isolated-timeout",
}); });
if (originalStateDir === undefined) { restoreStateDir();
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalStateDir;
}
resetTaskRegistryForTests();
}); });
it("keeps manual cron runs progressing when task ledger creation fails", async () => { it("keeps manual cron runs progressing when task ledger creation fails", async () => {
@@ -327,27 +341,17 @@ describe("cron service ops seam coverage", () => {
it("records startup catch-up timeouts as timed_out in the shared task registry", async () => { it("records startup catch-up timeouts as timed_out in the shared task registry", async () => {
const { storePath } = await makeStorePath(); const { storePath } = await makeStorePath();
const stateRoot = path.dirname(path.dirname(storePath));
const now = Date.parse("2026-03-23T12:00:00.000Z"); const now = Date.parse("2026-03-23T12:00:00.000Z");
const originalStateDir = process.env.OPENCLAW_STATE_DIR; const restoreStateDir = withStateDirForStorePath(storePath);
process.env.OPENCLAW_STATE_DIR = stateRoot;
resetTaskRegistryForTests();
await writeCronStoreSnapshot({ await writeCronStoreSnapshot({
storePath, storePath,
jobs: [createMissedIsolatedJob(now)], jobs: [createMissedIsolatedJob(now)],
}); });
const state = createCronServiceState({ const state = createTimedOutIsolatedCronState({
storePath, storePath,
cronEnabled: true, now,
log: logger,
nowMs: () => now,
enqueueSystemEvent: vi.fn(),
requestHeartbeatNow: vi.fn(),
runIsolatedAgentJob: vi.fn(async () => {
throw new Error("cron: job execution timed out");
}),
}); });
await start(state); await start(state);
@@ -358,12 +362,7 @@ describe("cron service ops seam coverage", () => {
sourceId: "startup-timeout", sourceId: "startup-timeout",
}); });
if (originalStateDir === undefined) { restoreStateDir();
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalStateDir;
}
resetTaskRegistryForTests();
stop(state); stop(state);
}); });
}); });