Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import { Boom } from '@hapi/boom';
import type { WASocket } from '@whiskeysockets/baileys';
import { BaileysGroups, BaileysGroupsHost } from './baileys-groups';
import { EngineTransportError } from '../../common/errors/engine-transport.error';
/**
* `groupFetchAllParticipating` builds its result as `const data = {}` and only fills it behind
* `if (groupsChild)`. On an unanswered query `getBinaryNodeChild(undefined, 'groups')` is undefined,
* the block is skipped, and `{}` comes back — the SAME value an account with no groups produces,
* because that answer carries a real `<groups>` node with no `<group>` children.
*
* So there is nothing in the return value to inspect, and the empty list is the dangerous shape: a
* caller reading `[]` has no way to tell "you are in no groups" from "WhatsApp never answered".
* Only a clock we own separates them.
*/
function groups(sock: Record<string, jest.Mock>, budgetMs: number): BaileysGroups {
const host = {
ensureReady: () => undefined,
getSocket: () => sock as unknown as WASocket,
logger: { warn: jest.fn(), debug: jest.fn(), info: jest.fn(), error: jest.fn() },
toNeutralJid: (j: string) => j,
toEngineJid: (j: string) => j,
normalizedSelfJid: () => '[email protected]',
} as unknown as BaileysGroupsHost;
return new BaileysGroups(host, budgetMs);
}
describe('getGroups', () => {
it('reports an unanswered query instead of an empty group list', async () => {
const groupFetchAllParticipating = jest.fn(() => new Promise<never>(() => undefined));
await expect(groups({ groupFetchAllParticipating }, 15).getGroups()).rejects.toBeInstanceOf(EngineTransportError);
});
it.each([408, 429])('reports a WA %s as retryable instead of a bare 500', async code => {
const groupFetchAllParticipating = jest.fn().mockRejectedValue(new Boom('rate-overlimit', { data: code }));
await expect(groups({ groupFetchAllParticipating }, 500).getGroups()).rejects.toBeInstanceOf(EngineTransportError);
});
it('lets any other refusal propagate untouched', async () => {
const err = new Boom('forbidden', { data: 403 });
const groupFetchAllParticipating = jest.fn().mockRejectedValue(err);
await expect(groups({ groupFetchAllParticipating }, 500).getGroups()).rejects.toBe(err);
});
it('still returns an empty list when WhatsApp answers that there are no groups', async () => {
// A real answer that happens to be empty: the library yields {} for this too, so the ONLY thing
// separating it from the case above is that it arrived.
const groupFetchAllParticipating = jest.fn().mockResolvedValue({});
await expect(groups({ groupFetchAllParticipating }, 500).getGroups()).resolves.toEqual([]);
});
it('maps a populated answer unchanged', async () => {
const groupFetchAllParticipating = jest.fn().mockResolvedValue({
'[email protected]': {
id: '[email protected]',
subject: 'Engineering',
participants: [{ id: '[email protected]', admin: 'superadmin' }],
},
});
const list = await groups({ groupFetchAllParticipating }, 500).getGroups();
expect(list).toHaveLength(1);
expect(list[0]).toMatchObject({ id: '[email protected]', name: 'Engineering' });
});
});