import test from 'node:test';
import assert from 'node:assert/strict';
import { compact, mergeRows, buildIndex, search, terms, liveCheck, probeable, marketStats } from '../src/engine.js';
import { mockNet, CDP_ITEMS } from './mock.js';
test('compact: cheapest mainnet USDC price, networks, testnet-only dropped, quality fields', () => {
const r = compact(CDP_ITEMS[0], 'cdp');
assert.deepEqual([r.p, r.n, r.c, r.y, r.h, r.m], [0.01, ['base'], 5000, 120, 'search.example.com', 'GET']);
assert.equal(compact(CDP_ITEMS[6], 'cdp'), null, 'testnet-only');
const v1 = compact({ resource: 'https://x.example/a', accepts: [{ network: 'base', asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', maxAmountRequired: '2500' }, { network: 'solana', asset: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', maxAmountRequired: '2000' }] }, 'payai');
assert.deepEqual([v1.p, v1.n.sort(), v1.c], [0.002, ['base', 'solana'], null]);
assert.equal(compact({ resource: 'ftp://x', accepts: [] }, 'cdp'), null);
});
test('buildIndex: merges CDP + PayAI; PayAI down = listed, still built; CDP down = throws (keeps last good index)', async () => {
const idx = await buildIndex({ fetchImpl: mockNet().fetchImpl, pageSize: 3 });
assert.equal(idx.counts.cdp_total, CDP_ITEMS.length); assert.equal(idx.rows.length, 8); assert.deepEqual(idx.sources_failed, []);
assert.equal(idx.rows.find((r) => r.u === 'https://search.example.com/v1/search').src, 'both');
const p = await buildIndex({ fetchImpl: mockNet({ payaiDown: true }).fetchImpl });
assert.deepEqual(p.sources_failed, ['payai_index']); assert.equal(p.rows.length, 7);
await assert.rejects(buildIndex({ fetchImpl: mockNet({ cdpDown: true }).fetchImpl }), /cdp index/);
});
test('search: relevance x payers x recency, filters, per-host cap, stop words', async () => {
const rows = mergeRows(CDP_ITEMS.map((x) => compact(x, 'cdp')), []);
assert.deepEqual(terms('Which API should I use for the best web search?'), ['web', 'search']);
const s = search(rows, 'web search');
assert.equal(s.top[0].r.s, 'SearchCo'); assert.ok(s.matches >= 3);
assert.equal(search(rows, 'web search', { maxPrice: 0.003 }).top[0].r.s, 'CheapSearch');
assert.equal(search(rows, 'token safety', { network: 'solana' }).top[0].r.s, 'SolSafe');
assert.equal(search(rows, 'web search', { minPayers: 100 }).matches, 1);
assert.equal(search(rows, 'zebra quantum').matches, 0);
const st = marketStats(s.all); assert.ok(st.price_usd.median > 0); assert.equal(st.hosts_with_3plus_payers, 3);
});
test('liveCheck: v2 header terms, v1 body terms, no 402, unreachable, path params not probed, never pays', async () => {
const net = mockNet();
const ok = await liveCheck({ u: 'https://search.example.com/v1/search', p: 0.01, m: 'GET' }, { fetchImpl: net.fetchImpl });
assert.deepEqual([ok.status, ok.x402_version, ok.live_price_usd, ok.price_matches_index], ['live', 2, 0.01, true]);
const v1only = await liveCheck({ u: 'https://v1.example.com/x', p: 0.001 }, { fetchImpl: async () => new Response(JSON.stringify({ x402Version: 1, accepts: [{ network: 'base', asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', maxAmountRequired: '1000' }] }), { status: 402 }) });
assert.deepEqual([v1only.status, v1only.x402_version, v1only.price_matches_index], ['live', 1, true]);
assert.equal((await liveCheck({ u: 'https://cheapsearch.example.com/search' }, { fetchImpl: mockNet({ live: { 'https://cheapsearch.example.com/search': '200' } }).fetchImpl })).status, 'no_402');
assert.equal((await liveCheck({ u: 'https://deadsearch.example.com/search' }, { fetchImpl: net.fetchImpl })).status, 'unreachable');
assert.equal((await liveCheck({ u: 'https://h.example/x' }, { fetchImpl: () => new Promise(() => {}), ms: 50 })).status, 'unreachable');
assert.equal((await liveCheck({ u: 'https://rug.example.com/api/:mint/rug' }, { fetchImpl: net.fetchImpl })).status, 'not_probed');
for (const u of ['http://a.example/x', 'https://127.0.0.1/x', 'https://localhost/x', 'https://a.example/{id}']) assert.equal(probeable(u), false, u);
const seen = []; await liveCheck({ u: 'https://pdf.example.com/extract', m: 'POST' }, { fetchImpl: async (u, init) => { seen.push(init); return new Response('', { status: 402 }); } });
assert.equal(seen[0].method, 'POST'); assert.equal(seen[0].body, '{}'); assert.equal(seen[0].headers['payment-signature'], undefined); assert.equal(seen[0].headers['x-payment'], undefined);
});
agentgate-source: code mirror on Cloudflare Pages. All repos