refactor(test): share telegram forum ctx helper

This commit is contained in:
Peter Steinberger
2026-02-14 23:41:56 +00:00
parent 165dbc232f
commit de34a809f4
3 changed files with 57 additions and 80 deletions

View File

@@ -3,6 +3,7 @@ import {
commandSpy,
getOnHandler,
getLoadConfigMock,
makeForumGroupMessageCtx,
onSpy,
replySpy,
sendMessageSpy,
@@ -42,23 +43,7 @@ describe("createTelegramBot", () => {
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
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() }),
});
await handler(makeForumGroupMessageCtx({ threadId: 99 }));
expect(replySpy).toHaveBeenCalledTimes(1);
const payload = replySpy.mock.calls[0][0];
@@ -85,23 +70,7 @@ describe("createTelegramBot", () => {
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
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() }),
});
await handler(makeForumGroupMessageCtx({ threadId: 99 }));
expect(sendMessageSpy).toHaveBeenCalledWith(
"-1001234567890",
@@ -132,19 +101,7 @@ describe("createTelegramBot", () => {
const handler = commandSpy.mock.calls[0][1] as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
message: {
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,
},
...makeForumGroupMessageCtx({ threadId: 99, text: "/status" }),
match: "",
});

View File

@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
getLoadConfigMock,
getOnHandler,
makeForumGroupMessageCtx,
onSpy,
replySpy,
sendChatActionSpy,
@@ -118,23 +119,7 @@ describe("createTelegramBot", () => {
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
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() }),
});
await handler(makeForumGroupMessageCtx({ threadId: 99 }));
expect(replySpy).toHaveBeenCalledTimes(1);
const payload = replySpy.mock.calls[0][0];
@@ -163,22 +148,7 @@ describe("createTelegramBot", () => {
createTelegramBot({ token: "tok" });
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
await handler({
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() }),
});
await handler(makeForumGroupMessageCtx({ threadId: undefined }));
expect(replySpy).toHaveBeenCalledTimes(1);
expect(sendChatActionSpy).toHaveBeenCalledWith(-1001234567890, "typing", {

View File

@@ -176,6 +176,56 @@ export const getOnHandler = (event: string) => {
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(() => {
resetInboundDedupe();
loadConfig.mockReturnValue({