mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 19:34:31 +00:00
refactor(test): share web broadcast-groups harness
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
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 { monitorWebChannelWithCapture } from "./auto-reply.broadcast-groups.test-harness.js";
|
||||||
import {
|
import {
|
||||||
createWebInboundDeliverySpies,
|
|
||||||
createWebListenerFactoryCapture,
|
|
||||||
installWebAutoReplyTestHomeHooks,
|
installWebAutoReplyTestHomeHooks,
|
||||||
installWebAutoReplyUnitTestHooks,
|
installWebAutoReplyUnitTestHooks,
|
||||||
resetLoadConfigMock,
|
resetLoadConfigMock,
|
||||||
@@ -37,16 +35,10 @@ describe("broadcast groups", () => {
|
|||||||
return { text: "ok" };
|
return { text: "ok" };
|
||||||
});
|
});
|
||||||
|
|
||||||
const spies = createWebInboundDeliverySpies();
|
const { spies, onMessage } = await monitorWebChannelWithCapture(resolver);
|
||||||
|
|
||||||
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
|
|
||||||
|
|
||||||
await monitorWebChannel(false, listenerFactory, false, resolver);
|
|
||||||
const onMessage = getOnMessage();
|
|
||||||
expect(onMessage).toBeDefined();
|
|
||||||
|
|
||||||
await sendWebDirectInboundMessage({
|
await sendWebDirectInboundMessage({
|
||||||
onMessage: onMessage!,
|
onMessage,
|
||||||
spies,
|
spies,
|
||||||
id: "m1",
|
id: "m1",
|
||||||
from: "+1000",
|
from: "+1000",
|
||||||
@@ -72,17 +64,12 @@ describe("broadcast groups", () => {
|
|||||||
},
|
},
|
||||||
} satisfies OpenClawConfig);
|
} satisfies OpenClawConfig);
|
||||||
|
|
||||||
const spies = createWebInboundDeliverySpies();
|
|
||||||
const resolver = vi.fn().mockResolvedValue({ text: "ok" });
|
const resolver = vi.fn().mockResolvedValue({ text: "ok" });
|
||||||
|
|
||||||
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
|
const { spies, onMessage } = await monitorWebChannelWithCapture(resolver);
|
||||||
|
|
||||||
await monitorWebChannel(false, listenerFactory, false, resolver);
|
|
||||||
const onMessage = getOnMessage();
|
|
||||||
expect(onMessage).toBeDefined();
|
|
||||||
|
|
||||||
await sendWebGroupInboundMessage({
|
await sendWebGroupInboundMessage({
|
||||||
onMessage: onMessage!,
|
onMessage,
|
||||||
spies,
|
spies,
|
||||||
body: "hello group",
|
body: "hello group",
|
||||||
id: "g1",
|
id: "g1",
|
||||||
@@ -94,7 +81,7 @@ describe("broadcast groups", () => {
|
|||||||
expect(resolver).not.toHaveBeenCalled();
|
expect(resolver).not.toHaveBeenCalled();
|
||||||
|
|
||||||
await sendWebGroupInboundMessage({
|
await sendWebGroupInboundMessage({
|
||||||
onMessage: onMessage!,
|
onMessage,
|
||||||
spies,
|
spies,
|
||||||
body: "@bot ping",
|
body: "@bot ping",
|
||||||
id: "g2",
|
id: "g2",
|
||||||
@@ -124,7 +111,7 @@ describe("broadcast groups", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await sendWebGroupInboundMessage({
|
await sendWebGroupInboundMessage({
|
||||||
onMessage: onMessage!,
|
onMessage,
|
||||||
spies,
|
spies,
|
||||||
body: "@bot ping 2",
|
body: "@bot ping 2",
|
||||||
id: "g3",
|
id: "g3",
|
||||||
@@ -177,20 +164,9 @@ describe("broadcast groups", () => {
|
|||||||
return { text: "ok" };
|
return { text: "ok" };
|
||||||
});
|
});
|
||||||
|
|
||||||
let capturedOnMessage:
|
const { onMessage: capturedOnMessage } = await monitorWebChannelWithCapture(resolver);
|
||||||
| ((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() };
|
|
||||||
};
|
|
||||||
|
|
||||||
await monitorWebChannel(false, listenerFactory, false, resolver);
|
await capturedOnMessage({
|
||||||
expect(capturedOnMessage).toBeDefined();
|
|
||||||
|
|
||||||
await capturedOnMessage?.({
|
|
||||||
id: "m1",
|
id: "m1",
|
||||||
from: "+1000",
|
from: "+1000",
|
||||||
conversationId: "+1000",
|
conversationId: "+1000",
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import "./test-helpers.js";
|
import "./test-helpers.js";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
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 { monitorWebChannelWithCapture } from "./auto-reply.broadcast-groups.test-harness.js";
|
||||||
import {
|
import {
|
||||||
createWebInboundDeliverySpies,
|
|
||||||
createWebListenerFactoryCapture,
|
|
||||||
installWebAutoReplyTestHomeHooks,
|
installWebAutoReplyTestHomeHooks,
|
||||||
installWebAutoReplyUnitTestHooks,
|
installWebAutoReplyUnitTestHooks,
|
||||||
resetLoadConfigMock,
|
resetLoadConfigMock,
|
||||||
@@ -35,15 +33,10 @@ describe("broadcast groups", () => {
|
|||||||
return { text: "ok" };
|
return { text: "ok" };
|
||||||
});
|
});
|
||||||
|
|
||||||
const spies = createWebInboundDeliverySpies();
|
const { spies, onMessage } = await monitorWebChannelWithCapture(resolver);
|
||||||
const { listenerFactory, getOnMessage } = createWebListenerFactoryCapture();
|
|
||||||
|
|
||||||
await monitorWebChannel(false, listenerFactory, false, resolver);
|
|
||||||
const onMessage = getOnMessage();
|
|
||||||
expect(onMessage).toBeDefined();
|
|
||||||
|
|
||||||
await sendWebDirectInboundMessage({
|
await sendWebDirectInboundMessage({
|
||||||
onMessage: onMessage!,
|
onMessage,
|
||||||
spies,
|
spies,
|
||||||
id: "m1",
|
id: "m1",
|
||||||
from: "+1000",
|
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