refactor(test): share web broadcast-groups harness

This commit is contained in:
Peter Steinberger
2026-02-15 15:03:14 +00:00
parent 86df160617
commit fe27215747
3 changed files with 34 additions and 43 deletions

View File

@@ -1,10 +1,8 @@
import "./test-helpers.js";
import { describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { monitorWebChannel } from "./auto-reply.js";
import { monitorWebChannelWithCapture } from "./auto-reply.broadcast-groups.test-harness.js";
import {
createWebInboundDeliverySpies,
createWebListenerFactoryCapture,
installWebAutoReplyTestHomeHooks,
installWebAutoReplyUnitTestHooks,
resetLoadConfigMock,
@@ -37,16 +35,10 @@ describe("broadcast groups", () => {
return { text: "ok" };
});
const spies = createWebInboundDeliverySpies();
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
await monitorWebChannel(false, listenerFactory, false, resolver);
const onMessage = getOnMessage();
expect(onMessage).toBeDefined();
const { spies, onMessage } = await monitorWebChannelWithCapture(resolver);
await sendWebDirectInboundMessage({
onMessage: onMessage!,
onMessage,
spies,
id: "m1",
from: "+1000",
@@ -72,17 +64,12 @@ describe("broadcast groups", () => {
},
} satisfies OpenClawConfig);
const spies = createWebInboundDeliverySpies();
const resolver = vi.fn().mockResolvedValue({ text: "ok" });
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
await monitorWebChannel(false, listenerFactory, false, resolver);
const onMessage = getOnMessage();
expect(onMessage).toBeDefined();
const { spies, onMessage } = await monitorWebChannelWithCapture(resolver);
await sendWebGroupInboundMessage({
onMessage: onMessage!,
onMessage,
spies,
body: "hello group",
id: "g1",
@@ -94,7 +81,7 @@ describe("broadcast groups", () => {
expect(resolver).not.toHaveBeenCalled();
await sendWebGroupInboundMessage({
onMessage: onMessage!,
onMessage,
spies,
body: "@bot ping",
id: "g2",
@@ -124,7 +111,7 @@ describe("broadcast groups", () => {
}
await sendWebGroupInboundMessage({
onMessage: onMessage!,
onMessage,
spies,
body: "@bot ping 2",
id: "g3",
@@ -177,20 +164,9 @@ describe("broadcast groups", () => {
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 { onMessage: capturedOnMessage } = await monitorWebChannelWithCapture(resolver);
await monitorWebChannel(false, listenerFactory, false, resolver);
expect(capturedOnMessage).toBeDefined();
await capturedOnMessage?.({
await capturedOnMessage({
id: "m1",
from: "+1000",
conversationId: "+1000",

View File

@@ -1,10 +1,8 @@
import "./test-helpers.js";
import { describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { monitorWebChannel } from "./auto-reply.js";
import { monitorWebChannelWithCapture } from "./auto-reply.broadcast-groups.test-harness.js";
import {
createWebInboundDeliverySpies,
createWebListenerFactoryCapture,
installWebAutoReplyTestHomeHooks,
installWebAutoReplyUnitTestHooks,
resetLoadConfigMock,
@@ -35,15 +33,10 @@ describe("broadcast groups", () => {
return { text: "ok" };
});
const spies = createWebInboundDeliverySpies();
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
await monitorWebChannel(false, listenerFactory, false, resolver);
const onMessage = getOnMessage();
expect(onMessage).toBeDefined();
const { spies, onMessage } = await monitorWebChannelWithCapture(resolver);
await sendWebDirectInboundMessage({
onMessage: onMessage!,
onMessage,
spies,
id: "m1",
from: "+1000",

View File

@@ -0,0 +1,22 @@
import type { WebInboundMessage } from "./inbound.js";
import { monitorWebChannel } from "./auto-reply.js";
import {
createWebInboundDeliverySpies,
createWebListenerFactoryCapture,
} from "./auto-reply.test-harness.js";
export async function monitorWebChannelWithCapture(resolver: unknown): Promise<{
spies: ReturnType<typeof createWebInboundDeliverySpies>;
onMessage: (msg: WebInboundMessage) => Promise<void>;
}> {
const spies = createWebInboundDeliverySpies();
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
await monitorWebChannel(false, listenerFactory, false, resolver as never);
const onMessage = getOnMessage();
if (!onMessage) {
throw new Error("Missing onMessage handler");
}
return { spies, onMessage };
}