Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import { paginate, resolveListWindow } from './paginate';
describe('paginate', () => {
const items = Array.from({ length: 1500 }, (_, i) => i);
it('caps an unbounded request at the default limit (1000)', () => {
const out = paginate(items);
expect(out.length).toBe(1000);
expect(out[0]).toBe(0);
expect(out[999]).toBe(999);
});
it('returns exactly `limit` items, clamped to the max (1000)', () => {
expect(paginate(items, 10)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(paginate(items, 99999).length).toBe(1000); // explicit huge limit clamped to the cap
expect(paginate(items, 0).length).toBe(1); // clamped up to the minimum of 1
});
it('pages with offset', () => {
expect(paginate(items, 5, 10)).toEqual([10, 11, 12, 13, 14]);
});
it('treats a non-finite limit/offset as the safe default (NaN limit -> cap, NaN offset -> 0)', () => {
expect(paginate(items, NaN).length).toBe(1000);
expect(paginate(items, 5, NaN)).toEqual([0, 1, 2, 3, 4]);
});
it('returns the whole collection when it is under the cap', () => {
expect(paginate([1, 2, 3])).toEqual([1, 2, 3]);
});
});
describe('resolveListWindow', () => {
it('defaults a missing/non-finite window to { limit: 1000, offset: 0 }', () => {
expect(resolveListWindow()).toEqual({ limit: 1000, offset: 0 });
expect(resolveListWindow(NaN, NaN)).toEqual({ limit: 1000, offset: 0 });
expect(resolveListWindow(Infinity, -Infinity)).toEqual({ limit: 1000, offset: 0 });
});
it('clamps limit to [1, 1000] and offset to >= 0', () => {
expect(resolveListWindow(5000, -5)).toEqual({ limit: 1000, offset: 0 });
expect(resolveListWindow(0, 25)).toEqual({ limit: 1, offset: 25 });
expect(resolveListWindow(50, 10)).toEqual({ limit: 50, offset: 10 });
});
it('caps an offset past the largest safe integer, which a driver would refuse as an OFFSET', () => {
expect(resolveListWindow(10, 2 ** 63)).toEqual({ limit: 10, offset: Number.MAX_SAFE_INTEGER });
expect(resolveListWindow(10, 1e21)).toEqual({ limit: 10, offset: Number.MAX_SAFE_INTEGER });
expect(resolveListWindow(10, Number.MAX_SAFE_INTEGER)).toEqual({ limit: 10, offset: Number.MAX_SAFE_INTEGER });
});
it('truncates fractional limit/offset toward zero', () => {
expect(resolveListWindow(10.9, 4.7)).toEqual({ limit: 10, offset: 4 });
});
it('keeps the boundary values exactly', () => {
expect(resolveListWindow(1, 0)).toEqual({ limit: 1, offset: 0 });
expect(resolveListWindow(1000, 0)).toEqual({ limit: 1000, offset: 0 });
expect(resolveListWindow(1001, 0)).toEqual({ limit: 1000, offset: 0 });
});
});