fix(whatsapp): trim leading whitespace in direct outbound sends (#43539)

Trim leading whitespace from direct WhatsApp text and media caption sends.

Also guard empty text-only web sends after trimming.
This commit is contained in:
Luke
2026-03-12 11:32:04 +11:00
committed by GitHub
parent 7e3787517f
commit a5ceb62d44
4 changed files with 151 additions and 7 deletions

View File

@@ -48,6 +48,34 @@ describe("web outbound", () => {
expect(sendMessage).toHaveBeenCalledWith("+1555", "hi", undefined, undefined);
});
it("trims leading whitespace before sending text and captions", async () => {
await sendMessageWhatsApp("+1555", "\n \thello", { verbose: false });
expect(sendMessage).toHaveBeenLastCalledWith("+1555", "hello", undefined, undefined);
const buf = Buffer.from("img");
loadWebMediaMock.mockResolvedValueOnce({
buffer: buf,
contentType: "image/jpeg",
kind: "image",
});
await sendMessageWhatsApp("+1555", "\n \tcaption", {
verbose: false,
mediaUrl: "/tmp/pic.jpg",
});
expect(sendMessage).toHaveBeenLastCalledWith("+1555", "caption", buf, "image/jpeg");
});
it("skips whitespace-only text sends without media", async () => {
const result = await sendMessageWhatsApp("+1555", "\n \t", { verbose: false });
expect(result).toEqual({
messageId: "",
toJid: "1555@s.whatsapp.net",
});
expect(sendComposingTo).not.toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
});
it("throws a helpful error when no active listener exists", async () => {
setActiveWebListener(null);
await expect(

View File

@@ -26,7 +26,11 @@ export async function sendMessageWhatsApp(
accountId?: string;
},
): Promise<{ messageId: string; toJid: string }> {
let text = body;
let text = body.trimStart();
const jid = toWhatsappJid(to);
if (!text && !options.mediaUrl) {
return { messageId: "", toJid: jid };
}
const correlationId = generateSecureUuid();
const startedAt = Date.now();
const { listener: active, accountId: resolvedAccountId } = requireActiveWebListener(
@@ -51,7 +55,6 @@ export async function sendMessageWhatsApp(
to: redactedTo,
});
try {
const jid = toWhatsappJid(to);
const redactedJid = redactIdentifier(jid);
let mediaBuffer: Buffer | undefined;
let mediaType: string | undefined;