x402-pkgguard / tree / test/osv-retry.test.js ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import test from 'node:test';
import assert from 'node:assert/strict';
import { queryBatch, UpstreamError } from '../src/osv.js';
const PKGS = [{ ecosystem: 'npm', name: 'lodash', version: '4.17.15' }];
const okRes = () => new Response(JSON.stringify({ results: [{ vulns: [{ id: 'GHSA-x' }] }] }), { headers: { 'content-type': 'application/json' } });

test('OSV querybatch: one retry on 5xx / network error, then success', async () => {
  for (const first of [() => new Response('busy', { status: 503 }), () => { throw new Error('connection reset'); }]) {
    let n = 0;
    const r = await queryBatch(PKGS, { fetchImpl: async () => (n++ === 0 ? first() : okRes()) });
    assert.equal(n, 2); assert.deepEqual(r[0].ids, ['GHSA-x']);
  }
});
test('OSV querybatch: no retry on 400; two failures surface as UpstreamError', async () => {
  let n = 0;
  await assert.rejects(queryBatch(PKGS, { fetchImpl: async () => { n++; return new Response('bad', { status: 400 }); } }), UpstreamError); assert.equal(n, 1);
  n = 0;
  await assert.rejects(queryBatch(PKGS, { fetchImpl: async () => { n++; return new Response('down', { status: 502 }); } }), UpstreamError); assert.equal(n, 2);
});

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