test(channels): dedupe whatsapp heartbeat fixtures and cover recipient sources

This commit is contained in:
Peter Steinberger
2026-02-21 19:58:56 +00:00
parent 42e181dd4b
commit 04a23f45b7

View File

@@ -23,49 +23,114 @@ function makeCfg(overrides?: Partial<OpenClawConfig>): OpenClawConfig {
}
describe("resolveWhatsAppHeartbeatRecipients", () => {
function setSessionStore(store: ReturnType<typeof loadSessionStore>) {
vi.mocked(loadSessionStore).mockReturnValue(store);
}
function setAllowFromStore(entries: string[]) {
vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(entries);
}
function resolveWith(
cfgOverrides: Partial<OpenClawConfig> = {},
opts?: Parameters<typeof resolveWhatsAppHeartbeatRecipients>[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" });
});
});