146 lines
4.3 KiB
JavaScript
146 lines
4.3 KiB
JavaScript
// 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/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 + "/files/series/scan_1.dcm");
|
|
await page.waitForFunction(
|
|
() =>
|
|
document.querySelector(".dicom-viewer .document-stage canvas")
|
|
?.width === 2,
|
|
null,
|
|
{ timeout: 20000 }
|
|
);
|
|
await page
|
|
.getByRole("button", { name: "Open as 3D (3)", exact: true })
|
|
.click();
|
|
await page
|
|
.locator(".dicom-volume-stage canvas")
|
|
.waitFor({ timeout: 20000 });
|
|
for (const label of ["Axial", "Sagittal", "Coronal", "3D"]) {
|
|
await page.getByRole("button", { name: label, exact: true }).click();
|
|
await page.waitForTimeout(150);
|
|
}
|
|
await page.getByRole("button", { name: "Back to 2D", exact: true }).click();
|
|
await page.waitForFunction(
|
|
() =>
|
|
!document.querySelector(".dicom-volume-stage") &&
|
|
document.querySelector(".dicom-viewer .document-stage canvas")
|
|
?.width === 2
|
|
);
|
|
if (errors.length) throw new Error(errors.join("\n"));
|
|
console.log(
|
|
"DICOM preview in embedded server: 2D, series offer, 3D, MPR and return to 2D passed"
|
|
);
|
|
} finally {
|
|
await browser.close();
|
|
server.close();
|
|
}
|
|
})().catch((e) => {
|
|
console.error(e);
|
|
process.exitCode = 1;
|
|
});
|