earshelf / source /scripts /phone-files.mjs
WilburDev's picture
Deploy 2026-11-19T12:25Z
4a58e48 verified
Raw History Blame Contribute Delete
2.18 kB
// Real phone: fresh site data, add the EPUB and PDF fixtures through the file input, check parsing + offline cache.
import { chromium } from '@playwright/test';
import { readFileSync } from 'node:fs';
const out = process.argv[2];
const b = await chromium.connectOverCDP('http://localhost:9333');
const ctx = b.contexts()[0];
const p = ctx.pages().find((x) => x.url().startsWith('http://localhost:4175'));
const errors = [];
p.on('pageerror', (e) => errors.push(e.message));
await (await ctx.newCDPSession(p)).send('Storage.clearDataForOrigin', { origin: 'http://localhost:4175', storageTypes: 'all' });
await p.reload();
await p.waitForTimeout(2000);
await p.evaluate(() => localStorage.setItem('earshelf.noWarm', '1'));
const add = async (name, type) => {
const b64 = readFileSync(new URL(`../tests/fixtures/${name}`, import.meta.url)).toString('base64');
await p.evaluate(({ b64, name, type }) => {
const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
const dt = new DataTransfer();
dt.items.add(new File([bytes], name, { type }));
const input = document.querySelector('#file');
input.files = dt.files;
input.dispatchEvent(new Event('change', { bubbles: true }));
}, { b64, name, type });
await p.waitForSelector('#player:not([hidden])', { timeout: 30000 });
await p.waitForTimeout(800);
const r = { title: await p.locator('#p-title').innerText(), chapter: await p.locator('#p-chapter').innerText(), first: (await p.locator('#reader').innerText()).slice(0, 80) };
await p.locator('#back').click();
await p.waitForTimeout(500);
return r;
};
const t0 = Date.now();
console.log('epub', JSON.stringify(await add('lighthouse.epub', 'application/epub+zip')));
console.log('pdf ', JSON.stringify(await add('river.pdf', 'application/pdf')), 'ms', Date.now() - t0);
const keys = await p.evaluate(async () => { const n = (await caches.keys()).filter((k) => k.startsWith('earshelf-')); return n.length ? (await (await caches.open(n[0])).keys()).length : 0; });
console.log('offline cache entries', keys, 'page errors', JSON.stringify(errors));
await p.screenshot({ path: `${out}/real-5-shelf.png` });
await b.close().catch(() => {});