mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 12:18:37 +00:00
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: f6e5f8172a
Co-authored-by: vikpos <24960005+vikpos@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import * as replyModule from "../auto-reply/reply.js";
|
|
import type { OpenClawConfig } from "../config/config.js";
|
|
import { resolveMainSessionKey } from "../config/sessions.js";
|
|
import { runHeartbeatOnce } from "./heartbeat-runner.js";
|
|
import { installHeartbeatRunnerTestRuntime } from "./heartbeat-runner.test-harness.js";
|
|
|
|
// Avoid pulling optional runtime deps during isolated runs.
|
|
vi.mock("jiti", () => ({ createJiti: () => () => ({}) }));
|
|
|
|
installHeartbeatRunnerTestRuntime({ includeSlack: true });
|
|
|
|
describe("runHeartbeatOnce", () => {
|
|
it("uses the delivery target as sender when lastTo differs", async () => {
|
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-"));
|
|
await fs.writeFile(path.join(tmpDir, "HEARTBEAT.md"), "- Check status\n", "utf-8");
|
|
const storePath = path.join(tmpDir, "sessions.json");
|
|
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
|
|
try {
|
|
const cfg: OpenClawConfig = {
|
|
agents: {
|
|
defaults: {
|
|
workspace: tmpDir,
|
|
heartbeat: {
|
|
every: "5m",
|
|
target: "slack",
|
|
to: "C0A9P2N8QHY",
|
|
},
|
|
},
|
|
},
|
|
session: { store: storePath },
|
|
};
|
|
const sessionKey = resolveMainSessionKey(cfg);
|
|
|
|
await fs.writeFile(
|
|
storePath,
|
|
JSON.stringify(
|
|
{
|
|
[sessionKey]: {
|
|
sessionId: "sid",
|
|
updatedAt: Date.now(),
|
|
lastChannel: "telegram",
|
|
lastProvider: "telegram",
|
|
lastTo: "1644620762",
|
|
},
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
);
|
|
|
|
replySpy.mockImplementation(async (ctx) => {
|
|
expect(ctx.To).toBe("C0A9P2N8QHY");
|
|
expect(ctx.From).toBe("C0A9P2N8QHY");
|
|
return { text: "ok" };
|
|
});
|
|
|
|
const sendSlack = vi.fn().mockResolvedValue({
|
|
messageId: "m1",
|
|
channelId: "C0A9P2N8QHY",
|
|
});
|
|
|
|
await runHeartbeatOnce({
|
|
cfg,
|
|
deps: {
|
|
sendSlack,
|
|
getQueueSize: () => 0,
|
|
nowMs: () => 0,
|
|
},
|
|
});
|
|
|
|
expect(sendSlack).toHaveBeenCalled();
|
|
} finally {
|
|
replySpy.mockRestore();
|
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|