|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { HEARTBEAT_TOKEN } from "../auto-reply/tokens.js"; |
| 3 | +import { readCommitmentsForTest, seedCommitmentsForTest } from "../commitments/store.test-utils.js"; |
| 4 | +import type { CommitmentRecord } from "../commitments/types.js"; |
| 5 | +import type { OpenClawConfig } from "../config/config.js"; |
| 6 | +import { setActivePluginRegistry } from "../plugins/runtime.js"; |
| 7 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 8 | +import { createOutboundTestPlugin, createTestRegistry } from "../test-utils/channel-plugins.js"; |
| 9 | +import { captureEnv, setTestEnvValue } from "../test-utils/env.js"; |
| 10 | +import { runHeartbeatOnce, setHeartbeatsEnabled } from "./heartbeat-runner.js"; |
| 11 | +import { installHeartbeatRunnerTestRuntime } from "./heartbeat-runner.test-harness.js"; |
| 12 | +import { seedSessionStore, withTempHeartbeatSandbox } from "./heartbeat-runner.test-utils.js"; |
| 13 | + |
| 14 | +vi.mock("../commitments/config.js", async (importOriginal) => ({ |
| 15 | + ...(await importOriginal<typeof import("../commitments/config.js")>()), |
| 16 | + resolveCommitmentsConfig: () => ({ |
| 17 | + enabled: true, |
| 18 | + maxPerDay: 3, |
| 19 | + extraction: { |
| 20 | + debounceMs: 15_000, |
| 21 | + batchMaxItems: 8, |
| 22 | + queueMaxItems: 64, |
| 23 | + confidenceThreshold: 0.72, |
| 24 | + careConfidenceThreshold: 0.86, |
| 25 | + timeoutSeconds: 45, |
| 26 | + }, |
| 27 | + }), |
| 28 | +})); |
| 29 | + |
| 30 | +installHeartbeatRunnerTestRuntime({ includeSlack: true }); |
| 31 | + |
| 32 | +describe("runHeartbeatOnce cross-channel commitments", () => { |
| 33 | + const nowMs = Date.parse("2026-04-29T17:00:00.000Z"); |
| 34 | + const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + closeOpenClawStateDatabaseForTest(); |
| 38 | + setHeartbeatsEnabled(true); |
| 39 | + vi.unstubAllEnvs(); |
| 40 | + envSnapshot.restore(); |
| 41 | + }); |
| 42 | + |
| 43 | + it.each(["gchat", "google-chat", " GCHAT "])( |
| 44 | + "retains the configured account for a same-channel heartbeat alias (%s)", |
| 45 | + async (heartbeatTarget) => { |
| 46 | + await withTempHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => { |
| 47 | + setTestEnvValue("OPENCLAW_STATE_DIR", tmpDir); |
| 48 | + const sessionKey = "agent:main:googlechat:spaces-room"; |
| 49 | + const sendGoogleChat = vi.fn(); |
| 50 | + const plugin = createOutboundTestPlugin({ |
| 51 | + id: "googlechat", |
| 52 | + outbound: { |
| 53 | + deliveryMode: "direct", |
| 54 | + sendText: async ({ to, text, accountId }) => { |
| 55 | + sendGoogleChat(to, text, { accountId }); |
| 56 | + return { channel: "googlechat", messageId: "m1" }; |
| 57 | + }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + setActivePluginRegistry( |
| 61 | + createTestRegistry([{ pluginId: "googlechat", plugin, source: "test" }]), |
| 62 | + ); |
| 63 | + const cfg: OpenClawConfig = { |
| 64 | + agents: { |
| 65 | + defaults: { |
| 66 | + workspace: tmpDir, |
| 67 | + heartbeat: { |
| 68 | + every: "5m", |
| 69 | + target: heartbeatTarget, |
| 70 | + accountId: "configured", |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + channels: { |
| 75 | + googlechat: { |
| 76 | + accounts: { default: {}, configured: {} }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + session: { store: storePath }, |
| 80 | + }; |
| 81 | + await seedSessionStore(storePath, sessionKey, { |
| 82 | + lastChannel: "googlechat", |
| 83 | + lastProvider: "googlechat", |
| 84 | + lastTo: "spaces/room", |
| 85 | + }); |
| 86 | + const commitment: CommitmentRecord = { |
| 87 | + id: "cm_same_channel_alias", |
| 88 | + agentId: "main", |
| 89 | + sessionKey, |
| 90 | + channel: "googlechat", |
| 91 | + to: "spaces/room", |
| 92 | + kind: "event_check_in", |
| 93 | + sensitivity: "routine", |
| 94 | + source: "inferred_user_context", |
| 95 | + status: "pending", |
| 96 | + reason: "The user asked for a follow-up.", |
| 97 | + suggestedText: "How did the interview go?", |
| 98 | + dedupeKey: "same-channel-alias-account", |
| 99 | + confidence: 0.92, |
| 100 | + dueWindow: { earliestMs: nowMs - 60_000, latestMs: nowMs + 60_000, timezone: "UTC" }, |
| 101 | + createdAtMs: nowMs - 120_000, |
| 102 | + updatedAtMs: nowMs - 120_000, |
| 103 | + attempts: 0, |
| 104 | + }; |
| 105 | + seedCommitmentsForTest([commitment]); |
| 106 | + replySpy.mockImplementation(async (ctx) => { |
| 107 | + expect(ctx.Body).toContain(HEARTBEAT_TOKEN); |
| 108 | + expect(ctx.OriginatingChannel).toBe("googlechat"); |
| 109 | + expect(ctx.OriginatingTo).toBe("spaces/room"); |
| 110 | + return { text: "How did the interview go?" }; |
| 111 | + }); |
| 112 | + |
| 113 | + const result = await runHeartbeatOnce({ |
| 114 | + cfg, |
| 115 | + agentId: "main", |
| 116 | + sessionKey, |
| 117 | + deps: { |
| 118 | + getReplyFromConfig: replySpy, |
| 119 | + getQueueSize: () => 0, |
| 120 | + nowMs: () => nowMs, |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result.status).toBe("ran"); |
| 125 | + expect(sendGoogleChat).toHaveBeenCalledWith("spaces/room", "How did the interview go?", { |
| 126 | + accountId: "configured", |
| 127 | + }); |
| 128 | + expect(readCommitmentsForTest()[0]).toMatchObject({ |
| 129 | + id: commitment.id, |
| 130 | + status: "sent", |
| 131 | + attempts: 1, |
| 132 | + sentAtMs: nowMs, |
| 133 | + }); |
| 134 | + }); |
| 135 | + }, |
| 136 | + ); |
| 137 | + |
| 138 | + it("does not apply a configured heartbeat account to an accountless commitment on another channel", async () => { |
| 139 | + await withTempHeartbeatSandbox(async ({ tmpDir, storePath, replySpy }) => { |
| 140 | + setTestEnvValue("OPENCLAW_STATE_DIR", tmpDir); |
| 141 | + const sessionKey = "agent:main:telegram:user-155462274"; |
| 142 | + const cfg: OpenClawConfig = { |
| 143 | + agents: { |
| 144 | + defaults: { |
| 145 | + workspace: tmpDir, |
| 146 | + heartbeat: { every: "5m", target: "last", accountId: "configured" }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + channels: { |
| 150 | + slack: { allowFrom: ["*"] }, |
| 151 | + telegram: { |
| 152 | + allowFrom: ["*"], |
| 153 | + accounts: { |
| 154 | + configured: { botToken: "heartbeat-token" }, |
| 155 | + primary: { botToken: "primary-token" }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + session: { store: storePath }, |
| 160 | + }; |
| 161 | + await seedSessionStore(storePath, sessionKey, { |
| 162 | + lastChannel: "telegram", |
| 163 | + lastProvider: "telegram", |
| 164 | + lastTo: "155462274", |
| 165 | + }); |
| 166 | + const commitment: CommitmentRecord = { |
| 167 | + id: "cm_cross_channel", |
| 168 | + agentId: "main", |
| 169 | + sessionKey, |
| 170 | + channel: "slack", |
| 171 | + to: "C123", |
| 172 | + kind: "event_check_in", |
| 173 | + sensitivity: "routine", |
| 174 | + source: "inferred_user_context", |
| 175 | + status: "pending", |
| 176 | + reason: "The user asked for a follow-up.", |
| 177 | + suggestedText: "How did the interview go?", |
| 178 | + dedupeKey: "cross-channel-account", |
| 179 | + confidence: 0.92, |
| 180 | + dueWindow: { earliestMs: nowMs - 60_000, latestMs: nowMs + 60_000, timezone: "UTC" }, |
| 181 | + createdAtMs: nowMs - 120_000, |
| 182 | + updatedAtMs: nowMs - 120_000, |
| 183 | + attempts: 0, |
| 184 | + }; |
| 185 | + seedCommitmentsForTest([commitment]); |
| 186 | + |
| 187 | + const sendSlack = vi.fn().mockResolvedValue({ messageId: "m1", channelId: "C123" }); |
| 188 | + const sendTelegram = vi.fn(); |
| 189 | + replySpy.mockImplementation(async (ctx) => { |
| 190 | + expect(ctx.Body).toContain(HEARTBEAT_TOKEN); |
| 191 | + expect(ctx.OriginatingChannel).toBe("slack"); |
| 192 | + expect(ctx.OriginatingTo).toBe("C123"); |
| 193 | + return { text: "How did the interview go?" }; |
| 194 | + }); |
| 195 | + |
| 196 | + const result = await runHeartbeatOnce({ |
| 197 | + cfg, |
| 198 | + agentId: "main", |
| 199 | + sessionKey, |
| 200 | + deps: { |
| 201 | + getReplyFromConfig: replySpy, |
| 202 | + slack: sendSlack, |
| 203 | + telegram: sendTelegram, |
| 204 | + getQueueSize: () => 0, |
| 205 | + nowMs: () => nowMs, |
| 206 | + }, |
| 207 | + }); |
| 208 | + |
| 209 | + expect(result.status).toBe("ran"); |
| 210 | + expect(sendTelegram).not.toHaveBeenCalled(); |
| 211 | + expect(sendSlack).toHaveBeenCalledWith( |
| 212 | + "C123", |
| 213 | + "How did the interview go?", |
| 214 | + expect.objectContaining({ accountId: undefined }), |
| 215 | + ); |
| 216 | + expect(readCommitmentsForTest()[0]).toMatchObject({ |
| 217 | + id: commitment.id, |
| 218 | + status: "sent", |
| 219 | + attempts: 1, |
| 220 | + sentAtMs: nowMs, |
| 221 | + }); |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
0 commit comments