From 04a23f45b755fdba4123b1a76ec57378a5b7ae65 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 19:58:56 +0000 Subject: [PATCH] test(channels): dedupe whatsapp heartbeat fixtures and cover recipient sources --- .../plugins/whatsapp-heartbeat.test.ts | 91 ++++++++++++++++--- 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/src/channels/plugins/whatsapp-heartbeat.test.ts b/src/channels/plugins/whatsapp-heartbeat.test.ts index 6d430ccf8dd..acde8d0650a 100644 --- a/src/channels/plugins/whatsapp-heartbeat.test.ts +++ b/src/channels/plugins/whatsapp-heartbeat.test.ts @@ -23,49 +23,114 @@ function makeCfg(overrides?: Partial): OpenClawConfig { } describe("resolveWhatsAppHeartbeatRecipients", () => { + function setSessionStore(store: ReturnType) { + vi.mocked(loadSessionStore).mockReturnValue(store); + } + + function setAllowFromStore(entries: string[]) { + vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(entries); + } + + function resolveWith( + cfgOverrides: Partial = {}, + opts?: Parameters[1], + ) { + return resolveWhatsAppHeartbeatRecipients(makeCfg(cfgOverrides), opts); + } + beforeEach(() => { vi.mocked(loadSessionStore).mockReset(); vi.mocked(readChannelAllowFromStoreSync).mockReset(); - vi.mocked(readChannelAllowFromStoreSync).mockReturnValue([]); + setAllowFromStore([]); }); it("uses allowFrom store recipients when session recipients are ambiguous", () => { - vi.mocked(loadSessionStore).mockReturnValue({ + setSessionStore({ a: { lastChannel: "whatsapp", lastTo: "+15550000001", updatedAt: 2, sessionId: "a" }, b: { lastChannel: "whatsapp", lastTo: "+15550000002", updatedAt: 1, sessionId: "b" }, }); - vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(["+15550000001"]); + setAllowFromStore(["+15550000001"]); - const cfg = makeCfg(); - const result = resolveWhatsAppHeartbeatRecipients(cfg); + const result = resolveWith(); expect(result).toEqual({ recipients: ["+15550000001"], source: "session-single" }); }); it("falls back to allowFrom when no session recipient is authorized", () => { - vi.mocked(loadSessionStore).mockReturnValue({ + setSessionStore({ a: { lastChannel: "whatsapp", lastTo: "+15550000099", updatedAt: 2, sessionId: "a" }, }); - vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(["+15550000001"]); + setAllowFromStore(["+15550000001"]); - const cfg = makeCfg(); - const result = resolveWhatsAppHeartbeatRecipients(cfg); + const result = resolveWith(); expect(result).toEqual({ recipients: ["+15550000001"], source: "allowFrom" }); }); it("includes both session and allowFrom recipients when --all is set", () => { - vi.mocked(loadSessionStore).mockReturnValue({ + setSessionStore({ a: { lastChannel: "whatsapp", lastTo: "+15550000099", updatedAt: 2, sessionId: "a" }, }); - vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(["+15550000001"]); + setAllowFromStore(["+15550000001"]); - const cfg = makeCfg(); - const result = resolveWhatsAppHeartbeatRecipients(cfg, { all: true }); + const result = resolveWith({}, { all: true }); expect(result).toEqual({ recipients: ["+15550000099", "+15550000001"], source: "all", }); }); + + it("returns explicit --to recipient and source flag", () => { + setSessionStore({ + a: { lastChannel: "whatsapp", lastTo: "+15550000099", updatedAt: 2, sessionId: "a" }, + }); + const result = resolveWith({}, { to: " +1 555 000 7777 " }); + expect(result).toEqual({ recipients: ["+15550007777"], source: "flag" }); + }); + + it("returns ambiguous session recipients when no allowFrom list exists", () => { + setSessionStore({ + a: { lastChannel: "whatsapp", lastTo: "+15550000001", updatedAt: 2, sessionId: "a" }, + b: { lastChannel: "whatsapp", lastTo: "+15550000002", updatedAt: 1, sessionId: "b" }, + }); + const result = resolveWith(); + expect(result).toEqual({ + recipients: ["+15550000001", "+15550000002"], + source: "session-ambiguous", + }); + }); + + it("returns single session recipient when allowFrom is empty", () => { + setSessionStore({ + a: { lastChannel: "whatsapp", lastTo: "+15550000001", updatedAt: 2, sessionId: "a" }, + }); + const result = resolveWith(); + expect(result).toEqual({ recipients: ["+15550000001"], source: "session-single" }); + }); + + it("returns all authorized session recipients when allowFrom matches multiple", () => { + setSessionStore({ + a: { lastChannel: "whatsapp", lastTo: "+15550000001", updatedAt: 2, sessionId: "a" }, + b: { lastChannel: "whatsapp", lastTo: "+15550000002", updatedAt: 1, sessionId: "b" }, + c: { lastChannel: "whatsapp", lastTo: "+15550000003", updatedAt: 0, sessionId: "c" }, + }); + setAllowFromStore(["+15550000001", "+15550000002"]); + const result = resolveWith(); + expect(result).toEqual({ + recipients: ["+15550000001", "+15550000002"], + source: "session-ambiguous", + }); + }); + + it("ignores session store when session scope is global", () => { + setSessionStore({ + a: { lastChannel: "whatsapp", lastTo: "+15550000001", updatedAt: 2, sessionId: "a" }, + }); + const result = resolveWith({ + session: { scope: "global" } as OpenClawConfig["session"], + channels: { whatsapp: { allowFrom: ["*", "+15550000009"] } as never }, + }); + expect(result).toEqual({ recipients: ["+15550000009"], source: "allowFrom" }); + }); });