fix: align heartbeat session store with default agent

This commit is contained in:
Peter Steinberger
2026-01-07 07:14:24 +00:00
parent 7176b114da
commit 1b81805d63
2 changed files with 69 additions and 3 deletions

View File

@@ -184,6 +184,70 @@ describe("runHeartbeatOnce", () => {
}
});
it("loads the default agent session from templated stores", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));
const storeTemplate = path.join(
tmpDir,
"agents",
"{agentId}",
"sessions.json",
);
const storePath = path.join(tmpDir, "agents", "work", "sessions.json");
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
try {
await fs.mkdir(path.dirname(storePath), { recursive: true });
await fs.writeFile(
storePath,
JSON.stringify(
{
"agent:work:main": {
sessionId: "sid",
updatedAt: Date.now(),
lastProvider: "whatsapp",
lastTo: "+1555",
},
},
null,
2,
),
);
const cfg: ClawdbotConfig = {
routing: { defaultAgentId: "work" },
agent: { heartbeat: { every: "5m" } },
whatsapp: { allowFrom: ["*"] },
session: { store: storeTemplate },
};
replySpy.mockResolvedValue({ text: "Hello from heartbeat" });
const sendWhatsApp = vi.fn().mockResolvedValue({
messageId: "m1",
toJid: "jid",
});
await runHeartbeatOnce({
cfg,
deps: {
sendWhatsApp,
getQueueSize: () => 0,
nowMs: () => 0,
webAuthExists: async () => true,
hasActiveWebListener: () => true,
},
});
expect(sendWhatsApp).toHaveBeenCalledTimes(1);
expect(sendWhatsApp).toHaveBeenCalledWith(
"+1555",
"Hello from heartbeat",
expect.any(Object),
);
} finally {
replySpy.mockRestore();
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("respects ackMaxChars for heartbeat acks", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-hb-"));
const storePath = path.join(tmpDir, "sessions.json");