// Profile editing/persistence and cross-origin browser switching, loopback only. // node tool/server_profiles_probe.mjs import assert from 'node:assert/strict'; import { mkdir, mkdtemp, writeFile } from 'node:fs/promises'; import { once } from 'node:events'; import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; import { startFixture } from './runs_probe_server.mjs'; import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs'; import { axNodes, enableSemantics, waitFor, capture, assertName, clickAt } from './pipelines_probe_cdp_input.mjs'; const root = resolve(process.argv[2] ?? 'build/server_profiles_web'); const output = resolve(process.argv[3] ?? 'build/server-profiles-evidence'); await mkdir(output, { recursive: true }); const home = await startFixture({ root }); const backup = await startFixture({ root }); const other = await startFixture({ root }); const ws = url => url.replace('http:', 'ws:') + '/connect'; const profile = await mkdtemp(join(tmpdir(), 'server-profiles-')); const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank'); const log = { checks: [], captures: [] }; let c; try { c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl); await c.send('Page.enable'); await c.send('Emulation.setUserAgentOverride', { userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36', platform: 'Win32', }); await c.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true }); const semantics = async () => { assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 12000)); await enableSemantics(c); }; const click = async label => { for (let step = 0; step < 6; step++) { const target = (await axNodes(c)).find(node => node.name?.value === label && node.backendDOMNodeId && ['button', 'tab', 'link'].includes(node.role?.value)); if (target) { const { model } = await c.send('DOM.getBoxModel', { backendNodeId: target.backendDOMNodeId }); const [x1, y1, , , x3, y3] = model.content; if (y1 >= 0 && y3 <= 844) return clickAt(c, (x1 + x3) / 2, (y1 + y3) / 2); } await c.send('Input.dispatchMouseEvent', { type: 'mouseWheel', x: 195, y: 500, deltaX: 0, deltaY: 350 }); await sleep(200); } throw new Error(`Control not found/in view: ${label}`); }; const type = async (index, text) => { const node = (await axNodes(c)).filter(node => node.role?.value === 'textbox')[index]; assert.ok(node, `text field ${index}`); const { model } = await c.send('DOM.getBoxModel', { backendNodeId: node.backendDOMNodeId }); const [x1, y1, , , x3, y3] = model.content; await clickAt(c, (x1 + x3) / 2, (y1 + y3) / 2); await c.send('Input.dispatchKeyEvent', { type: 'keyDown', key: 'a', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 4, commands: ['selectAll'] }); await c.send('Input.dispatchKeyEvent', { type: 'keyUp', key: 'a', code: 'KeyA', windowsVirtualKeyCode: 65, modifiers: 4 }); await c.send('Input.insertText', { text }); await sleep(300); }; await c.send('Page.navigate', { url: home.url + '/settings' }); await semantics(); await assertName(c, 'My server', 'existing server migrated into profiles', log); await click('Edit My server'); await type(0, 'Home'); await type(1, `${ws(home.url)}\n${ws(backup.url)}`); await capture(c, output, log, 'edit-home-addresses'); await click('Save server'); await assertName(c, `Backup 1: ${ws(backup.url)}`, 'ordered backup address saved', log); await capture(c, output, log, 'home-with-backup'); await click('Add server'); await type(0, 'Other'); await type(1, ws(other.url)); await click('Save server'); await assertName(c, 'Use Other', 'adding another server does not switch', log); assert.equal(await c.evaluate('location.origin'), home.url); await capture(c, output, log, 'two-saved-servers'); await c.send('Page.reload'); await semantics(); await assertName(c, `Backup 1: ${ws(backup.url)}`, 'aliases persist after reload', log); await assertName(c, 'Use Other', 'second server persists after reload', log); await click('Use Other'); assert.ok(await waitFor(async () => await c.evaluate('location.origin') === other.url, 12000)); log.checks.push('browser server switch navigates origin, never crosses socket credentials'); await semantics(); await c.evaluate('history.back()'); assert.ok(await waitFor(async () => await c.evaluate('location.origin') === home.url, 12000)); await semantics(); await assertName(c, 'Use Other', 'browser Back restores actual Home selection', log); assert.ok(!(await axNodes(c)).some(node => node.name?.value === 'Use Home')); await capture(c, output, log, 'back-restores-home-selection'); await click('Remove Other'); await click('Remove'); await sleep(300); assert.ok(!(await axNodes(c)).some(node => node.name?.value === 'Use Other')); log.checks.push('remove configuration leaves selected server intact'); await capture(c, output, log, 'other-removed'); console.log(JSON.stringify(log, null, 2)); } catch (error) { if (c) { await capture(c, output, log, 'failure'); await writeFile(join(output, 'failure-ax.json'), JSON.stringify(await axNodes(c), null, 2)); await writeFile(join(output, 'failure-storage.json'), JSON.stringify(await c.evaluate('Object.entries(localStorage)'), null, 2)); } throw error; } finally { await writeFile(join(output, 'result.json'), JSON.stringify(log, null, 2)); c?.close(); const exited = once(chrome, 'exit'); chrome.kill(); await exited; await Promise.all([home.close(), backup.close(), other.close()]); }