From c091ad738a41b55b9e0132bad07c890eaf1b22d4 Mon Sep 17 00:00:00 2001 From: Yurii Hulyk Date: Sat, 28 Nov 2026 18:37:33 +0000 Subject: [PATCH] test(inspection): cover locale message keys Refs #104 --- scripts/test-elements-cdp.mjs | 167 +++++++++++++++++++++++++++++++--- 1 file changed, 152 insertions(+), 15 deletions(-) diff --git a/scripts/test-elements-cdp.mjs b/scripts/test-elements-cdp.mjs index 6d199548..8b87f6d4 100644 --- a/scripts/test-elements-cdp.mjs +++ b/scripts/test-elements-cdp.mjs @@ -1,6 +1,7 @@ -// Covers: native view! text/styles/accessibility through the actual CDP adapter. +// Covers: native view! text, styles and accessibility through CDP, and localized inspection through CDP and ctl. // Real: GPUI window/GPU, private Unix control, CLI, TCP and WebSocket. Doubles: none. // Requires: macOS GUI, Node 22+, cargo build -p rake-app-cli -p rake-view --features rake/devtools. +// For localized inspection, build with rake-view/i18n-icu,rake-view/devtools and pass --locale. // This verifies live protocol behavior; it does not automate Chrome's UI. import assert from 'node:assert/strict'; import {spawn, execFileSync} from 'node:child_process'; @@ -9,13 +10,17 @@ import {fileURLToPath} from 'node:url'; import {join} from 'node:path'; import {createInterface} from 'node:readline'; import {setTimeout as delay} from 'node:timers/promises'; +import {RakeAgent} from '../crates/rake-cli/scripts/rake-agent.mjs'; assert.equal(process.platform, 'darwin', 'requires a real macOS GUI; never silently skip'); const root = fileURLToPath(new URL('../', import.meta.url)); const serve = process.argv.includes('--serve'); +const locale = process.argv.includes('--locale'); +assert.ok(!(serve && locale), 'run --locale without --serve'); const directory = mkdtempSync('/tmp/rake-elements-cdp-'), control = join(directory, 'control'); mkdirSync(control, {mode:0o700}); -const env = {...process.env, RAKE_DEV_CONTROL:'1', RAKE_CONTROL_DIR:control}; +const env = {...process.env, RAKE_DEV_CONTROL:'1', RAKE_CONTROL_DIR:control, + ...(locale ? {RAKE_DEV_PREVIEW:'1', RAKE_VIEW_LOCALE:'en'} : {})}; const cli = join(root, 'target/debug/rake'), logPath = join(directory, 'runtime.log'); const log = openSync(logPath, 'w', 0o600), children = []; let socket, intentionalClose = false; @@ -43,11 +48,133 @@ async function evaluate(expression) { function flatten(node) { return [node, ...(node.children || []).flatMap(flatten)]; } function semantic(node, id) { return node.attributes?.some((value, i, values) => i % 2 === 0 && value === 'id' && values[i+1] === id); } +function ctlInspect(...args) { + const reply = JSON.parse(execFileSync(cli, ['ctl','--session',directory,'--json','inspect',...args], + {env, timeout:4000, stdio:['ignore','pipe','pipe']})); + assert.equal(reply.type, 'elements'); + return reply.value; +} + +async function verifyLocale(client) { + const window = client.windowId; + const key = 'inbox-add'; + const query = {messageKey:key}; + const buttonQuery = {...query, role:'Button'}; + const findElements = (query, options = {}) => client.call('Rake.findElements', {query, ...options}); + const ctlByKey = (...args) => ctlInspect('message-key',key,...args); + const windowScope = id => ['--window',`window-${id}`]; + + async function inspectAdd(tag, name) { + const button = await eventually(async () => { + const reply = await findElements(buttonQuery); + assert.equal(reply.locale, tag); + assert.equal(reply.elements[0]?.name, name); + return reply; + }); + assert.equal(button.totalMatches, 1); + const [control] = button.elements; + assert.equal(control.id, key); + assert.equal(control.messageKey, key); + const all = await findElements(query); + assert.equal(all.totalMatches, 2); + const label = all.elements.find(element => element.parentId === control.nodeId); + assert.equal(label?.messageKey, key); + assert.equal(label.text, name); + assert.equal((await findElements(query, {rootNodeId:label.nodeId})).totalMatches, 1); + assert.equal((await findElements(buttonQuery, {rootNodeId:label.nodeId})).totalMatches, 0); + await assert.rejects(client.call('Rake.getActionability', {query}), + error => error.category === 'ambiguous_element'); + assert.equal((await findElements({messageKey:key.toUpperCase()})).totalMatches, 0); + const ax = await client.call('Accessibility.getFullAXTree'); + const accessible = ax.nodes.find(node => node.backendDOMNodeId === control.nodeId); + assert.equal(accessible?.messageKey, key); + assert.equal(accessible.name.value, name); + + const ctlAll = ctlByKey(...windowScope(client.windowId)); + assert.equal(ctlAll.elements.length, 2); + assert.ok(ctlAll.elements.every(element => element.accessibility.messageKey === key)); + assert.equal(ctlAll.elements.find(element => element.semantic_id === key)?.accessibility.name, name); + assert.ok(ctlAll.elements.some(element => element.text === name)); + const ctlButton = ctlByKey('--role','button',...windowScope(client.windowId)); + assert.equal(ctlButton.locale, tag); + assert.equal(ctlButton.elements.length, 1); + assert.equal(ctlButton.elements[0].semantic_id, key); + assert.equal(ctlButton.elements[0].accessibility.name, name); + return {cdp:button.revision, ctl:ctlButton.capture, stableId:control.stableId}; + } + + const english = await inspectAdd('en','Add a message'); + assert.ok(english.stableId); + assert.equal(ctlByKey().elements.length, 2); + assert.equal(ctlInspect('message-key',key.toUpperCase()).elements.length, 0); + await client.click(await client.find({id:'inbox-portuguese'})); + const portuguese = await inspectAdd('pt-PT','Adicionar uma mensagem'); + assert.notEqual(portuguese.cdp.capture, english.cdp.capture); + assert.notEqual(portuguese.ctl, english.ctl); + assert.equal(portuguese.stableId, english.stableId); + await assert.rejects(findElements(query, {expectedRevision:english.cdp}), + error => error.category === 'snapshot_changed'); + + await client.click(await client.find({id:'inbox-new-window'})); + const second = await eventually(async () => (await client.windows()).find(entry => entry.window !== window)); + await client.selectWindow(second.window); + const secondPortuguese = await inspectAdd('pt-PT','Adicionar uma mensagem'); + await client.click(await client.find({id:'inbox-english'})); + const secondEnglish = await inspectAdd('en','Add a message'); + assert.notEqual(secondEnglish.cdp.capture, secondPortuguese.cdp.capture); + assert.notEqual(secondEnglish.ctl, secondPortuguese.ctl); + assert.equal(secondEnglish.stableId, secondPortuguese.stableId); + await client.click(await client.find({id:'inbox-counter'})); + const showInputs = await eventually(async () => client.find({id:'try-inputs'})); + await client.click(showInputs); + const fields = [ + {id:'base-message',name:'Message',value:'private-message-104', + summary:value => `Message characters: ${value.length}`}, + {id:'base-password',name:'Password',value:'private-password-104', + summary:() => 'Password entered'}, + {id:'base-notes',name:'Notes',value:'private-notes-104', + summary:value => `Note characters: ${value.length} across 1 lines`}, + ]; + for (const {id, name, summary, value} of fields) { + const field = await eventually(async () => client.find({id})); + assert.equal(field.name, name); + await client.click(field); + await client.typeText(value); + await client.waitFor({text:summary(value)}); + const result = await client.find({id}); + assert.equal(result.textContent, ''); + assert.equal(result.messageKey, null); + assert.equal((await findElements({messageKey:value})).totalMatches, 0); + assert.equal(ctlInspect('message-key',value,...windowScope(second.window)).elements.length, 0); + } + assert.equal((await findElements(query)).totalMatches, 0); + assert.equal(ctlByKey(...windowScope(second.window)).elements.length, 0); + assert.equal(ctlByKey('--role','Button',...windowScope(window)).elements.length, 1); + const surfaces = { + semantic: await client.snapshot({}, {limit:256}), + DOM: await client.call('DOM.getDocument',{depth:-1}), + AX: await client.call('Accessibility.getFullAXTree'), + ctl: ctlInspect('tree'), + }; + assert.equal(surfaces.semantic.hasMore, false); + for (const {id, value} of fields) { + assert.ok(surfaces.ctl.elements.some(element => element.semantic_id === id), + `ctl tree is missing ${id}`); + for (const [surface, reply] of Object.entries(surfaces)) { + assert.ok(!JSON.stringify(reply).includes(value), `${surface} exposed the ${id} value`); + } + } + await client.selectWindow(window); + assert.equal((await findElements(query)).totalMatches, 2); + console.log('Localized inspection passed duplicate keys, scoping, translated labels, capture revisions and input privacy through CDP and ctl.'); +} + +let agent; try { children.push(spawn(join(root, 'target/debug/rake-view'), [], {cwd:root, env, stdio:['ignore', log, log]})); const capture = await eventually(() => { - const reply = JSON.parse(execFileSync(cli, ['ctl','--json','inspect','tree'], {env, timeout:4000, stdio:['ignore','pipe','ignore']})); - return reply.value?.elements.some(node => node.semantic_id === 'increment') && reply.value; + const tree = ctlInspect('tree'); + return tree.elements.some(node => node.semantic_id === (locale ? 'inbox-add' : 'increment')) && tree; }); const adapter = spawn(cli, ['cdp','--session',directory,'--no-open','--json'], {cwd:root, env, stdio:['ignore','pipe',log]}); children.push(adapter); const discovery = await new Promise((resolve, reject) => { @@ -55,6 +182,27 @@ try { lines.on('line', line => { try { const value = JSON.parse(line); if (value.event === 'cdp_listening') { clearTimeout(timer); resolve(value.url); } } catch {} }); adapter.once('error', reject); }); + if (locale) { + agent = await RakeAgent.connect(discovery); + await verifyLocale(agent); + } else { + await verifyElements(discovery, capture); + } +} catch (error) { + console.error(readFileSync(logPath,'utf8')); throw error; +} finally { + agent?.close(); + intentionalClose = true; socket?.close(); + for (const request of pending.values()) clearTimeout(request.timer); + for (const child of children.reverse()) { + if (child.exitCode !== null || child.signalCode !== null) continue; + const ended = new Promise(resolve=>child.once('exit',resolve)); child.kill('SIGTERM'); + await Promise.race([ended,delay(3000)]); if (child.exitCode === null && child.signalCode === null) { child.kill('SIGKILL'); await ended; } + } + closeSync(log); rmSync(directory,{recursive:true,force:true}); +} + +async function verifyElements(discovery, capture) { const target = await eventually(async () => (await (await fetch(discovery + '/json/list')).json())[0]); socket = new WebSocket(target.webSocketDebuggerUrl); await new Promise((resolve, reject) => { socket.addEventListener('open',resolve,{once:true}); socket.addEventListener('error',reject,{once:true}); }); @@ -154,15 +302,4 @@ try { await call('Overlay.highlightNode',{nodeId:button.nodeId}); await call('Overlay.hideHighlight'); console.log(`Native Elements/CDP passed: ${capture.elements.length} boundaries; temporary text/Unicode/empty edits and reset/undo, real relayout, stable IDs, lazy text, copy, sources, AX, style edits/toggles, stable polling, $0, focus and highlight.`); } -} catch (error) { - console.error(readFileSync(logPath,'utf8')); throw error; -} finally { - intentionalClose = true; socket?.close(); - for (const request of pending.values()) clearTimeout(request.timer); - for (const child of children.reverse()) { - if (child.exitCode !== null || child.signalCode !== null) continue; - const ended = new Promise(resolve=>child.once('exit',resolve)); child.kill('SIGTERM'); - await Promise.race([ended,delay(3000)]); if (child.exitCode === null && child.signalCode === null) { child.kill('SIGKILL'); await ended; } - } - closeSync(log); rmSync(directory,{recursive:true,force:true}); }