From 0b59c480877e22148689d7622a99241623bdc7b5 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 21:10:53 +0000 Subject: [PATCH] refactor(test): dedupe web broadcast group inbound setup --- ...asts-sequentially-configured-order.test.ts | 30 ++++++------------ ...wn-broadcast-agent-ids-agents-list.test.ts | 31 ++++++------------- src/web/auto-reply.test-harness.ts | 26 ++++++++++++++++ 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/web/auto-reply.broadcast-groups.broadcasts-sequentially-configured-order.test.ts b/src/web/auto-reply.broadcast-groups.broadcasts-sequentially-configured-order.test.ts index a2f7aa5a2a3..42a263708ae 100644 --- a/src/web/auto-reply.broadcast-groups.broadcasts-sequentially-configured-order.test.ts +++ b/src/web/auto-reply.broadcast-groups.broadcasts-sequentially-configured-order.test.ts @@ -8,6 +8,7 @@ import { installWebAutoReplyTestHomeHooks, installWebAutoReplyUnitTestHooks, resetLoadConfigMock, + sendWebDirectInboundMessage, sendWebGroupInboundMessage, setLoadConfigMock, } from "./auto-reply.test-harness.js"; @@ -30,40 +31,27 @@ describe("broadcast groups", () => { }, } satisfies OpenClawConfig); - const sendMedia = vi.fn(); - const reply = vi.fn().mockResolvedValue(undefined); - const sendComposing = vi.fn(); const seen: string[] = []; const resolver = vi.fn(async (ctx: { SessionKey?: unknown }) => { seen.push(String(ctx.SessionKey)); return { text: "ok" }; }); - let capturedOnMessage: - | ((msg: import("./inbound.js").WebInboundMessage) => Promise) - | undefined; - const listenerFactory = async (opts: { - onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise; - }) => { - capturedOnMessage = opts.onMessage; - return { close: vi.fn() }; - }; + const spies = createWebInboundDeliverySpies(); + + const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture(); await monitorWebChannel(false, listenerFactory, false, resolver); - expect(capturedOnMessage).toBeDefined(); + const onMessage = getOnMessage(); + expect(onMessage).toBeDefined(); - await capturedOnMessage?.({ + await sendWebDirectInboundMessage({ + onMessage: onMessage!, + spies, id: "m1", from: "+1000", - conversationId: "+1000", to: "+2000", body: "hello", - timestamp: Date.now(), - chatType: "direct", - chatId: "direct:+1000", - sendComposing, - reply, - sendMedia, }); expect(resolver).toHaveBeenCalledTimes(2); diff --git a/src/web/auto-reply.broadcast-groups.skips-unknown-broadcast-agent-ids-agents-list.test.ts b/src/web/auto-reply.broadcast-groups.skips-unknown-broadcast-agent-ids-agents-list.test.ts index 5abe523cf5e..78bb489e435 100644 --- a/src/web/auto-reply.broadcast-groups.skips-unknown-broadcast-agent-ids-agents-list.test.ts +++ b/src/web/auto-reply.broadcast-groups.skips-unknown-broadcast-agent-ids-agents-list.test.ts @@ -3,9 +3,12 @@ import { describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../config/config.js"; import { monitorWebChannel } from "./auto-reply.js"; import { + createWebInboundDeliverySpies, + createWebListenerFactoryCapture, installWebAutoReplyTestHomeHooks, installWebAutoReplyUnitTestHooks, resetLoadConfigMock, + sendWebDirectInboundMessage, setLoadConfigMock, } from "./auto-reply.test-harness.js"; @@ -26,40 +29,26 @@ describe("broadcast groups", () => { }, } satisfies OpenClawConfig); - const sendMedia = vi.fn(); - const reply = vi.fn().mockResolvedValue(undefined); - const sendComposing = vi.fn(); const seen: string[] = []; const resolver = vi.fn(async (ctx: { SessionKey?: unknown }) => { seen.push(String(ctx.SessionKey)); return { text: "ok" }; }); - let capturedOnMessage: - | ((msg: import("./inbound.js").WebInboundMessage) => Promise) - | undefined; - const listenerFactory = async (opts: { - onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise; - }) => { - capturedOnMessage = opts.onMessage; - return { close: vi.fn() }; - }; + const spies = createWebInboundDeliverySpies(); + const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture(); await monitorWebChannel(false, listenerFactory, false, resolver); - expect(capturedOnMessage).toBeDefined(); + const onMessage = getOnMessage(); + expect(onMessage).toBeDefined(); - await capturedOnMessage?.({ + await sendWebDirectInboundMessage({ + onMessage: onMessage!, + spies, id: "m1", from: "+1000", - conversationId: "+1000", to: "+2000", body: "hello", - timestamp: Date.now(), - chatType: "direct", - chatId: "direct:+1000", - sendComposing, - reply, - sendMedia, }); expect(resolver).toHaveBeenCalledTimes(1); diff --git a/src/web/auto-reply.test-harness.ts b/src/web/auto-reply.test-harness.ts index 6ead596455f..f412e485ad2 100644 --- a/src/web/auto-reply.test-harness.ts +++ b/src/web/auto-reply.test-harness.ts @@ -180,3 +180,29 @@ export async function sendWebGroupInboundMessage(params: { sendMedia: params.spies.sendMedia, } as WebInboundMessage); } + +export async function sendWebDirectInboundMessage(params: { + onMessage: (msg: WebInboundMessage) => Promise; + body: string; + id: string; + from: string; + to: string; + spies: ReturnType; + accountId?: string; +}) { + const accountId = params.accountId ?? "default"; + await params.onMessage({ + accountId, + id: params.id, + from: params.from, + conversationId: params.from, + to: params.to, + body: params.body, + timestamp: Date.now(), + chatType: "direct", + chatId: `direct:${params.from}`, + sendComposing: params.spies.sendComposing, + reply: params.spies.reply, + sendMedia: params.spies.sendMedia, + } as WebInboundMessage); +}