Files
ESP32_Serial_Swiss_Army_Knife/tests/web_ui_session/browser.cjs
T
Commander1024 e6db5428eb Add browser Serial/Admin terminal switching
Keep the serial connection and lease intact while providing a separate,
bounded admin terminal with explicit open and close controls. Fence
retained
terminal state across sessions and add fit-readiness retries with
regression
coverage.
2026-09-06 19:46:38 +02:00

360 lines
27 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const vm = require('node:vm');
const {script, loader} = JSON.parse(require('node:fs').readFileSync(process.argv[2], 'utf8'));
const token = 'a'.repeat(64);
const json = value => new Response(JSON.stringify(value));
const session = (extra = {}) => json({username: '<img>', role: 'user', csrf: token, expires_in: 3600, ...extra});
const ticket = () => json({ticket: 't'.repeat(32)});
const failure = status => new Response('SECRET ERROR BODY', {status, headers: {'Retry-After': '7'}});
const deferred = () => { let resolve; const promise = new Promise(r => { resolve = r; }); return {promise, resolve}; };
const tick = async () => { for (let i = 0; i < 6; ++i) await new Promise(r => setImmediate(r)); };
function browser({onlyLoader = false, withLoader = false, role = 'user'} = {}) {
const nodes = {}, events = {}, calls = [], redirects = [], timers = new Map(), sockets = [], terminals = [];
const queues = {'/api/session': [], '/api/status': [], '/api/ws-ticket': [], '/api/admin/ws-ticket': [], '/api/logout': []};
const fits = [];
let serial = 0;
const on = (key, fn) => { if (!(events[key] ||= []).includes(fn)) events[key].push(fn); };
const emit = (key, event = {}) => { for (const fn of events[key] || []) fn(event); };
const timeout = (fn, ms, interval = false) => { timers.set(++serial, {fn, ms, interval}); return serial; };
class Socket {
static OPEN = 1;
constructor(url) { this.url = url; this.readyState = 0; this.bufferedAmount = 0; this.events = {}; this.sent = []; sockets.push(this); }
addEventListener(k, fn) { this.events[k] = fn; }
emit(k, event = {}) { if (k === 'open') this.readyState = 1; this.events[k]?.(event); this['on' + k]?.(event); }
close() { this.closed = true; this.readyState = 3; this.emit('close'); }
send(value) { this.sent.push(value); }
}
class Terminal {
constructor(options) { this.options = options; this.writes = []; terminals.push(this); }
loadAddon() {} open() {} focus() {} resize(cols, rows) { this.cols = cols; this.rows = rows; } onData(fn) { this.input = fn; }
write(bytes, callback) { this.writes.push([...bytes]); if (this.holdWrites) (this.pending ||= []).push(callback); else callback?.(); }
}
const window = {addEventListener: on, removeEventListener(k, fn) { events[k] = (events[k] || []).filter(f => f !== fn); },
setTimeout: timeout, clearTimeout: id => timers.delete(id),
setInterval: (fn, ms) => timeout(fn, ms, true), clearInterval: id => timers.delete(id),
requestAnimationFrame: fn => timeout(fn, -1), cancelAnimationFrame: id => timers.delete(id),
location: {origin: 'https://sak.local', replace: path => redirects.push(path)}};
const context = vm.createContext({window, document: {getElementById(id) {
return nodes[id] ||= {textContent: '', dataset: {}, classList: {toggle() {}},
setAttribute(k, v) { this[k] = v; },
getBoundingClientRect: () => ({width: 100, height: 100}),
addEventListener(k, fn) { this[k] = fn; }};
}}, Terminal, FitAddon: {FitAddon: class {
constructor() { this.measurements = []; this.calls = 0; fits.push(this); }
proposeDimensions() { ++this.calls; return this.measurements.length ? this.measurements.shift() : {cols: 80, rows: 24}; }
}},
TextEncoder, TextDecoder, Uint8Array, ArrayBuffer, AbortController, URL, Date, WebSocket: Socket,
fetch: async (url, options) => {
// Apply the Origin regression guard to every mutation, including logout.
assert.ok(Object.hasOwn(queues, url));
if (options.method === 'POST') assert.equal(options.mode, 'cors');
assert.equal(options.headers?.Origin, undefined);
calls.push({url, ...options});
const next = queues[url].shift();
if (next !== undefined) return typeof next === 'function' ? next(options) : next;
if (url === '/api/session') return session({role});
if (url === '/api/status') return json({});
if (url === '/api/ws-ticket' || url === '/api/admin/ws-ticket') return ticket();
throw new Error('network unavailable');
}});
if (withLoader || onlyLoader) vm.runInContext(loader, context);
const start = () => vm.runInContext(script, context);
const fire = ms => {
const match = [...timers].find(([, t]) => t.ms === ms); assert.ok(match, `missing timer ${ms}`);
const [id, t] = match; if (!t.interval) timers.delete(id); t.fn();
};
return {nodes, calls, redirects, timers, sockets, terminals, queues, fits, events, emit, start, fire,
click: id => nodes[id].click(), window};
}
async function connected() { const b = browser(); b.start(); await tick(); assert.equal(b.sockets.length, 1); return b; }
let passed = 0;
async function test(name, fn) { await fn(); ++passed; console.log('PASS JS:', name); }
(async () => {
await test('bootstrap, CSRF, bounded expiry safe text, serial protocol and disconnect pause', async () => {
const b = await connected();
assert.equal(b.calls[0].url, '/api/session');
const post = b.calls.find(c => c.url === '/api/ws-ticket');
assert.equal(post.method, 'POST'); assert.equal(post.body, ''); assert.equal(post.headers['X-CSRF-Token'], token);
for (const call of b.calls) for (const [k, v] of Object.entries({credentials: 'same-origin', mode: call.method === 'POST' ? 'cors' : 'same-origin', cache: 'no-store', redirect: 'error'})) assert.equal(call[k], v);
assert.match(b.nodes['session-info'].textContent, /^<img>.*one hour absolute/);
const ws = b.sockets[0], term = b.terminals[0]; ws.emit('open');
ws.emit('message', {data: JSON.stringify({type: 'hello', clientId: 8, writerId: 8, role: 'writer'})});
term.input('x'.repeat(2050)); assert.deepEqual(ws.sent.map(x => x.length), [1024, 1024, 2]);
ws.emit('message', {data: Uint8Array.of(0, 255, 13, 10).buffer}); assert.deepEqual(term.writes, [[0, 255, 13, 10]]);
b.click('release-control'); assert.equal(ws.sent.at(-1), 'release-writer');
ws.emit('message', {data: JSON.stringify({type: 'writer', writerId: 0, role: 'observer'})});
b.click('request-control'); assert.equal(ws.sent.at(-1), 'request-writer');
b.click('connection-toggle'); assert.ok(ws.closed); assert.equal(b.nodes['connection-toggle'].textContent, 'Connect');
const count = b.calls.length; ws.emit('close'); await tick(); assert.equal(b.calls.length, count);
b.click('connection-toggle'); await tick(); assert.equal(b.calls[count].url, '/api/session');
});
await test('401 at session/ticket/status stops everything and navigates only once', async () => {
for (const path of ['/api/session', '/api/ws-ticket', '/api/status']) {
const b = browser(); b.queues[path].push(failure(401)); b.start(); await tick();
assert.deepEqual(b.redirects, ['/login']); assert.ok(b.sockets.every(s => s.closed));
assert.equal(b.timers.size, 0); b.window.sakSessionExpired(); assert.equal(b.redirects.length, 1);
b.click('connection-toggle'); await tick(); assert.equal(b.redirects.length, 1);
}
});
await test('403 mutation is manual-only; capacity backoff is not credentials and revalidates', async () => {
for (const status of [403, 429, 503]) {
const b = browser(); b.queues['/api/ws-ticket'].push(failure(status)); b.start(); await tick();
assert.deepEqual(b.redirects, []); assert.equal(b.sockets.length, 0);
if (status === 403) {
assert.match(b.nodes['connection-detail'].textContent, /security check/);
assert.equal(b.nodes['connection-toggle'].textContent, 'Connect'); b.click('connection-toggle');
} else {
assert.match(b.nodes['connection-detail'].textContent, /capacity or backoff/); b.fire(7000);
}
await tick(); assert.equal(b.calls.filter(c => c.url === '/api/session').length, 2);
assert.equal(b.sockets.length, 1);
}
});
await test('logout success, lost success, uncertain network and explicit recovery', async () => {
for (const outcome of ['204', 'lost401', 'lost200', 'offline', '403', '503']) {
const b = await connected();
b.queues['/api/logout'].push(outcome === '204' ? new Response(null, {status: 204}) :
['403', '503'].includes(outcome) ? failure(Number(outcome)) : () => { throw new Error('SECRET NETWORK'); });
if (outcome === 'lost401') b.queues['/api/session'].push(session(), failure(401));
if (outcome === 'offline') b.queues['/api/session'].push(session(), () => { throw new Error('offline'); });
await b.click('sign-out'); await tick(); assert.ok(b.sockets[0].closed);
assert.equal(b.calls.filter(c => c.url === '/api/logout').length, 1);
const post = b.calls.find(c => c.url === '/api/logout'); assert.equal(post.body, ''); assert.equal(post.headers['X-CSRF-Token'], token);
if (['204', 'lost401'].includes(outcome)) { assert.deepEqual(b.redirects, ['/login']); assert.equal(b.timers.size, 0); }
else {
assert.deepEqual(b.redirects, []); assert.match(b.nodes['connection-status'].textContent, /not confirmed/);
assert.ok(!b.nodes['connection-detail'].textContent.includes('SECRET'));
assert.equal(b.nodes['sign-out'].disabled, false);
const count = b.calls.length; b.click('connection-toggle'); await tick(); assert.equal(b.calls[count].url, '/api/session');
assert.equal(b.sockets.length, 2);
}
}
});
await test('logout cancels pending status/ticket/session; late 401 and WS events cannot affect new work', async () => {
for (const path of ['/api/session', '/api/ws-ticket', '/api/status']) {
const d = deferred(), b = browser(); b.queues[path].push(d.promise); b.start(); await tick();
b.queues['/api/logout'].push(failure(403)); await b.click('sign-out'); await tick();
assert.ok(b.calls.find(c => c.url === path).signal.aborted);
const detail = b.nodes['connection-detail'].textContent;
d.resolve(failure(401)); await tick(); assert.deepEqual(b.redirects, []); assert.equal(b.nodes['connection-detail'].textContent, detail);
}
const b = await connected(), old = b.sockets[0]; b.click('connection-toggle'); b.click('connection-toggle'); await tick();
old.emit('open'); old.emit('message', {data: JSON.stringify({type: 'hello', clientId: 99, writerId: 99, role: 'writer'})}); old.emit('error'); old.emit('close');
assert.equal(b.nodes['client-id'].textContent, '—'); assert.equal(b.terminals[0].options.disableStdin, true);
});
await test('pagehide/restore revalidates, preserves pause; late logout cannot navigate restored page', async () => {
for (const paused of [false, true]) {
const b = await connected(); if (paused) b.click('connection-toggle');
b.emit('pagehide'); const count = b.calls.length; b.emit('pageshow', {persisted: true}); await tick();
assert.equal(b.calls[count].url, '/api/session'); assert.equal(b.sockets.length, paused ? 1 : 2);
if (paused) assert.equal(b.nodes['connection-toggle'].textContent, 'Connect');
}
const b = await connected(), d = deferred(); b.queues['/api/logout'].push(d.promise);
const pending = b.click('sign-out'); await tick(); b.emit('pagehide'); b.emit('pageshow', {persisted: true}); await tick();
d.resolve(new Response(null, {status: 204})); await pending; assert.deepEqual(b.redirects, []);
});
await test('bounded schema/body validation, timeout, expiry and retry session checks', async () => {
for (const response of [session({csrf: 'A'.repeat(64)}), session({expires_in: 3601}), session({expires_in: -1}),
session({expires_in: 1.5}), session({role: 'root'}), session({username: 'x'.repeat(17)}),
new Response(' '.repeat(513)), new Response(Uint8Array.of(255)), json(null)]) {
const b = browser(); b.queues['/api/session'].push(response); b.start(); await tick();
assert.equal(b.sockets.length, 0); assert.equal(b.calls.length, 1); assert.ok(b.calls[0].signal.aborted);
}
const b = browser(); b.queues['/api/session'].push(o => new Promise((_, reject) => o.signal.addEventListener('abort', () => reject(new Error('timeout')))));
b.start(); b.fire(15000); await tick(); b.fire(1000); await tick(); assert.equal(b.calls[1].url, '/api/session');
const c = browser(); c.queues['/api/session'].push(session({expires_in: 2})); c.start(); await tick();
const expiry = [...c.timers.values()].find(timer => timer.ms >= 0 && timer.ms <= 2000);
assert.ok(expiry); c.fire(expiry.ms); assert.deepEqual(c.redirects, ['/login']);
});
await test('late body completions and superseded restore session are ignored', async () => {
for (const path of ['/api/session', '/api/ws-ticket', '/api/status']) {
let stream;
const b = browser(); b.queues[path].push(new Response(new ReadableStream({start(c) { stream = c; }})));
b.start(); await tick(); b.emit('pagehide');
const text = path === '/api/session' ? {username: 'late', role: 'admin', csrf: token, expires_in: 3600} :
path === '/api/ws-ticket' ? {ticket: 't'.repeat(32)} : {wifi: {available: true, state: 'LATE'}};
stream.enqueue(new TextEncoder().encode(JSON.stringify(text))); stream.close(); await tick();
assert.ok(b.sockets.every(socket => socket.closed)); assert.deepEqual(b.redirects, []); assert.equal(b.timers.size, 0);
assert.ok(!b.nodes['wifi-summary'].textContent.includes('LATE'));
}
const b = await connected(); b.click('connection-toggle'); b.emit('pagehide');
const d = deferred(); b.queues['/api/session'].push(d.promise);
b.emit('pageshow', {persisted: true}); await tick(); b.click('connection-toggle'); await tick();
d.resolve(failure(401)); await tick(); assert.deepEqual(b.redirects, []); assert.equal(b.sockets.length, 2);
});
await test('inline asset failures: 401 login, offline usable fallback, pagehide and shared navigation guard', async () => {
for (const status of [401, 503]) {
const b = browser({onlyLoader: true}); b.queues['/api/session'].push(failure(status));
b.emit('error', {target: {tagName: 'SCRIPT'}}); await tick();
assert.deepEqual(b.redirects, status === 401 ? ['/login'] : []); assert.equal(b.timers.size, 0);
b.emit('error', {target: {tagName: 'SCRIPT'}}); assert.equal(b.calls.length, 1);
}
const b = browser({onlyLoader: true}), d = deferred(); b.queues['/api/session'].push(d.promise);
b.emit('error', {target: {tagName: 'LINK'}}); b.emit('pagehide'); d.resolve(failure(401)); await tick(); assert.deepEqual(b.redirects, []);
const c = browser({withLoader: true}); c.start(); await tick();
c.queues['/api/session'].push(failure(401)); c.emit('error', {target: {tagName: 'IMG'}}); await tick();
assert.deepEqual(c.redirects, ['/login']); assert.ok(c.sockets[0].closed); assert.equal(c.timers.size, 0);
});
async function adminBrowser() {
const b = browser({role: 'admin'}); b.queues['/api/session'].push(session({role: 'admin'}));
b.start(); await tick(); const ws = b.sockets[0]; ws.emit('open');
ws.emit('message', {data: JSON.stringify({type: 'hello', clientId: 8, writerId: 8, role: 'writer'})});
b.click('select-admin'); b.click('admin-toggle'); await tick();
assert.equal(b.sockets.length, 2); b.sockets[1].emit('open'); return b;
}
await test('ordinary user is serial-only; admin selection preserves socket/lease and isolates input/output', async () => {
const u = await connected(); assert.equal(u.nodes['terminal-selector'].hidden, true);
u.click('select-admin'); u.click('admin-toggle'); await tick(); assert.equal(u.terminals.length, 1); assert.equal(u.sockets.length, 1);
const b = await adminBrowser(), [serial, admin] = b.sockets, [st, at] = b.terminals;
assert.match(admin.url, /\/ws\/admin\?ticket=/);
assert.equal(b.calls.find(c => c.url === '/api/admin/ws-ticket').headers['X-CSRF-Token'], token);
st.input('WRONG'); at.input('x'.repeat(1025)); assert.equal(serial.sent.length, 0);
assert.deepEqual(admin.sent.map(x => x.length), [512, 512, 1]);
serial.emit('message', {data: Uint8Array.of(0, 255).buffer});
admin.emit('message', {data: Uint8Array.of(27, 91).buffer});
assert.deepEqual(st.writes, [[0, 255]]); assert.deepEqual(at.writes, [[27, 91]]);
b.click('release-control'); assert.equal(serial.sent.at(-1), 'release-writer');
for (let i = 0; i < 20; ++i) { b.click('select-serial'); at.input('WRONG'); b.click('select-admin'); }
assert.equal(b.sockets.length, 2); assert.equal(b.terminals.length, 2); assert.ok(!serial.closed);
assert.equal(b.nodes['client-id'].textContent, '8'); assert.equal(b.nodes['writer-id'].textContent, '8');
assert.equal(b.nodes['release-control'].disabled, false); assert.equal(admin.sent.length, 3);
b.click('admin-toggle'); assert.ok(admin.closed); assert.equal(admin.onmessage, null); assert.ok(!serial.closed);
b.click('admin-toggle'); await tick(); const replacement = b.sockets[2]; replacement.emit('open'); replacement.emit('close');
assert.ok(!serial.closed); assert.equal(b.nodes['admin-toggle'].textContent, 'Open admin');
b.click('select-serial'); st.input('ok'); assert.deepEqual([...serial.sent.at(-1)], [111, 107]);
});
await test('bounded hidden output continues draining, separate scrollback and input overflow closes only admin', async () => {
const b = await adminBrowser(), [serial, admin] = b.sockets, [st, at] = b.terminals;
assert.equal(st.options.scrollback, 5000); assert.equal(at.options.scrollback, 5000);
st.holdWrites = true;
for (let i = 0; i < 65; ++i) serial.emit('message', {data: new Uint8Array(1024).buffer});
assert.equal(st.writes.length, 64); assert.match(b.nodes['output-detail'].textContent, /serial 1024 B, admin 0 B/);
st.pending.shift()(); serial.emit('message', {data: Uint8Array.of(42).buffer}); assert.equal(st.writes.at(-1)[0], 42);
at.input('x'.repeat(4097)); assert.ok(admin.closed); assert.ok(!serial.closed); assert.equal(admin.sent.length, 0);
});
await test('admin failures and cancellation are isolated; 401/logout/expiry/pagehide close both', async () => {
for (const status of [403, 503, 401]) {
const b = await adminBrowser(), serial = b.sockets[0]; b.click('admin-toggle');
b.queues['/api/admin/ws-ticket'].push(failure(status)); b.click('admin-toggle'); await tick();
assert.equal(!!serial.closed, status === 401);
assert.deepEqual(b.redirects, status === 401 ? ['/login'] : []);
assert.equal(b.sockets.length, 2);
}
for (const action of ['pagehide', 'expiry', 'logout']) {
const b = await adminBrowser();
if (action === 'pagehide') b.emit('pagehide');
if (action === 'expiry') b.window.sakSessionExpired();
if (action === 'logout') { b.queues['/api/logout'].push(new Response(null, {status: 204})); await b.click('sign-out'); }
await tick(); assert.ok(b.sockets.every(s => s.closed)); assert.equal(b.sockets[1].onmessage, null);
if (action === 'pagehide') {
b.queues['/api/session'].push(session({role: 'admin'})); b.emit('pageshow', {persisted: true}); await tick();
assert.equal(b.sockets.length, 3); assert.match(b.sockets[2].url, /\/ws\/serial/);
assert.equal(b.nodes['admin-toggle'].textContent, 'Open admin');
}
}
const b = await adminBrowser(); b.click('admin-toggle'); const d = deferred();
b.queues['/api/admin/ws-ticket'].push(d.promise); b.click('admin-toggle'); await tick();
b.click('admin-toggle'); d.resolve(failure(401)); await tick();
assert.deepEqual(b.redirects, []); assert.ok(!b.sockets[0].closed); assert.equal(b.sockets.length, 2);
});
await test('selected resize, listener cleanup, admin handshake deadline and stale callback fencing', async () => {
const b = await adminBrowser(), [serial, old] = b.sockets;
b.fire(-1); assert.equal(b.terminals[1].cols, 80); assert.equal(b.terminals[0].cols, undefined);
b.click('select-serial'); b.fire(-1); assert.equal(b.terminals[0].rows, 24);
const stale = old.onmessage;
b.click('select-admin'); b.click('admin-toggle'); b.click('admin-toggle'); await tick();
stale({data: Uint8Array.of(99).buffer}); assert.equal(b.terminals[1].writes.length, 0);
b.fire(15000); assert.ok(b.sockets[2].closed); assert.ok(!serial.closed);
assert.match(b.nodes['admin-detail'].textContent, /timed out/);
for (let i = 0; i < 3; ++i) {
b.emit('pagehide'); assert.equal(b.events.resize.length, 0);
assert.ok(b.sockets.every(s => s.onmessage === null));
b.queues['/api/session'].push(session({role: 'admin'}));
b.emit('pageshow', {persisted: true}); await tick(); assert.equal(b.events.resize.length, 1);
}
b.window.sakSessionExpired(); assert.equal(b.events.resize.length, 0);
});
await test('changed session identity replaces document before adoption, including live admin and paused restore', async () => {
const identities = [
{username: 'other-admin', role: 'admin', csrf: 'b'.repeat(64)},
{username: 'other-user', role: 'user', csrf: 'b'.repeat(64)},
{role: 'admin', csrf: 'b'.repeat(64)}, // Same account, different login session.
{username: 'other-admin', role: 'admin'}, // Principal fields are checked independently.
{role: 'user'}
];
for (const identity of identities) for (const route of ['restore', 'paused-restore', 'live', 'logout']) {
const b = await adminBrowser(), [serial, admin] = b.sockets;
serial.emit('message', {data: Uint8Array.of(65).buffer});
admin.emit('message', {data: Uint8Array.of(66).buffer});
const stale = admin.onmessage, info = b.nodes['session-info'].textContent;
const count = b.calls.length;
b.queues['/api/session'].push(session(identity));
if (route.includes('restore')) {
if (route === 'paused-restore') b.click('connection-toggle');
b.emit('pagehide');
assert.equal(b.nodes.terminal.hidden, true); assert.equal(b.nodes['admin-terminal'].hidden, true);
b.emit('pageshow', {persisted: true}); b.click('select-admin');
assert.equal(b.nodes['admin-terminal'].hidden, true);
} else if (route === 'live') {
b.click('connection-toggle'); assert.ok(!admin.closed); b.click('connection-toggle');
} else await b.click('sign-out');
await tick();
assert.deepEqual(b.redirects, ['/']); assert.ok(serial.closed && admin.closed);
assert.equal(b.nodes['session-info'].textContent, info); // B was never adopted into A's document.
assert.equal(b.nodes.terminal.hidden, true); assert.equal(b.nodes['admin-terminal'].hidden, true);
assert.deepEqual(b.calls.slice(count).map(c => c.url), ['/api/session']);
assert.equal(b.timers.size, 0); assert.equal(b.events.resize.length, 0);
stale({data: Uint8Array.of(67).buffer}); b.terminals[1].input('WRONG');
b.click('select-admin'); b.click('admin-toggle'); b.emit('pageshow', {persisted: true});
assert.deepEqual(b.terminals[1].writes, [[66]]); assert.equal(admin.sent.length, 0);
assert.equal(b.nodes['admin-terminal'].hidden, true); assert.equal(b.sockets.length, 2);
}
});
await test('same-session restore preserves both scrollbacks; pending validation never reveals them', async () => {
for (const paused of [false, true]) {
const b = await adminBrowser();
b.sockets[0].emit('message', {data: Uint8Array.of(65).buffer});
b.sockets[1].emit('message', {data: Uint8Array.of(66).buffer});
if (paused) b.click('connection-toggle');
b.emit('pagehide'); const d = deferred(); b.queues['/api/session'].push(d.promise);
b.emit('pageshow', {persisted: true}); await tick(); b.click('select-serial'); b.click('select-admin');
assert.equal(b.nodes.terminal.hidden, true); assert.equal(b.nodes['admin-terminal'].hidden, true);
d.resolve(session({role: 'admin', expires_in: 3500})); await tick();
assert.deepEqual(b.redirects, []); assert.equal(b.terminals.length, 2);
assert.deepEqual(b.terminals.map(t => t.writes), [[[65]], [[66]]]);
assert.equal(b.nodes['admin-terminal'].hidden, false); assert.equal(b.sockets.length, paused ? 2 : 3);
}
const b = await adminBrowser(), admin = b.sockets[1];
admin.emit('message', {data: Uint8Array.of(66).buffer});
b.click('connection-toggle'); b.click('connection-toggle'); await tick();
assert.ok(!admin.closed); assert.deepEqual(b.redirects, []); assert.deepEqual(b.terminals[1].writes, [[66]]);
});
await test('undefined first fit retries unchanged bounds, stops after three retries and fences stale work', async () => {
const b = await adminBrowser(), fit = b.fits[1];
fit.measurements.push(undefined); b.fire(-1);
assert.equal(b.terminals[1].cols, undefined); b.fire(-1);
assert.equal(b.terminals[1].cols, 80); assert.equal(fit.calls, 2);
b.emit('resize'); b.fire(-1); assert.equal(fit.calls, 2); // Successful measurement is cached.
b.click('select-serial'); b.click('select-admin');
fit.measurements.push(undefined, undefined, undefined, undefined);
for (let i = 0; i < 4; ++i) b.fire(-1);
assert.equal(fit.calls, 6); assert.ok(![...b.timers.values()].some(t => t.ms === -1));
b.emit('resize'); b.fire(-1); assert.equal(fit.calls, 7); // Failed bounds were never cached.
for (const action of ['pagehide', 'expiry', 'logout']) {
const c = await adminBrowser(); c.fits[1].measurements.push(undefined); c.fire(-1);
const stale = [...c.timers.values()].find(t => t.ms === -1).fn;
if (action === 'pagehide') c.emit('pagehide');
if (action === 'expiry') c.window.sakSessionExpired();
if (action === 'logout') { c.queues['/api/logout'].push(failure(403)); await c.click('sign-out'); }
await tick(); stale(); assert.equal(c.fits[1].calls, 1);
assert.ok(![...c.timers.values()].some(t => t.ms === -1));
if (action === 'pagehide') {
c.emit('pageshow', {persisted: true}); await tick(); stale();
assert.equal(c.fits[1].calls, 1); c.fire(-1); assert.equal(c.fits[1].calls, 2);
}
}
});
console.log(`PASS ${passed} browser behavior groups (production C-rendered JS)`);
})().catch(error => { console.error(error); process.exitCode = 1; });