import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises"; import { spawnSync } from "node:child_process"; import http from "node:http"; import path from "node:path"; import { chromium } from "playwright"; const base = process.env.BASE_URL || "http://127.0.0.1:4173"; await mkdir("test-results", { recursive: true }); const root = await mkdtemp(path.resolve("test-results/export-consumer-")); const zip = path.join(root, "snapshot.zip"); const browser = await chromium.launch({ executablePath: process.env.PLAYWRIGHT_EXECUTABLE_PATH || "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe", headless: true, }); let server, passed = 0; try { const page = await browser.newPage({ acceptDownloads: true }); await page.goto(base + "/?page=tokens&theme=dark"); const download = page.waitForEvent("download"); await page.locator('[data-action="download-system"]').click(); await (await download).saveAs(zip); const result = spawnSync( "python", [ "-c", `import sys,zipfile,pathlib root=pathlib.Path(sys.argv[2]).resolve() with zipfile.ZipFile(sys.argv[1]) as archive: for name in archive.namelist(): target=(root/name).resolve() if not target.is_relative_to(root): raise ValueError(name) archive.extractall(root) `, zip, root, ], { encoding: "utf8", windowsHide: true }, ); assert.equal(result.status, 0, result.stderr); const mime = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".json": "application/json", ".svg": "image/svg+xml", }; server = http.createServer(async (req, res) => { try { const url = new URL(req.url, "http://localhost"), name = decodeURIComponent(url.pathname); const file = path.resolve( root, "." + (name.endsWith("/") ? name + "index.html" : name), ); if (!file.startsWith(root + path.sep)) throw Error(); const body = await readFile(file); res.writeHead(200, { "Content-Type": mime[path.extname(file)] || "application/octet-stream", "Cache-Control": "no-store", }); res.end(body); } catch { res.writeHead(404); res.end(); } }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); const origin = `http://127.0.0.1:${server.address().port}`, errors = [], failures = [], external = []; page.on("pageerror", (e) => errors.push(e.message)); page.on("response", (r) => { if (r.status() >= 400 && !r.url().endsWith("/favicon.ico")) failures.push(`${r.status()} ${r.url()}`); }); page.on("request", (r) => { if (!r.url().startsWith(origin + "/") && !r.url().startsWith("blob:")) external.push(r.url()); }); for (const route of [ "/?page=statistics&panel=admin", "/?page=documents&path=/server-guide.md", "/icons/", ]) { await page.goto(origin + route); await page .locator( route.includes("statistics") ? ".statistics-panel .chart-plot" : route.includes("documents") ? ".markdown-body h1" : ".icon-tile", ) .first() .waitFor(); passed++; } assert.equal(await page.locator(".icon-tile").count(), 76); assert.deepEqual(errors, []); assert.deepEqual(failures, []); assert.deepEqual(external, []); await writeFile( "test-results/export-qa.json", JSON.stringify({ passed, errors, failures, external }, null, 2), ); console.log( `Gallery export: ${passed} isolated ZIP routes passed; no missing or external resources`, ); } finally { await browser.close(); if (server) await new Promise((resolve) => server.close(resolve)); }