import test from 'node:test' import assert from 'node:assert/strict' import { loadModule } from './load.mjs' const { createFinanceRequest, PORTAL_DOWN_TEXT } = await loadModule('src/lib/financeRequest.js') const BASE = 'https://portal.example.invalid/api/admin/finanzen' /** Antwort wie fetch: { ok, status, json(), blob() }; ohne `json` ist der Körper kein JSON (json() wirft). */ const reply = (status, { json, blob } = {}) => ({ ok: status >= 200 && status < 300, status, json: async () => { if (json === undefined) throw new SyntaxError('Unexpected token % in JSON') return json }, blob: async () => blob, }) function client({ responses = [], createJWT } = {}) { const calls = [] const queue = [...responses] const api = createFinanceRequest({ baseUrl: BASE, createJWT: createJWT || (async () => ({ jwt: 'jwt-1' })), fetchImpl: async (url, init) => { calls.push({ url, init }) return queue.shift() || reply(200, { json: {} }) }, }) return { api, calls } } const fails = async (promise) => promise.then(() => assert.fail('hätte scheitern müssen'), (err) => err) const pdf = () => new Blob(['%PDF-1.7 Probe'], { type: 'application/pdf' }) test('as blob: Erfolg liefert den rohen Inhalt als Blob, mit Bearer-JWT, ohne JSON zu lesen', async () => { const { api, calls } = client({ responses: [{ ...reply(200, { blob: pdf() }), json: async () => assert.fail('json() darf bei as blob nicht gelesen werden') }], }) const result = await api('/auftraege/a1/pdf', { as: 'blob' }) assert.ok(result instanceof Blob) assert.equal(result.type, 'application/pdf') assert.equal(await result.text(), '%PDF-1.7 Probe') assert.equal(calls[0].url, `${BASE}/auftraege/a1/pdf`) assert.equal(calls[0].init.method, 'GET') assert.equal(calls[0].init.headers.Authorization, 'Bearer jwt-1') }) test('as blob: Fehler kommen weiter als Meldung des Portals mit Status (kein kaputtes PDF)', async () => { const { api } = client({ responses: [reply(404, { json: { error: 'PDF nicht gefunden.' } })] }) const err = await fails(api('/auftraege/a1/pdf', { as: 'blob' })) assert.equal(err.message, 'PDF nicht gefunden.') assert.equal(err.status, 404) // Proxy-Fehlerseite ohne JSON: dieselbe Meldung wie bei JSON-Anfragen const { api: api2 } = client({ responses: [reply(502)] }) assert.equal((await fails(api2('/auftraege/a1/pdf', { as: 'blob' }))).message, PORTAL_DOWN_TEXT) }) test('as blob: bei 401 einmal mit frischem JWT wiederholt, dann kommt der Blob', async () => { let n = 0 const datei = pdf() const { api, calls } = client({ createJWT: async () => ({ jwt: `jwt-${++n}` }), responses: [reply(401, { json: { error: 'abgelaufen' } }), reply(200, { blob: datei })], }) assert.equal(await api('/auftraege/a1/pdf', { as: 'blob' }), datei) assert.deepEqual(calls.map((c) => c.init.headers.Authorization), ['Bearer jwt-1', 'Bearer jwt-2']) }) test('ohne as bleibt es JSON (der Blob wird nicht gelesen), ein unbekannter Wert verhält sich ebenso', async () => { const { api } = client({ responses: [{ ...reply(200, { json: { ok: 1 } }), blob: async () => assert.fail('blob() darf ohne as nicht gelesen werden') }], }) assert.deepEqual(await api('/x'), { ok: 1 }) const { api: api2 } = client({ responses: [reply(200, { json: { ok: 2 } })] }) assert.deepEqual(await api2('/x', { as: 'text' }), { ok: 2 }) })