// © 2026 Evntx FZCO, Dubai, UAE. All rights reserved. // Runs the BUILT connector (dist/) with a stand-in for the API (global fetch replaced) and an in-memory store. // 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'); function fakeApi(handler) { const calls = []; globalThis.fetch = async (url, init = {}) => { const u = new URL(url); calls.push({ url: u, init }); const r = await handler(u, init, calls.length); return new Response(JSON.stringify(r.body ?? {}), { status: r.status ?? 200, headers: { 'content-type': 'application/json', ...(r.headers ?? {}) } }); }; return calls; } const memStore = () => { const s = new Map(); return { s, put: async (k, v) => (s.set(k, v), v), get: async (k) => s.get(k) ?? null, delete: async (k) => { s.delete(k); } }; }; const conn = { type: 'SECRET_TEXT', secret_text: 'key-1' }; const c = (id, at) => ({ id, email: `${id}@example.org`, name: id, listId: 'l1', createdAt: at }); const pageOf = (items, page, total) => ({ items, page, total_pages: total }); test('the connector has the action, the polling trigger and a secret-text key', () => { assert.ok(m.exampleContacts.getAction('list_contacts')); assert.equal(m.exampleContacts.getTrigger('new_contact').type, 'POLLING'); assert.equal(m.auth.type, 'SECRET_TEXT'); }); test('list_contacts reads every numbered page with the key in the header', async () => { const calls = fakeApi((u) => { const p = Number(u.searchParams.get('page')); return { body: pageOf([c(`p${p}a`, 't'), c(`p${p}b`, 't')], p, 3) }; }); const r = await m.listContacts.run({ auth: conn, propsValue: { listId: 'l1', maxItems: 100 } }); assert.equal(r.contacts.length, 6); assert.deepEqual(calls.map((x) => x.url.searchParams.get('page')), ['1', '2', '3']); assert.equal(calls[0].init.headers['x-api-key'], 'key-1'); assert.equal(calls[0].url.pathname, '/v2/lists/l1/contacts'); }); test('list_contacts stops reading pages once it has the limit', async () => { const calls = fakeApi((u) => ({ body: pageOf(Array.from({ length: 100 }, (_, k) => c(`x${u.searchParams.get('page')}-${k}`, 't')), Number(u.searchParams.get('page')), 50) })); const r = await m.listContacts.run({ auth: conn, propsValue: { listId: 'l1', maxItems: 150 } }); assert.equal(r.contacts.length, 150); assert.equal(calls.length, 2); }); test('the list picker is filled from the API, and asks for a connection first without one', async () => { fakeApi(() => ({ body: pageOf([{ id: 'l1', name: 'Customers' }], 1, 1) })); const prop = m.listContacts.props.listId; assert.deepEqual((await prop.options({ auth: conn }, {})).options, [{ label: 'Customers', value: 'l1' }]); assert.equal((await prop.options({}, {})).disabled, true); }); test('a refused key gives a message that names the connection', async () => { fakeApi(() => ({ status: 401 })); await assert.rejects(m.listContacts.run({ auth: conn, propsValue: { listId: 'l1' } }), /API key was refused/); }); test('polling: enable starts from the newest contact, then each contact starts the flow exactly once', async () => { const store = memStore(); let data = [c('a', '2026-01-01T10:00:00Z')]; fakeApi((u) => { if (u.searchParams.get('sort') === '-createdAt') return { body: pageOf(data.slice(-1), 1, 1) }; const since = u.searchParams.get('created_since'); return { body: pageOf(data.filter((x) => x.createdAt >= since), 1, 1) }; }); const t = m.newContact; const ctx = { auth: conn, propsValue: { listId: 'l1' }, store }; await t.onEnable(ctx); assert.deepEqual(await t.run(ctx), [], 'the contact that existed at enable does not start the flow'); data = [...data, c('b', '2026-01-01T11:00:00Z'), c('c', '2026-01-01T11:00:00Z')]; assert.deepEqual((await t.run(ctx)).map((x) => x.id), ['b', 'c']); assert.deepEqual(await t.run(ctx), [], 'a second poll with nothing new starts nothing'); data = [...data, c('d', '2026-01-01T11:00:00Z')]; assert.deepEqual((await t.run(ctx)).map((x) => x.id), ['d'], 'same timestamp as the mark, new id: started once'); await t.onDisable(ctx); assert.equal(store.s.size, 0); }); test('fresh(): order, high-water mark and ids at the mark', () => { const r = m.fresh([c('z', '2'), c('y', '3'), c('x', '3'), c('old', '1')], { at: '2', ids: ['z'] }); assert.deepEqual(r.items.map((x) => x.id), ['x', 'y']); assert.deepEqual(r.mark, { at: '3', ids: ['x', 'y'] }); const same = m.fresh([], { at: '3', ids: ['x'] }); assert.deepEqual(same.mark, { at: '3', ids: ['x'] }); });