mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 05:32:53 +00:00
refactor(test): share web broadcast-groups harness
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
22
src/web/auto-reply.broadcast-groups.test-harness.ts
Normal file
22
src/web/auto-reply.broadcast-groups.test-harness.ts
Normal 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 };
|
||||
}
|
||||
Reference in New Issue
Block a user