// Run by the optional Go browser regression. Every API response is synthetic; // HTML, CSP and static assets come from the real embedded Go server. const path = require("node:path"), http = require("node:http"); const { pathToFileURL } = require("node:url"); const { chromium } = require(process.env.PLAYWRIGHT_MODULE || "playwright"); (async () => { const { fixture } = await import( pathToFileURL( path.join(__dirname, "../src/utils/__tests__/fixtures/dicom.ts") ) ); const item = (i) => ({ name: `scan_${i}.dcm`, path: `/series/scan_${i}.dcm`, extension: ".dcm", type: "blob", isDir: false, size: 500, modified: "2026-10-22T00:00:00Z", mode: 420, }); const listing = { name: "series", path: "/series/", isDir: true, type: "dir", items: [item(0), item(1), item(2)], sorting: { by: "name", asc: true }, numFiles: 3, numDirs: 0, }; const perm = { admin: true, create: true, rename: true, modify: true, delete: true, share: true, download: true, }; const user = { id: 1, username: "smoke", locale: "en", perm, scope: "/", viewMode: "list", sorting: { by: "name", asc: true }, rules: [], commands: [], theme: "dark", showPreview: true, }; const server = http.createServer((req, res) => { const u = new URL(req.url, "http://localhost"); const json = (data) => { res.setHeader("Content-Type", "application/json"); res.end(JSON.stringify(data)); }; if (u.pathname === "/api/session") return json(user); if (u.pathname === "/api/users/1" && req.method === "PUT") { let body = ""; req.on("data", (chunk) => { body += chunk; }); req.on("end", () => { const update = JSON.parse(body); if (!update.which.includes("dicom3D")) { res.writeHead(400); res.end(); return; } Object.assign(user, update.data); json({}); }); return; } if (u.pathname === "/api/addons") return json([]); if (u.pathname.startsWith("/api/resources")) return json( /scan_(\d+)\.dcm$/.test(u.pathname) ? item(Number(u.pathname.match(/scan_(\d+)/)[1])) : listing ); if (u.pathname.startsWith("/api/usage")) return json({ used: 0, total: 1000 }); if (u.pathname.startsWith("/api/raw")) { res.setHeader("Content-Type", "application/dicom"); return res.end( Buffer.from( fixture(16, { slice: Number(u.pathname.match(/scan_(\d+)/)?.[1] || 0), }) ) ); } res.writeHead(404); res.end(); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); const browser = await chromium.launch({ executablePath: process.env.BROWSER_EXECUTABLE, headless: true, args: ["--use-angle=swiftshader", "--enable-unsafe-swiftshader"], }); try { const page = await browser.newPage(), errors = []; page.on("pageerror", (e) => errors.push(String(e))); page.on("response", (r) => { if (r.status() >= 400 && new URL(r.url()).pathname.startsWith("/static/")) errors.push(`HTTP ${r.status()}: ${r.url()}`); }); const base = process.env.FILEBROWSER_PREVIEW_URL; if (!base) throw new Error("FILEBROWSER_PREVIEW_URL is required"); await page.route(base + "/api/**", async (route) => { const response = await route.fetch({ url: "http://127.0.0.1:" + server.address().port + new URL(route.request().url()).pathname, }); await route.fulfill({ response }); }); await page.goto(base + "/settings/profile"); if (await page.getByLabel("3D preview quality").count()) throw new Error("Disabled 3D settings are still visible"); await page.goto(base + "/files/series/scan_1.dcm"); await page.waitForFunction( () => document.querySelector(".dicom-viewer .document-stage canvas") ?.width === 2, null, { timeout: 20000 } ); if ( (await page.locator(".dicom-volume-stage").count()) || (await page.getByRole("button", { name: /Open as 3D/ }).count()) ) throw new Error("3D must remain disabled"); if (errors.length) throw new Error(errors.join("\n")); console.log("DICOM preview: 2D passed; 3D is disabled"); } finally { await browser.close(); server.close(); } })().catch((e) => { console.error(e); process.exitCode = 1; });