Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

9.7 KB
Raw Blame
import {
  chatKind,
  isAddressableParticipant,
  isChannelJid,
  isIndividualWid,
  parseWaId,
  toNeutralJid,
  toParticipantWid,
  userPart,
} from './wa-id';

describe('wa-id', () => {
  describe('userPart', () => {
    it('strips the domain and the device suffix', () => {
      expect(userPart('[email protected]')).toBe('628111');
      expect(userPart('628111:[email protected]')).toBe('628111');
      expect(userPart('[email protected]')).toBe('120363-456');
    });
  });

  describe('parseWaId', () => {
    it('classifies each dialect, folding @s.whatsapp.net and @c.us into one user kind', () => {
      expect(parseWaId('[email protected]')).toMatchObject({ kind: 'user', userPart: '628111' });
      expect(parseWaId('[email protected]')).toMatchObject({ kind: 'user', userPart: '628111' });
      expect(parseWaId('628111:[email protected]')).toMatchObject({ kind: 'user', userPart: '628111', device: '3' });
      expect(parseWaId('[email protected]')).toMatchObject({ kind: 'group' });
      expect(parseWaId('111@lid')).toMatchObject({ kind: 'lid', userPart: '111' });
      expect(parseWaId('status@broadcast')).toMatchObject({ kind: 'status' });
      expect(parseWaId('abc@newsletter')).toMatchObject({ kind: 'newsletter' });
      expect(parseWaId('not-a-jid')).toMatchObject({ kind: 'unknown' });
    });

    it('classifies broadcast and lowercases the parsed parts', () => {
      expect(parseWaId('123@broadcast')).toMatchObject({ kind: 'broadcast', userPart: '123' });
      expect(parseWaId('ABC@NEWSLETTER')).toMatchObject({ kind: 'newsletter', userPart: 'abc' });
      expect(parseWaId('AbCd@LID')).toMatchObject({ kind: 'lid', userPart: 'abcd' });
    });
  });

  describe('isChannelJid', () => {
    it('is true only for a @newsletter (channel) JID', () => {
      expect(isChannelJid('120363401234567890@newsletter')).toBe(true);
      expect(isChannelJid('ABC@NEWSLETTER')).toBe(true); // case-insensitive
    });

    it('is false for user, group, lid, broadcast and status JIDs (they resolve to a real Chat)', () => {
      expect(isChannelJid('[email protected]')).toBe(false);
      expect(isChannelJid('[email protected]')).toBe(false);
      expect(isChannelJid('[email protected]')).toBe(false);
      expect(isChannelJid('111@lid')).toBe(false);
      expect(isChannelJid('123@broadcast')).toBe(false);
      expect(isChannelJid('status@broadcast')).toBe(false);
      expect(isChannelJid('not-a-jid')).toBe(false);
    });
  });

  describe('toNeutralJid', () => {
    it('maps @s.whatsapp.net (and device suffixes) to @c.us, idempotent on @c.us', () => {
      expect(toNeutralJid('[email protected]')).toBe('[email protected]');
      expect(toNeutralJid('628111:[email protected]')).toBe('[email protected]');
      expect(toNeutralJid('[email protected]')).toBe('[email protected]');
    });

    it('keeps groups as @g.us and passes status / empty through', () => {
      expect(toNeutralJid('[email protected]')).toBe('[email protected]');
      expect(toNeutralJid('status@broadcast')).toBe('status@broadcast');
      expect(toNeutralJid('')).toBe('');
    });

    it('resolves a lid to <phone>@c.us when the resolver knows it, else keeps the raw lid', () => {
      const resolve = (jid: string) => (jid === '111@lid' ? '628999' : null);
      expect(toNeutralJid('111@lid', resolve)).toBe('[email protected]');
      expect(toNeutralJid('222@lid', resolve)).toBe('222@lid'); // unresolved: kept as a privacy id
      expect(toNeutralJid('111@lid')).toBe('111@lid'); // no resolver supplied
    });

    it('keeps newsletter and broadcast channels in their own dialect', () => {
      expect(toNeutralJid('120363-abc@newsletter')).toBe('120363-abc@newsletter');
      expect(toNeutralJid('120363-def@broadcast')).toBe('120363-def@broadcast');
    });

    it('passes an unrecognized format through unchanged', () => {
      expect(toNeutralJid('weird-thing')).toBe('weird-thing');
    });
  });

  describe('chatKind', () => {
    it.each([
      ['[email protected]', 'individual'],
      ['[email protected]', 'individual'],
      ['628111:[email protected]', 'individual'],
      ['4707@lid', 'individual'],
      ['[email protected]', 'group'],
      ['abc@newsletter', 'channel'],
      ['status@broadcast', 'status'],
      ['12036@broadcast', 'broadcast'],
      ['not-a-jid', 'unknown'],
      ['', 'unknown'],
    ])('classifies %s as %s', (jid, expected) => {
      expect(chatKind(jid)).toBe(expected);
    });
  });

  describe('isAddressableParticipant', () => {
    it.each([
      ['628123456789', true],
      ['[email protected]', true],
      ['[email protected]', true],
      ['12345678901234567890@lid', true],
      ['628123456789:[email protected]', true],
      ['  [email protected]  ', true],
      ['NOT A USER', false],
      ['', false],
      ['1234', false],
      ['[email protected]', false],
      ['status@broadcast', false],
      ['abc@newsletter', false],
      ['abc@weird', false],
      // A recognised domain is not enough: the user-part has to look like a WhatsApp id, or the
      // string still reaches the page-side createWid and throws there — the very failure the guard
      // exists to prevent. Reproduced against a live session before this row was added.
      ['NOT A [email protected]', false],
      ['[email protected]', false],
      ['[email protected]', false],
      ['abc@lid', false],
      ['@c.us', false],
      ['x [email protected]', false],
      ['[email protected]', false],
      ['[email protected]', false],
      ['[email protected]', false],
    ])('classifies %s as %s', (value, expected) => {
      expect(isAddressableParticipant(value)).toBe(expected);
    });
  });

  describe('toParticipantWid', () => {
    it('qualifies a bare number to the c.us dialect', () => {
      expect(toParticipantWid('628123456789')).toBe('[email protected]');
    });

    it('leaves an already-neutral id untouched', () => {
      expect(toParticipantWid('[email protected]')).toBe('[email protected]');
      expect(toParticipantWid('12345678901234567890@lid')).toBe('12345678901234567890@lid');
    });

    it.each([
      [' 628123456789:[email protected] ', '[email protected]'],
      ['[email protected]', '[email protected]'],
      ['628123456789@hosted', '[email protected]'],
      ['12345678901234567890:5@LID', '12345678901234567890@lid'],
      ['[email protected]', '12345678901234567890@lid'],
    ])('reduces the accepted variant %s to the neutral id %s, keeping a lid a lid', (value, expected) => {
      // isIndividualWid accepts every one of these, so forwarding the raw string would hand the
      // engine an id the group's participant list is not keyed by.
      expect(isIndividualWid(value)).toBe(true);
      expect(toParticipantWid(value)).toBe(expected);
    });

    it.each([['abc@weird'], ['abc'], ['NOT A USER'], ['']])(
      'leaves %s alone rather than minting a nonsense id from it',
      value => {
        // The old rule was `p.includes('@') ? p : p + '@c.us'`, which turned every un-domained
        // string into one — `abc` became `[email protected]`, a well-formed id naming nobody. Keying on the
        // bare-number shape instead means only a number is ever qualified. (`abc@weird` alone would
        // NOT show this: the old rule passed it through too, so it discriminates nothing.)
        expect(toParticipantWid(value)).toBe(value);
      },
    );
  });

  /**
   * WhatsApp issues Meta-hosted dialects of a phone id and a lid, and Baileys decodes them off the
   * wire (`WAJIDDomains.HOSTED` = 128, `HOSTED_LID` = 129). They used to parse as `unknown` here, so
   * an id we had emitted on GET /chats was refused with a 400 when it was handed back to us.
   *
   * They fold into `user` and `lid` rather than becoming kinds of their own, because the user-part
   * names the SAME account: Baileys rewrites `<n>@hosted` to `<n>@s.whatsapp.net` on every inbound
   * message (`cleanMessage`). A separate kind would split one person's history in two.
   */
  describe('Meta-hosted dialects', () => {
    it('folds each hosted dialect into the entity it names', () => {
      expect(parseWaId('628111@hosted')).toMatchObject({ kind: 'user', userPart: '628111' });
      expect(parseWaId('[email protected]')).toMatchObject({ kind: 'lid', userPart: '4707' });
      expect(parseWaId('628111:3@hosted')).toMatchObject({ kind: 'user', userPart: '628111', device: '3' });
      expect(parseWaId('[email protected]')).toMatchObject({ kind: 'lid', userPart: 'abc' });
    });

    it('does not confuse @hosted.lid with @hosted: the domain is the whole suffix', () => {
      // Same user-part, different entity: one is a phone number, the other a privacy id.
      expect(parseWaId('[email protected]').kind).toBe('lid');
      expect(parseWaId('4707@hosted').kind).toBe('user');
    });

    it('accepts them as individuals, which is what the guards refused before', () => {
      expect(isIndividualWid('628123456789@hosted')).toBe(true);
      expect(isIndividualWid('[email protected]')).toBe(true);
      expect(isAddressableParticipant('628123456789@hosted')).toBe(true);
      // The user-part still has to look like an id: the domain alone was never enough.
      expect(isIndividualWid('NOT A USER@hosted')).toBe(false);
      expect(isIndividualWid('[email protected]')).toBe(false);
    });

    it('normalizes to the neutral dialect of the same account, so history stays in one place', () => {
      expect(toNeutralJid('628111@hosted')).toBe('[email protected]');
      expect(toNeutralJid('628111:3@hosted')).toBe('[email protected]');
      // An unresolved hosted lid keeps the lid dialect, exactly like a plain @lid.
      expect(toNeutralJid('[email protected]')).toBe('4707@lid');
      expect(toNeutralJid('[email protected]', () => '628111')).toBe('[email protected]');
    });

    it('surfaces as an individual chat, not unknown', () => {
      expect(chatKind('628111@hosted')).toBe('individual');
      expect(chatKind('[email protected]')).toBe('individual');
    });
  });
});