bimr-web/tests/offline-viewer.spec.ts
2026-08-31 10:49:15 +02:00

52 lines
1.7 KiB
TypeScript

import { test, expect } from "@playwright/test";
function spyViewer() {
return `
Object.defineProperty(window, "__bimrViewer", {
configurable: true,
set(v) {
const record = [];
(window).__addedModels = record;
v.addModel = (name, m) => { record.push({ name, m }); };
this._v = v;
},
get() { return this._v; },
});
`;
}
// The viewer is bundled with the app (three resolved at build time) —
// blocking the CDN must not affect it. Regression: the viewer used to load
// three from jsDelivr via the import map, silently dying offline.
test("viewer works with the CDN blocked", async ({ page }) => {
await page.route("https://cdn.jsdelivr.net/**", (route) => route.abort());
await page.addInitScript(spyViewer());
await page.goto("/");
// The editor builds after the default sample fetch resolves (top-level
// await in main.ts) — dispatching before that races the open-file
// listener registration.
await page.waitForFunction(
() => (document.querySelector(".cm-content")?.textContent ?? "").length > 0,
undefined,
{ timeout: 15000 },
);
await page.evaluate((text) => {
window.dispatchEvent(new CustomEvent("bimr-open-file", {
detail: { text, name: "test.py" },
}));
}, "p1 = Point(0,0,0)\np2 = Point(5000,0,0)\nl1 = Line(p1,p2)\nw1 = Wall(l1,200,3000)");
await expect
.poll(
() =>
page.evaluate(() => {
const live = ((window as any).__addedModels ?? []).filter(
(a: { name: string }) => a.name === "live",
);
return live.length ? (live[live.length - 1].m?.data?.[0]?.path ?? null) : null;
}),
{ timeout: 20000 },
)
.toBe("wall-001");
});