test(heartbeat): reuse shared sandbox for ghost reminder scenarios

This commit is contained in:
Peter Steinberger
2026-02-22 09:23:29 +00:00
parent c0995103a5
commit 694a9eb6d3

View File

@@ -1,17 +1,13 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { telegramPlugin } from "../../extensions/telegram/src/channel.js"; import { telegramPlugin } from "../../extensions/telegram/src/channel.js";
import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js"; import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js";
import * as replyModule from "../auto-reply/reply.js"; import * as replyModule from "../auto-reply/reply.js";
import type { OpenClawConfig } from "../config/config.js"; import type { OpenClawConfig } from "../config/config.js";
import { resolveMainSessionKey } from "../config/sessions.js";
import { setActivePluginRegistry } from "../plugins/runtime.js"; import { setActivePluginRegistry } from "../plugins/runtime.js";
import { createPluginRuntime } from "../plugins/runtime/index.js"; import { createPluginRuntime } from "../plugins/runtime/index.js";
import { createTestRegistry } from "../test-utils/channel-plugins.js"; import { createTestRegistry } from "../test-utils/channel-plugins.js";
import { runHeartbeatOnce } from "./heartbeat-runner.js"; import { runHeartbeatOnce } from "./heartbeat-runner.js";
import { seedSessionStore } from "./heartbeat-runner.test-utils.js"; import { seedMainSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js";
import { enqueueSystemEvent, resetSystemEventsForTest } from "./system-events.js"; import { enqueueSystemEvent, resetSystemEventsForTest } from "./system-events.js";
// Avoid pulling optional runtime deps during isolated runs. // Avoid pulling optional runtime deps during isolated runs.
@@ -32,18 +28,6 @@ afterEach(() => {
}); });
describe("Ghost reminder bug (issue #13317)", () => { describe("Ghost reminder bug (issue #13317)", () => {
const withTempDir = async <T>(
prefix: string,
run: (tmpDir: string) => Promise<T>,
): Promise<T> => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
try {
return await run(tmpDir);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
};
const createHeartbeatDeps = (replyText: string) => { const createHeartbeatDeps = (replyText: string) => {
const sendTelegram = vi.fn().mockResolvedValue({ const sendTelegram = vi.fn().mockResolvedValue({
messageId: "m1", messageId: "m1",
@@ -55,14 +39,14 @@ describe("Ghost reminder bug (issue #13317)", () => {
return { sendTelegram, getReplySpy }; return { sendTelegram, getReplySpy };
}; };
const createConfig = async ( const createConfig = async (params: {
tmpDir: string, tmpDir: string;
): Promise<{ cfg: OpenClawConfig; sessionKey: string }> => { storePath: string;
const storePath = path.join(tmpDir, "sessions.json"); }): Promise<{ cfg: OpenClawConfig; sessionKey: string }> => {
const cfg: OpenClawConfig = { const cfg: OpenClawConfig = {
agents: { agents: {
defaults: { defaults: {
workspace: tmpDir, workspace: params.tmpDir,
heartbeat: { heartbeat: {
every: "5m", every: "5m",
target: "telegram", target: "telegram",
@@ -70,11 +54,9 @@ describe("Ghost reminder bug (issue #13317)", () => {
}, },
}, },
channels: { telegram: { allowFrom: ["*"] } }, channels: { telegram: { allowFrom: ["*"] } },
session: { store: storePath }, session: { store: params.storePath },
}; };
const sessionKey = resolveMainSessionKey(cfg); const sessionKey = await seedMainSessionStore(params.storePath, cfg, {
await seedSessionStore(storePath, sessionKey, {
lastChannel: "telegram", lastChannel: "telegram",
lastProvider: "telegram", lastProvider: "telegram",
lastTo: "155462274", lastTo: "155462274",
@@ -84,14 +66,13 @@ describe("Ghost reminder bug (issue #13317)", () => {
}; };
const expectCronEventPrompt = ( const expectCronEventPrompt = (
getReplySpy: { mock: { calls: unknown[][] } }, calledCtx: {
reminderText: string,
) => {
expect(getReplySpy).toHaveBeenCalledTimes(1);
const calledCtx = (getReplySpy.mock.calls[0]?.[0] ?? null) as {
Provider?: string; Provider?: string;
Body?: string; Body?: string;
} | null; } | null,
reminderText: string,
) => {
expect(calledCtx).not.toBeNull();
expect(calledCtx?.Provider).toBe("cron-event"); expect(calledCtx?.Provider).toBe("cron-event");
expect(calledCtx?.Body).toContain("scheduled reminder has been triggered"); expect(calledCtx?.Body).toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).toContain(reminderText); expect(calledCtx?.Body).toContain(reminderText);
@@ -105,63 +86,73 @@ describe("Ghost reminder bug (issue #13317)", () => {
): Promise<{ ): Promise<{
result: Awaited<ReturnType<typeof runHeartbeatOnce>>; result: Awaited<ReturnType<typeof runHeartbeatOnce>>;
sendTelegram: ReturnType<typeof vi.fn>; sendTelegram: ReturnType<typeof vi.fn>;
getReplySpy: ReturnType<typeof vi.fn>; calledCtx: { Provider?: string; Body?: string } | null;
}> => { }> => {
return await withTempDir(tmpPrefix, async (tmpDir) => { return withTempHeartbeatSandbox(
const { sendTelegram, getReplySpy } = createHeartbeatDeps("Relay this reminder now"); async ({ tmpDir, storePath }) => {
const { cfg, sessionKey } = await createConfig(tmpDir); const { sendTelegram, getReplySpy } = createHeartbeatDeps("Relay this reminder now");
enqueue(sessionKey); const { cfg, sessionKey } = await createConfig({ tmpDir, storePath });
const result = await runHeartbeatOnce({ enqueue(sessionKey);
cfg, const result = await runHeartbeatOnce({
agentId: "main", cfg,
reason: "cron:reminder-job", agentId: "main",
deps: { reason: "cron:reminder-job",
sendTelegram, deps: {
}, sendTelegram,
}); },
return { result, sendTelegram, getReplySpy }; });
}); const calledCtx = (getReplySpy.mock.calls[0]?.[0] ?? null) as {
Provider?: string;
Body?: string;
} | null;
return { result, sendTelegram, calledCtx };
},
{ prefix: tmpPrefix },
);
}; };
it("does not use CRON_EVENT_PROMPT when only a HEARTBEAT_OK event is present", async () => { it("does not use CRON_EVENT_PROMPT when only a HEARTBEAT_OK event is present", async () => {
await withTempDir("openclaw-ghost-", async (tmpDir) => { await withTempHeartbeatSandbox(
const { sendTelegram, getReplySpy } = createHeartbeatDeps("Heartbeat check-in"); async ({ tmpDir, storePath }) => {
const { cfg } = await createConfig(tmpDir); const { sendTelegram, getReplySpy } = createHeartbeatDeps("Heartbeat check-in");
enqueueSystemEvent("HEARTBEAT_OK", { sessionKey: resolveMainSessionKey(cfg) }); const { cfg, sessionKey } = await createConfig({ tmpDir, storePath });
enqueueSystemEvent("HEARTBEAT_OK", { sessionKey });
const result = await runHeartbeatOnce({ const result = await runHeartbeatOnce({
cfg, cfg,
agentId: "main", agentId: "main",
reason: "cron:test-job", reason: "cron:test-job",
deps: { deps: {
sendTelegram, sendTelegram,
}, },
}); });
expect(result.status).toBe("ran"); expect(result.status).toBe("ran");
expect(getReplySpy).toHaveBeenCalledTimes(1); expect(getReplySpy).toHaveBeenCalledTimes(1);
const calledCtx = getReplySpy.mock.calls[0]?.[0]; const calledCtx = getReplySpy.mock.calls[0]?.[0];
expect(calledCtx?.Provider).toBe("heartbeat"); expect(calledCtx?.Provider).toBe("heartbeat");
expect(calledCtx?.Body).not.toContain("scheduled reminder has been triggered"); expect(calledCtx?.Body).not.toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).not.toContain("relay this reminder"); expect(calledCtx?.Body).not.toContain("relay this reminder");
expect(sendTelegram).toHaveBeenCalled(); expect(sendTelegram).toHaveBeenCalled();
}); },
{ prefix: "openclaw-ghost-" },
);
}); });
it("uses CRON_EVENT_PROMPT when an actionable cron event exists", async () => { it("uses CRON_EVENT_PROMPT when an actionable cron event exists", async () => {
const { result, sendTelegram, getReplySpy } = await runCronReminderCase( const { result, sendTelegram, calledCtx } = await runCronReminderCase(
"openclaw-cron-", "openclaw-cron-",
(sessionKey) => { (sessionKey) => {
enqueueSystemEvent("Reminder: Check Base Scout results", { sessionKey }); enqueueSystemEvent("Reminder: Check Base Scout results", { sessionKey });
}, },
); );
expect(result.status).toBe("ran"); expect(result.status).toBe("ran");
expectCronEventPrompt(getReplySpy, "Reminder: Check Base Scout results"); expectCronEventPrompt(calledCtx, "Reminder: Check Base Scout results");
expect(sendTelegram).toHaveBeenCalled(); expect(sendTelegram).toHaveBeenCalled();
}); });
it("uses CRON_EVENT_PROMPT when cron events are mixed with heartbeat noise", async () => { it("uses CRON_EVENT_PROMPT when cron events are mixed with heartbeat noise", async () => {
const { result, sendTelegram, getReplySpy } = await runCronReminderCase( const { result, sendTelegram, calledCtx } = await runCronReminderCase(
"openclaw-cron-mixed-", "openclaw-cron-mixed-",
(sessionKey) => { (sessionKey) => {
enqueueSystemEvent("HEARTBEAT_OK", { sessionKey }); enqueueSystemEvent("HEARTBEAT_OK", { sessionKey });
@@ -169,37 +160,39 @@ describe("Ghost reminder bug (issue #13317)", () => {
}, },
); );
expect(result.status).toBe("ran"); expect(result.status).toBe("ran");
expectCronEventPrompt(getReplySpy, "Reminder: Check Base Scout results"); expectCronEventPrompt(calledCtx, "Reminder: Check Base Scout results");
expect(sendTelegram).toHaveBeenCalled(); expect(sendTelegram).toHaveBeenCalled();
}); });
it("uses CRON_EVENT_PROMPT for tagged cron events on interval wake", async () => { it("uses CRON_EVENT_PROMPT for tagged cron events on interval wake", async () => {
await withTempDir("openclaw-cron-interval-", async (tmpDir) => { await withTempHeartbeatSandbox(
await fs.writeFile(path.join(tmpDir, "HEARTBEAT.md"), "- Check status\n", "utf-8"); async ({ tmpDir, storePath }) => {
const { sendTelegram, getReplySpy } = createHeartbeatDeps("Relay this cron update now"); const { sendTelegram, getReplySpy } = createHeartbeatDeps("Relay this cron update now");
const { cfg, sessionKey } = await createConfig(tmpDir); const { cfg, sessionKey } = await createConfig({ tmpDir, storePath });
enqueueSystemEvent("Cron: QMD maintenance completed", { enqueueSystemEvent("Cron: QMD maintenance completed", {
sessionKey, sessionKey,
contextKey: "cron:qmd-maintenance", contextKey: "cron:qmd-maintenance",
}); });
const result = await runHeartbeatOnce({ const result = await runHeartbeatOnce({
cfg, cfg,
agentId: "main", agentId: "main",
reason: "interval", reason: "interval",
deps: { deps: {
sendTelegram, sendTelegram,
}, },
}); });
expect(result.status).toBe("ran"); expect(result.status).toBe("ran");
expect(getReplySpy).toHaveBeenCalledTimes(1); expect(getReplySpy).toHaveBeenCalledTimes(1);
const calledCtx = getReplySpy.mock.calls[0]?.[0]; const calledCtx = getReplySpy.mock.calls[0]?.[0];
expect(calledCtx?.Provider).toBe("cron-event"); expect(calledCtx?.Provider).toBe("cron-event");
expect(calledCtx?.Body).toContain("scheduled reminder has been triggered"); expect(calledCtx?.Body).toContain("scheduled reminder has been triggered");
expect(calledCtx?.Body).toContain("Cron: QMD maintenance completed"); expect(calledCtx?.Body).toContain("Cron: QMD maintenance completed");
expect(calledCtx?.Body).not.toContain("Read HEARTBEAT.md"); expect(calledCtx?.Body).not.toContain("Read HEARTBEAT.md");
expect(sendTelegram).toHaveBeenCalled(); expect(sendTelegram).toHaveBeenCalled();
}); },
{ prefix: "openclaw-cron-interval-" },
);
}); });
}); });