refactor: dedupe core config and runtime helpers

This commit is contained in:
Peter Steinberger
2026-02-22 17:11:34 +00:00
parent 24ea941e28
commit 34ea33f057
29 changed files with 720 additions and 874 deletions

View File

@@ -87,16 +87,35 @@ describe("Ghost reminder bug (issue #13317)", () => {
result: Awaited<ReturnType<typeof runHeartbeatOnce>>;
sendTelegram: ReturnType<typeof vi.fn>;
calledCtx: { Provider?: string; Body?: string } | null;
}> => {
return runHeartbeatCase({
tmpPrefix,
replyText: "Relay this reminder now",
reason: "cron:reminder-job",
enqueue,
});
};
const runHeartbeatCase = async (params: {
tmpPrefix: string;
replyText: string;
reason: string;
enqueue: (sessionKey: string) => void;
}): Promise<{
result: Awaited<ReturnType<typeof runHeartbeatOnce>>;
sendTelegram: ReturnType<typeof vi.fn>;
calledCtx: { Provider?: string; Body?: string } | null;
replyCallCount: number;
}> => {
return withTempHeartbeatSandbox(
async ({ tmpDir, storePath }) => {
const { sendTelegram, getReplySpy } = createHeartbeatDeps("Relay this reminder now");
const { sendTelegram, getReplySpy } = createHeartbeatDeps(params.replyText);
const { cfg, sessionKey } = await createConfig({ tmpDir, storePath });
enqueue(sessionKey);
params.enqueue(sessionKey);
const result = await runHeartbeatOnce({
cfg,
agentId: "main",
reason: "cron:reminder-job",
reason: params.reason,
deps: {
sendTelegram,
},
@@ -105,38 +124,32 @@ describe("Ghost reminder bug (issue #13317)", () => {
Provider?: string;
Body?: string;
} | null;
return { result, sendTelegram, calledCtx };
return {
result,
sendTelegram,
calledCtx,
replyCallCount: getReplySpy.mock.calls.length,
};
},
{ prefix: tmpPrefix },
{ prefix: params.tmpPrefix },
);
};
it("does not use CRON_EVENT_PROMPT when only a HEARTBEAT_OK event is present", async () => {
await withTempHeartbeatSandbox(
async ({ tmpDir, storePath }) => {
const { sendTelegram, getReplySpy } = createHeartbeatDeps("Heartbeat check-in");
const { cfg, sessionKey } = await createConfig({ tmpDir, storePath });
const { result, sendTelegram, calledCtx, replyCallCount } = await runHeartbeatCase({
tmpPrefix: "openclaw-ghost-",
replyText: "Heartbeat check-in",
reason: "cron:test-job",
enqueue: (sessionKey) => {
enqueueSystemEvent("HEARTBEAT_OK", { sessionKey });
const result = await runHeartbeatOnce({
cfg,
agentId: "main",
reason: "cron:test-job",
deps: {
sendTelegram,
},
});
expect(result.status).toBe("ran");
expect(getReplySpy).toHaveBeenCalledTimes(1);
const calledCtx = getReplySpy.mock.calls[0]?.[0];
expect(calledCtx?.Provider).toBe("heartbeat");
expect(calledCtx?.Body).not.toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).not.toContain("relay this reminder");
expect(sendTelegram).toHaveBeenCalled();
},
{ prefix: "openclaw-ghost-" },
);
});
expect(result.status).toBe("ran");
expect(replyCallCount).toBe(1);
expect(calledCtx?.Provider).toBe("heartbeat");
expect(calledCtx?.Body).not.toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).not.toContain("relay this reminder");
expect(sendTelegram).toHaveBeenCalled();
});
it("uses CRON_EVENT_PROMPT when an actionable cron event exists", async () => {
@@ -165,34 +178,23 @@ describe("Ghost reminder bug (issue #13317)", () => {
});
it("uses CRON_EVENT_PROMPT for tagged cron events on interval wake", async () => {
await withTempHeartbeatSandbox(
async ({ tmpDir, storePath }) => {
const { sendTelegram, getReplySpy } = createHeartbeatDeps("Relay this cron update now");
const { cfg, sessionKey } = await createConfig({ tmpDir, storePath });
const { result, sendTelegram, calledCtx, replyCallCount } = await runHeartbeatCase({
tmpPrefix: "openclaw-cron-interval-",
replyText: "Relay this cron update now",
reason: "interval",
enqueue: (sessionKey) => {
enqueueSystemEvent("Cron: QMD maintenance completed", {
sessionKey,
contextKey: "cron:qmd-maintenance",
});
const result = await runHeartbeatOnce({
cfg,
agentId: "main",
reason: "interval",
deps: {
sendTelegram,
},
});
expect(result.status).toBe("ran");
expect(getReplySpy).toHaveBeenCalledTimes(1);
const calledCtx = getReplySpy.mock.calls[0]?.[0];
expect(calledCtx?.Provider).toBe("cron-event");
expect(calledCtx?.Body).toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).toContain("Cron: QMD maintenance completed");
expect(calledCtx?.Body).not.toContain("Read HEARTBEAT.md");
expect(sendTelegram).toHaveBeenCalled();
},
{ prefix: "openclaw-cron-interval-" },
);
});
expect(result.status).toBe("ran");
expect(replyCallCount).toBe(1);
expect(calledCtx?.Provider).toBe("cron-event");
expect(calledCtx?.Body).toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).toContain("Cron: QMD maintenance completed");
expect(calledCtx?.Body).not.toContain("Read HEARTBEAT.md");
expect(sendTelegram).toHaveBeenCalled();
});
});