test(telegram): table-drive sticker and forum-topic cases

This commit is contained in:
Peter Steinberger
2026-02-21 23:04:40 +00:00
parent b1c50cc5c0
commit d5cc357737

View File

@@ -1077,26 +1077,46 @@ describe("sendStickerTelegram", () => {
botCtorSpy.mockReset(); botCtorSpy.mockReset();
}); });
it("sends a sticker by file_id", async () => { 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;
for (const testCase of positiveSendCases) {
it(testCase.name, async () => {
const chatId = "123"; const chatId = "123";
const fileId = "CAACAgIAAxkBAAI...sticker_file_id";
const sendSticker = vi.fn().mockResolvedValue({ const sendSticker = vi.fn().mockResolvedValue({
message_id: 100, message_id: testCase.expectedMessageId,
chat: { id: chatId }, chat: { id: chatId },
}); });
const api = { sendSticker } as unknown as { const api = { sendSticker } as unknown as {
sendSticker: typeof sendSticker; sendSticker: typeof sendSticker;
}; };
const res = await sendStickerTelegram(chatId, fileId, { const res = await sendStickerTelegram(chatId, testCase.fileId, {
token: "tok", token: "tok",
api, api,
}); });
expect(sendSticker).toHaveBeenCalledWith(chatId, fileId, undefined); expect(sendSticker).toHaveBeenCalledWith(chatId, testCase.expectedFileId, undefined);
expect(res.messageId).toBe("100"); if (testCase.assertResult) {
expect(res.messageId).toBe(String(testCase.expectedMessageId));
expect(res.chatId).toBe(chatId); expect(res.chatId).toBe(chatId);
}
}); });
}
it("throws error when fileId is blank", async () => { it("throws error when fileId is blank", async () => {
for (const fileId of ["", " "]) { for (const fileId of ["", " "]) {
@@ -1132,24 +1152,6 @@ describe("sendStickerTelegram", () => {
expect(sendSticker).toHaveBeenNthCalledWith(2, chatId, "fileId123", undefined); expect(sendSticker).toHaveBeenNthCalledWith(2, chatId, "fileId123", undefined);
expect(res.messageId).toBe("109"); 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", () => { describe("shared send behaviors", () => {
@@ -1438,43 +1440,54 @@ describe("sendPollTelegram", () => {
}); });
describe("createForumTopicTelegram", () => { describe("createForumTopicTelegram", () => {
it("uses base chat id when target includes topic suffix", async () => { const cases = [
const createForumTopic = vi.fn().mockResolvedValue({ {
message_thread_id: 272, name: "uses base chat id when target includes topic suffix",
name: "Build Updates", target: "telegram:group:-1001234567890:topic:271",
}); title: "x",
const api = { createForumTopic } as unknown as Bot["api"]; response: { message_thread_id: 272, name: "Build Updates" },
expectedCall: ["-1001234567890", "x", undefined] as const,
const result = await createForumTopicTelegram("telegram:group:-1001234567890:topic:271", "x", { expectedResult: {
token: "tok",
api,
});
expect(createForumTopic).toHaveBeenCalledWith("-1001234567890", "x", undefined);
expect(result).toEqual({
topicId: 272, topicId: 272,
name: "Build Updates", name: "Build Updates",
chatId: "-1001234567890", chatId: "-1001234567890",
}); },
}); },
{
it("forwards optional icon fields", async () => { name: "forwards optional icon fields",
const createForumTopic = vi.fn().mockResolvedValue({ target: "-1001234567890",
message_thread_id: 300, title: "Roadmap",
name: "Roadmap", response: { message_thread_id: 300, name: "Roadmap" },
}); options: {
const api = { createForumTopic } as unknown as Bot["api"];
await createForumTopicTelegram("-1001234567890", "Roadmap", {
token: "tok",
api,
iconColor: 0x6fb9f0, iconColor: 0x6fb9f0,
iconCustomEmojiId: " 1234567890 ", iconCustomEmojiId: " 1234567890 ",
},
expectedCall: [
"-1001234567890",
"Roadmap",
{ icon_color: 0x6fb9f0, icon_custom_emoji_id: "1234567890" },
] as const,
expectedResult: {
topicId: 300,
name: "Roadmap",
chatId: "-1001234567890",
},
},
] as const;
for (const testCase of cases) {
it(testCase.name, async () => {
const createForumTopic = vi.fn().mockResolvedValue(testCase.response);
const api = { createForumTopic } as unknown as Bot["api"];
const result = await createForumTopicTelegram(testCase.target, testCase.title, {
token: "tok",
api,
...testCase.options,
}); });
expect(createForumTopic).toHaveBeenCalledWith("-1001234567890", "Roadmap", { expect(createForumTopic).toHaveBeenCalledWith(...testCase.expectedCall);
icon_color: 0x6fb9f0, expect(result).toEqual(testCase.expectedResult);
icon_custom_emoji_id: "1234567890",
});
}); });
}
}); });