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, installWebAutoReplyTestHomeHooks,
installWebAutoReplyUnitTestHooks, installWebAutoReplyUnitTestHooks,
resetLoadConfigMock, resetLoadConfigMock,
sendWebDirectInboundMessage,
sendWebGroupInboundMessage, sendWebGroupInboundMessage,
setLoadConfigMock, setLoadConfigMock,
} from "./auto-reply.test-harness.js"; } from "./auto-reply.test-harness.js";
@@ -30,40 +31,27 @@ describe("broadcast groups", () => {
}, },
} satisfies OpenClawConfig); } satisfies OpenClawConfig);
const sendMedia = vi.fn();
const reply = vi.fn().mockResolvedValue(undefined);
const sendComposing = vi.fn();
const seen: string[] = []; const seen: string[] = [];
const resolver = vi.fn(async (ctx: { SessionKey?: unknown }) => { const resolver = vi.fn(async (ctx: { SessionKey?: unknown }) => {
seen.push(String(ctx.SessionKey)); seen.push(String(ctx.SessionKey));
return { text: "ok" }; return { text: "ok" };
}); });
let capturedOnMessage: const spies = createWebInboundDeliverySpies();
| ((msg: import("./inbound.js").WebInboundMessage) => Promise<void>)
| undefined; const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
const listenerFactory = async (opts: {
onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise<void>;
}) => {
capturedOnMessage = opts.onMessage;
return { close: vi.fn() };
};
await monitorWebChannel(false, listenerFactory, false, resolver); await monitorWebChannel(false, listenerFactory, false, resolver);
expect(capturedOnMessage).toBeDefined(); const onMessage = getOnMessage();
expect(onMessage).toBeDefined();
await capturedOnMessage?.({ await sendWebDirectInboundMessage({
onMessage: onMessage!,
spies,
id: "m1", id: "m1",
from: "+1000", from: "+1000",
conversationId: "+1000",
to: "+2000", to: "+2000",
body: "hello", body: "hello",
timestamp: Date.now(),
chatType: "direct",
chatId: "direct:+1000",
sendComposing,
reply,
sendMedia,
}); });
expect(resolver).toHaveBeenCalledTimes(2); 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 type { OpenClawConfig } from "../config/config.js";
import { monitorWebChannel } from "./auto-reply.js"; import { monitorWebChannel } from "./auto-reply.js";
import { import {
createWebInboundDeliverySpies,
createWebListenerFactoryCapture,
installWebAutoReplyTestHomeHooks, installWebAutoReplyTestHomeHooks,
installWebAutoReplyUnitTestHooks, installWebAutoReplyUnitTestHooks,
resetLoadConfigMock, resetLoadConfigMock,
sendWebDirectInboundMessage,
setLoadConfigMock, setLoadConfigMock,
} from "./auto-reply.test-harness.js"; } from "./auto-reply.test-harness.js";
@@ -26,40 +29,26 @@ describe("broadcast groups", () => {
}, },
} satisfies OpenClawConfig); } satisfies OpenClawConfig);
const sendMedia = vi.fn();
const reply = vi.fn().mockResolvedValue(undefined);
const sendComposing = vi.fn();
const seen: string[] = []; const seen: string[] = [];
const resolver = vi.fn(async (ctx: { SessionKey?: unknown }) => { const resolver = vi.fn(async (ctx: { SessionKey?: unknown }) => {
seen.push(String(ctx.SessionKey)); seen.push(String(ctx.SessionKey));
return { text: "ok" }; return { text: "ok" };
}); });
let capturedOnMessage: const spies = createWebInboundDeliverySpies();
| ((msg: import("./inbound.js").WebInboundMessage) => Promise<void>) const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
| undefined;
const listenerFactory = async (opts: {
onMessage: (msg: import("./inbound.js").WebInboundMessage) => Promise<void>;
}) => {
capturedOnMessage = opts.onMessage;
return { close: vi.fn() };
};
await monitorWebChannel(false, listenerFactory, false, resolver); await monitorWebChannel(false, listenerFactory, false, resolver);
expect(capturedOnMessage).toBeDefined(); const onMessage = getOnMessage();
expect(onMessage).toBeDefined();
await capturedOnMessage?.({ await sendWebDirectInboundMessage({
onMessage: onMessage!,
spies,
id: "m1", id: "m1",
from: "+1000", from: "+1000",
conversationId: "+1000",
to: "+2000", to: "+2000",
body: "hello", body: "hello",
timestamp: Date.now(),
chatType: "direct",
chatId: "direct:+1000",
sendComposing,
reply,
sendMedia,
}); });
expect(resolver).toHaveBeenCalledTimes(1); expect(resolver).toHaveBeenCalledTimes(1);

View File

@@ -180,3 +180,29 @@ export async function sendWebGroupInboundMessage(params: {
sendMedia: params.spies.sendMedia, sendMedia: params.spies.sendMedia,
} as WebInboundMessage); } 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);
}