test(heartbeat): use shared sandbox in sender target suite

This commit is contained in:
Peter Steinberger
2026-02-22 09:29:34 +00:00
parent 29e41d4c0a
commit c8d473c8e8

View File

@@ -1,13 +1,8 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
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 { runHeartbeatOnce } from "./heartbeat-runner.js"; import { runHeartbeatOnce } from "./heartbeat-runner.js";
import { installHeartbeatRunnerTestRuntime } from "./heartbeat-runner.test-harness.js"; import { installHeartbeatRunnerTestRuntime } from "./heartbeat-runner.test-harness.js";
import { seedSessionStore } from "./heartbeat-runner.test-utils.js"; import { seedMainSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js";
// Avoid pulling optional runtime deps during isolated runs. // Avoid pulling optional runtime deps during isolated runs.
vi.mock("jiti", () => ({ createJiti: () => () => ({}) })); vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
@@ -16,56 +11,51 @@ installHeartbeatRunnerTestRuntime({ includeSlack: true });
describe("runHeartbeatOnce", () => { describe("runHeartbeatOnce", () => {
it("uses the delivery target as sender when lastTo differs", async () => { it("uses the delivery target as sender when lastTo differs", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-")); await withTempHeartbeatSandbox(
await fs.writeFile(path.join(tmpDir, "HEARTBEAT.md"), "- Check status\n", "utf-8"); async ({ tmpDir, storePath, replySpy }) => {
const storePath = path.join(tmpDir, "sessions.json"); const cfg: OpenClawConfig = {
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig"); agents: {
try { defaults: {
const cfg: OpenClawConfig = { workspace: tmpDir,
agents: { heartbeat: {
defaults: { every: "5m",
workspace: tmpDir, target: "slack",
heartbeat: { to: "C0A9P2N8QHY",
every: "5m", },
target: "slack",
to: "C0A9P2N8QHY",
}, },
}, },
}, session: { store: storePath },
session: { store: storePath }, };
};
const sessionKey = resolveMainSessionKey(cfg);
await seedSessionStore(storePath, sessionKey, { await seedMainSessionStore(storePath, cfg, {
lastChannel: "telegram", lastChannel: "telegram",
lastProvider: "telegram", lastProvider: "telegram",
lastTo: "1644620762", lastTo: "1644620762",
}); });
replySpy.mockImplementation(async (ctx) => { replySpy.mockImplementation(async (ctx: { To?: string; From?: string }) => {
expect(ctx.To).toBe("C0A9P2N8QHY"); expect(ctx.To).toBe("C0A9P2N8QHY");
expect(ctx.From).toBe("C0A9P2N8QHY"); expect(ctx.From).toBe("C0A9P2N8QHY");
return { text: "ok" }; return { text: "ok" };
}); });
const sendSlack = vi.fn().mockResolvedValue({ const sendSlack = vi.fn().mockResolvedValue({
messageId: "m1", messageId: "m1",
channelId: "C0A9P2N8QHY", channelId: "C0A9P2N8QHY",
}); });
await runHeartbeatOnce({ await runHeartbeatOnce({
cfg, cfg,
deps: { deps: {
sendSlack, sendSlack,
getQueueSize: () => 0, getQueueSize: () => 0,
nowMs: () => 0, nowMs: () => 0,
}, },
}); });
expect(sendSlack).toHaveBeenCalled(); expect(sendSlack).toHaveBeenCalled();
} finally { },
replySpy.mockRestore(); { prefix: "openclaw-hb-" },
await fs.rm(tmpDir, { recursive: true, force: true }); );
}
}); });
}); });