earshelf / source /scripts /make-fixtures.mjs
WilburDev's picture
Deploy 2026-11-19T12:25Z
4a58e48 verified
Raw History Blame Contribute Delete
4.94 kB
// 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', `<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles><rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/></rootfiles></container>`);
const chap = (t, body) => `<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"><head><title>${t}</title><style>p{margin:0}</style></head><body><h1>${t}</h1>${body}</body></html>`;
zip.file('OEBPS/text/title.xhtml', chap('', ''));
zip.file('OEBPS/text/ch1.xhtml', chap('The Lighthouse', '<p>Mara climbed the stairs of the old lighthouse. The wind pushed at the windows.<sup><a epub:type="noteref" href="#n1">1</a></sup></p><p>She had been told never to come here at night. But tonight, the lamp was lit.</p>'));
zip.file('OEBPS/text/ch2.xhtml', chap('The Keeper', '<p>An old man stood by the lamp. “You are late,” he said, without turning around.</p><p>Mara froze. Nobody knew she was coming.</p>'));
zip.file('OEBPS/text/ch 3.xhtml', chap('The Signal', '<p>Far out at sea, a light answered. Three short flashes, then darkness.</p>'));
zip.file('OEBPS/nav.xhtml', `<?xml version="1.0" encoding="utf-8"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"><body><nav epub:type="toc"><ol><li><a href="text/ch1.xhtml">1. The Lighthouse</a></li><li><a href="text/ch2.xhtml#start">2. The Keeper</a></li><li><a href="text/ch%203.xhtml">3. The Signal</a></li></ol></nav></body></html>`);
const png = execFileSync('magick', ['-size', '60x90', 'gradient:#1e3a8a-#f59e0b', 'png:-']);
zip.file('OEBPS/images/cover.png', png);
zip.file('OEBPS/content.opf', `<?xml version="1.0"?><package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="id"><metadata xmlns:dc="http://purl.org/dc/elements/1.1/"><dc:identifier id="id">test-lighthouse</dc:identifier><dc:title>The Lighthouse Keeper</dc:title><dc:creator>Ana Test</dc:creator><dc:language>en</dc:language></metadata><manifest><item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/><item id="cover" href="images/cover.png" media-type="image/png" properties="cover-image"/><item id="t" href="text/title.xhtml" media-type="application/xhtml+xml"/><item id="c1" href="text/ch1.xhtml" media-type="application/xhtml+xml"/><item id="c2" href="text/ch2.xhtml" media-type="application/xhtml+xml"/><item id="c3" href="text/ch%203.xhtml" media-type="application/xhtml+xml"/></manifest><spine><itemref idref="t"/><itemref idref="c1"/><itemref idref="c2"/><itemref idref="c3"/></spine></package>`);
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) => `<p>This is paragraph ${i + 1} of the section. The river ran quietly past the mill, and every-<br>body in the village knew the miller's secret.</p>`).join('');
await p.setContent(`<html><head><title>River Notes</title><style>h1{page-break-before:always} body{font:14pt serif}</style></head><body><h1>Spring</h1>${para(8)}<h1>Summer</h1>${para(8)}<h1>Autumn</h1>${para(3)}</body></html>`);
writeFileSync(out + 'river.pdf', await p.pdf({ format: 'A5', outline: true, tagged: true, displayHeaderFooter: true, footerTemplate: '<div style="font-size:9px;width:100%;text-align:center"><span class="pageNumber"></span></div>', headerTemplate: '<span></span>', 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(`<img src="data:image/png;base64,${img}">`);
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);