import { createApp } from '../src/app.js';
import { Stats } from '../src/stats.js';
import { buildIndex } from '../src/engine.js';
import { makeStore, memoryStub } from '../src/store.js';
import { mockNet, CDP_ITEMS, PAYAI_ITEMS } from './mock.js';
export const PAY_TO = '0x3c1A1E01e790Cd326495631B3b38Bd7068F08d6F';
export const NETWORK = 'eip155:8453';
export const BASE = 'https://toolpick.example.com';
export const AYB = 'web search'; // need used by the shared publicbase / cdp tests
export { mockNet, CDP_ITEMS, PAYAI_ITEMS };
export function fakeNet() { const calls = []; return { calls, fetchImpl: async (u) => { calls.push(String(u)); return new Response('no network in tests', { status: 599 }); } }; }
/** A store pre-filled with an index built from the mock (optionally older). */
export async function seededStore({ net = mockNet(), ageMs = 0 } = {}) {
const index = await buildIndex({ fetchImpl: net.fetchImpl, now: Date.now() - ageMs });
const stub = memoryStub(index);
return { stub, store: makeStore(stub), index };
}
export function fakeFacilitator() {
const f = { verified: [], settled: [],
async getSupported() { return { kinds: [{ x402Version: 2, scheme: 'exact', network: NETWORK }], extensions: ['bazaar'], signers: {} }; },
async verify(payload, req) { f.verified.push(req); const a = payload?.payload?.authorization; return a && a.to.toLowerCase() === req.payTo.toLowerCase() && String(a.value) === String(req.amount) ? { isValid: true, payer: a.from } : { isValid: false, invalidReason: 'fake_invalid' }; },
async settle(payload, req) { f.settled.push(req); return { success: true, transaction: '0x' + 'ab'.repeat(32), network: req.network, payer: payload.payload.authorization.from }; },
};
return f;
}
export function memKv() { const m = new Map(); return { async get(k) { return m.has(k) ? JSON.parse(m.get(k)) : null; }, async put(k, v) { m.set(k, v); } }; }
export async function makeApp({ limiter, v1FetchImpl, net = mockNet(), appOpts = {}, store, seed = true } = {}) {
const facilitator = fakeFacilitator(); const stats = new Stats(memKv(), { flushMs: 0 });
const st = store || (seed ? (await seededStore({ net })).store : makeStore(memoryStub(null)));
const app = createApp({ facilitatorClient: facilitator, payTo: PAY_TO, network: NETWORK, publicUrl: BASE, stats, freeLimiter: limiter, v1FetchImpl, dataFetch: net.fetchImpl, store: st, ...appOpts });
const fetchApp = (input, init) => app.fetch(input instanceof Request ? input : new Request(input, init));
return { app, fetchApp, facilitator, stats, net, store: st };
}
// compatibility with the shared cdp/publicbase tests
export const mockVenues = mockNet;
agentgate-source: code mirror on Cloudflare Pages. All repos