From d5cc35773728f7e7eb29620382109f424803b5d0 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 21 Feb 2026 23:04:40 +0000 Subject: [PATCH] test(telegram): table-drive sticker and forum-topic cases --- src/telegram/send.test.ts | 155 +++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 71 deletions(-) diff --git a/src/telegram/send.test.ts b/src/telegram/send.test.ts index 41b10ed91..4abccf829 100644 --- a/src/telegram/send.test.ts +++ b/src/telegram/send.test.ts @@ -1077,26 +1077,46 @@ describe("sendStickerTelegram", () => { botCtorSpy.mockReset(); }); - it("sends a sticker by file_id", async () => { - const chatId = "123"; - const fileId = "CAACAgIAAxkBAAI...sticker_file_id"; - const sendSticker = vi.fn().mockResolvedValue({ - message_id: 100, - chat: { id: chatId }, - }); - const api = { sendSticker } as unknown as { - sendSticker: typeof sendSticker; - }; + const positiveSendCases = [ + { + name: "sends a sticker by file_id", + fileId: "CAACAgIAAxkBAAI...sticker_file_id", + expectedFileId: "CAACAgIAAxkBAAI...sticker_file_id", + expectedMessageId: 100, + assertResult: true, + }, + { + name: "trims whitespace from fileId", + fileId: " fileId123 ", + expectedFileId: "fileId123", + expectedMessageId: 106, + assertResult: false, + }, + ] as const; - const res = await sendStickerTelegram(chatId, fileId, { - token: "tok", - api, - }); + for (const testCase of positiveSendCases) { + it(testCase.name, async () => { + const chatId = "123"; + const sendSticker = vi.fn().mockResolvedValue({ + message_id: testCase.expectedMessageId, + chat: { id: chatId }, + }); + const api = { sendSticker } as unknown as { + sendSticker: typeof sendSticker; + }; - expect(sendSticker).toHaveBeenCalledWith(chatId, fileId, undefined); - expect(res.messageId).toBe("100"); - expect(res.chatId).toBe(chatId); - }); + const res = await sendStickerTelegram(chatId, testCase.fileId, { + token: "tok", + api, + }); + + expect(sendSticker).toHaveBeenCalledWith(chatId, testCase.expectedFileId, undefined); + if (testCase.assertResult) { + expect(res.messageId).toBe(String(testCase.expectedMessageId)); + expect(res.chatId).toBe(chatId); + } + }); + } it("throws error when fileId is blank", async () => { for (const fileId of ["", " "]) { @@ -1132,24 +1152,6 @@ describe("sendStickerTelegram", () => { expect(sendSticker).toHaveBeenNthCalledWith(2, chatId, "fileId123", undefined); expect(res.messageId).toBe("109"); }); - - it("trims whitespace from fileId", async () => { - const chatId = "123"; - const sendSticker = vi.fn().mockResolvedValue({ - message_id: 106, - chat: { id: chatId }, - }); - const api = { sendSticker } as unknown as { - sendSticker: typeof sendSticker; - }; - - await sendStickerTelegram(chatId, " fileId123 ", { - token: "tok", - api, - }); - - expect(sendSticker).toHaveBeenCalledWith(chatId, "fileId123", undefined); - }); }); describe("shared send behaviors", () => { @@ -1438,43 +1440,54 @@ describe("sendPollTelegram", () => { }); describe("createForumTopicTelegram", () => { - it("uses base chat id when target includes topic suffix", async () => { - const createForumTopic = vi.fn().mockResolvedValue({ - message_thread_id: 272, - name: "Build Updates", - }); - const api = { createForumTopic } as unknown as Bot["api"]; + const cases = [ + { + name: "uses base chat id when target includes topic suffix", + target: "telegram:group:-1001234567890:topic:271", + title: "x", + response: { message_thread_id: 272, name: "Build Updates" }, + expectedCall: ["-1001234567890", "x", undefined] as const, + expectedResult: { + topicId: 272, + name: "Build Updates", + chatId: "-1001234567890", + }, + }, + { + name: "forwards optional icon fields", + target: "-1001234567890", + title: "Roadmap", + response: { message_thread_id: 300, name: "Roadmap" }, + options: { + iconColor: 0x6fb9f0, + iconCustomEmojiId: " 1234567890 ", + }, + expectedCall: [ + "-1001234567890", + "Roadmap", + { icon_color: 0x6fb9f0, icon_custom_emoji_id: "1234567890" }, + ] as const, + expectedResult: { + topicId: 300, + name: "Roadmap", + chatId: "-1001234567890", + }, + }, + ] as const; - const result = await createForumTopicTelegram("telegram:group:-1001234567890:topic:271", "x", { - token: "tok", - api, - }); + for (const testCase of cases) { + it(testCase.name, async () => { + const createForumTopic = vi.fn().mockResolvedValue(testCase.response); + const api = { createForumTopic } as unknown as Bot["api"]; - expect(createForumTopic).toHaveBeenCalledWith("-1001234567890", "x", undefined); - expect(result).toEqual({ - topicId: 272, - name: "Build Updates", - chatId: "-1001234567890", - }); - }); + const result = await createForumTopicTelegram(testCase.target, testCase.title, { + token: "tok", + api, + ...testCase.options, + }); - it("forwards optional icon fields", async () => { - const createForumTopic = vi.fn().mockResolvedValue({ - message_thread_id: 300, - name: "Roadmap", + expect(createForumTopic).toHaveBeenCalledWith(...testCase.expectedCall); + expect(result).toEqual(testCase.expectedResult); }); - const api = { createForumTopic } as unknown as Bot["api"]; - - await createForumTopicTelegram("-1001234567890", "Roadmap", { - token: "tok", - api, - iconColor: 0x6fb9f0, - iconCustomEmojiId: " 1234567890 ", - }); - - expect(createForumTopic).toHaveBeenCalledWith("-1001234567890", "Roadmap", { - icon_color: 0x6fb9f0, - icon_custom_emoji_id: "1234567890", - }); - }); + } });