Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(sdk-js): pin the request timer, default timeout and warning
Yudhi Armyndharis2c8e44b9d0b9
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.tsindex a29f086d4..75b23ebf3 100644--- a/sdk/javascript/test/client.test.ts+++ b/sdk/javascript/test/client.test.ts@@ -335,6 +335,33 @@ describe('OpenWAClient', () => {expect(plain.retryAfterSeconds).toBeUndefined();});+ it('keeps the text of an error body that is not JSON, and names the error', async () => {+ // A proxy in front of the gateway answers 502 with a page of its own.+ const page = '<html>Bad Gateway</html>';+ const t = new MockTransport().on('GET', '/api/sessions', { status: 502, text: page, contentType: 'text/html' });+ const err = (await client(t)+ .sessions.list()+ .catch((e: unknown) => e)) as OpenWAApiError;++ expect(err).toBeInstanceOf(OpenWAApiError);+ expect(err.name).toBe('OpenWAApiError');+ expect(err.status).toBe(502);+ expect(err.body).toBe(page);+ expect(err.message).toContain(page);+ });++ it('joins a list of validation messages into the error message', async () => {+ const t = new MockTransport().on('POST', '/api/sessions', {+ status: 400,+ body: { statusCode: 400, message: ['name must be a string', 'name should not be empty'], error: 'Bad Request' },+ });+ const err = (await client(t)+ .sessions.create({ name: '' })+ .catch((e: unknown) => e)) as OpenWAApiError;++ expect(err.message).toContain('name must be a string, name should not be empty');+ });+it('falls back to the generic OpenWAApiError (with .status) for an unmapped status', async () => {const t = new MockTransport().on('GET', '/api/sessions', {status: 418,@@ -354,6 +381,81 @@ describe('OpenWAClient', () => {await expect(c.sessions.list()).rejects.toBeInstanceOf(OpenWATimeoutError);});+ it('leaves no timer running once a request has resolved or been refused', async () => {+ // A timer left pending keeps a short script's process alive until the whole timeout has passed.+ vi.useFakeTimers();+ try {+ const t = new MockTransport()+ .on('GET', '/api/sessions/missing', {+ status: 404,+ body: { statusCode: 404, message: 'Session not found', error: 'Not Found' },+ })+ .on('GET', '/api/sessions', { body: [] });+ const c = client(t);+ await c.sessions.list();+ expect(vi.getTimerCount()).toBe(0);++ await expect(c.sessions.get('missing')).rejects.toBeInstanceOf(OpenWANotFoundError);+ expect(vi.getTimerCount()).toBe(0);+ } finally {+ vi.useRealTimers();+ }+ });++ it('gives up on a request after 30 seconds unless told otherwise', async () => {+ vi.useFakeTimers();+ try {+ const unanswered: FetchLike = (_url, init) =>+ new Promise((_resolve, reject) => {+ init?.signal?.addEventListener('abort', () => {+ const e = new Error('aborted');+ e.name = 'AbortError';+ reject(e);+ });+ });+ const c = new OpenWAClient({ baseUrl: 'http://x', apiKey: 'k', fetch: unanswered });+ const outcome = c.sessions.list().then(+ () => 'resolved',+ (e: unknown) => e,+ );+ // Armed before any time passes: a client that never times out fails here instead of hanging.+ expect(vi.getTimerCount()).toBe(1);++ await vi.advanceTimersByTimeAsync(29_999);+ expect(vi.getTimerCount()).toBe(1);++ await vi.advanceTimersByTimeAsync(2);+ const err = await outcome;+ expect(err).toBeInstanceOf(OpenWATimeoutError);+ expect((err as OpenWATimeoutError).name).toBe('OpenWATimeoutError');+ expect((err as OpenWATimeoutError).message).toContain('30000ms');+ } finally {+ vi.useRealTimers();+ }+ });++ it('warns about a remote http:// base URL, and about no other', () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);+ try {+ new OpenWAClient({ baseUrl: 'http://gateway.example', apiKey: 'k' });+ expect(warn).toHaveBeenCalledTimes(1);+ expect(String(warn.mock.calls[0][0])).toContain('gateway.example');++ warn.mockClear();+ for (const baseUrl of [+ 'http://localhost:2785',+ 'http://127.0.0.1:2785',+ 'http://[::1]:2785',+ 'https://gateway.example',+ ]) {+ new OpenWAClient({ baseUrl, apiKey: 'k' });+ }+ expect(warn).not.toHaveBeenCalled();+ } finally {+ warn.mockRestore();+ }+ });+it('keeps the timeout armed while reading a stalled response body', async () => {const stalledBodyFetch: FetchLike = async (_url, init) => {const signal = init?.signal;