From 66de7311c71e7d7060b6b68826d6338cf9b730a1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Mar 2026 01:14:21 +0000 Subject: [PATCH] test: share whatsapp outbound poll fixtures --- .../whatsapp/src/channel.outbound.test.ts | 22 +++++-------- .../plugins/outbound/whatsapp.poll.test.ts | 22 +++++-------- src/test-helpers/whatsapp-outbound.ts | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 src/test-helpers/whatsapp-outbound.ts diff --git a/extensions/whatsapp/src/channel.outbound.test.ts b/extensions/whatsapp/src/channel.outbound.test.ts index 758274619e0..70220dcac3b 100644 --- a/extensions/whatsapp/src/channel.outbound.test.ts +++ b/extensions/whatsapp/src/channel.outbound.test.ts @@ -1,5 +1,8 @@ -import type { OpenClawConfig } from "openclaw/plugin-sdk/whatsapp"; import { describe, expect, it, vi } from "vitest"; +import { + createWhatsAppPollFixture, + expectWhatsAppPollSent, +} from "../../../src/test-helpers/whatsapp-outbound.js"; const hoisted = vi.hoisted(() => ({ sendPollWhatsApp: vi.fn(async () => ({ messageId: "wa-poll-1", toJid: "1555@s.whatsapp.net" })), @@ -22,25 +25,16 @@ import { whatsappPlugin } from "./channel.js"; describe("whatsappPlugin outbound sendPoll", () => { it("threads cfg into runtime sendPollWhatsApp call", async () => { - const cfg = { marker: "resolved-cfg" } as OpenClawConfig; - const poll = { - question: "Lunch?", - options: ["Pizza", "Sushi"], - maxSelections: 1, - }; + const { cfg, poll, to, accountId } = createWhatsAppPollFixture(); const result = await whatsappPlugin.outbound!.sendPoll!({ cfg, - to: "+1555", + to, poll, - accountId: "work", + accountId, }); - expect(hoisted.sendPollWhatsApp).toHaveBeenCalledWith("+1555", poll, { - verbose: false, - accountId: "work", - cfg, - }); + expectWhatsAppPollSent(hoisted.sendPollWhatsApp, { cfg, poll, to, accountId }); expect(result).toEqual({ messageId: "wa-poll-1", toJid: "1555@s.whatsapp.net" }); }); }); diff --git a/src/channels/plugins/outbound/whatsapp.poll.test.ts b/src/channels/plugins/outbound/whatsapp.poll.test.ts index 7164a6b152e..6474322264a 100644 --- a/src/channels/plugins/outbound/whatsapp.poll.test.ts +++ b/src/channels/plugins/outbound/whatsapp.poll.test.ts @@ -1,5 +1,8 @@ import { describe, expect, it, vi } from "vitest"; -import type { OpenClawConfig } from "../../../config/config.js"; +import { + createWhatsAppPollFixture, + expectWhatsAppPollSent, +} from "../../../test-helpers/whatsapp-outbound.js"; const hoisted = vi.hoisted(() => ({ sendPollWhatsApp: vi.fn(async () => ({ messageId: "poll-1", toJid: "1555@s.whatsapp.net" })), @@ -17,25 +20,16 @@ import { whatsappOutbound } from "./whatsapp.js"; describe("whatsappOutbound sendPoll", () => { it("threads cfg through poll send options", async () => { - const cfg = { marker: "resolved-cfg" } as OpenClawConfig; - const poll = { - question: "Lunch?", - options: ["Pizza", "Sushi"], - maxSelections: 1, - }; + const { cfg, poll, to, accountId } = createWhatsAppPollFixture(); const result = await whatsappOutbound.sendPoll!({ cfg, - to: "+1555", + to, poll, - accountId: "work", + accountId, }); - expect(hoisted.sendPollWhatsApp).toHaveBeenCalledWith("+1555", poll, { - verbose: false, - accountId: "work", - cfg, - }); + expectWhatsAppPollSent(hoisted.sendPollWhatsApp, { cfg, poll, to, accountId }); expect(result).toEqual({ messageId: "poll-1", toJid: "1555@s.whatsapp.net" }); }); }); diff --git a/src/test-helpers/whatsapp-outbound.ts b/src/test-helpers/whatsapp-outbound.ts new file mode 100644 index 00000000000..71433107fba --- /dev/null +++ b/src/test-helpers/whatsapp-outbound.ts @@ -0,0 +1,33 @@ +import { expect, type MockInstance } from "vitest"; +import type { OpenClawConfig } from "../config/config.js"; + +export function createWhatsAppPollFixture() { + const cfg = { marker: "resolved-cfg" } as OpenClawConfig; + const poll = { + question: "Lunch?", + options: ["Pizza", "Sushi"], + maxSelections: 1, + }; + return { + cfg, + poll, + to: "+1555", + accountId: "work", + }; +} + +export function expectWhatsAppPollSent( + sendPollWhatsApp: MockInstance, + params: { + cfg: OpenClawConfig; + poll: { question: string; options: string[]; maxSelections: number }; + to?: string; + accountId?: string; + }, +) { + expect(sendPollWhatsApp).toHaveBeenCalledWith(params.to ?? "+1555", params.poll, { + verbose: false, + accountId: params.accountId ?? "work", + cfg: params.cfg, + }); +}