WhatsApp: honor outbound mediaMaxMb (#38097)

* WhatsApp: add media cap helper

* WhatsApp: cap outbound media loads

* WhatsApp: align auto-reply media caps

* WhatsApp: add outbound media cap test

* WhatsApp: update auto-reply cap tests

* Docs: update WhatsApp media caps

* Changelog: note WhatsApp media cap fix
This commit is contained in:
Vincent Koc
2026-03-06 11:08:15 -05:00
committed by GitHub
parent 20038fb955
commit 222d635aee
7 changed files with 121 additions and 10 deletions

View File

@@ -73,7 +73,14 @@ describe("web auto-reply", () => {
}
async function withMediaCap<T>(mediaMaxMb: number, run: () => Promise<T>): Promise<T> {
setLoadConfigMock(() => ({ agents: { defaults: { mediaMaxMb } } }));
setLoadConfigMock(() => ({
channels: {
whatsapp: {
allowFrom: ["*"],
mediaMaxMb,
},
},
}));
try {
return await run();
} finally {
@@ -215,7 +222,7 @@ describe("web auto-reply", () => {
});
});
it("honors mediaMaxMb from config", async () => {
it("honors channels.whatsapp.mediaMaxMb for outbound auto-replies", async () => {
const bigPng = await sharp({
create: {
width: 256,
@@ -235,6 +242,53 @@ describe("web auto-reply", () => {
mediaMaxMb: SMALL_MEDIA_CAP_MB,
});
});
it("prefers per-account WhatsApp media caps for outbound auto-replies", async () => {
const bigPng = await sharp({
create: {
width: 256,
height: 256,
channels: 3,
background: { r: 255, g: 0, b: 0 },
},
})
.png({ compressionLevel: 0 })
.toBuffer();
expect(bigPng.length).toBeGreaterThan(SMALL_MEDIA_CAP_BYTES);
setLoadConfigMock(() => ({
channels: {
whatsapp: {
allowFrom: ["*"],
mediaMaxMb: 1,
accounts: {
work: {
mediaMaxMb: SMALL_MEDIA_CAP_MB,
},
},
},
},
}));
try {
const sendMedia = vi.fn();
const { reply, dispatch } = await setupSingleInboundMessage({
resolverValue: { text: "hi", mediaUrl: "https://example.com/account-big.png" },
sendMedia,
});
const fetchMock = mockFetchMediaBuffer(bigPng, "image/png");
await dispatch("msg-account-cap", { accountId: "work" });
const payload = getSingleImagePayload(sendMedia);
expect(payload.image.length).toBeLessThanOrEqual(SMALL_MEDIA_CAP_BYTES);
expect(payload.mimetype).toBe("image/jpeg");
expect(reply).not.toHaveBeenCalled();
fetchMock.mockRestore();
} finally {
resetLoadConfigMock();
}
});
it("falls back to text when media is unsupported", async () => {
const sendMedia = vi.fn();
const { reply, dispatch } = await setupSingleInboundMessage({