import test from 'node:test' import assert from 'node:assert/strict' import { loadModule } from './load.mjs' const { PDF_FREIGABE_NACH_MS, alsPdfBlob, oeffnePdfImTab } = await loadModule('src/lib/auftragPdf.js') // Fester Ablauf: Tab zuerst (der Browser blockiert sonst das Pop-up), dann Blob holen, Objekt-URL erzeugen, später freigeben function umgebung() { const ablauf = [] const tab = { location: {}, opener: 'woms', close: () => ablauf.push('schliessen') } const win = { open: (...args) => { ablauf.push('oeffnen'); win.args = args; return tab } } const urlApi = { createObjectURL: () => { ablauf.push('url'); return 'blob:https://woms.test/abc' }, revokeObjectURL: (url) => ablauf.push(`frei:${url}`), } const timer = (fn, ms) => { ablauf.push(`timer:${ms}`); timer.fn = fn } return { ablauf, tab, win, urlApi, timer } } test('alsPdfBlob: setzt den Typ application/pdf, ein passender Blob bleibt unverändert', async () => { const roh = new Blob(['%PDF-1.4'], { type: 'application/octet-stream' }) const pdf = alsPdfBlob(roh) assert.equal(pdf.type, 'application/pdf') assert.equal(await pdf.text(), '%PDF-1.4') const schon = new Blob(['x'], { type: 'application/pdf' }) assert.equal(alsPdfBlob(schon), schon) }) test('oeffnePdfImTab: Tab zuerst, dann Blob, Objekt-URL im Tab, später freigeben', async () => { const u = umgebung() await oeffnePdfImTab(async () => { u.ablauf.push('holen'); return new Blob(['%PDF'], { type: 'application/pdf' }) }, u) assert.equal(PDF_FREIGABE_NACH_MS, 300000) assert.deepEqual(u.ablauf, ['oeffnen', 'holen', 'url', 'timer:300000']) assert.equal(u.tab.location.href, 'blob:https://woms.test/abc') assert.equal(u.tab.opener, null) u.timer.fn() assert.equal(u.ablauf.at(-1), 'frei:blob:https://woms.test/abc') }) test('oeffnePdfImTab: Fehler beim Holen schließt den Tab, es entsteht keine URL', async () => { const u = umgebung() await assert.rejects(() => oeffnePdfImTab(async () => { u.ablauf.push('holen'); throw new Error('PDF nicht gefunden.') }, u), /PDF nicht gefunden/) assert.deepEqual(u.ablauf, ['oeffnen', 'holen', 'schliessen']) }) test('oeffnePdfImTab: blockiertes Pop-up, der Blob wird gar nicht geholt', async () => { let geholt = false const win = { open: () => null } await assert.rejects(() => oeffnePdfImTab(async () => { geholt = true; return new Blob() }, { win }), { message: 'Neuer Tab wurde vom Browser blockiert.' }) assert.equal(geholt, false) })