aere-cloud-sdk/test/sdk.test.mjs

130 lines
7.3 KiB
JavaScript

// Testele SDK-ului: rutele contra unui server FALS (deterministe, offline) + verifyWebhook cu
// perechea negativa + o proba VIE optionala contra productiei daca AERE_CHEIE e in mediu.
import assert from 'node:assert';
import crypto from 'node:crypto';
import { AereCloud, AereCloudError, verifyWebhook } from '../index.mjs';
let treceri = 0;
async function test(nume, fn) {
try { await fn(); treceri++; console.log(' OK ' + nume); }
catch (e) { console.log(' ESEC ' + nume + ' — ' + e.message); process.exitCode = 1; }
}
// ---- server fals: raspunde dupa cale, ca sa testam constructia cererii si maparea erorilor ----
function fetchFals(rute) {
return async (url, opts = {}) => {
const u = new URL(url);
const cale = u.pathname.replace(/^\/v1/, ''); // baseUrl de test include /v1
const cheie = opts.method + ' ' + cale + u.search;
const r = rute[cheie] || rute[opts.method + ' ' + cale];
if (!r) return { ok: false, status: 404, json: async () => ({ error: 'not_found' }) };
r._vazut = { headers: opts.headers, body: opts.body ? JSON.parse(opts.body) : undefined };
return { ok: r.status < 400, status: r.status, json: async () => r.corp };
};
}
// AERE-SINTETIC: cheile de mai jos (ak2800.0xAAA.secret, secretul-meu) sunt FICTIVE, pentru
// testul offline; nu sunt chei ale retelei. Marcajul spune portii de secrete exact asta.
const BAZA_TEST = 'https://x.test/v1';
await test('constructorul cere cheia', () => {
assert.throws(() => new AereCloud({}), /apiKey is required/);
});
await test('cheia merge in antetul x-api-key, health e fara cheie', async () => {
const rute = { 'GET /health': { status: 200, corp: { ok: true, chainId: 2800, block: 5 } },
'GET /account': { status: 200, corp: { address: '0xabc', planId: 5 } } };
const c = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-secret', baseUrl: BAZA_TEST, fetch: fetchFals(rute) });
const h = await c.health();
assert.equal(h.block, 5);
assert.equal(rute['GET /health']._vazut.headers['x-api-key'], undefined, 'health nu trimite cheie');
await c.account();
assert.equal(rute['GET /account']._vazut.headers['x-api-key'], 'ak2800.0xAAA.AERE-SINTETIC-secret');
});
await test('rpc impacheteaza corect si ridica eroarea RPC', async () => {
const rute = {
'POST /rpc': { status: 200, corp: { jsonrpc: '2.0', id: 1, result: '0x10' } },
};
const c = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-s', baseUrl: BAZA_TEST, fetch: fetchFals(rute) });
assert.equal(await c.blockNumber(), 16);
assert.deepEqual(rute['POST /rpc']._vazut.body, { jsonrpc: '2.0', id: 1, method: 'eth_blockNumber', params: [] });
// eroare RPC in plic -> AereCloudError
const rute2 = { 'POST /rpc': { status: 200, corp: { error: { code: -32601, message: 'nope' } } } };
const c2 = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-s', baseUrl: BAZA_TEST, fetch: fetchFals(rute2) });
await assert.rejects(() => c2.rpc('qbft_proposeValidatorVote'), (e) => e instanceof AereCloudError && e.body.error.code === -32601);
});
await test('un status HTTP de eroare devine AereCloudError cu corpul serverului', async () => {
const rute = { 'POST /pq/verify': { status: 400, corp: { error: 'bad_publicKey', hint: '0x hex' } } };
const c = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-s', baseUrl: BAZA_TEST, fetch: fetchFals(rute) });
await assert.rejects(() => c.pqVerify({ scheme: 'ml-dsa-44', publicKey: 'nu-e-hex' }),
(e) => e instanceof AereCloudError && e.status === 400 && e.body.error === 'bad_publicKey');
});
await test('rutele de date construiesc corect query-ul', async () => {
const rute = {
'GET /data/anchors?limit=3': { status: 200, corp: { anchors: [] } },
'GET /data/transfers?address=0xBeef&limit=25': { status: 200, corp: { tokenTransfers: [], nativeTransfers: [] } },
};
const c = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-s', baseUrl: BAZA_TEST, fetch: fetchFals(rute) });
await c.anchors(3);
await c.transfers('0xBeef', 25);
// daca reperele nu s-ar potrivi, fetchFals ar da 404 si _req ar arunca
});
await test('sponsorCreatePqAccount: cale si corp corecte', async () => {
const rute = { 'POST /sponsor/createPqAccount': { status: 200, corp: { txHash: '0xab', account: '0xcd', alreadyDeployed: false } } };
const c = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-s', baseUrl: BAZA_TEST, fetch: fetchFals(rute) });
const d = await c.sponsorCreatePqAccount('0x09aa', 5);
assert.equal(d.account, '0xcd');
const trimis = rute['POST /sponsor/createPqAccount']._vazut.body; // fetchFals parseaza deja
assert.equal(trimis.salt, 5);
assert.equal(trimis.falconPubKey, '0x09aa');
});
await test('2026-09-17: proof(hash) si readiness(): ruta, cheia doar cand se cere, corpul corect', async () => {
const H = '0x' + 'ab'.repeat(32);
const vazute = [];
const rute = {
['GET /proof/' + H]: { status: 200, corp: { hash: H, notarized: true, finality: 'post-quantum' } },
'POST /pq/readiness': { status: 200, corp: { domain: 'aere.network', score: 75 } },
'GET /pq/readiness/aere.network': { status: 200, corp: { domain: 'aere.network', score: 75, cached: true } },
};
const f = fetchFals(rute);
const spion = async (url, init) => { vazute.push({ url, cheie: !!(init.headers && init.headers['x-api-key']), corp: init.body }); return f(url, init); };
const c = new AereCloud({ apiKey: 'ak2800.0xAAA.AERE-SINTETIC-s', baseUrl: BAZA_TEST, fetch: spion });
const p = await c.proof(H); assert.equal(p.finality, 'post-quantum'); assert.equal(vazute[0].cheie, true, 'proof cere cheia');
await c.readiness('aere.network'); assert.equal(vazute[1].cheie, false, 'scanarea libera nu trimite cheia'); assert.equal(vazute[1].corp, JSON.stringify({ domain: 'aere.network' }));
await c.readiness('aere.network', { attest: true, fresh: true }); assert.equal(vazute[2].cheie, true, 'atestarea trimite cheia'); assert.deepEqual(JSON.parse(vazute[2].corp), { domain: 'aere.network', fresh: true, attest: true });
const g = await c.readinessReport('aere.network'); assert.equal(g.cached, true); assert.equal(vazute[3].cheie, false);
});
await test('verifyWebhook: semnatura buna trece, una gresita pica, o litera in plus pica', async () => {
const secret = 'AERE-SINTETIC-secretul-meu';
const corp = JSON.stringify({ event: 'pq-anchor', data: { height: 42 } });
const bun = crypto.createHmac('sha256', secret).update(corp).digest('hex'); // ca serverul
assert.equal(await verifyWebhook(corp, bun, secret), true);
assert.equal(await verifyWebhook(corp, bun.slice(0, -1) + (bun.slice(-1) === 'a' ? 'b' : 'a'), secret), false);
assert.equal(await verifyWebhook(corp, bun + 'x', secret), false);
assert.equal(await verifyWebhook(corp + ' ', bun, secret), false, 'corp modificat = alta semnatura');
});
// ---- proba VIE optionala ----
if (process.env.AERE_CHEIE) {
await test('LIVE: health + account + anchors prin productie', async () => {
const c = new AereCloud({ apiKey: process.env.AERE_CHEIE });
const h = await c.health();
assert.equal(h.chainId, 2800);
assert.ok(h.block > 0);
const a = await c.anchors(1);
assert.ok(a.anchors[0].falconSeals >= 6, 'ancora vie sub cvorum');
const acc = await c.account();
assert.ok(acc.usageLast31Days);
});
} else {
console.log(' (sar proba LIVE: AERE_CHEIE nu e in mediu)');
}
console.log('\n' + treceri + ' teste trecute');