// Page behaviour on the Windows edition with the review stand-in powershell: every text field of the "server" carries HTML.
// node t_ui.js
const fs = require('fs'), { launch, sleep } = require('./cdp'), { check, done } = require('./lib');
const [base, cfg] = process.argv.slice(2), ctl = w => fs.writeFileSync(cfg + '.ctl', w), log = () => fs.readFileSync(cfg + '.log', 'utf8');
const H = `const $ = (sel, root = document) => root.querySelector(sel), $$ = (sel, root = document) => [...root.querySelectorAll(sel)];
const sleep = ms => new Promise(r => setTimeout(r, ms)), until = async (fn, ms = 6000) => { for (const t0 = Date.now(); Date.now() - t0 < ms; await sleep(100)) { const v = fn(); if (v) return v; } return fn(); };
const page = () => $('main > section:not([hidden])'), tab = () => $('.tab', page()), dlg = () => $('dialog[open]'), byText = (sel, text, root = document) => $$(sel, root).find(e => e.textContent.trim() === text);
const down = el => el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true })), rows = (root = tab()) => $$('tbody tr', root).filter(r => !r.hidden), row = pid => rows().find(r => r.cells[0].textContent === String(pid));
const type = (el, v) => { el.value = v; el.dispatchEvent(new Event('input', { bubbles: true })); el.dispatchEvent(new Event('change', { bubbles: true })); };
const X = '
', clean = () => window.__xss === undefined && $$('img').length === 1 && $$('script').length === 2 && !$$('*').some(e => e.hasAttribute('onerror') || e.hasAttribute('onclick'));
const open = async key => { go('server', S.id, key); await until(() => S.tab === key && tab() && S.m); await sleep(350); };`;
(async () => {
const b = await launch(), p = await b.first(), ev = body => p.evaluate(H + body).catch(e => (console.log(' step threw: ' + e.message.split('\n')[0]), false));
await p.go(base + '/#/server/0/overview'), await sleep(3500);
check('deep link #/server/0/overview opens the first server with live data', await ev(`return !!(await until(() => S.page === 'server' && S.m && tab())) && $('.tabbar .on').textContent === 'Overview' && $('h1', page()).textContent`));
// every tab with hostile data
const seen = await ev(`const out = []; for (const key of Object.keys(TABS)) { await open(key); out.push(key + ':' + +clean() + ':' + +tab().textContent.includes(X) + ':' + +(page().textContent.includes(X))); } return out.join(' ')`);
check('all 11 tabs: no element is created from server data (img / script / handlers), the text is shown literally', seen.split(' ').length === 11 && seen.split(' ').every(s => /:1:[01]:1$/.test(s)) && ['overview', 'cpu', 'memory', 'storage', 'network', 'processes', 'services'].every(k => seen.includes(k + ':1:1:1')), seen);
await p.shot(cfg + '-hostile-processes.png');
check('console output and service logs are text', await ev(`await open('console'); const i = $('input.mono', tab()); i.value = 'echo hi'; byText('button', 'Run', tab()).click(); await until(() => $('.console .good'), 8000); const c = $('.console').textContent.includes(X + '