const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const { test } = require('node:test'); // No browser dependencies; exercise the same theme/tab functions as the shipped UI. function element(dataset = {}) { const classes = new Set(); return { dataset, handlers: {}, attributes: {}, hidden: false, classList: { toggle(name, on) { on ? classes.add(name) : classes.delete(name); } }, addEventListener(name, handler) { this.handlers[name] = handler; }, setAttribute(name, value) { this.attributes[name] = value; }, focus() { this.focused = true; } }; } function harness() { const themes = ['light','dark','system'].map(theme => element({ theme })); const tabs = ['points','bacnet','opcua','modbus','knx','mqtt','serial','status'].map(pointsTab => element({ pointsTab })); const panels = tabs.map(tab => element({ pointsPanel: tab.dataset.pointsTab })); const storage = new Map(); const document = { documentElement: element(), querySelector: () => null, querySelectorAll(selector) { return selector === 'button[data-theme]' ? themes : selector === '[data-points-tab]' ? tabs : selector === '[data-points-panel]' ? panels : []; } }; const context = vm.createContext({ document, window: { matchMedia: () => ({ matches: true, addEventListener() {} }) }, sessionStorage: { getItem() { return null; } }, localStorage: { getItem: key => storage.get(key), setItem: (key,value) => storage.set(key,value) } }); const root = path.join(__dirname, '../src/Gateway.Api/wwwroot'); const app = fs.readFileSync(path.join(root,'app.js'),'utf8'); vm.runInContext(fs.readFileSync(path.join(root,'ltd.js'),'utf8'),context); vm.runInContext(fs.readFileSync(path.join(root,'gateway-status.js'),'utf8'),context); vm.runInContext(app.slice(0,app.lastIndexOf('boot().catch(')),context); return { context, document, themes, tabs, panels, storage, run: code => vm.runInContext(code,context) }; } test('theme updates every switch in place, persists preference, and does not rebuild forms', async () => { const h = harness(); h.run("renderShell = () => { throw new Error('Theme must preserve forms'); }; api = async () => ({ theme: 'light', sidebarPinned: true });"); await h.run("setTheme('light')"); assert.equal(h.document.documentElement.dataset.theme,'light'); assert.equal(h.storage.get('seiugw-theme'),'light'); assert.equal(h.themes[0].attributes['aria-pressed'],'true'); assert.equal(h.themes[1].attributes['aria-pressed'],'false'); h.run("applyTheme('system')"); assert.equal(h.document.documentElement.dataset.theme,'dark'); assert.equal(h.themes[2].attributes['aria-pressed'],'true'); }); test('tab clicks and arrow keys select exactly one retained panel', () => { const h = harness(); h.run("renderView = () => { throw new Error('Tabs must preserve mounted forms'); }; bindPointTabs();"); h.panels[3].draft = 'unsaved'; h.tabs[3].handlers.click(); assert.deepEqual(h.panels.filter(p => !p.hidden).map(p => p.dataset.pointsPanel),['modbus']); h.tabs[3].handlers.keydown({ key: 'ArrowRight', preventDefault() {} }); assert.deepEqual(h.panels.filter(p => !p.hidden).map(p => p.dataset.pointsPanel),['knx']); assert.equal(h.tabs[4].focused,true); h.tabs[3].handlers.click(); assert.equal(h.panels[3].draft,'unsaved'); assert.equal(h.tabs.filter(tab => tab.tabIndex === 0).length,1); }); test('protocol forms and browser hosts belong to separate accessible panels', () => { const h = harness(); h.run("state.configuration = { bacnet: {}, opcUa: {}, knx: {} }; state.pointsTab = 'bacnet';"); const markup = h.run('viewProtocols()'); const expected = { bacnet: ['bacnet-form','bacnet-browser'], opcua: ['opcua-form','opcua-browser'], modbus: ['modbus-server-form','modbus-import-form'], knx: ['knx-form'], serial: ['serial-port-form'] }; const sections = [...markup.matchAll(/