



















@@ -843,6 +843,46 @@ describe("sendMessageTelegram", () => {
843843expect(res.messageId).toBe("71");
844844});
845845846+it("chunks long default markdown media follow-up text", async () => {
847+const chatId = "123";
848+const longText = `**${"A".repeat(5000)}**`;
849+850+const sendPhoto = vi.fn().mockResolvedValue({
851+message_id: 72,
852+chat: { id: chatId },
853+});
854+const sendMessage = vi
855+.fn()
856+.mockResolvedValueOnce({ message_id: 73, chat: { id: chatId } })
857+.mockResolvedValueOnce({ message_id: 74, chat: { id: chatId } });
858+const api = { sendPhoto, sendMessage } as unknown as {
859+sendPhoto: typeof sendPhoto;
860+sendMessage: typeof sendMessage;
861+};
862+863+mockLoadedMedia({
864+buffer: Buffer.from("fake-image"),
865+contentType: "image/jpeg",
866+fileName: "photo.jpg",
867+});
868+869+const res = await sendMessageTelegram(chatId, longText, {
870+cfg: TELEGRAM_TEST_CFG,
871+token: "tok",
872+ api,
873+mediaUrl: "https://example.com/photo.jpg",
874+});
875+876+expect(sendPhoto).toHaveBeenCalledWith(chatId, expect.anything(), {
877+caption: undefined,
878+});
879+expect(sendMessage).toHaveBeenCalledTimes(2);
880+expect(sendMessage.mock.calls.every((call) => call[2]?.parse_mode === "HTML")).toBe(true);
881+expect(sendMessage.mock.calls.every((call) => String(call[1] ?? "").length <= 4000)).toBe(true);
882+expect(sendMessage.mock.calls.map((call) => String(call[1] ?? "")).join("")).toContain("<b>");
883+expect(res.messageId).toBe("74");
884+});
885+846886it("uses caption when text is within 1024 char limit", async () => {
847887const chatId = "123";
848888const shortText = "B".repeat(1024);
@@ -1898,6 +1938,41 @@ describe("sendMessageTelegram", () => {
18981938expect(res.messageId).toBe("91");
18991939});
190019401941+it("chunks long default markdown text and keeps buttons on the last chunk only", async () => {
1942+const chatId = "123";
1943+const markdownText = `**${"A".repeat(5000)}**`;
1944+1945+const sendMessage = vi
1946+.fn()
1947+.mockResolvedValueOnce({ message_id: 90, chat: { id: chatId } })
1948+.mockResolvedValueOnce({ message_id: 91, chat: { id: chatId } });
1949+const api = { sendMessage } as unknown as { sendMessage: typeof sendMessage };
1950+1951+const res = await sendMessageTelegram(chatId, markdownText, {
1952+cfg: TELEGRAM_TEST_CFG,
1953+token: "tok",
1954+ api,
1955+buttons: [[{ text: "OK", callback_data: "ok" }]],
1956+});
1957+1958+expect(sendMessage).toHaveBeenCalledTimes(2);
1959+const firstCall = sendMessage.mock.calls[0];
1960+const secondCall = sendMessage.mock.calls[1];
1961+expect(firstCall).toBeDefined();
1962+expect(secondCall).toBeDefined();
1963+expect(String(firstCall[1] ?? "").length).toBeLessThanOrEqual(4000);
1964+expect(String(secondCall[1] ?? "").length).toBeLessThanOrEqual(4000);
1965+expect(firstCall[2]?.parse_mode).toBe("HTML");
1966+expect(secondCall[2]?.parse_mode).toBe("HTML");
1967+expect(String(firstCall[1] ?? "")).toMatch(/^<b>[\s\S]*<\/b>$/);
1968+expect(String(secondCall[1] ?? "")).toMatch(/^<b>[\s\S]*<\/b>$/);
1969+expect(firstCall[2]?.reply_markup).toBeUndefined();
1970+expect(secondCall[2]?.reply_markup).toEqual({
1971+inline_keyboard: [[{ text: "OK", callback_data: "ok" }]],
1972+});
1973+expect(res.messageId).toBe("91");
1974+});
1975+19011976it("preserves caller plain-text fallback across chunked html parse retries", async () => {
19021977const chatId = "123";
19031978const htmlText = `<b>${"A".repeat(5000)}</b>`;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。