Download dev/tests/web/t_ui.js from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 11.5 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/tests/web/t_ui.js
- Command line
-
hf download hf://mchuca/ServerMonitor/dev/tests/web/t_ui.js
-
curl -L -o t_ui.js https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/tests/web/t_ui.js
11.5 kB
| // Page behaviour on the Windows edition with the review stand-in powershell: every text field of the "server" carries HTML. | |
| // node t_ui.js <base url> <cfgdir> | |
| 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 = '<img src=x onerror=window.__xss=1>', 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 + '<script>') && clean(); | |
| await open('services'); down(rows().find(r => r.cells[0].textContent === 'W3SVC')); byText('button', 'Logs', tab()).click(); const d = await until(dlg); await until(() => /would run/.test($('pre', d).textContent), 8000); const l = $('pre', d).textContent.includes(X) && $('h2', d).textContent.includes(X) && clean(); byText('button', 'Close', d).click(); return c && l`)); | |
| check('service logs command uses the quoted name', /Where-Object Name -eq 'W3SVC'; \$s \| Format-List/.test(log()), log().split('\n').filter(l => /W3SVC/.test(l))[0]?.slice(0, 90)); | |
| check('dashboard card, sidebar, events page and edit dialog with a hostile server name / group', await ev(`go('dashboard'); await sleep(500); const a = $('.card b').textContent === X && $('.item b').textContent === X && clean(); go('events'); await sleep(500); const e = clean(); | |
| go('server', S.servers[0].cfg.id, 'overview'); await until(() => S.page === 'server' && S.m && tab()); byText('button', 'Edit', page()).click(); const d = await until(dlg); const f = $('input', d).value === X && $$('datalist option', d).some(o => o.value === '<b>grp</b>') && clean(); byText('button', 'Cancel', d).click(); await sleep(200); return a && e && f && !dlg()`)); | |
| // lists: selection, filter, confirmation | |
| check('process list keeps its selection across refreshes', await ev(`await open('processes'); await until(() => row(2040)); down(row(2040)); const t = built.processes.tbl; await sleep(3200); return t.sel === '2040' && $('tr.sel', tab())?.cells[0].textContent === '2040' && $$('tr.sel', tab()).length === 1`)); | |
| check('a filtered-out row is not a selection', await ev(`const f = $('input[type=search]', tab()); type(f, 'svchost'); byText('button', 'End task', tab()).click(); await sleep(200); const msg = $('.tab > .muted', page()).textContent, none = !dlg(); type(f, ''); return none && msg === 'Select a process first.' && msg`)); | |
| const before = log(); | |
| check('Esc on a confirmation runs nothing', await ev(`down(row(2040)); byText('button', 'Kill', tab()).click(); const d = await until(dlg); const q = $('p', d).textContent; d.dispatchEvent(new Event('cancel')); d.close(); await sleep(500); return !dlg() && q === 'Kill chrome.exe (2040) on ' + X + '?' && q`) && log() === before); | |
| check('pid is read before the question: the entry may vanish while the dialog is open', await ev(`down(row(1200)); byText('button', 'Kill tree', tab()).click(); const d = await until(dlg); $('input[type=checkbox]', tab()).click(); built.processes.tbl.fill([]); const gone = built.processes.tbl.sel === undefined; byText('button', 'Yes', d).click(); | |
| const st = await until(() => /^[✓✗]/.test($('.tab > .muted', page()).textContent) && $('.tab > .muted', page()).textContent, 8000); $('input[type=checkbox]', tab()).click(); return gone && st === '✓ taskkill /F /T /PID 1200' && st`) && /taskkill \/F \/T \/PID 1200/.test(log())); | |
| check('priority dialog: the chosen class reaches the command for the selected pid', await ev(`await until(() => row(2040)); down(row(2040)); byText('button', 'Priority…', tab()).click(); const d = await until(dlg); const s = $('select', d); const def = s.value; s.selectedIndex = 0; byText('button', 'OK', d).click(); | |
| const st = await until(() => /PriorityClass/.test($('.tab > .muted', page()).textContent) && /^[✓✗]/.test($('.tab > .muted', page()).textContent) && $('.tab > .muted', page()).textContent, 8000); return def === 'Normal' && st === "✓ (Get-Process -Id 2040).PriorityClass = 'Idle'" && st`)); | |
| check('service action: quoted name in the command', await ev(`await open('services'); down(rows().find(r => r.cells[0].textContent === 'W3SVC')); byText('button', 'Start', tab()).click(); const st = await until(() => /^[✓✗]/.test($('.tab > .muted', page()).textContent) && $('.tab > .muted', page()).textContent, 8000); return st === "✓ Start-Service -Name 'W3SVC'" && st`)); | |
| check('sidebar "Edit" without an open server does nothing', await ev(`go('dashboard'); await sleep(300); byText('aside button', 'Edit').click(); await sleep(300); const none = !dlg(); byText('aside button', 'Remove').click(); await sleep(300); return none && !dlg()`)); | |
| // routing | |
| check('hash routing: unknown server -> dashboard, tab by hash, events, back', await ev(`location.hash = '#/server/nope/cpu'; await sleep(300); const a = S.page === 'dashboard'; location.hash = '#/server/0/network'; await until(() => S.page === 'server' && S.tab === 'network' && S.m && tab()); const n = $('.tabbar .on').textContent === 'Network'; | |
| location.hash = '#/events'; await sleep(300); const e = S.page === 'events' && !$('main > section:nth-child(3)').hidden; history.back(); await until(() => S.page === 'server'); await sleep(300); return a && n && e && S.tab === 'network' && $('.tabbar .on').textContent`)); | |
| // unseen badge survives navigation; one stream per page | |
| ctl('failsvc'); await sleep(4500); | |
| check('badge counts events while another page is open and survives navigation', await ev(`go('dashboard'); await api('server/analyze', { id: S.servers[0].cfg.id }); const b = $('.badge', $('aside button.nav:nth-of-type(2)') || document); const n1 = await until(() => !$('aside .nav .badge').hidden && $('aside .nav .badge').textContent); | |
| go('server', S.servers[0].cfg.id, 'insights'); await until(() => S.m && tab()); await sleep(600); const n2 = $('aside .nav .badge').textContent, kept = !$('aside .nav .badge').hidden; const dry = $$('.finding').some(f => /Autopilot \\(dry run\\) would act on this/.test(f.textContent) && /Spooler/.test(f.textContent)); | |
| go('events'); await sleep(400); const r = { n1, n2, kept, dry, hiddenOnEvents: $('aside .nav .badge').hidden }; return +n1 >= 1 && kept && +n2 >= +n1 && dry && r.hiddenOnEvents ? 'badge ' + n1 + ' -> ' + n2 : (console.info(JSON.stringify(r)), window.__why = JSON.stringify(r), false)`) || (console.log(' ' + await p.evaluate('return window.__why')), false)); | |
| ctl('ok'); | |
| check('one live connection per page', await ev(`return performance.getEntriesByType('resource').filter(r => r.name.includes('/api/events')).length >= 1 && feed.readyState === 1 && feed.url`)); | |
| // card text while there is no data yet | |
| ctl('stall'); | |
| check('card of a server without data says "Waiting for data…"', await ev(`go('dashboard'); await api('server/reconnect', { id: S.servers[0].cfg.id }); const t = await until(() => S.servers[0].status === 'Connecting' && $('.card .foot').textContent, 5000); return t === 'Waiting for data…' && $('.card .chip').textContent + ': ' + t`)); | |
| ctl('ok'); await ev(`await api('server/reconnect', { id: S.servers[0].cfg.id }); return await until(() => S.servers[0].online, 8000)`); | |
| // resize | |
| await p.send('Emulation.setDeviceMetricsOverride', { width: 1100, height: 700, deviceScaleFactor: 2, mobile: false }); | |
| check('resize + device pixel ratio 2: canvases are redrawn at the new size, no horizontal overflow', await ev(`go('server', S.servers[0].cfg.id, 'overview'); await until(() => S.m && tab()); await sleep(900); const cv = $$('canvas', page()).filter(c => c.offsetWidth); | |
| return cv.length >= 10 && cv.every(c => c.width === Math.round(c.parentElement.clientWidth * 2) && c.height === Math.round(c.parentElement.clientHeight * 2)) && document.documentElement.scrollWidth <= innerWidth && cv.length + ' canvases'`)); | |
| await p.shot(cfg + '-hostile-overview-1100.png'); | |
| await p.send('Emulation.setDeviceMetricsOverride', { width: 1500, height: 940, deviceScaleFactor: 1, mobile: false }); | |
| // removing the server that is open, with its tabs alive | |
| check('removing the open server returns to the dashboard without errors', await ev(`await open('history'); await sleep(500); byText('button', '⏻ Power', page()).click(); await sleep(200); byText('.menu button', 'Remove from monitor…').click(); const d = await until(dlg); byText('button', 'Remove + delete history', d).click(); | |
| return await until(() => S.servers.length === 0 && S.page === 'dashboard' && !$('main > section').hidden) && $$('.card').length === 1 && clean()`)); | |
| await sleep(1500); | |
| check('no console error, warning, CSP violation or exception in the browser', !p.problems.length, p.problems.join(' | ')); | |
| b.close(), done(), setTimeout(() => process.exit(0), 700); | |
| })(); | |