refactor(test): simplify heartbeat model override tests

This commit is contained in:
Peter Steinberger
2026-02-15 15:36:58 +00:00
parent ee331e8d55
commit d979c6c089

View File

@@ -28,8 +28,8 @@ async function withHeartbeatFixture(
tmpDir: string; tmpDir: string;
storePath: string; storePath: string;
seedSession: (sessionKey: string, input: SeedSessionInput) => Promise<void>; seedSession: (sessionKey: string, input: SeedSessionInput) => Promise<void>;
}) => Promise<void>, }) => Promise<unknown>,
) { ): Promise<unknown> {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-model-")); const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hb-model-"));
const storePath = path.join(tmpDir, "sessions.json"); const storePath = path.join(tmpDir, "sessions.json");
@@ -52,7 +52,7 @@ async function withHeartbeatFixture(
}; };
try { try {
await run({ tmpDir, storePath, seedSession }); return await run({ tmpDir, storePath, seedSession });
} finally { } finally {
await fs.rm(tmpDir, { recursive: true, force: true }); await fs.rm(tmpDir, { recursive: true, force: true });
} }
@@ -75,8 +75,8 @@ afterEach(() => {
}); });
describe("runHeartbeatOnce heartbeat model override", () => { describe("runHeartbeatOnce heartbeat model override", () => {
it("passes heartbeatModelOverride from defaults heartbeat config", async () => { async function runDefaultsHeartbeat(params: { model?: string }) {
await withHeartbeatFixture(async ({ tmpDir, storePath, seedSession }) => { return withHeartbeatFixture(async ({ tmpDir, storePath, seedSession }) => {
const cfg: OpenClawConfig = { const cfg: OpenClawConfig = {
agents: { agents: {
defaults: { defaults: {
@@ -84,7 +84,7 @@ describe("runHeartbeatOnce heartbeat model override", () => {
heartbeat: { heartbeat: {
every: "5m", every: "5m",
target: "whatsapp", target: "whatsapp",
model: "ollama/llama3.2:1b", model: params.model,
}, },
}, },
}, },
@@ -105,14 +105,16 @@ describe("runHeartbeatOnce heartbeat model override", () => {
}, },
}); });
expect(replySpy).toHaveBeenCalledWith( expect(replySpy).toHaveBeenCalledTimes(1);
expect.any(Object), return replySpy.mock.calls[0]?.[1];
expect.objectContaining({ });
isHeartbeat: true, }
heartbeatModelOverride: "ollama/llama3.2:1b",
}), it("passes heartbeatModelOverride from defaults heartbeat config", async () => {
cfg, const replyOpts = await runDefaultsHeartbeat({ model: "ollama/llama3.2:1b" });
); expect(replyOpts).toEqual({
isHeartbeat: true,
heartbeatModelOverride: "ollama/llama3.2:1b",
}); });
}); });
@@ -168,79 +170,15 @@ describe("runHeartbeatOnce heartbeat model override", () => {
}); });
it("does not pass heartbeatModelOverride when no heartbeat model is configured", async () => { it("does not pass heartbeatModelOverride when no heartbeat model is configured", async () => {
await withHeartbeatFixture(async ({ tmpDir, storePath, seedSession }) => { const replyOpts = await runDefaultsHeartbeat({ model: undefined });
const cfg: OpenClawConfig = { expect(replyOpts).toStrictEqual({ isHeartbeat: true });
agents: {
defaults: {
workspace: tmpDir,
heartbeat: {
every: "5m",
target: "whatsapp",
},
},
},
channels: { whatsapp: { allowFrom: ["*"] } },
session: { store: storePath },
};
const sessionKey = resolveMainSessionKey(cfg);
await seedSession(sessionKey, { lastChannel: "whatsapp", lastTo: "+1555" });
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
replySpy.mockResolvedValue({ text: "HEARTBEAT_OK" });
await runHeartbeatOnce({
cfg,
deps: {
getQueueSize: () => 0,
nowMs: () => 0,
},
});
expect(replySpy).toHaveBeenCalledTimes(1);
const replyOpts = replySpy.mock.calls[0]?.[1];
expect(replyOpts).toStrictEqual({ isHeartbeat: true });
expect(replyOpts).not.toHaveProperty("heartbeatModelOverride");
});
}); });
it("trims heartbeat model override before passing it downstream", async () => { it("trims heartbeat model override before passing it downstream", async () => {
await withHeartbeatFixture(async ({ tmpDir, storePath, seedSession }) => { const replyOpts = await runDefaultsHeartbeat({ model: " ollama/llama3.2:1b " });
const cfg: OpenClawConfig = { expect(replyOpts).toEqual({
agents: { isHeartbeat: true,
defaults: { heartbeatModelOverride: "ollama/llama3.2:1b",
workspace: tmpDir,
heartbeat: {
every: "5m",
target: "whatsapp",
model: " ollama/llama3.2:1b ",
},
},
},
channels: { whatsapp: { allowFrom: ["*"] } },
session: { store: storePath },
};
const sessionKey = resolveMainSessionKey(cfg);
await seedSession(sessionKey, { lastChannel: "whatsapp", lastTo: "+1555" });
const replySpy = vi.spyOn(replyModule, "getReplyFromConfig");
replySpy.mockResolvedValue({ text: "HEARTBEAT_OK" });
await runHeartbeatOnce({
cfg,
deps: {
getQueueSize: () => 0,
nowMs: () => 0,
},
});
expect(replySpy).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
isHeartbeat: true,
heartbeatModelOverride: "ollama/llama3.2:1b",
}),
cfg,
);
}); });
}); });
}); });