const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const html = fs.readFileSync(path.join(__dirname, '..', 'index.html'), 'utf8'); assert.match(html, /id="downloadPdf"[^>]*>PDF herunterladen<\/button>/, 'PDF-Download-Button fehlt'); const start = html.indexOf('/* PDF-DOWNLOAD */'); const end = html.indexOf('/* PDF = Druckdialog', start); assert.ok(start >= 0 && end > start, 'PDF-Download-Implementierung fehlt'); const sandbox = { Uint8Array, TextEncoder, Blob }; vm.createContext(sandbox); vm.runInContext(`${html.slice(start, end)}\nglobalThis.api={pdfFilename,buildPdfBytes};`, sandbox); const { pdfFilename, buildPdfBytes } = sandbox.api; assert.equal( pdfFilename('Napoletana', 4, 250, '24 Stunden'), 'Pizzateig-Napoletana-4x250g-24-Stunden.pdf' ); assert.equal( pdfFilename('Frei/Test', 2, 199.6, 'Same:Day'), 'Pizzateig-Frei-Test-2x200g-Same-Day.pdf' ); assert.equal( pdfFilename('Römisch tonda', 4, 200, 'Über Nacht'), 'Pizzateig-Romisch-tonda-4x200g-Uber-Nacht.pdf' ); const bytes = buildPdfBytes([ 'PIZZATEIG — Napoletana', '4 Teiglinge à 250 g · 1.000 g Teig', 'Wasser 620 g · Salz 28 g · 20 °C', ]); assert.ok(bytes instanceof Uint8Array); const ascii = Buffer.from(bytes).toString('latin1'); assert.ok(ascii.startsWith('%PDF-1.4')); assert.match(ascii, /\/Type \/Catalog/); assert.match(ascii, /\/Type \/Page\b/); assert.match(ascii, /\/Encoding \/WinAnsiEncoding/); assert.match(ascii, /xref\n0 \d+/); assert.match(ascii, /%%EOF\n$/); fs.writeFileSync('/tmp/teig-terminal-download-test.pdf', Buffer.from(bytes)); console.log('pdf download tests: OK');