fix: make sendMediaGroup injectable for tests + format
This commit is contained in:
parent
74b867084e
commit
aa60fafcb0
@ -34,11 +34,9 @@ describe("telegramOutbound.sendPayload", () => {
|
|||||||
expect(result).toEqual({ channel: "telegram", messageId: "m1", chatId: "c1" });
|
expect(result).toEqual({ channel: "telegram", messageId: "m1", chatId: "c1" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sends media payloads and attaches buttons only to first", async () => {
|
it("sends media payloads as album and buttons in follow-up", async () => {
|
||||||
const sendTelegram = vi
|
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m-btn", chatId: "c1" });
|
||||||
.fn()
|
const sendMediaGroup = vi.fn().mockResolvedValue({ messageIds: ["m1", "m2"], chatId: "c1" });
|
||||||
.mockResolvedValueOnce({ messageId: "m1", chatId: "c1" })
|
|
||||||
.mockResolvedValueOnce({ messageId: "m2", chatId: "c1" });
|
|
||||||
|
|
||||||
const result = await telegramOutbound.sendPayload?.({
|
const result = await telegramOutbound.sendPayload?.({
|
||||||
cfg: {} as MoltbotConfig,
|
cfg: {} as MoltbotConfig,
|
||||||
@ -53,12 +51,50 @@ describe("telegramOutbound.sendPayload", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
deps: { sendTelegram, sendMediaGroup },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Media group is sent with both URLs
|
||||||
|
expect(sendMediaGroup).toHaveBeenCalledTimes(1);
|
||||||
|
expect(sendMediaGroup).toHaveBeenCalledWith(
|
||||||
|
"telegram:123",
|
||||||
|
["https://example.com/a.png", "https://example.com/b.png"],
|
||||||
|
"Caption",
|
||||||
|
expect.objectContaining({ accountId: undefined }),
|
||||||
|
);
|
||||||
|
// Buttons sent in follow-up message (media groups don't support buttons)
|
||||||
|
expect(sendTelegram).toHaveBeenCalledTimes(1);
|
||||||
|
expect(sendTelegram).toHaveBeenCalledWith(
|
||||||
|
"telegram:123",
|
||||||
|
"",
|
||||||
|
expect.objectContaining({
|
||||||
|
buttons: [[{ text: "Go", callback_data: "/go" }]],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(result).toEqual({ channel: "telegram", messageId: "m1", chatId: "c1" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sends single media with buttons directly", async () => {
|
||||||
|
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" });
|
||||||
|
|
||||||
|
const result = await telegramOutbound.sendPayload?.({
|
||||||
|
cfg: {} as MoltbotConfig,
|
||||||
|
to: "telegram:123",
|
||||||
|
text: "ignored",
|
||||||
|
payload: {
|
||||||
|
text: "Caption",
|
||||||
|
mediaUrls: ["https://example.com/a.png"],
|
||||||
|
channelData: {
|
||||||
|
telegram: {
|
||||||
|
buttons: [[{ text: "Go", callback_data: "/go" }]],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
deps: { sendTelegram },
|
deps: { sendTelegram },
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(sendTelegram).toHaveBeenCalledTimes(2);
|
expect(sendTelegram).toHaveBeenCalledTimes(1);
|
||||||
expect(sendTelegram).toHaveBeenNthCalledWith(
|
expect(sendTelegram).toHaveBeenCalledWith(
|
||||||
1,
|
|
||||||
"telegram:123",
|
"telegram:123",
|
||||||
"Caption",
|
"Caption",
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@ -66,16 +102,6 @@ describe("telegramOutbound.sendPayload", () => {
|
|||||||
buttons: [[{ text: "Go", callback_data: "/go" }]],
|
buttons: [[{ text: "Go", callback_data: "/go" }]],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
const secondOpts = sendTelegram.mock.calls[1]?.[2] as { buttons?: unknown } | undefined;
|
expect(result).toEqual({ channel: "telegram", messageId: "m1", chatId: "c1" });
|
||||||
expect(sendTelegram).toHaveBeenNthCalledWith(
|
|
||||||
2,
|
|
||||||
"telegram:123",
|
|
||||||
"",
|
|
||||||
expect.objectContaining({
|
|
||||||
mediaUrl: "https://example.com/b.png",
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
expect(secondOpts?.buttons).toBeUndefined();
|
|
||||||
expect(result).toEqual({ channel: "telegram", messageId: "m2", chatId: "c1" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -85,7 +85,8 @@ export const telegramOutbound: ChannelOutboundAdapter = {
|
|||||||
|
|
||||||
// Use media group (album) for 2-10 images - sends as a single album
|
// Use media group (album) for 2-10 images - sends as a single album
|
||||||
if (mediaUrls.length >= 2 && mediaUrls.length <= 10) {
|
if (mediaUrls.length >= 2 && mediaUrls.length <= 10) {
|
||||||
const groupResult = await sendMediaGroupTelegram(to, mediaUrls, text, {
|
const sendGroup = deps?.sendMediaGroup ?? sendMediaGroupTelegram;
|
||||||
|
const groupResult = await sendGroup(to, mediaUrls, text, {
|
||||||
messageThreadId,
|
messageThreadId,
|
||||||
replyToMessageId,
|
replyToMessageId,
|
||||||
accountId: accountId ?? undefined,
|
accountId: accountId ?? undefined,
|
||||||
|
|||||||
@ -745,7 +745,9 @@ export async function sendMediaGroupTelegram(
|
|||||||
|
|
||||||
// Media groups only support photos and videos
|
// Media groups only support photos and videos
|
||||||
if (kind !== "image" && kind !== "video" && !isGif) {
|
if (kind !== "image" && kind !== "video" && !isGif) {
|
||||||
throw new Error(`Media group only supports photos and videos. Got: ${kind} for ${trimmedUrl}`);
|
throw new Error(
|
||||||
|
`Media group only supports photos and videos. Got: ${kind} for ${trimmedUrl}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileName = media.fileName ?? inferFilename(kind) ?? "file";
|
const fileName = media.fileName ?? inferFilename(kind) ?? "file";
|
||||||
@ -765,9 +767,7 @@ export async function sendMediaGroupTelegram(
|
|||||||
const mediaGroup = mediaItems.map((item, index) => ({
|
const mediaGroup = mediaItems.map((item, index) => ({
|
||||||
type: item.type,
|
type: item.type,
|
||||||
media: item.media,
|
media: item.media,
|
||||||
...(index === 0 && htmlCaption
|
...(index === 0 && htmlCaption ? { caption: htmlCaption, parse_mode: "HTML" as const } : {}),
|
||||||
? { caption: htmlCaption, parse_mode: "HTML" as const }
|
|
||||||
: {}),
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const groupParams = {
|
const groupParams = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user