refactor(test): dedupe web broadcast group inbound setup

This commit is contained in:
Peter Steinberger
2026-02-14 21:10:53 +00:00
parent 3c043f5d2d
commit 0b59c48087
3 changed files with 45 additions and 42 deletions

View File

@@ -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<void>)
| undefined;
const listenerFactory = async (opts: {
onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise<void>;
}) => {
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);

View File

@@ -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<void>)
| undefined;
const listenerFactory = async (opts: {
onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise<void>;
}) => {
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);

View File

@@ -180,3 +180,29 @@ export async function sendWebGroupInboundMessage(params: {
sendMedia: params.spies.sendMedia,
} as WebInboundMessage);
}
export async function sendWebDirectInboundMessage(params: {
onMessage: (msg: WebInboundMessage) => Promise<void>;
body: string;
id: string;
from: string;
to: string;
spies: ReturnType<typeof createWebInboundDeliverySpies>;
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);
}