// © 2026 Evntx FZCO, Dubai, UAE. All rights reserved. // Runs the BUILT connector (dist/) with a stand-in for the API: global fetch is replaced, nothing leaves the machine. // npm run build && npm test import test from 'node:test'; import assert from 'node:assert/strict'; import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const m = require('../dist/src/index.js'); /** Replaces fetch with `handler(url, init)` → { status, body, headers }; returns the list of calls. */ function fakeApi(handler) { const calls = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: new URL(url), init }); const r = await handler(new URL(url), init, calls.length); return new Response(r.body === undefined ? null : JSON.stringify(r.body), { status: r.status ?? 200, headers: { 'content-type': 'application/json', ...(r.headers ?? {}) } }); }; return calls; } const ctx = (propsValue) => ({ auth: { access_token: 'tok-1', data: {} }, propsValue }); const task = (i) => ({ id: `t${i}`, title: `Task ${i}`, done: false, updatedAt: '2026-01-01T00:00:00Z' }); test('the connector carries both actions and an OAuth2 connection with PKCE', () => { assert.equal(m.exampleTasks.getAction('list_tasks').name, 'list_tasks'); assert.equal(m.exampleTasks.getAction('create_task').name, 'create_task'); assert.equal(m.auth.type, 'OAUTH2'); assert.equal(m.auth.pkce, true); }); test('list_tasks follows the cursor over three pages and sends the bearer token', async () => { const pages = { '': { data: [task(1), task(2)], next_cursor: 'c2' }, c2: { data: [task(3)], next_cursor: 'c3' }, c3: { data: [task(4)], next_cursor: null } }; const calls = fakeApi((u) => ({ body: pages[u.searchParams.get('cursor') ?? ''] })); const r = await m.listTasks.run(ctx({ onlyOpen: true, maxItems: 50 })); assert.deepEqual(r.tasks.map((t) => t.id), ['t1', 't2', 't3', 't4']); assert.equal(r.complete, true); assert.equal(calls.length, 3); assert.equal(calls[0].init.headers.authorization, 'Bearer tok-1'); assert.equal(calls[0].url.searchParams.get('done'), 'false'); }); test('list_tasks stops at the limit and says the list is not complete', async () => { const calls = fakeApi((u, _i, n) => ({ body: { data: Array.from({ length: Number(u.searchParams.get('limit')) }, (_, k) => task(n * 100 + k)), next_cursor: `c${n}` } })); const r = await m.listTasks.run(ctx({ maxItems: 150 })); assert.equal(r.tasks.length, 150); assert.equal(r.complete, false); assert.deepEqual(calls.map((c) => c.url.searchParams.get('limit')), ['100', '50']); }); test('a 429 is retried after Retry-After, then the call succeeds', async () => { const calls = fakeApi((_u, _i, n) => (n === 1 ? { status: 429, headers: { 'retry-after': '0' }, body: { error: 'slow down' } } : { body: { data: [task(1)], next_cursor: null } })); const r = await m.listTasks.run(ctx({})); assert.equal(r.tasks.length, 1); assert.equal(calls.length, 2); }); test('a server that keeps failing is given up after 4 retries with the status in the message', async () => { const calls = fakeApi(() => ({ status: 503, headers: { 'retry-after': '0' }, body: { error: 'down' } })); await assert.rejects(m.listTasks.run(ctx({})), /HTTP 503/); assert.equal(calls.length, 5); }); test('a 401 asks to reconnect and is not retried', async () => { const calls = fakeApi(() => ({ status: 401, body: { error: 'expired' } })); await assert.rejects(m.createTask.run(ctx({ title: 'x' })), /Reconnect the connection/); assert.equal(calls.length, 1); }); test('create_task trims the title and refuses an empty one before any call', async () => { const calls = fakeApi((_u, init) => ({ status: 201, body: { ...task(9), title: JSON.parse(init.body).title } })); assert.equal((await m.createTask.run(ctx({ title: ' Buy milk ' }))).title, 'Buy milk'); assert.equal(calls[0].init.method, 'POST'); await assert.rejects(m.createTask.run(ctx({ title: ' ' })), /Title is empty/); assert.equal(calls.length, 1); }); test('retryDelay: seconds, an HTTP date, no header, and the 30 s cap', () => { const now = Date.parse('2026-01-01T00:00:00Z'); assert.equal(m.retryDelay('2', 0, now), 2000); assert.equal(m.retryDelay('Thu, 01 Jan 2026 00:00:05 GMT', 0, now), 5000); assert.equal(m.retryDelay(null, 2, now), 2000); assert.equal(m.retryDelay('3600', 0, now), 30_000); assert.equal(m.retryDelay('garbage', 1, now), 1000); });