Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import type { WASocket } from '@whiskeysockets/baileys';
import { BaileysContacts, BaileysContactsHost } from './baileys-contacts';
import { BaileysMessaging, BaileysMessagingHost } from './baileys-messaging';
import { EngineTransportError } from '../../common/errors/engine-transport.error';
/**
* The last group of writes whose confirmation the library discards. Same reasoning as the group and
* profile writes: `chatModify`, the addressbook writes and the block toggles all await a query and
* return void, so the call resolves identically whether WhatsApp applied the change or never
* answered. One case per call site — a missing wrapper is invisible any other way.
*/
const never = (): Promise<never> => new Promise<never>(() => undefined);
const stub = () => jest.fn(never);
const LAST = { key: { id: 'M1', remoteJid: '[email protected]' }, timestamp: 1 };
function contacts(sock: Record<string, jest.Mock>, budgetMs: number): BaileysContacts {
const host = {
ensureReady: () => undefined,
getSocket: () => sock as unknown as WASocket,
logger: { warn: jest.fn(), debug: jest.fn(), info: jest.fn(), error: jest.fn() },
normalizedSelfJid: () => '[email protected]',
listContacts: () => [],
findContact: () => null,
resolvePhone: () => null,
listChats: () => [],
lastMessage: () => LAST,
chatDeletedAt: () => null,
toEngineJid: (j: string) => j,
chatJid: (j: string) => j,
lidOf: () => null,
} as unknown as BaileysContactsHost;
return new BaileysContacts(host, budgetMs);
}
function messaging(sock: Record<string, jest.Mock>, budgetMs: number): BaileysMessaging {
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]',
getEphemeralExpiration: () => undefined,
toUnixSeconds: () => 1,
loadLib: () => Promise.resolve({} as never),
getStoredMessage: () => Promise.resolve({ key: { ...LAST.key, fromMe: true }, message: {}, messageTimestamp: 1 }),
wasDeletedForEveryone: () => false,
pendingEditOf: () => undefined,
markDeletedForEveryone: () => undefined,
putStoredMessage: () => undefined,
recordLidMapping: () => undefined,
getOnMessageCreate: () => undefined,
mapMessage: () => Promise.resolve({} as never),
} as unknown as BaileysMessagingHost;
return new BaileysMessaging(host, budgetMs);
}
describe('contact and chat-state writes report an unconfirmed write', () => {
it.each([
['upsertContact', 'addOrEditContact', (c: BaileysContacts) => c.upsertContact('[email protected]', 'Ada')],
['deleteContact', 'removeContact', (c: BaileysContacts) => c.deleteContact('[email protected]')],
['blockContact', 'updateBlockStatus', (c: BaileysContacts) => c.blockContact('[email protected]')],
['unblockContact', 'updateBlockStatus', (c: BaileysContacts) => c.unblockContact('[email protected]')],
['markUnread', 'chatModify', (c: BaileysContacts) => c.markUnread('[email protected]')],
['clearChatMessages', 'chatModify', (c: BaileysContacts) => c.clearChatMessages('[email protected]')],
['archiveChat', 'chatModify', (c: BaileysContacts) => c.archiveChat('[email protected]', true)],
['deleteChat', 'chatModify', (c: BaileysContacts) => c.deleteChat('[email protected]')],
])('%s', async (_n, method, call) => {
await expect(call(contacts({ [method]: stub() }, 15))).rejects.toBeInstanceOf(EngineTransportError);
});
it('still resolves when WhatsApp answers inside the budget', async () => {
const updateBlockStatus = jest.fn().mockResolvedValue(undefined);
await expect(contacts({ updateBlockStatus }, 500).blockContact('[email protected]')).resolves.toBeUndefined();
});
});
describe('message chat-state writes report an unconfirmed write', () => {
it.each([
['deleteMessage (for me)', (m: BaileysMessaging) => m.deleteMessage('[email protected]', 'M1', false)],
['starMessage', (m: BaileysMessaging) => m.starMessage('[email protected]', 'M1', true)],
])('%s', async (_n, call) => {
await expect(call(messaging({ chatModify: stub() }, 15))).rejects.toBeInstanceOf(EngineTransportError);
});
});