|
| 1 | +// Whatsapp tests cover the durable outbound handoff across startup recovery. |
| 2 | +import { |
| 3 | + createEmptyPluginRegistry, |
| 4 | + createOutboundTestPlugin, |
| 5 | + createTestRegistry, |
| 6 | + deliverOutboundPayloads, |
| 7 | + releasePinnedPluginChannelRegistry, |
| 8 | + setActivePluginRegistry, |
| 9 | +} from "openclaw/plugin-sdk/channel-test-helpers"; |
| 10 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 11 | +import { drainPendingDeliveries } from "openclaw/plugin-sdk/delivery-queue-runtime"; |
| 12 | +import { PlatformMessageNotDispatchedError } from "openclaw/plugin-sdk/error-runtime"; |
| 13 | +import { withStateDirEnv } from "openclaw/plugin-sdk/test-env"; |
| 14 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 15 | +import { whatsappChannelOutbound } from "./channel-outbound.js"; |
| 16 | +import { |
| 17 | + getRegisteredWhatsAppConnectionController, |
| 18 | + registerWhatsAppConnectionController, |
| 19 | + unregisterWhatsAppConnectionController, |
| 20 | +} from "./connection-controller-registry.js"; |
| 21 | +import { createAcceptedWhatsAppSendResult } from "./inbound/send-result.test-helper.js"; |
| 22 | +import type { ActiveWebListener } from "./inbound/types.js"; |
| 23 | + |
| 24 | +const cfg = { channels: { whatsapp: {} } } as OpenClawConfig; |
| 25 | +const accountId = "default"; |
| 26 | + |
| 27 | +function clearDefaultController(): void { |
| 28 | + const controller = getRegisteredWhatsAppConnectionController(accountId); |
| 29 | + if (controller) { |
| 30 | + unregisterWhatsAppConnectionController(accountId, controller); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async function drainDefaultWhatsAppDeliveries(stateDir: string) { |
| 35 | + const log = { |
| 36 | + info: vi.fn(), |
| 37 | + warn: vi.fn(), |
| 38 | + error: vi.fn(), |
| 39 | + }; |
| 40 | + await drainPendingDeliveries({ |
| 41 | + drainKey: `whatsapp:${accountId}`, |
| 42 | + logLabel: "WhatsApp reconnect drain", |
| 43 | + cfg, |
| 44 | + log, |
| 45 | + stateDir, |
| 46 | + selectEntry: (entry) => ({ |
| 47 | + match: |
| 48 | + entry.channel === "whatsapp" && ((entry.accountId ?? "").trim() || accountId) === accountId, |
| 49 | + bypassBackoff: |
| 50 | + typeof entry.lastError === "string" && |
| 51 | + entry.lastError.includes("No active WhatsApp Web listener"), |
| 52 | + }), |
| 53 | + }); |
| 54 | + return log; |
| 55 | +} |
| 56 | + |
| 57 | +describe("WhatsApp delivery recovery", () => { |
| 58 | + beforeEach(() => { |
| 59 | + clearDefaultController(); |
| 60 | + setActivePluginRegistry( |
| 61 | + createTestRegistry([ |
| 62 | + { |
| 63 | + pluginId: "whatsapp", |
| 64 | + source: "test", |
| 65 | + plugin: createOutboundTestPlugin({ |
| 66 | + id: "whatsapp", |
| 67 | + outbound: whatsappChannelOutbound, |
| 68 | + }), |
| 69 | + }, |
| 70 | + ]), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(() => { |
| 75 | + clearDefaultController(); |
| 76 | + releasePinnedPluginChannelRegistry(); |
| 77 | + setActivePluginRegistry(createEmptyPluginRegistry()); |
| 78 | + }); |
| 79 | + |
| 80 | + it("keeps pre-connect recovery replayable, then sends exactly once after connect", async () => { |
| 81 | + await withStateDirEnv("openclaw-whatsapp-delivery-recovery-", async ({ stateDir }) => { |
| 82 | + const initialError = await deliverOutboundPayloads({ |
| 83 | + cfg, |
| 84 | + channel: "whatsapp", |
| 85 | + to: "+1555", |
| 86 | + payloads: [{ text: "queued before listener startup" }], |
| 87 | + queuePolicy: "required", |
| 88 | + }).catch((err: unknown) => err); |
| 89 | + expect(initialError).toMatchObject({ |
| 90 | + cause: expect.any(PlatformMessageNotDispatchedError), |
| 91 | + }); |
| 92 | + |
| 93 | + const preConnectLog = await drainDefaultWhatsAppDeliveries(stateDir); |
| 94 | + expect(preConnectLog.warn).toHaveBeenCalledWith( |
| 95 | + expect.stringContaining("No active WhatsApp Web listener"), |
| 96 | + ); |
| 97 | + |
| 98 | + const sendMessage = vi.fn(async () => |
| 99 | + createAcceptedWhatsAppSendResult("text", "recovered-message"), |
| 100 | + ); |
| 101 | + const listener: ActiveWebListener = { |
| 102 | + sendComposingTo: vi.fn(async () => {}), |
| 103 | + sendMessage, |
| 104 | + sendPoll: vi.fn(async () => createAcceptedWhatsAppSendResult("poll", "poll")), |
| 105 | + sendReaction: vi.fn(async () => createAcceptedWhatsAppSendResult("reaction", "reaction")), |
| 106 | + }; |
| 107 | + const controller = { |
| 108 | + getActiveListener: () => listener, |
| 109 | + getCurrentSock: () => null, |
| 110 | + getSelfIdentity: () => null, |
| 111 | + }; |
| 112 | + registerWhatsAppConnectionController(accountId, controller); |
| 113 | + |
| 114 | + await drainDefaultWhatsAppDeliveries(stateDir); |
| 115 | + await drainDefaultWhatsAppDeliveries(stateDir); |
| 116 | + |
| 117 | + expect(sendMessage).toHaveBeenCalledTimes(1); |
| 118 | + expect(sendMessage).toHaveBeenCalledWith( |
| 119 | + "+1555", |
| 120 | + "queued before listener startup", |
| 121 | + undefined, |
| 122 | + undefined, |
| 123 | + ); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments