Files
Pier_v1/scripts/verify-template-download.cjs
T

44 lines
2.0 KiB
JavaScript

// Clicks the template download button in the import dialog through Chrome DevTools
// and reports the saved file. Run with Windows Node (same machine as Chrome):
// node verify-template-download.cjs "C:\\Users\\...\\Temp\\verify-downloads"
const fs = require('fs');
const path = require('path');
const DL_DIR = process.argv[2];
const CDP = 'http://127.0.0.1:9444';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function main() {
fs.mkdirSync(DL_DIR, { recursive: true });
const page = (await (await fetch(`${CDP}/json`)).json()).find((t) => t.type === 'page');
const ws = new WebSocket(page.webSocketDebuggerUrl);
await new Promise((res, rej) => { ws.onopen = res; ws.onerror = rej; });
let seq = 0;
const waiting = new Map();
ws.onmessage = (ev) => {
const m = JSON.parse(ev.data);
if (m.id && waiting.has(m.id)) { waiting.get(m.id)(m); waiting.delete(m.id); }
};
const send = (method, params = {}) => new Promise((res) => {
const id = ++seq; waiting.set(id, res); ws.send(JSON.stringify({ id, method, params }));
});
const evalJs = async (expression) => {
const r = await send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true });
if (r.result.exceptionDetails) throw new Error(JSON.stringify(r.result.exceptionDetails).slice(0, 300));
return r.result.result.value;
};
await send('Page.enable');
await send('Runtime.enable');
await send('Browser.setDownloadBehavior', { behavior: 'allow', downloadPath: DL_DIR, eventsEnabled: false });
await send('Page.navigate', { url: 'http://127.0.0.1:5173/' });
await sleep(4000);
await evalJs(`document.getElementById('open-import').click()`);
await sleep(500);
await evalJs(`document.getElementById('download-template').click()`);
await sleep(4000);
const files = fs.readdirSync(DL_DIR);
console.log('DOWNLOADED', JSON.stringify(files));
for (const f of files) console.log('SIZE', f, fs.statSync(path.join(DL_DIR, f)).size);
ws.close();
}
main().catch((e) => { console.error('FAILED', e); process.exit(1); });