fix: harden discord media fallback regressions (#28906) (thanks @Sid-Qin)

This commit is contained in:
Peter Steinberger
2026-03-02 03:04:56 +00:00
parent 0a67033fe3
commit 25b731c34a
2 changed files with 78 additions and 0 deletions

View File

@@ -334,6 +334,83 @@ describe("resolveMediaList", () => {
]);
});
it("falls back to URL when saveMediaBuffer fails", async () => {
const attachment = {
id: "att-save-fail",
url: "https://cdn.discordapp.com/attachments/1/photo.png",
filename: "photo.png",
content_type: "image/png",
};
fetchRemoteMedia.mockResolvedValueOnce({
buffer: Buffer.from("image"),
contentType: "image/png",
});
saveMediaBuffer.mockRejectedValueOnce(new Error("disk full"));
const result = await resolveMediaList(
asMessage({
attachments: [attachment],
}),
512,
);
expect(fetchRemoteMedia).toHaveBeenCalledTimes(1);
expect(saveMediaBuffer).toHaveBeenCalledTimes(1);
expect(result).toEqual([
{
path: attachment.url,
contentType: "image/png",
placeholder: "<media:image>",
},
]);
});
it("preserves downloaded attachments alongside failed ones", async () => {
const goodAttachment = {
id: "att-good",
url: "https://cdn.discordapp.com/attachments/1/good.png",
filename: "good.png",
content_type: "image/png",
};
const badAttachment = {
id: "att-bad",
url: "https://cdn.discordapp.com/attachments/1/bad.pdf",
filename: "bad.pdf",
content_type: "application/pdf",
};
fetchRemoteMedia
.mockResolvedValueOnce({
buffer: Buffer.from("image"),
contentType: "image/png",
})
.mockRejectedValueOnce(new Error("network timeout"));
saveMediaBuffer.mockResolvedValueOnce({
path: "/tmp/good.png",
contentType: "image/png",
});
const result = await resolveMediaList(
asMessage({
attachments: [goodAttachment, badAttachment],
}),
512,
);
expect(result).toEqual([
{
path: "/tmp/good.png",
contentType: "image/png",
placeholder: "<media:image>",
},
{
path: badAttachment.url,
contentType: "application/pdf",
placeholder: "<media:document>",
},
]);
});
it("keeps sticker metadata when sticker download fails", async () => {
const sticker = {
id: "sticker-fallback",