mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 04:30:42 +00:00
refactor(test): share telegram forum ctx helper
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
commandSpy,
|
commandSpy,
|
||||||
getOnHandler,
|
getOnHandler,
|
||||||
getLoadConfigMock,
|
getLoadConfigMock,
|
||||||
|
makeForumGroupMessageCtx,
|
||||||
onSpy,
|
onSpy,
|
||||||
replySpy,
|
replySpy,
|
||||||
sendMessageSpy,
|
sendMessageSpy,
|
||||||
@@ -42,23 +43,7 @@ describe("createTelegramBot", () => {
|
|||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
|
|
||||||
await handler({
|
await handler(makeForumGroupMessageCtx({ threadId: 99 }));
|
||||||
message: {
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum Group",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
from: { id: 12345, username: "testuser" },
|
|
||||||
text: "hello",
|
|
||||||
date: 1736380800,
|
|
||||||
message_id: 42,
|
|
||||||
message_thread_id: 99,
|
|
||||||
},
|
|
||||||
me: { username: "openclaw_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||||
const payload = replySpy.mock.calls[0][0];
|
const payload = replySpy.mock.calls[0][0];
|
||||||
@@ -85,23 +70,7 @@ describe("createTelegramBot", () => {
|
|||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
|
|
||||||
await handler({
|
await handler(makeForumGroupMessageCtx({ threadId: 99 }));
|
||||||
message: {
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum Group",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
from: { id: 12345, username: "testuser" },
|
|
||||||
text: "hello",
|
|
||||||
date: 1736380800,
|
|
||||||
message_id: 42,
|
|
||||||
message_thread_id: 99,
|
|
||||||
},
|
|
||||||
me: { username: "openclaw_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(sendMessageSpy).toHaveBeenCalledWith(
|
expect(sendMessageSpy).toHaveBeenCalledWith(
|
||||||
"-1001234567890",
|
"-1001234567890",
|
||||||
@@ -132,19 +101,7 @@ describe("createTelegramBot", () => {
|
|||||||
const handler = commandSpy.mock.calls[0][1] as (ctx: Record<string, unknown>) => Promise<void>;
|
const handler = commandSpy.mock.calls[0][1] as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
|
|
||||||
await handler({
|
await handler({
|
||||||
message: {
|
...makeForumGroupMessageCtx({ threadId: 99, text: "/status" }),
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum Group",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
from: { id: 12345, username: "testuser" },
|
|
||||||
text: "/status",
|
|
||||||
date: 1736380800,
|
|
||||||
message_id: 42,
|
|
||||||
message_thread_id: 99,
|
|
||||||
},
|
|
||||||
match: "",
|
match: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
|
|||||||
import {
|
import {
|
||||||
getLoadConfigMock,
|
getLoadConfigMock,
|
||||||
getOnHandler,
|
getOnHandler,
|
||||||
|
makeForumGroupMessageCtx,
|
||||||
onSpy,
|
onSpy,
|
||||||
replySpy,
|
replySpy,
|
||||||
sendChatActionSpy,
|
sendChatActionSpy,
|
||||||
@@ -118,23 +119,7 @@ describe("createTelegramBot", () => {
|
|||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
|
|
||||||
await handler({
|
await handler(makeForumGroupMessageCtx({ threadId: 99 }));
|
||||||
message: {
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum Group",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
from: { id: 12345, username: "testuser" },
|
|
||||||
text: "hello",
|
|
||||||
date: 1736380800,
|
|
||||||
message_id: 42,
|
|
||||||
message_thread_id: 99,
|
|
||||||
},
|
|
||||||
me: { username: "openclaw_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||||
const payload = replySpy.mock.calls[0][0];
|
const payload = replySpy.mock.calls[0][0];
|
||||||
@@ -163,22 +148,7 @@ describe("createTelegramBot", () => {
|
|||||||
createTelegramBot({ token: "tok" });
|
createTelegramBot({ token: "tok" });
|
||||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
|
|
||||||
await handler({
|
await handler(makeForumGroupMessageCtx({ threadId: undefined }));
|
||||||
message: {
|
|
||||||
chat: {
|
|
||||||
id: -1001234567890,
|
|
||||||
type: "supergroup",
|
|
||||||
title: "Forum Group",
|
|
||||||
is_forum: true,
|
|
||||||
},
|
|
||||||
from: { id: 12345, username: "testuser" },
|
|
||||||
text: "hello",
|
|
||||||
date: 1736380800,
|
|
||||||
message_id: 42,
|
|
||||||
},
|
|
||||||
me: { username: "openclaw_bot" },
|
|
||||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(replySpy).toHaveBeenCalledTimes(1);
|
expect(replySpy).toHaveBeenCalledTimes(1);
|
||||||
expect(sendChatActionSpy).toHaveBeenCalledWith(-1001234567890, "typing", {
|
expect(sendChatActionSpy).toHaveBeenCalledWith(-1001234567890, "typing", {
|
||||||
|
|||||||
@@ -176,6 +176,56 @@ export const getOnHandler = (event: string) => {
|
|||||||
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
return handler as (ctx: Record<string, unknown>) => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function makeTelegramMessageCtx(params: {
|
||||||
|
chat: {
|
||||||
|
id: number;
|
||||||
|
type: string;
|
||||||
|
title?: string;
|
||||||
|
is_forum?: boolean;
|
||||||
|
};
|
||||||
|
from: { id: number; username?: string };
|
||||||
|
text: string;
|
||||||
|
date?: number;
|
||||||
|
messageId?: number;
|
||||||
|
messageThreadId?: number;
|
||||||
|
}) {
|
||||||
|
return {
|
||||||
|
message: {
|
||||||
|
chat: params.chat,
|
||||||
|
from: params.from,
|
||||||
|
text: params.text,
|
||||||
|
date: params.date ?? 1736380800,
|
||||||
|
message_id: params.messageId ?? 42,
|
||||||
|
...(params.messageThreadId === undefined
|
||||||
|
? {}
|
||||||
|
: { message_thread_id: params.messageThreadId }),
|
||||||
|
},
|
||||||
|
me: { username: "openclaw_bot" },
|
||||||
|
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function makeForumGroupMessageCtx(params?: {
|
||||||
|
chatId?: number;
|
||||||
|
threadId?: number;
|
||||||
|
text?: string;
|
||||||
|
fromId?: number;
|
||||||
|
username?: string;
|
||||||
|
title?: string;
|
||||||
|
}) {
|
||||||
|
return makeTelegramMessageCtx({
|
||||||
|
chat: {
|
||||||
|
id: params?.chatId ?? -1001234567890,
|
||||||
|
type: "supergroup",
|
||||||
|
title: params?.title ?? "Forum Group",
|
||||||
|
is_forum: true,
|
||||||
|
},
|
||||||
|
from: { id: params?.fromId ?? 12345, username: params?.username ?? "testuser" },
|
||||||
|
text: params?.text ?? "hello",
|
||||||
|
messageThreadId: params?.threadId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
resetInboundDedupe();
|
resetInboundDedupe();
|
||||||
loadConfig.mockReturnValue({
|
loadConfig.mockReturnValue({
|
||||||
|
|||||||
Reference in New Issue
Block a user