/** Compact AX state and generation-aware diffs for BrowserBatch observations. */ export interface BrowserAxNode { id: string; parentId?: string; role?: string; name?: string; value?: string; description?: string; checked?: boolean | 'mixed'; pressed?: boolean | 'mixed'; selected?: boolean; disabled?: boolean; focused?: boolean; focusable?: boolean; expanded?: boolean; readonly?: boolean; required?: boolean; busy?: boolean; invalid?: string | boolean; valueNow?: number; valueMin?: number; valueMax?: number; valueText?: string; orientation?: string; frameId?: string; elementRef?: string; redacted?: boolean; clippedFields?: string[]; fieldLengths?: Record; ignored?: boolean; backendDOMNodeId?: number; bounds?: { x: number; y: number; width: number; height: number }; } export interface BrowserAxDiff { generation: number; pageRevision: number; full: boolean; added: BrowserAxNode[]; changed: BrowserAxNode[]; removed: string[]; } type RawAxProperty = { name?: unknown; value?: { value?: unknown } }; type RawAxNode = { nodeId?: unknown; id?: unknown; parentId?: unknown; role?: unknown; name?: unknown; value?: unknown; description?: unknown; frameId?: unknown; ignored?: unknown; backendDOMNodeId?: unknown; bounds?: { x?: unknown; y?: unknown; width?: unknown; height?: unknown }; properties?: RawAxProperty[]; }; function axValue(input: unknown): unknown { if (input && typeof input === 'object' && 'value' in input) return (input as { value?: unknown }).value; return input; } export function normalizeAxTree(raw: unknown): BrowserAxNode[] { const root = raw && typeof raw === 'object' ? (raw as { nodes?: unknown }) : {}; const nodes = Array.isArray(raw) ? raw : Array.isArray(root.nodes) ? root.nodes : []; return nodes .map((rawNode: unknown) => { const node = rawNode && typeof rawNode === 'object' ? (rawNode as RawAxNode) : {}; const bounds = node?.bounds; const properties = new Map( (node.properties ?? []).map((p) => [p.name, axValue(p.value)]), ); const states: Partial = {}; for (const key of [ 'selected', 'disabled', 'focused', 'focusable', 'expanded', 'readonly', 'required', 'busy', ] as const) { const value = properties.get(key); if (typeof value === 'boolean') states[key] = value; } for (const key of ['checked', 'pressed'] as const) { const value = properties.get(key); if (value === 'mixed' || typeof value === 'boolean') states[key] = value; else if (value === 'true' || value === 'false') states[key] = value === 'true'; } for (const [key, rawKey] of [ ['valueMin', 'valuemin'], ['valueMax', 'valuemax'], ] as const) { const value = properties.get(rawKey); if (typeof value === 'number' && Number.isFinite(value)) states[key] = value; } const value = axValue(node.value) ?? properties.get('value'); if (typeof value === 'number' && Number.isFinite(value)) states.valueNow = value; if ( typeof properties.get('invalid') === 'boolean' || typeof properties.get('invalid') === 'string' ) states.invalid = properties.get('invalid') as string | boolean; if (typeof properties.get('valuetext') === 'string') states.valueText = properties.get('valuetext') as string; if (typeof properties.get('orientation') === 'string') states.orientation = properties.get('orientation') as string; return { id: String(node.nodeId ?? node.id ?? ''), ...(node.parentId !== undefined ? { parentId: String(node.parentId) } : {}), ...(axValue(node.role) || node.role ? { role: String(axValue(node.role) ?? node.role) } : {}), ...(axValue(node.name) || node.name ? { name: String(axValue(node.name) ?? node.name) } : {}), ...(value !== undefined && value !== null ? { value: String(value) } : {}), ...(axValue(node.description) !== undefined ? { description: String(axValue(node.description)) } : {}), ...(typeof node.frameId === 'string' ? { frameId: node.frameId } : {}), ...states, ...(node.ignored !== undefined ? { ignored: Boolean(node.ignored) } : {}), ...(node.backendDOMNodeId !== undefined ? { backendDOMNodeId: Number(node.backendDOMNodeId) } : {}), ...(bounds && typeof bounds.x === 'number' && typeof bounds.y === 'number' && typeof bounds.width === 'number' && typeof bounds.height === 'number' ? { bounds: { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height, }, } : {}), } satisfies BrowserAxNode; }) .filter((node: BrowserAxNode) => node.id.length > 0); } function nodeKey(node: BrowserAxNode): string { // Eviction can issue a fresh reference while the DOM and AX node ID stay // unchanged. A diff must deliver that replacement to the consumer. return JSON.stringify(node); } export function diffAxTree( previous: Map | undefined, current: BrowserAxNode[], generation: number, pageRevision: number, forceFull = false, ): BrowserAxDiff { const next = new Map(current.map((node) => [node.id, node])); if (!previous || forceFull) { return { generation, pageRevision, full: true, added: current, changed: [], removed: [], }; } const added: BrowserAxNode[] = []; const changed: BrowserAxNode[] = []; for (const node of current) { const old = previous.get(node.id); if (!old) added.push(node); else if (nodeKey(old) !== nodeKey(node)) changed.push(node); } const removed = [...previous.keys()].filter((id) => !next.has(id)); return { generation, pageRevision, full: false, added, changed, removed }; } export function conciseAxTree(nodes: BrowserAxNode[]): string { return nodes .map((node) => { const details = [node.role, node.name, node.value] .filter(Boolean) .join(' '); const states = [ 'checked', 'selected', 'disabled', 'focused', 'expanded', 'pressed', 'busy', ] .filter((key) => node[key as keyof BrowserAxNode] !== undefined) .map((key) => `${key}=${node[key as keyof BrowserAxNode]}`) .join(' '); return `[ax=${node.id}]${details ? ` ${details}` : ''}${node.elementRef ? ` ref=${node.elementRef}` : ''}${states ? ` ${states}` : ''}${node.description ? ` description=${node.description}` : ''}${node.ignored ? ' (ignored)' : ''}`; }) .join('\n'); }