fix: separate hierarchy management from selected building editing

This commit is contained in:
jhartworks committed 2026-11-19 09:46:31 +01:00
1 parent bffc4d0acc
commit 382d6e04f2
18 files changed
+2103 -1648

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
// Regression coverage for the editor workflow: metadata separately, navigation without writes,
// storeys created inside the selected building, native category links and explicit storey moves.
import assert from "node:assert/strict";
import { createServer } from "node:http";
import { existsSync, mkdirSync, readFileSync } from "node:fs";
import { extname, join, relative, resolve } from "node:path";
import puppeteer from "puppeteer-core";
const root = resolve(import.meta.dirname, ".."), out = join(root, "preview/screenshots");
mkdirSync(out, { recursive: true });
const server = createServer((req, res) => {
const file = resolve(root, `.${decodeURIComponent(new URL(req.url, "http://localhost").pathname)}`);
if (relative(root, file).startsWith("..") || !existsSync(file)) return res.writeHead(404).end();
res.writeHead(200, { "content-type": ({ ".html": "text/html", ".js": "text/javascript", ".json": "application/json" })[extname(file)] ?? "application/octet-stream" }).end(readFileSync(file));
});
await new Promise((done) => server.listen(0, "127.0.0.1", done));
let browser;
try {
const executablePath = [process.env.CHROME_PATH, "C:/Program Files/Google/Chrome/Application/chrome.exe", "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"].find((p) => p && existsSync(p));
browser = await puppeteer.launch({ executablePath, headless: true, args: ["--use-angle=swiftshader", "--enable-unsafe-swiftshader"] });
const page = await browser.newPage(), errors = [];
page.on("pageerror", (e) => errors.push(String(e)));
await page.setViewport({ width: 1440, height: 1000 });
await page.goto(`http://127.0.0.1:${server.address().port}/preview/index.html?noshowcase`, { waitUntil: "networkidle0" });
await page.waitForFunction(() => window.nfPanel?.data.building?.floors.length);
await page.evaluate(async () => {
const p = window.nfPanel, b = structuredClone(p.data.building), base = b.floors[0];
const room = (id, name, area_id) => ({ id, name, area_id, points: [[0, 0], [6, 0], [6, 5], [0, 5]], floor_material: "wood" });
const floor = (id, building_id, rooms) => ({ ...structuredClone(base), id, name: "EG", building_id, rooms, elevation: 0, openings: [], furniture: [], placements: [], outdoor: [], walls: [], notes: [], background: null, utility_meters: null, power_entity: null, start_view: null });
b.topology = { locations: [{ id: "u", name: "Urbach" }], branches: [{ id: "nl", name: "Zentrale", location_id: "u" }], buildings: [{ id: "a", name: "RAS19", branch_id: "nl" }, { id: "b", name: "RAS21", branch_id: "nl" }, { id: "empty", name: "Neubau", branch_id: "nl" }] };
b.floors = [floor("eg-a", "a", [room("it-a", "IT", "symcon-category-100")]), floor("eg-b", "b", [room("it-b", "Büro", "symcon-category-200")])];
b.settings.roof.type = "none"; b.settings.lock_plan = false; b.settings.start_view = null;
window.editorProject = structuredClone(b);
p.data.edit(b);
p.hass = { ...p.hass, language: "de", symconObjects: [], symconTitle: "Standorte", floors: {}, areas: { "symcon-category-100": { area_id: "symcon-category-100", name: "Urbach / RAS19 / EG / IT" }, "symcon-category-200": { area_id: "symcon-category-200", name: "Urbach / RAS21 / EG / Technik" } }, states: {}, user: { ...p.hass.user, is_admin: true } };
await p.updateComplete;
[...p.shadowRoot.querySelectorAll("[role=tab]")].find((b) => b.textContent.includes("Editor")).click();
});
await page.waitForFunction(() => window.nfPanel.shadowRoot.querySelector("nf-editor")?.shadowRoot.querySelector(".nf-editor-building-select"));
await page.evaluate(() => {
window.e = window.nfPanel.shadowRoot.querySelector("nf-editor");
window.settle = async () => { await e.updateComplete; await window.nfPanel.updateComplete; await e.updateComplete; };
window.pickBuilding = async (id) => { const s = e.shadowRoot.querySelector(".nf-editor-building-select"); s.value = `building:${id}`; s.dispatchEvent(new Event("change", { bubbles: true })); await settle(); };
window.manager = () => e.shadowRoot.querySelector("nf-hierarchy-manager");
window.writes = 0; e.addEventListener("building-changed", () => window.writes++);
});
const initial = await page.evaluate(() => JSON.stringify(e._doc.floors));
await page.evaluate(async () => { await pickBuilding("b"); e.moveFloor(-1); await settle(); await pickBuilding("empty"); });
assert.deepEqual(await page.evaluate(() => ({ floor: e._floorId, scope: e._editorScope.id, chips: e.shadowRoot.querySelectorAll("[data-editor-floor]").length, writes })), { floor: null, scope: "empty", chips: 0, writes: 0 });
assert.equal(await page.evaluate(() => JSON.stringify(e._doc.floors)), initial, "building selection must not change or save floor assignments");
await page.evaluate(async () => { const b = structuredClone(e._doc); b.topology.locations[0].name = "Urbach neu"; const p = window.nfPanel; p.data.building = b; p.requestUpdate(); await settle(); });
assert.equal(await page.evaluate(() => e._floorId), null, "empty selection must survive a data refresh");
await page.evaluate(async () => { e.shadowRoot.querySelector(".nf-editor-add-floor").click(); await settle(); window.addedFloor = e._floorId; });
assert.deepEqual(await page.evaluate(() => ({ parent: e.floor.building_id, elevation: e.floor.elevation })), { parent: "empty", elevation: 0 });
assert.equal(await page.evaluate(() => JSON.stringify(e._doc.floors.filter((f) => f.id !== window.addedFloor))), initial);
// Native category selection remains a property of the selected room, never a building assignment.
await page.evaluate(async () => { await pickBuilding("a"); e.shadowRoot.querySelector(".nf-room-list .nf-row").click(); await settle(); window.oldRoomActions = e.roomActions(e.room); });
assert.match(await page.evaluate(() => e.shadowRoot.querySelector(".nf-room-context").textContent), /Urbach neu \/ Zentrale \/ RAS19 \/ EG/);
assert.equal(await page.evaluate(() => !!e.shadowRoot.querySelector("aside nf-hierarchy-manager, aside .nf-topology-form")), false);
await page.evaluate(async () => {
const label = [...e.shadowRoot.querySelectorAll("label.nf-field")].find((l) => l.textContent.trim().startsWith("Bereich (Symcon)"));
const s = label.querySelector("select"); s.value = "symcon-category-200"; s.dispatchEvent(new Event("change", { bubbles: true })); await settle();
});
assert.deepEqual(await page.evaluate(() => [e.room.name, e.room.area_id, e.floor.building_id]), ["IT", "symcon-category-200", "a"]);
await page.screenshot({ path: join(out, "symcon-editor-building.png") });
await page.evaluate(async () => { await pickBuilding("b"); window.beforeStale = JSON.stringify(e._doc); oldRoomActions.setArea("symcon-category-100"); oldRoomActions.patch({ name: "WRONG ROOM" }); await settle(); });
assert.equal(await page.evaluate(() => JSON.stringify(e._doc)), await page.evaluate(() => window.beforeStale), "stale room form must not mutate a room in a different building");
assert.equal(await page.evaluate(() => e.sel.room), null);
// A separate move dialog names and captures the selected floor; picking a target has no side effects.
await page.evaluate(async () => { await pickBuilding("a"); e.shadowRoot.querySelector(".nf-editor-move-floor").click(); await settle(); });
await page.waitForFunction(() => e.shadowRoot.querySelector(".nf-floor-move-dialog").open);
const beforeMove = await page.evaluate(() => JSON.stringify(e._doc.floors));
await page.evaluate(async () => { const s = e.shadowRoot.querySelector(".nf-floor-move-target"); s.value = "b"; s.dispatchEvent(new Event("change", { bubbles: true })); await settle(); });
assert.equal(await page.evaluate(() => JSON.stringify(e._doc.floors)), beforeMove);
await page.evaluate(async () => { e.shadowRoot.querySelector(".nf-floor-move-dialog header button").click(); await settle(); });
assert.equal(await page.evaluate(() => JSON.stringify(e._doc.floors)), beforeMove);
await page.evaluate(async () => { e.shadowRoot.querySelector(".nf-editor-move-floor").click(); await settle(); const s = e.shadowRoot.querySelector(".nf-floor-move-target"); s.value = "b"; s.dispatchEvent(new Event("change", { bubbles: true })); await settle(); e.shadowRoot.querySelector(".nf-floor-move-apply").click(); await settle(); });
const expectedMove = JSON.parse(beforeMove); expectedMove.find((f) => f.id === "eg-a").building_id = "b";
assert.deepEqual(await page.evaluate(() => e._doc.floors), expectedMove);
assert.deepEqual(await page.evaluate(() => [e._editorScope.id, e._floorId]), ["b", "eg-a"]);
// Metadata creation is isolated from storey creation, even when opening a new empty building.
await page.evaluate(async () => { e.shadowRoot.querySelector(".nf-hierarchy-open").click(); await settle(); });
assert.equal(await page.evaluate(() => e.shadowRoot.querySelector(".nf-hierarchy-dialog").open), true);
const beforeMetadata = await page.evaluate(() => JSON.stringify(e._doc.floors));
await page.evaluate(async () => {
const click = async (sel) => { manager().shadowRoot.querySelector(sel).click(); await manager().updateComplete; await settle(); await manager().updateComplete; };
const rename = async (label, value) => { const input = [...manager().shadowRoot.querySelectorAll("label")].find((l) => l.textContent.startsWith(label)).querySelector("input"); input.value = value; input.dispatchEvent(new Event("change", { bubbles: true })); await settle(); await manager().updateComplete; };
window.beforePickWrites = writes;
await click('[data-building="empty"]'); await click('[data-building="b"]');
if (writes !== beforePickWrites) throw new Error("Metadata selection must not write the plan");
await click(".add-location"); await rename("Name des Ortes", "Stuttgart");
await click(".add-branch"); await rename("Name der Niederlassung", "Werk");
await click(".add-building"); await rename("Name des Gebäudes", "Halle 2");
window.newMetadataBuilding = manager().buildingId;
});
assert.equal(await page.evaluate(() => JSON.stringify(e._doc.floors)), beforeMetadata);
await page.screenshot({ path: join(out, "symcon-editor-hierarchy.png") });
await page.evaluate(async () => { manager().shadowRoot.querySelector(".open-building").click(); await settle(); });
assert.deepEqual(await page.evaluate(() => [e._editorScope.id, e._floorId, e.shadowRoot.querySelector(".nf-hierarchy-dialog").open]), [await page.evaluate(() => window.newMetadataBuilding), null, false]);
assert.equal(await page.evaluate(() => JSON.stringify(e._doc.floors)), beforeMetadata);
await page.evaluate(async () => { e.shadowRoot.querySelector(".nf-editor-add-floor").click(); await settle(); });
assert.equal(await page.evaluate(() => e.floor.building_id), await page.evaluate(() => window.newMetadataBuilding));
// Each location's outdoor plan is an explicit action, outside any building's storeys.
await page.evaluate(async () => {
const locations = e._doc.topology.locations.map((l) => l.id);
for (const id of locations) { e.shadowRoot.querySelector(".nf-hierarchy-open").click(); await settle(); manager().shadowRoot.querySelector(`[data-location="${id}"]`).click(); await manager().updateComplete; manager().shadowRoot.querySelector(".outside").click(); await settle(); }
});
assert.equal(await page.evaluate(() => new Set(e._doc.floors.filter((f) => f.outdoor_location_id).map((f) => f.outdoor_location_id)).size), 2);
assert.equal(await page.evaluate(() => !!e.shadowRoot.querySelector(".nf-editor-add-floor")), false, "outdoor plans do not add indoor storeys");
await page.setViewport({ width: 390, height: 844 });
await page.evaluate(async () => { e.shadowRoot.querySelector(".nf-hierarchy-open").click(); await settle(); });
assert.ok(await page.evaluate(() => { const d = e.shadowRoot.querySelector(".nf-hierarchy-dialog"), r = d.getBoundingClientRect(); return r.left >= 0 && r.right <= innerWidth && r.height <= innerHeight && d.scrollWidth <= d.clientWidth; }), "hierarchy dialog must fit a phone without horizontal scrolling");
await page.screenshot({ path: join(out, "symcon-editor-hierarchy-mobile.png") });
// Legacy setup is explicit and preserves stable IDs, geometry and native category links.
await page.evaluate(async () => {
e.shadowRoot.querySelector(".nf-hierarchy-dialog").close(); const p = window.nfPanel, b = structuredClone(window.editorProject); delete b.topology; for (const f of b.floors) delete f.building_id; window.legacyFloors = structuredClone(b.floors); p.data.edit(b); await settle();
e.shadowRoot.querySelector(".nf-hierarchy-open").click(); await settle(); manager().shadowRoot.querySelector(".setup-button").click(); await settle();
});
assert.deepEqual(await page.evaluate(() => e._doc.floors.map(({ building_id, ...f }) => f)), await page.evaluate(() => window.legacyFloors));
assert.equal(await page.evaluate(() => new Set(e._doc.floors.map((f) => f.building_id)).size), 1);
assert.deepEqual(errors, []);
console.log("OK: separate hierarchy dialog, pure building navigation and refresh, selected-building storeys, native room category links, stale form guards, explicit/cancelled floor moves, metadata without implicit storeys, per-location outdoors, legacy migration and mobile layout");
} finally {
await browser?.close(); await new Promise((done) => server.close(done));
}