// Generated, do not edit. sha256:4bf1820de70357b7a7e29ccee6fc599f970ee70dfc5b2f00ce791c38d33bc92e import { describe, it, mock, afterEach } from 'node:test'; import assert from 'node:assert/strict'; const { fetchJson } = await import('../../client/price/btc-explorer.js'); const echteFetch = globalThis.fetch; afterEach(() => { globalThis.fetch = echteFetch; mock.timers.reset(); }); async function uitzitten(belofte) { let klaar = false; const uit = belofte.then(v => ({ v }), e => ({ e })).finally(() => { klaar = true; }); while (!klaar) { await new Promise(r => setImmediate(r)); mock.timers.runAll(); } return uit; } const ok = body => ({ ok: true, status: 200, json: async () => body }); const status = s => ({ ok: false, status: s, json: async () => ({}) }); describe('fetchJson: opnieuw proberen', () => { it('zit drie netwerkfouten uit en geeft dan het antwoord', async () => { mock.timers.enable({ apis: ['setTimeout'] }); let n = 0; globalThis.fetch = async () => { if (++n <= 3) throw new TypeError('Failed to fetch'); return ok({ x: 1 }); }; const { v, e } = await uitzitten(fetchJson('https://x/api', 'X')); assert.equal(e, undefined); assert.deepEqual(v, { x: 1 }); assert.equal(n, 4); }); it('geeft na de vierde mislukte poging de fout van de laatste', async () => { mock.timers.enable({ apis: ['setTimeout'] }); let n = 0; globalThis.fetch = async () => { n++; return status(429); }; const { e } = await uitzitten(fetchJson('https://x/api', 'X')); assert.ok(e); assert.equal(n, 4); }); it('probeert een 404 niet opnieuw', async () => { mock.timers.enable({ apis: ['setTimeout'] }); let n = 0; globalThis.fetch = async () => { n++; return status(404); }; const { e } = await uitzitten(fetchJson('https://x/api', 'X')); assert.ok(e); assert.equal(n, 1); }); });