// Builds the test books in tests/fixtures (real EPUB, real PDFs made by Chrome).
import JSZip from 'jszip';
import { chromium } from '@playwright/test';
import { writeFileSync, readFileSync, mkdirSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
const out = new URL('../tests/fixtures/', import.meta.url).pathname;
mkdirSync(out, { recursive: true });
/* EPUB 3 with nav, cover image and 3 chapters (+ a near-empty title page) */
const zip = new JSZip();
zip.file('mimetype', 'application/epub+zip', { compression: 'STORE' });
zip.file('META-INF/container.xml', ` `);
const chap = (t, body) => `
${t} ${t} ${body}`;
zip.file('OEBPS/text/title.xhtml', chap('', ''));
zip.file('OEBPS/text/ch1.xhtml', chap('The Lighthouse', 'Mara climbed the stairs of the old lighthouse. The wind pushed at the windows.1
She had been told never to come here at night. But tonight, the lamp was lit.
'));
zip.file('OEBPS/text/ch2.xhtml', chap('The Keeper', 'An old man stood by the lamp. “You are late,” he said, without turning around.
Mara froze. Nobody knew she was coming.
'));
zip.file('OEBPS/text/ch 3.xhtml', chap('The Signal', 'Far out at sea, a light answered. Three short flashes, then darkness.
'));
zip.file('OEBPS/nav.xhtml', `1. The Lighthouse 2. The Keeper 3. The Signal `);
const png = execFileSync('magick', ['-size', '60x90', 'gradient:#1e3a8a-#f59e0b', 'png:-']);
zip.file('OEBPS/images/cover.png', png);
zip.file('OEBPS/content.opf', `test-lighthouse The Lighthouse Keeper Ana Test en `);
writeFileSync(out + 'lighthouse.epub', await zip.generateAsync({ type: 'nodebuffer', mimeType: 'application/epub+zip' }));
/* PDFs printed by Chrome: one with headings (outline), one scanned (image only) */
const b = await chromium.launch({ channel: 'chrome' });
const p = await b.newPage();
const para = (n) => Array.from({ length: n }, (_, i) => `This is paragraph ${i + 1} of the section. The river ran quietly past the mill, and every- body in the village knew the miller's secret.
`).join('');
await p.setContent(`River Notes Spring ${para(8)}Summer ${para(8)}Autumn ${para(3)}`);
writeFileSync(out + 'river.pdf', await p.pdf({ format: 'A5', outline: true, tagged: true, displayHeaderFooter: true, footerTemplate: '
', headerTemplate: ' ', margin: { top: '1cm', bottom: '1.5cm', left: '1cm', right: '1cm' } }));
const img = execFileSync('magick', ['-size', '400x300', 'xc:white', '-fill', 'black', '-pointsize', '28', '-annotate', '+20+150', 'Scanned page text', 'png:-']).toString('base64');
await p.setContent(` `);
writeFileSync(out + 'scanned.pdf', await p.pdf({ format: 'A5' }));
await b.close();
writeFileSync(out + 'conto.txt', `Capítulo 1\n\nEla abriu a porta devagar. Não havia ninguém na sala, mas a luz estava acesa.\n\nCapítulo 2\n\nNo dia seguinte, você chegou cedo. Os vizinhos disseram que não ouviram nada.\n`);
writeFileSync(out + 'notes.docx', 'not really a docx');
console.log('fixtures written to', out);