From de34a809f4d1efa578d19f39be7c352ea48e6bb7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 23:41:56 +0000 Subject: [PATCH] refactor(test): share telegram forum ctx helper --- ...topic-skill-filters-system-prompts.test.ts | 51 ++----------------- ...lowfrom-entries-case-insensitively.test.ts | 36 ++----------- .../bot.create-telegram-bot.test-harness.ts | 50 ++++++++++++++++++ 3 files changed, 57 insertions(+), 80 deletions(-) diff --git a/src/telegram/bot.create-telegram-bot.applies-topic-skill-filters-system-prompts.test.ts b/src/telegram/bot.create-telegram-bot.applies-topic-skill-filters-system-prompts.test.ts index d68d74454e..bf7486644b 100644 --- a/src/telegram/bot.create-telegram-bot.applies-topic-skill-filters-system-prompts.test.ts +++ b/src/telegram/bot.create-telegram-bot.applies-topic-skill-filters-system-prompts.test.ts @@ -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) => Promise; - 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) => Promise; - 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) => Promise; 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: "", }); diff --git a/src/telegram/bot.create-telegram-bot.matches-tg-prefixed-allowfrom-entries-case-insensitively.test.ts b/src/telegram/bot.create-telegram-bot.matches-tg-prefixed-allowfrom-entries-case-insensitively.test.ts index d91415deab..8e3717949f 100644 --- a/src/telegram/bot.create-telegram-bot.matches-tg-prefixed-allowfrom-entries-case-insensitively.test.ts +++ b/src/telegram/bot.create-telegram-bot.matches-tg-prefixed-allowfrom-entries-case-insensitively.test.ts @@ -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) => Promise; - 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) => Promise; - 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", { diff --git a/src/telegram/bot.create-telegram-bot.test-harness.ts b/src/telegram/bot.create-telegram-bot.test-harness.ts index 60824b8a2e..7eb9aaf874 100644 --- a/src/telegram/bot.create-telegram-bot.test-harness.ts +++ b/src/telegram/bot.create-telegram-bot.test-harness.ts @@ -176,6 +176,56 @@ export const getOnHandler = (event: string) => { return handler as (ctx: Record) => Promise; }; +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({