agentcomply / tree / test/helpers.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import { importSigner } from '../src/receipt.js';
import { MemoryStore } from '../src/store.js';
import fs from 'node:fs';
export const policy = JSON.parse(fs.readFileSync(new URL('../policy.json', import.meta.url)));
let signer;
export async function testSigner() {
  if (signer) return signer;
  const kp = await crypto.subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']);
  const jwk = await crypto.subtle.exportKey('jwk', kp.privateKey);
  signer = await importSigner({ ...jwk, kid: 'test' });
  return signer;
}
// routes: { '/counterpartycheck/v1/preview': (url)=>[status, json] | 'throw' | 'hang' }
export function mockFetch(routes, calls = []) {
  return async (u, init = {}) => {
    const url = new URL(u); calls.push({ path: url.pathname, query: Object.fromEntries(url.searchParams), headers: init.headers });
    const h = routes[url.pathname];
    if (!h) return new Response(JSON.stringify({ error: 'no mock' }), { status: 404 });
    const r = typeof h === 'function' ? h(url, calls) : h;
    if (r === 'throw') throw new TypeError('fetch failed');
    if (r === 'hang') return new Promise((_, rej) => init.signal.addEventListener('abort', () => { const e = new Error('aborted'); e.name = 'AbortError'; rej(e); }));
    const [status, body] = r;
    return new Response(typeof body === 'string' ? body : JSON.stringify(body), { status });
  };
}
export const CP_OK = [200, { address: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', chain: 'ethereum', verdict: 'ok', confidence: 'high', kind: 'eoa', signals: [], sources_failed: [], engine: 'counterpartycheck-0.1.1', checked_at: '2026-10-22T17:00:00Z', future_field: { x: 1 } }];
export const PG_CLEAR = [200, { address: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', sanctioned: false, match_count: 0, source: 'OFAC SDN', list_updated_at: '2026-10-21', engine: 'payguard' }];
export const PG_HIT = [200, { address: '0x098b716b8aaf21512996dc57eb0615e2383e2f96', sanctioned: true, match_count: 1, source: 'OFAC SDN', list_updated_at: '2026-10-21' }];
export async function deps(routes, extra = {}) {
  const calls = [];
  return { calls, d: { fetch: mockFetch(routes, calls), signer: await testSigner(), store: new MemoryStore(), policy, retryDelayMs: 5, timeoutMs: 200, now: () => new Date('2026-10-22T17:30:00Z'), ...extra } };
}

agentgate-source: code mirror on Cloudflare Pages. All repos