fix(whatsapp): preserve outbound document filenames

This commit is contained in:
Tseka Luk
2026-02-14 01:06:41 +08:00
committed by Peter Steinberger
parent f59df95896
commit eaa546a8b3
5 changed files with 59 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
export type ActiveWebSendOptions = {
gifPlayback?: boolean;
accountId?: string;
fileName?: string;
};
export type ActiveWebListener = {

View File

@@ -0,0 +1,49 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const recordChannelActivity = vi.fn();
vi.mock("../../infra/channel-activity.js", () => ({
recordChannelActivity: (...args: unknown[]) => recordChannelActivity(...args),
}));
import { createWebSendApi } from "./send-api.js";
describe("createWebSendApi", () => {
const sendMessage = vi.fn(async () => ({ key: { id: "msg-1" } }));
const sendPresenceUpdate = vi.fn(async () => {});
const api = createWebSendApi({
sock: { sendMessage, sendPresenceUpdate },
defaultAccountId: "main",
});
beforeEach(() => {
vi.clearAllMocks();
});
it("uses sendOptions fileName for outbound documents", async () => {
const payload = Buffer.from("pdf");
await api.sendMessage("+1555", "doc", payload, "application/pdf", { fileName: "invoice.pdf" });
expect(sendMessage).toHaveBeenCalledWith(
"1555@s.whatsapp.net",
expect.objectContaining({
document: payload,
fileName: "invoice.pdf",
caption: "doc",
mimetype: "application/pdf",
}),
);
});
it("falls back to default document filename when fileName is absent", async () => {
const payload = Buffer.from("pdf");
await api.sendMessage("+1555", "doc", payload, "application/pdf");
expect(sendMessage).toHaveBeenCalledWith(
"1555@s.whatsapp.net",
expect.objectContaining({
document: payload,
fileName: "file",
caption: "doc",
mimetype: "application/pdf",
}),
);
});
});

View File

@@ -38,9 +38,10 @@ export function createWebSendApi(params: {
...(gifPlayback ? { gifPlayback: true } : {}),
};
} else {
const fileName = sendOptions?.fileName?.trim() || "file";
payload = {
document: mediaBuffer,
fileName: "file",
fileName,
caption: text || undefined,
mimetype: mediaType,
};

View File

@@ -130,7 +130,9 @@ describe("web outbound", () => {
verbose: false,
mediaUrl: "/tmp/file.pdf",
});
expect(sendMessage).toHaveBeenLastCalledWith("+1555", "doc", buf, "application/pdf");
expect(sendMessage).toHaveBeenLastCalledWith("+1555", "doc", buf, "application/pdf", {
fileName: "file.pdf",
});
});
it("sends polls via active listener", async () => {

View File

@@ -45,6 +45,7 @@ export async function sendMessageWhatsApp(
const jid = toWhatsappJid(to);
let mediaBuffer: Buffer | undefined;
let mediaType: string | undefined;
let documentFileName: string | undefined;
if (options.mediaUrl) {
const media = await loadWebMedia(options.mediaUrl);
const caption = text || undefined;
@@ -62,6 +63,7 @@ export async function sendMessageWhatsApp(
text = caption ?? "";
} else {
text = caption ?? "";
documentFileName = media.fileName;
}
}
outboundLog.info(`Sending message -> ${jid}${options.mediaUrl ? " (media)" : ""}`);
@@ -70,9 +72,10 @@ export async function sendMessageWhatsApp(
const hasExplicitAccountId = Boolean(options.accountId?.trim());
const accountId = hasExplicitAccountId ? resolvedAccountId : undefined;
const sendOptions: ActiveWebSendOptions | undefined =
options.gifPlayback || accountId
options.gifPlayback || accountId || documentFileName
? {
...(options.gifPlayback ? { gifPlayback: true } : {}),
...(documentFileName ? { fileName: documentFileName } : {}),
accountId,
}
: undefined;