Files
openclaw/extensions/whatsapp/src/inbound/extract.test.ts
lurebat 5d91b68af3 fix(whatsapp): exclude quoted message mentionedJids from mention detection (#52711)
Merged via squash.

Prepared head SHA: 2ac325f615
Co-authored-by: lurebat <154669821+lurebat@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
2026-03-27 02:35:49 -03:00

104 lines
2.8 KiB
TypeScript

import type { proto } from "@whiskeysockets/baileys";
import { describe, expect, it } from "vitest";
import { extractMentionedJids } from "./extract.js";
describe("extractMentionedJids", () => {
const botJid = "5511999999999@s.whatsapp.net";
const otherJid = "5511888888888@s.whatsapp.net";
it("returns direct mentions from the current message", () => {
const message: proto.IMessage = {
extendedTextMessage: {
text: "Hey @bot",
contextInfo: {
mentionedJid: [botJid],
},
},
};
expect(extractMentionedJids(message)).toEqual([botJid]);
});
it("ignores mentionedJids from quoted messages", () => {
const message: proto.IMessage = {
extendedTextMessage: {
text: "I agree",
contextInfo: {
// The quoted message originally @mentioned the bot, but the
// current message does not — this should NOT leak through.
quotedMessage: {
extendedTextMessage: {
text: "Hey @bot what do you think?",
contextInfo: {
mentionedJid: [botJid],
},
},
},
},
},
};
expect(extractMentionedJids(message)).toBeUndefined();
});
it("returns direct mentions even when quoted message also has mentions", () => {
const message: proto.IMessage = {
extendedTextMessage: {
text: "Hey @other",
contextInfo: {
mentionedJid: [otherJid],
quotedMessage: {
extendedTextMessage: {
text: "Hey @bot",
contextInfo: {
mentionedJid: [botJid],
},
},
},
},
},
};
// Should return only the direct mention, not the quoted one.
expect(extractMentionedJids(message)).toEqual([otherJid]);
});
it("returns mentions from media message types", () => {
const message: proto.IMessage = {
imageMessage: {
contextInfo: {
mentionedJid: [botJid],
},
},
};
expect(extractMentionedJids(message)).toEqual([botJid]);
});
it("returns undefined for messages with no mentions", () => {
const message: proto.IMessage = {
extendedTextMessage: {
text: "Just a regular message",
},
};
expect(extractMentionedJids(message)).toBeUndefined();
});
it("returns undefined for undefined input", () => {
expect(extractMentionedJids(undefined)).toBeUndefined();
});
it("deduplicates mentions across message types", () => {
const message: proto.IMessage = {
extendedTextMessage: {
text: "Hey @bot",
contextInfo: {
mentionedJid: [botJid],
},
},
imageMessage: {
contextInfo: {
mentionedJid: [botJid],
},
},
};
expect(extractMentionedJids(message)).toEqual([botJid]);
});
});