// © 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 { createHmac } from 'node:crypto'; 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(r.body === undefined ? null : JSON.stringify(r.body), { status: r.status ?? 200, headers: { 'content-type': 'application/json' } }); }; 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 SECRET = 'whsec-test-1'; const sign = (raw, t = Math.floor(Date.now() / 1000), secret = SECRET) => `t=${t},v1=${createHmac('sha256', secret).update(`${t}.${raw}`).digest('hex')}`; const ev = { id: 'e1', type: 'order.created', occurredAt: '2026-01-01T00:00:00Z', data: { orderId: 'o1' } }; const t = m.eventReceived; async function enabled() { const store = memStore(); const calls = fakeApi(() => ({ status: 201, body: { id: 'wh_1', secret: SECRET } })); await t.onEnable({ auth: conn, propsValue: { types: ['order.created'] }, webhookUrl: 'https://aancer.invalid/hook/abc', store }); return { store, calls }; } const delivery = (body, headers) => ({ payload: { body, rawBody: JSON.stringify(body), headers, queryParams: {} } }); test('the trigger is a webhook with a query handshake and a daily renewal', () => { assert.equal(t.type, 'WEBHOOK'); assert.equal(t.handshakeConfiguration.strategy, 'QUERY_PRESENT'); assert.equal(t.renewConfiguration.strategy, 'CRON'); }); test('enable registers the installation URL for the chosen types and keeps the signing secret', async () => { const { store, calls } = await enabled(); assert.equal(calls[0].init.method, 'POST'); assert.deepEqual(JSON.parse(calls[0].init.body), { url: 'https://aancer.invalid/hook/abc', events: ['order.created'] }); assert.deepEqual(store.s.get('subscription'), { id: 'wh_1', secret: SECRET }); }); test('a correctly signed delivery of a chosen type starts the flow once', async () => { const { store } = await enabled(); const raw = JSON.stringify(ev); assert.deepEqual(await t.run({ ...delivery(ev, { 'X-Signature': sign(raw) }), store, propsValue: { types: ['order.created'] } }), [ev]); }); test('unsigned, wrongly signed, replayed or other-type deliveries start nothing', async () => { const { store } = await enabled(); const raw = JSON.stringify(ev); const pv = { types: ['order.created'] }; assert.deepEqual(await t.run({ ...delivery(ev, {}), store, propsValue: pv }), [], 'no signature'); assert.deepEqual(await t.run({ ...delivery(ev, { 'x-signature': sign(raw, undefined, 'other') }), store, propsValue: pv }), [], 'wrong secret'); assert.deepEqual(await t.run({ ...delivery(ev, { 'x-signature': sign(raw, Math.floor(Date.now() / 1000) - 3600) }), store, propsValue: pv }), [], 'an hour old'); const tampered = { ...ev, data: { orderId: 'o2' } }; assert.deepEqual(await t.run({ payload: { body: tampered, rawBody: JSON.stringify(tampered), headers: { 'x-signature': sign(raw) }, queryParams: {} }, store, propsValue: pv }), [], 'body changed after signing'); const paid = { ...ev, type: 'order.paid' }; assert.deepEqual(await t.run({ ...delivery(paid, { 'x-signature': sign(JSON.stringify(paid)) }), store, propsValue: pv }), [], 'type not chosen'); }); test('the handshake echoes the challenge', async () => { const r = await t.onHandshake({ payload: { body: {}, headers: {}, queryParams: { challenge: 'abc123' } } }); assert.equal(r.status, 200); assert.deepEqual(r.body, { challenge: 'abc123' }); }); test('renew calls the service for the stored subscription; disable removes it, also when the service already lost it', async () => { const { store } = await enabled(); let calls = fakeApi(() => ({ status: 204 })); await t.onRenew({ auth: conn, store }); assert.equal(calls[0].url.pathname, '/v1/webhooks/wh_1/renew'); calls = fakeApi(() => ({ status: 404, body: { error: 'gone' } })); await t.onDisable({ auth: conn, store }); assert.equal(calls[0].init.method, 'DELETE'); assert.equal(store.s.size, 0); }); test('verifySignature rejects malformed headers', () => { const raw = Buffer.from('{}'); for (const h of [undefined, '', 't=abc,v1=00', 't=1,v1=zz', `t=${Math.floor(Date.now() / 1000)},v1=${'0'.repeat(63)}`]) assert.equal(m.verifySignature(h, raw, SECRET), false, String(h)); });