Files
RuneStone/browser-runtime/scripts/test-engine.mjs
T
2026-11-16 04:21:21 -07:00

60 lines
4.0 KiB
JavaScript

import { spawn } from 'node:child_process';
import { mkdtemp, readFile, cp } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path';
import electron from 'electron';
const directory = await mkdtemp(path.join(os.tmpdir(), 'runestone-plugin-browser-'));
// A two-frame GIF that the server gives only one time, as some image hosts do.
const onceGif = Buffer.from('R0lGODlhAQABAIAAAAAAAP///yH/C05FVFNDQVBFMi4wAwEAAAAh+QQACgAAACwAAAAAAQABAAACAkQBACH5BAAKAAAALAAAAAABAAEAAAICTAEAOw==', 'base64');
let onceGifServed = false;
// The same GIF with 7 MB of comment blocks, so that Copy image needs more than one part.
const comment = Buffer.concat([Buffer.from([255]), Buffer.alloc(255, 0x61)]);
const largeGif = Buffer.concat([onceGif.subarray(0, 32), Buffer.from([0x21, 0xfe]), ...Array(28_000).fill(comment), Buffer.from([0]), onceGif.subarray(32)]);
const server = http.createServer((req, res) => {
if (req.url === '/direct.gif') {
res.writeHead(200, { 'Content-Type': 'image/gif' });
return res.end(largeGif);
}
if (req.url === '/large.gif') {
res.writeHead(200, { 'Content-Type': 'image/gif' });
return res.end(largeGif);
}
if (req.url === '/once.gif') {
if (onceGifServed) { res.writeHead(200, { 'Content-Type': 'text/html' }); return res.end('Invalid request'); }
onceGifServed = true;
res.writeHead(200, { 'Content-Type': 'image/gif' });
return res.end(onceGif);
}
if (req.url === '/picture.png') {
res.writeHead(200, { 'Content-Type': 'image/png' });
return res.end(Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aD1sAAAAASUVORK5CYII=', 'base64'));
}
if (req.url === '/slow') return;
if (req.url === '/icon.svg') {
res.writeHead(200, { 'Content-Type': 'image/svg+xml' });
return res.end('<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><rect width="16" height="16" fill="red"/></svg>');
}
if (req.url === '/favicon.ico') { res.writeHead(404); return res.end(); }
if (req.url === '/no-icon') return res.end('<!doctype html><title>No icon</title>');
if (req.url === '/with-icon') return res.end('<!doctype html><title>Icon fixture</title><link rel="icon" href="/icon.svg">');
res.writeHead(200, { 'Content-Type': req.url.endsWith('.js') ? 'text/javascript' : 'text/html', 'Cache-Control': 'no-store' });
res.end(req.url.endsWith('.js') ? 'window.adExecuted=true;' : '<!doctype html><title>Browser fixture</title><input style="position:absolute;left:20px;top:20px;width:200px;height:30px"><p class="advertisement">Ad</p><p style="margin-top:80px">Runestone needle. Runestone needle. Runestone needle.</p><script src="/blocked-ad.js"></script>');
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
async function phase(name, plan) {
const child = spawn(electron, ['test/engine-smoke.mjs'], { stdio: 'inherit', windowsHide: true,
env: { ...process.env, RS_TEST_ROOT: directory, RS_TEST_URL: `http://127.0.0.1:${server.address().port}/`, RS_TEST_PHASE: name, RS_TEST_PLAN: JSON.stringify(plan || {}),
...(process.argv.includes('--packaged') ? { RUNESTONE_TEST_PACKAGE: path.resolve(`dist/RuneStone-Browser-${process.platform}-${process.arch}/resources/app.asar`) } : {}) } });
const timer = setTimeout(() => child.kill(), 60_000);
const code = await new Promise((resolve, reject) => { child.once('exit', resolve); child.once('error', reject); }); clearTimeout(timer);
const result = JSON.parse(await readFile(`output/engine-${name}.json`, 'utf8'));
if (code !== 0 || result.exitCode !== 0) throw new Error(`Engine ${name} checks failed.`);
}
try {
await phase('initial');
const plan = JSON.parse(await readFile(path.join(directory, 'clone-plan.json'), 'utf8'));
await cp(path.join(directory, 'Partitions', `runestone-${plan.sourceId}`), path.join(directory, 'Partitions', `runestone-${plan.id}`), { recursive: true });
await phase('restore', plan);
} finally { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); }