|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { telegramPlugin } from "../../extensions/telegram/src/channel.js"; |
| 3 | +import { setTelegramRuntime } from "../../extensions/telegram/src/runtime.js"; |
| 4 | +import { whatsappPlugin } from "../../extensions/whatsapp/src/channel.js"; |
| 5 | +import { setWhatsAppRuntime } from "../../extensions/whatsapp/src/runtime.js"; |
| 6 | +import * as replyModule from "../auto-reply/reply.js"; |
| 7 | +import type { OpenClawConfig } from "../config/config.js"; |
| 8 | +import { resolveMainSessionKey } from "../config/sessions.js"; |
| 9 | +import { setActivePluginRegistry } from "../plugins/runtime.js"; |
| 10 | +import { createPluginRuntime } from "../plugins/runtime/index.js"; |
| 11 | +import { createTestRegistry } from "../test-utils/channel-plugins.js"; |
| 12 | +import { runHeartbeatOnce } from "./heartbeat-runner.js"; |
| 13 | +import { seedSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js"; |
| 14 | + |
| 15 | +// Avoid pulling optional runtime deps during isolated runs. |
| 16 | +vi.mock("jiti", () => ({ createJiti: () => () => ({}) })); |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + const runtime = createPluginRuntime(); |
| 20 | + setTelegramRuntime(runtime); |
| 21 | + setWhatsAppRuntime(runtime); |
| 22 | + setActivePluginRegistry( |
| 23 | + createTestRegistry([ |
| 24 | + { pluginId: "whatsapp", plugin: whatsappPlugin, source: "test" }, |
| 25 | + { pluginId: "telegram", plugin: telegramPlugin, source: "test" }, |
| 26 | + ]), |
| 27 | + ); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + vi.restoreAllMocks(); |
| 32 | +}); |
| 33 | + |
| 34 | +describe("runHeartbeatOnce – responsePrefix template interpolation", () => { |
| 35 | + it("passes onModelSelected callback to getReplyFromConfig", async () => { |
| 36 | + await withTempHeartbeatSandbox( |
| 37 | + async ({ tmpDir, storePath }) => { |
| 38 | + const cfg: OpenClawConfig = { |
| 39 | + agents: { |
| 40 | + defaults: { |
| 41 | + workspace: tmpDir, |
| 42 | + heartbeat: { every: "5m", target: "whatsapp" }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + channels: { |
| 46 | + whatsapp: { |
| 47 | + allowFrom: ["*"], |
| 48 | + responsePrefix: "{model}: ", |
| 49 | + }, |
| 50 | + }, |
| 51 | + session: { store: storePath }, |
| 52 | + }; |
| 53 | + const sessionKey = resolveMainSessionKey(cfg); |
| 54 | + await seedSessionStore(storePath, sessionKey, { |
| 55 | + lastChannel: "whatsapp", |
| 56 | + lastProvider: "whatsapp", |
| 57 | + lastTo: "+1555", |
| 58 | + }); |
| 59 | + |
| 60 | + const replySpy = vi.spyOn(replyModule, "getReplyFromConfig"); |
| 61 | + replySpy.mockResolvedValue({ text: "HEARTBEAT_OK" }); |
| 62 | + |
| 63 | + await runHeartbeatOnce({ |
| 64 | + cfg, |
| 65 | + deps: { getQueueSize: () => 0, nowMs: () => 0 }, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(replySpy).toHaveBeenCalledTimes(1); |
| 69 | + const replyOpts = replySpy.mock.calls[0]?.[1]; |
| 70 | + expect(replyOpts).toHaveProperty("onModelSelected"); |
| 71 | + expect(typeof replyOpts?.onModelSelected).toBe("function"); |
| 72 | + }, |
| 73 | + { prefix: "openclaw-hb-prefix-" }, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("passes onModelSelected even without heartbeatModelOverride", async () => { |
| 78 | + await withTempHeartbeatSandbox( |
| 79 | + async ({ tmpDir, storePath }) => { |
| 80 | + const cfg: OpenClawConfig = { |
| 81 | + agents: { |
| 82 | + defaults: { |
| 83 | + workspace: tmpDir, |
| 84 | + heartbeat: { every: "5m", target: "whatsapp" }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + channels: { |
| 88 | + whatsapp: { |
| 89 | + allowFrom: ["*"], |
| 90 | + responsePrefix: "{model}: ", |
| 91 | + }, |
| 92 | + }, |
| 93 | + session: { store: storePath }, |
| 94 | + }; |
| 95 | + const sessionKey = resolveMainSessionKey(cfg); |
| 96 | + await seedSessionStore(storePath, sessionKey, { |
| 97 | + lastChannel: "whatsapp", |
| 98 | + lastProvider: "whatsapp", |
| 99 | + lastTo: "+1555", |
| 100 | + }); |
| 101 | + |
| 102 | + const replySpy = vi.spyOn(replyModule, "getReplyFromConfig"); |
| 103 | + replySpy.mockResolvedValue({ text: "HEARTBEAT_OK" }); |
| 104 | + |
| 105 | + await runHeartbeatOnce({ |
| 106 | + cfg, |
| 107 | + deps: { getQueueSize: () => 0, nowMs: () => 0 }, |
| 108 | + }); |
| 109 | + |
| 110 | + const replyOpts = replySpy.mock.calls[0]?.[1]; |
| 111 | + // onModelSelected should be present regardless of model override |
| 112 | + expect(replyOpts).toEqual( |
| 113 | + expect.objectContaining({ |
| 114 | + isHeartbeat: true, |
| 115 | + onModelSelected: expect.any(Function), |
| 116 | + }), |
| 117 | + ); |
| 118 | + }, |
| 119 | + { prefix: "openclaw-hb-prefix-no-override-" }, |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it("passes onModelSelected with heartbeatModelOverride", async () => { |
| 124 | + await withTempHeartbeatSandbox( |
| 125 | + async ({ tmpDir, storePath }) => { |
| 126 | + const cfg: OpenClawConfig = { |
| 127 | + agents: { |
| 128 | + defaults: { |
| 129 | + workspace: tmpDir, |
| 130 | + heartbeat: { every: "5m", target: "whatsapp", model: "ollama/llama3.2:1b" }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + channels: { |
| 134 | + whatsapp: { |
| 135 | + allowFrom: ["*"], |
| 136 | + responsePrefix: "{model}: ", |
| 137 | + }, |
| 138 | + }, |
| 139 | + session: { store: storePath }, |
| 140 | + }; |
| 141 | + const sessionKey = resolveMainSessionKey(cfg); |
| 142 | + await seedSessionStore(storePath, sessionKey, { |
| 143 | + lastChannel: "whatsapp", |
| 144 | + lastProvider: "whatsapp", |
| 145 | + lastTo: "+1555", |
| 146 | + }); |
| 147 | + |
| 148 | + const replySpy = vi.spyOn(replyModule, "getReplyFromConfig"); |
| 149 | + replySpy.mockResolvedValue({ text: "HEARTBEAT_OK" }); |
| 150 | + |
| 151 | + await runHeartbeatOnce({ |
| 152 | + cfg, |
| 153 | + deps: { getQueueSize: () => 0, nowMs: () => 0 }, |
| 154 | + }); |
| 155 | + |
| 156 | + const replyOpts = replySpy.mock.calls[0]?.[1]; |
| 157 | + expect(replyOpts).toEqual( |
| 158 | + expect.objectContaining({ |
| 159 | + isHeartbeat: true, |
| 160 | + heartbeatModelOverride: "ollama/llama3.2:1b", |
| 161 | + onModelSelected: expect.any(Function), |
| 162 | + }), |
| 163 | + ); |
| 164 | + }, |
| 165 | + { prefix: "openclaw-hb-prefix-with-override-" }, |
| 166 | + ); |
| 167 | + }); |
| 168 | +}); |
0 commit comments