import assert from "node:assert/strict"; import { test, type TestContext } from "node:test"; import { SymconBridge } from "./symcon-bridge.ts"; import { entityOptions } from "./components/editor/entities.ts"; import { areaEntities } from "./entities/registry.ts"; function mockWindow(context: TestContext, title?: string): void { const previous = Object.getOwnPropertyDescriptor(globalThis, "window"); Object.defineProperty(globalThis, "window", { configurable: true, value: { __NEXTFLOOR_SYMCON__: { endpoint: "/hook/test", token: "fixture", title } } }); context.after(() => { if (previous) Object.defineProperty(globalThis, "window", previous); else Reflect.deleteProperty(globalThis, "window"); }); } test("configured view names are exposed to the header with a safe empty-name fallback", (context) => { mockWindow(context, " Haus & Büro "); assert.equal(new SymconBridge().snapshot().symconTitle, "Haus & Büro "); window.__NEXTFLOOR_SYMCON__!.title = " "; assert.equal(new SymconBridge().snapshot().symconTitle, "NextFloor"); }); test("numeric control roles stay selectable in the real tree without duplicating automatic device lists", async (context) => { mockWindow(context); context.mock.method(globalThis, "fetch", async () => new Response(JSON.stringify({ state_revision: 1, building_revision: 1, areas: [{ area_id: "room", name: "Raum" }], variables: [{ entity_id: "sensor.6", name: "Dimmerwert", path: "Haus / KNX / Dimmerwert", state: "128", area_id: "room", attributes: { symcon_id: 6, symcon_variable_type: 1, symcon_numeric_value: 128, symcon_min: 0, symcon_max: 255 } }], objects: [{ object_id: 6, parent_id: 0, entity_id: "sensor.6", name: "Dimmerwert", object_type: 2 }] }))); const bridge = new SymconBridge(); try { const hass = await bridge.start(); assert.equal(hass.states["light.6"].attributes.brightness, 128); assert.equal(hass.states["cover.6"].attributes.current_position, 128 / 255 * 100); assert.equal(entityOptions(hass, (id) => id.startsWith("light."))[0].sourceEntity, "sensor.6"); assert.ok(!areaEntities(hass, "room").includes("light.6")); assert.ok(!areaEntities(hass, "room").includes("cover.6")); assert.equal(hass.symconObjects?.length, 1, "no artificial duplicate nodes in the native object tree"); } finally { bridge.stop(); } }); test("manual reload waits for polling, replaces removed variables and reloads the whole tree", async (context) => { mockWindow(context); const variable = (id: number) => ({ entity_id: `sensor.${id}`, name: "KNX Wert", path: "Haus / KNX / Wert", state: "21.75", attributes: { unit_of_measurement: "°C" } }); const actions: string[] = []; let releasePoll!: () => void; const pollGate = new Promise((resolve) => { releasePoll = resolve; }); context.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { const { action } = JSON.parse(String(init.body)); actions.push(action); if (action === "variables/snapshot") { await pollGate; return new Response(JSON.stringify({ state_revision: 1, building_revision: 1 })); } const reload = actions.filter((a) => a === "bootstrap").length > 1; return new Response(JSON.stringify({ state_revision: 1, building_revision: 1, variables: [variable(reload ? 2 : 1)], areas: [], objects: [{ object_id: reload ? 2 : 1 }] })); }); const bridge = new SymconBridge(); try { const hass = await bridge.start(); bridge.stop(); let changes = 0; bridge.onChange(() => { changes++; }); const poll = (bridge as unknown as { refresh(): Promise }).refresh(); const reload = hass.symconRefresh!(); assert.deepEqual(actions, ["bootstrap", "variables/snapshot"], "manual bootstrap must not overlap the running poll"); releasePoll(); await Promise.all([poll, reload]); assert.deepEqual(actions, ["bootstrap", "variables/snapshot", "bootstrap"]); assert.deepEqual(Object.keys(bridge.snapshot().states), ["sensor.2"], "even an unchanged revision cannot suppress an explicit reload"); assert.equal(bridge.snapshot().symconObjects?.[0].object_id, 2); assert.equal(changes, 1); } finally { releasePoll(); bridge.stop(); } }); test("a failed manual reload reports the error and preserves existing values for a retry", async (context) => { mockWindow(context); let fail = false; context.mock.method(globalThis, "fetch", async () => fail ? new Response("offline", { status: 503 }) : new Response(JSON.stringify({ state_revision: 1, building_revision: 1, variables: [{ entity_id: "sensor.1", name: "Temperatur", path: "Haus / Temperatur", state: "21.75", attributes: {} }], objects: [], areas: [] }))); const bridge = new SymconBridge(); try { const hass = await bridge.start(); bridge.stop(); fail = true; await assert.rejects(hass.symconRefresh!(), /503/); assert.equal(bridge.snapshot().states["sensor.1"].state, "21.75"); fail = false; await hass.symconRefresh!(); assert.equal(bridge.snapshot().states["sensor.1"].state, "21.75"); } finally { bridge.stop(); } });