import test from 'node:test';
import assert from 'node:assert/strict';
import { onRequest } from '../functions/[[path]].js';
// Fake service bindings: echo what the Worker would see, or return a fixed 402.
function fakeEnv(log) {
const svc = (name) => ({ async fetch(req) {
log.push({ name, url: req.url, method: req.method, headers: Object.fromEntries(req.headers), body: req.body ? await req.text() : null });
const u = new URL(req.url);
const base = `https://${req.headers.get('x-forwarded-host')}${req.headers.get('x-forwarded-prefix')}`;
if (u.pathname === '/.well-known/x402') return Response.json({ version: 1, resources: [`${base}/v1/x`] });
if (u.pathname === '/openapi.json') return Response.json({ openapi: '3.1.0', servers: [{ url: base }], paths: { '/v1/x': { get: { summary: 'x' } } } });
if (u.pathname === '/stream') return new Response(new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode('data: 1\n\n')); c.close(); } }), { headers: { 'content-type': 'text/event-stream' } });
return new Response('{}', { status: 402, headers: { 'content-type': 'application/json', 'PAYMENT-REQUIRED': 'abc', 'X-PAYMENT-RESPONSE': 'r1', 'PAYMENT-RESPONSE': 'r2' } });
} });
return { PUBLIC_HOST: 'api.youraitools.co', AGENTGATE: svc('agentgate'), WEBREAD: svc('webread'), INJECTSCAN: svc('injectscan'), DOMAINCHECK: svc('domaincheck'), PAYGUARD: svc('payguard'), PKGGUARD: svc('pkgguard'), STACKDETECT: svc('stackdetect'), TOKENVERDICT: svc('tokenverdict'), SIGNCHECK: svc('signcheck'), COUNTERPARTYCHECK: svc('counterparty'), X402FIX: svc('x402fix'), SOLVERDICT: svc('solverdict'), AGENTREADY: svc('agentready'), PERPRADAR: svc('perpradar'), TASIGNAL: svc('tasignal'), LEAKGUARD: svc('leakguard'), SMARTPERPS: svc('smartperps'), TOOLPICK: svc('toolpick'), PERPCHECK: svc('perpcheck'), X402TEMPLATE: svc('x402-template'), CREDITS: svc('credits'), LISTING: svc('listing') };
}
const call = (path, init, env) => onRequest({ request: new Request(`https://youraitools-api.pages.dev${path}`, init), env });
test('proxy passes method, query, body, payment headers and 402 status untouched; strips the prefix', async () => {
const log = []; const env = fakeEnv(log);
const r = await call('/injectscan/v1/scan?x=1&y=%20z', { method: 'POST', headers: { 'content-type': 'application/json', 'PAYMENT-SIGNATURE': 'sig', 'X-PAYMENT': 'xp', 'x-forwarded-host': 'evil.example', 'cf-connecting-ip': '203.0.113.9' }, body: '{"text":"hi"}' }, env);
assert.equal(r.status, 402);
assert.equal(r.headers.get('PAYMENT-REQUIRED'), 'abc'); assert.equal(r.headers.get('PAYMENT-RESPONSE'), 'r2'); assert.equal(r.headers.get('X-PAYMENT-RESPONSE'), 'r1');
const u = log[0];
assert.equal(u.name, 'injectscan'); assert.equal(u.url, 'https://injectscan.agentgate.workers.dev/v1/scan?x=1&y=%20z');
assert.equal(u.method, 'POST'); assert.equal(u.body, '{"text":"hi"}');
assert.equal(u.headers['payment-signature'], 'sig'); assert.equal(u.headers['x-payment'], 'xp');
assert.equal(u.headers['x-forwarded-host'], 'api.youraitools.co'); assert.equal(u.headers['x-forwarded-prefix'], '/injectscan');
assert.equal(u.headers['x-forwarded-for'], '203.0.113.9');
});
test('/<tool> and /<tool>/ map to the Worker root; unknown prefixes 404', async () => {
const log = []; const env = fakeEnv(log);
await call('/agentgate', {}, env); await call('/domaincheck/', {}, env);
assert.deepEqual(log.map((l) => l.url), ['https://agentgate.agentgate.workers.dev/', 'https://domaincheck.agentgate.workers.dev/']);
assert.equal((await call('/nope/x', {}, env)).status, 404);
});
test('streaming bodies pass through', async () => {
const r = await call('/webread/stream', {}, fakeEnv([]));
assert.equal(r.headers.get('content-type'), 'text/event-stream'); assert.equal(await r.text(), 'data: 1\n\n');
});
test('root index, combined /.well-known/x402 and /openapi.json use api.youraitools.co/<prefix>', async () => {
const env = fakeEnv([]);
const idx = await (await call('/', {}, env)).json();
assert.equal(idx.tools.length, 21); assert.equal(idx.tools[20].baseUrl, 'https://api.youraitools.co/credits'); assert.equal(idx.tools[18].baseUrl, 'https://api.youraitools.co/perpcheck'); assert.equal(idx.tools[17].baseUrl, 'https://api.youraitools.co/toolpick'); assert.equal(idx.tools[16].baseUrl, 'https://api.youraitools.co/smartperps'); assert.equal(idx.tools[12].baseUrl, 'https://api.youraitools.co/agentready'); assert.equal(idx.tools[13].baseUrl, 'https://api.youraitools.co/perpradar'); assert.equal(idx.tools[14].baseUrl, 'https://api.youraitools.co/tasignal'); assert.equal(idx.tools[15].baseUrl, 'https://api.youraitools.co/leakguard'); assert.equal(idx.tools[11].baseUrl, 'https://api.youraitools.co/solverdict'); assert.equal(idx.tools[10].baseUrl, 'https://api.youraitools.co/x402fix'); assert.equal(idx.tools[9].baseUrl, 'https://api.youraitools.co/counterpartycheck'); assert.equal(idx.credits.buy[0], 'https://api.youraitools.co/credits/v1/pack/1'); assert.equal(idx.tools[19].baseUrl, 'https://api.youraitools.co/x402-template'); assert.equal(idx.tools[19].mcp, undefined); assert.equal(idx.tools[8].baseUrl, 'https://api.youraitools.co/signcheck'); assert.equal(idx.tools[7].baseUrl, 'https://api.youraitools.co/tokenverdict'); assert.equal(idx.tools[6].baseUrl, 'https://api.youraitools.co/stackdetect'); assert.equal(idx.tools[5].baseUrl, 'https://api.youraitools.co/pkgguard'); assert.equal(idx.tools[3].baseUrl, 'https://api.youraitools.co/domaincheck'); assert.equal(idx.tools[4].baseUrl, 'https://api.youraitools.co/payguard');
assert.match(await (await call('/', { headers: { accept: 'text/html' } }, env)).text(), /YourAITools API/);
const wk = await (await call('/.well-known/x402', {}, env)).json();
assert.deepEqual(wk.resources, ['agentgate', 'webread', 'injectscan', 'domaincheck', 'payguard', 'pkgguard', 'stackdetect', 'tokenverdict', 'signcheck', 'counterpartycheck', 'x402fix', 'solverdict', 'agentready', 'perpradar', 'tasignal', 'leakguard', 'smartperps', 'toolpick', 'perpcheck', 'x402-template', 'credits', 'listing'].map((p) => `https://api.youraitools.co/${p}/v1/x`));
assert.equal(wk.tools.length, 21); assert.deepEqual(wk.services.map((x) => x.baseUrl), ['https://api.youraitools.co/listing']);
const oa = await (await call('/openapi.json', {}, env)).json();
assert.equal(oa.servers[0].url, 'https://api.youraitools.co');
assert.deepEqual(Object.keys(oa.paths), ['/agentgate/v1/x', '/webread/v1/x', '/injectscan/v1/x', '/domaincheck/v1/x', '/payguard/v1/x', '/pkgguard/v1/x', '/stackdetect/v1/x', '/tokenverdict/v1/x', '/signcheck/v1/x', '/counterpartycheck/v1/x', '/x402fix/v1/x', '/solverdict/v1/x', '/agentready/v1/x', '/perpradar/v1/x', '/tasignal/v1/x', '/leakguard/v1/x', '/smartperps/v1/x', '/toolpick/v1/x', '/perpcheck/v1/x', '/x402-template/v1/x', '/credits/v1/x', '/listing/v1/x']);
});
test('listing is a proxied service, not a tool: routed, listed under services, not counted in tools', async () => {
const log = []; const env = fakeEnv(log);
const r = await call('/listing/v1/fast-track', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{"name":"x"}' }, env);
assert.equal(r.status, 402);
assert.equal(log[0].name, 'listing'); assert.equal(log[0].url, 'https://listing.agentgate.workers.dev/v1/fast-track'); assert.equal(log[0].headers['x-forwarded-prefix'], '/listing'); assert.equal(log[0].body, '{"name":"x"}');
const idx = await (await call('/', {}, env)).json();
assert.equal(idx.tools.length, 21); assert.ok(!idx.tools.some((t) => /listing/i.test(t.baseUrl)));
assert.equal(idx.services.length, 1); assert.equal(idx.services[0].kind, 'service'); assert.equal(idx.services[0].paid[0].url, 'https://api.youraitools.co/listing/v1/fast-track');
const h = await (await call('/health', {}, env)).json();
assert.ok(!h.tools.includes('listing')); assert.deepEqual(h.services, ['listing']);
assert.match(await (await call('/llms.txt', {}, env)).text(), /## Services \(not tools\)\n- YourAITools Listings/);
assert.match(await (await call('/', { headers: { accept: 'text/html' } }, env)).text(), /a service, not a tool/);
});
agentgate-source: code mirror on Cloudflare Pages. All repos