Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
// The Webhooks modal keeps Create/Save disabled while filtersIncomplete is true, so a filter the gateway
// would reject never reaches it (a production build answers with a bare "Bad Request").
import { test } from 'node:test';
import assert from 'node:assert/strict';
import type { WebhookFilterCondition } from '../services/api.ts';
import { filtersIncomplete } from './webhookFilters.ts';
const sender = (value: string[]): WebhookFilterCondition => ({ field: 'sender', operator: 'is', value });
test('no filters, or complete ones, pass', () => {
assert.equal(filtersIncomplete(null), false);
assert.equal(filtersIncomplete(undefined), false);
assert.equal(
filtersIncomplete({
conditions: [
sender(['[email protected]']),
{ field: 'body', operator: 'contains', value: '' },
{ field: 'isGroup', operator: 'is', value: false },
],
}),
false,
);
});
test('a list condition with no values is incomplete', () => {
assert.equal(filtersIncomplete({ conditions: [sender(['[email protected]']), sender([])] }), true);
assert.equal(filtersIncomplete({ conditions: [{ field: 'type', operator: 'is', value: [] }] }), true);
});
test('the gateway limits are enforced', () => {
assert.equal(filtersIncomplete({ conditions: Array.from({ length: 20 }, () => sender(['[email protected]'])) }), false);
assert.equal(filtersIncomplete({ conditions: Array.from({ length: 21 }, () => sender(['[email protected]'])) }), true);
const values = (n: number) => Array.from({ length: n }, (_, i) => `${i}@c.us`);
assert.equal(filtersIncomplete({ conditions: [sender(values(100))] }), false);
assert.equal(filtersIncomplete({ conditions: [sender(values(101))] }), true);
assert.equal(
filtersIncomplete({ conditions: [{ field: 'body', operator: 'contains', value: 'x'.repeat(1001) }] }),
true,
);
});