Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

3.4 KB
Raw Blame
import { buildIncomingStatus } from './incoming-status';
import type { IncomingMessage } from '../../engine/interfaces/whatsapp-engine.interface';

const base: IncomingMessage = {
  id: 'false_status@broadcast_ABC',
  from: 'status@broadcast',
  to: '[email protected]',
  chatId: 'status@broadcast',
  author: '[email protected]',
  body: 'hello story',
  type: 'text',
  timestamp: 1700000000,
  fromMe: false,
  isGroup: false,
  isStatusBroadcast: true,
  kind: 'status',
};

it('maps a text status to IncomingStatus with the poster as contactJid', () => {
  const s = buildIncomingStatus(base)!;
  expect(s.contactJid).toBe('[email protected]');
  expect(s.waStatusId).toBe('false_status@broadcast_ABC');
  expect(s.type).toBe('text');
  expect(s.caption).toBe('hello story');
  expect(s.postedAt).toBe(1700000000 * 1000);
});

it('carries media and collapses the type to the status union', () => {
  const s = buildIncomingStatus({
    ...base,
    type: 'image',
    body: 'cap',
    media: { mimetype: 'image/jpeg', data: 'AAAA' },
  })!;
  expect(s.type).toBe('image');
  expect(s.caption).toBe('cap');
  expect(s.media).toEqual({ mimetype: 'image/jpeg', data: 'AAAA' });
});

// The store picks the omitted-media reason from these two: without them an engine-omitted status
// is recorded as over the cap, and that reason goes out on the status.received webhook.
it('carries the engine-omitted marker and the size of media it did not download', () => {
  const s = buildIncomingStatus({
    ...base,
    type: 'image',
    media: { mimetype: 'image/jpeg', omitted: true, sizeBytes: 99 },
  })!;
  expect(s.media).toStrictEqual({ mimetype: 'image/jpeg', data: undefined, omitted: true, sizeBytes: 99 });
});

it('carries text-status styling through to the store row', () => {
  const s = buildIncomingStatus({ ...base, backgroundColor: '#25d366', font: 2 })!;
  expect(s.backgroundColor).toBe('#25d366');
  expect(s.font).toBe(2);
});

it('returns null when the message is not a status', () => {
  expect(buildIncomingStatus({ ...base, isStatusBroadcast: false })).toBeNull();
});

it('falls back to `from` as the poster when the engine did not set `author`', () => {
  const s = buildIncomingStatus({ ...base, author: undefined, from: '[email protected]' })!;
  expect(s.contactJid).toBe('[email protected]');
});

it('returns null when there is no resolvable poster (author/from both the broadcast pseudo-JID)', () => {
  expect(buildIncomingStatus({ ...base, author: undefined, from: 'status@broadcast' })).toBeNull();
});

it('returns null when the message has no id', () => {
  expect(buildIncomingStatus({ ...base, id: '' })).toBeNull();
});

/**
 * Both engines map a PTT message to `voice`, and everything that was not an image or a video used to
 * collapse into `text`. That silently turned an incoming voice status into a text one — a status with
 * audio attached but a type saying otherwise.
 */
it('keeps a voice status as voice rather than collapsing it to text', () => {
  const s = buildIncomingStatus({
    ...base,
    type: 'voice',
    body: '',
    media: { mimetype: 'audio/ogg; codecs=opus', data: 'AAAA' },
  })!;

  expect(s.type).toBe('voice');
  expect(s.media?.mimetype).toBe('audio/ogg; codecs=opus');
});

it('still collapses a type the status union does not model', () => {
  expect(buildIncomingStatus({ ...base, type: 'document' })!.type).toBe('text');
  expect(buildIncomingStatus({ ...base, type: 'audio' })!.type).toBe('text');
});