|
1 | 1 | // Whatsapp plugin module implements monitor inbox.allows messages from senders allowfrom list support behavior. |
2 | 2 | import "./monitor-inbox.test-harness.js"; |
| 3 | +import { cleanMessage } from "baileys"; |
3 | 4 | import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { isRecentOutboundMessage } from "./inbound/dedupe.js"; |
4 | 6 | import { |
5 | 7 | buildNotifyMessageUpsert, |
6 | 8 | expectPairingPromptSent, |
@@ -415,6 +417,135 @@ describe("web monitor inbox", () => { |
415 | 417 | await listener.close(); |
416 | 418 | }); |
417 | 419 |
|
| 420 | + it.each([ |
| 421 | + { |
| 422 | + name: "device-qualified phone JIDs", |
| 423 | + outboundJid: "123:[email protected]", |
| 424 | + |
| 425 | + }, |
| 426 | + { |
| 427 | + name: "legacy c.us phone JIDs", |
| 428 | + |
| 429 | + |
| 430 | + }, |
| 431 | + { |
| 432 | + name: "hosted phone JIDs", |
| 433 | + outboundJid: "123:7@hosted", |
| 434 | + echoedJid: "123:7@hosted", |
| 435 | + }, |
| 436 | + { |
| 437 | + name: "hosted LID JIDs", |
| 438 | + outboundJid: "777:[email protected]", |
| 439 | + echoedJid: "777:[email protected]", |
| 440 | + }, |
| 441 | + { |
| 442 | + name: "phone aliases carried alongside direct LID echoes", |
| 443 | + |
| 444 | + echoedJid: "777@lid", |
| 445 | + echoedAlternateJid: "123:[email protected]", |
| 446 | + }, |
| 447 | + { |
| 448 | + name: "LID aliases carried alongside direct phone echoes", |
| 449 | + outboundJid: "777@lid", |
| 450 | + |
| 451 | + echoedAlternateJid: "777:3@lid", |
| 452 | + }, |
| 453 | + ])( |
| 454 | + "filters provider-normalized outbound self echoes for $name", |
| 455 | + async ({ outboundJid, echoedJid, echoedAlternateJid }) => { |
| 456 | + mockLoadConfig.mockReturnValue({ |
| 457 | + channels: { |
| 458 | + whatsapp: { |
| 459 | + selfChatMode: true, |
| 460 | + allowFrom: ["+123"], |
| 461 | + }, |
| 462 | + }, |
| 463 | + messages: DEFAULT_MESSAGES_CFG, |
| 464 | + }); |
| 465 | + |
| 466 | + const onMessage = vi.fn(); |
| 467 | + const { listener, sock } = await startInboxMonitor(onMessage); |
| 468 | + sock.signalRepository.lidMapping.getPNForLID.mockImplementation(async (jid: string) => |
| 469 | + jid.startsWith("777@") ? "[email protected]" : null, |
| 470 | + ); |
| 471 | + const messageId = `bot-provider-echo-${outboundJid}`; |
| 472 | + sock.sendMessage.mockResolvedValueOnce({ key: { id: messageId } }); |
| 473 | + await listener.sendMessage(outboundJid, "gateway self echo"); |
| 474 | + |
| 475 | + const echoedMessage = { |
| 476 | + key: { |
| 477 | + id: messageId, |
| 478 | + fromMe: true, |
| 479 | + remoteJid: echoedJid, |
| 480 | + ...(echoedAlternateJid ? { remoteJidAlt: echoedAlternateJid } : {}), |
| 481 | + }, |
| 482 | + message: { conversation: "gateway self echo" }, |
| 483 | + messageTimestamp: nowSeconds(), |
| 484 | + }; |
| 485 | + cleanMessage(echoedMessage, "[email protected]", "777@lid"); |
| 486 | + |
| 487 | + sock.ev.emit("messages.upsert", { |
| 488 | + type: "notify", |
| 489 | + messages: [echoedMessage], |
| 490 | + }); |
| 491 | + await settleInboundWork(); |
| 492 | + |
| 493 | + expect(onMessage).not.toHaveBeenCalled(); |
| 494 | + expectOnlyOutboundChannelActivity(); |
| 495 | + await listener.close(); |
| 496 | + }, |
| 497 | + ); |
| 498 | + |
| 499 | + it("keeps provider-normalized echo matches scoped to their account and message", async () => { |
| 500 | + mockLoadConfig.mockReturnValue({ |
| 501 | + channels: { |
| 502 | + whatsapp: { |
| 503 | + selfChatMode: true, |
| 504 | + allowFrom: ["+123"], |
| 505 | + }, |
| 506 | + }, |
| 507 | + messages: DEFAULT_MESSAGES_CFG, |
| 508 | + }); |
| 509 | + |
| 510 | + const { listener, sock } = await startInboxMonitor(vi.fn()); |
| 511 | + sock.sendMessage.mockResolvedValueOnce({ key: { id: "scoped-hosted-echo" } }); |
| 512 | + await listener.sendMessage("123:7@hosted", "gateway self echo"); |
| 513 | + |
| 514 | + expect( |
| 515 | + isRecentOutboundMessage({ |
| 516 | + accountId: "other-account", |
| 517 | + |
| 518 | + messageId: "scoped-hosted-echo", |
| 519 | + }), |
| 520 | + ).toBe(false); |
| 521 | + expect( |
| 522 | + isRecentOutboundMessage({ |
| 523 | + accountId: "default", |
| 524 | + |
| 525 | + messageId: "other-message", |
| 526 | + }), |
| 527 | + ).toBe(false); |
| 528 | + |
| 529 | + await listener.close(); |
| 530 | + }); |
| 531 | + |
| 532 | + it("never treats another group as an alternate direct-chat echo", async () => { |
| 533 | + const { listener, sock } = await startInboxMonitor(vi.fn()); |
| 534 | + sock.sendMessage.mockResolvedValueOnce({ key: { id: "group-alias-control" } }); |
| 535 | + await listener.sendMessage("[email protected]", "gateway group message"); |
| 536 | + |
| 537 | + expect( |
| 538 | + isRecentOutboundMessage({ |
| 539 | + accountId: "default", |
| 540 | + |
| 541 | + alternateRemoteJid: "[email protected]", |
| 542 | + messageId: "group-alias-control", |
| 543 | + }), |
| 544 | + ).toBe(false); |
| 545 | + |
| 546 | + await listener.close(); |
| 547 | + }); |
| 548 | + |
418 | 549 | it("handles append messages by marking them read but skipping auto-reply", async () => { |
419 | 550 | const { onMessage, listener, sock } = await openInboxMonitor(); |
420 | 551 | const staleTs = Math.floor(Date.now() / 1000) - 300; |
|
0 commit comments