import type { Page } from '@playwright/test'; let _depth = 0; export function rbEnter(): void { _depth += 1; } export function rbBack(): void { _depth = Math.max(0, _depth - 1); } export function rbResetDepth(): number { _depth = 0; return _depth; } /** Snapshot visible, meaningful elements as a set of "role|name" strings. * The web analog of RecreationBench rb_showing(root). Bounded + discriminating: * only landmarks, headings, links, buttons, [role], and dialogs. */ export async function rbShowing(page: Page): Promise> { const items: string[] = await page.evaluate(() => { const SEL = 'h1,h2,h3,h4,h5,h6,a[href],button,[role],nav,main,header,footer,aside,dialog,[aria-modal="true"]'; const out: string[] = []; const seen = new Set(); const nodes = Array.from(document.querySelectorAll(SEL)).slice(0, 4000); for (const el of nodes) { const r = (el as HTMLElement).getBoundingClientRect(); const s = getComputedStyle(el as HTMLElement); const visible = r.width > 0 && r.height > 0 && s.visibility !== 'hidden' && s.display !== 'none' && parseFloat(s.opacity || '1') > 0.01; if (!visible) continue; const role = el.getAttribute('role') || el.tagName.toLowerCase(); const name = (el.getAttribute('aria-label') || (el as HTMLElement).innerText || el.textContent || '') .replace(/\s+/g, ' ').trim().slice(0, 80); const key = `${role}|${name}`; if (!seen.has(key)) { seen.add(key); out.push(key); } } return out; }); return new Set(items); } /** True-jump verdict: target's discriminating element newly APPEARED and * (if source given) source's element DISAPPEARED. Verbatim port of rb_reach. */ export function rbReach( before: Iterable, after: Iterable, target: string, source?: string, ): boolean { const b = new Set(before), a = new Set(after); const t = target.toLowerCase(); const appeared = [...a].some(k => !b.has(k) && k.toLowerCase().includes(t)); if (source === undefined) return appeared; const sName = source.toLowerCase(); const srcBefore = [...b].some(k => k.toLowerCase().includes(sName)); const srcAfter = [...a].some(k => k.toLowerCase().includes(sName)); return appeared && srcBefore && !srcAfter; }