fix: persist resolved telegram delivery targets at runtime

This commit is contained in:
Ayaan Zaidi
2026-02-23 09:13:35 +05:30
committed by Ayaan Zaidi
parent 35fbf26d24
commit dcc52850c3
8 changed files with 632 additions and 77 deletions

View File

@@ -9,7 +9,8 @@ import { clearSentMessageCache, recordSentMessage, wasSentByBot } from "./sent-m
installTelegramSendTestHooks();
const { botApi, botCtorSpy, loadConfig, loadWebMedia } = getTelegramSendTestMocks();
const { botApi, botCtorSpy, loadConfig, loadWebMedia, maybePersistResolvedTelegramTarget } =
getTelegramSendTestMocks();
const {
buildInlineKeyboard,
createForumTopicTelegram,
@@ -369,6 +370,48 @@ describe("sendMessageTelegram", () => {
});
});
it("resolves t.me targets to numeric chat ids via getChat", async () => {
const sendMessage = vi.fn().mockResolvedValue({
message_id: 1,
chat: { id: "-100123" },
});
const getChat = vi.fn().mockResolvedValue({ id: -100123 });
const api = { sendMessage, getChat } as unknown as {
sendMessage: typeof sendMessage;
getChat: typeof getChat;
};
await sendMessageTelegram("https://t.me/mychannel", "hi", {
token: "tok",
api,
});
expect(getChat).toHaveBeenCalledWith("@mychannel");
expect(sendMessage).toHaveBeenCalledWith("-100123", "hi", {
parse_mode: "HTML",
});
expect(maybePersistResolvedTelegramTarget).toHaveBeenCalledWith(
expect.objectContaining({
rawTarget: "https://t.me/mychannel",
resolvedChatId: "-100123",
}),
);
});
it("fails clearly when a legacy target cannot be resolved", async () => {
const getChat = vi.fn().mockRejectedValue(new Error("400: Bad Request: chat not found"));
const api = { getChat } as unknown as {
getChat: typeof getChat;
};
await expect(
sendMessageTelegram("@missingchannel", "hi", {
token: "tok",
api,
}),
).rejects.toThrow(/could not be resolved to a numeric chat ID/i);
});
it("includes thread params in media messages", async () => {
const chatId = "-1001234567890";
const sendPhoto = vi.fn().mockResolvedValue({
@@ -1100,6 +1143,31 @@ describe("reactMessageTelegram", () => {
expect(setMessageReaction).toHaveBeenCalledWith("123", 456, testCase.expected);
});
it("resolves legacy telegram targets before reacting", async () => {
const setMessageReaction = vi.fn().mockResolvedValue(undefined);
const getChat = vi.fn().mockResolvedValue({ id: -100123 });
const api = { setMessageReaction, getChat } as unknown as {
setMessageReaction: typeof setMessageReaction;
getChat: typeof getChat;
};
await reactMessageTelegram("@mychannel", 456, "✅", {
token: "tok",
api,
});
expect(getChat).toHaveBeenCalledWith("@mychannel");
expect(setMessageReaction).toHaveBeenCalledWith("-100123", 456, [
{ type: "emoji", emoji: "✅" },
]);
expect(maybePersistResolvedTelegramTarget).toHaveBeenCalledWith(
expect.objectContaining({
rawTarget: "@mychannel",
resolvedChatId: "-100123",
}),
);
});
});
describe("sendStickerTelegram", () => {