mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 17:44:57 +00:00
refactor(test): dedupe web broadcast group inbound setup
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user