High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: recovery intent cache preservation, dashboard proxy routes, Windows process detection
Yash Jadhav361f24162270
diff --git a/.gitignore b/.gitignoreindex 9dc30d64..46d22efd 100644--- a/.gitignore+++ b/.gitignore@@ -32,3 +32,8 @@ AGENTS.md*.testmemory/pinchtab-small++# Local development files+MY_PROJECT_NOTES.md+debug.log+pinchtab.exediff --git a/cmd/pinchtab/cmd_dashboard.go b/cmd/pinchtab/cmd_dashboard.goindex d6978598..03186bdc 100644--- a/cmd/pinchtab/cmd_dashboard.go+++ b/cmd/pinchtab/cmd_dashboard.go@@ -95,6 +95,7 @@ func runDashboard(cfg *config.RuntimeConfig) {"GET /download", "POST /upload","GET /stealth/status", "POST /fingerprint/rotate","GET /screencast", "GET /screencast/tabs",+ "POST /find", "POST /macro",}for _, ep := range proxyEndpoints {endpoint := epdiff --git a/internal/handlers/actions.go b/internal/handlers/actions.goindex 14f48898..7301c161 100644--- a/internal/handlers/actions.go+++ b/internal/handlers/actions.go@@ -126,6 +126,7 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {go web.CancelOnClientDone(r.Context(), tCancel)// Resolve ref → nodeID+ refMissing := falseif req.Ref != "" && req.NodeID == 0 && req.Selector == "" {cache := h.Bridge.GetRefCache(resolvedTabID)if cache != nil {@@ -134,34 +135,27 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {}}if req.NodeID == 0 {- web.Error(w, 404, fmt.Errorf("ref %s not found - take a /snapshot first", req.Ref))- return+ refMissing = true}}// Cache intent before execution so recovery can reconstruct the query.- if req.Ref != "" && h.Recovery != nil {+ // Only cache when the ref IS in the snapshot — otherwise we'd overwrite+ // the richer /find-cached entry (which has the Query) with a blank one.+ if req.Ref != "" && h.Recovery != nil && !refMissing {h.cacheActionIntent(resolvedTabID, req)}- result, err := h.Bridge.ExecuteAction(tCtx, req.Kind, req)- if err != nil && req.Ref != "" && shouldRetryStaleRef(err) {- recordStaleRefRetry()- h.refreshRefCache(tCtx, resolvedTabID)- if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {- if nid, ok := cache.Refs[req.Ref]; ok {- req.NodeID = nid- result, err = h.Bridge.ExecuteAction(tCtx, req.Kind, req)- }- }- }- // Semantic self-healing: if stale-ref retry still failed, attempt- // recovery via the semantic matcher.+ // If ref was not in snapshot cache, attempt semantic recovery before+ // returning 404. This handles the common case where a page reload+ // cleared the snapshot (DeleteRefCache) but the intent is still cached.+ var result map[string]any+ var actionErr errorvar recoveryResult *semantic.RecoveryResult- if err != nil && req.Ref != "" && h.Recovery != nil && h.Recovery.ShouldAttempt(err, req.Ref) {- rr, actionRes, recoveryErr := h.Recovery.AttemptWithClassification(++ if refMissing && req.Ref != "" && h.Recovery != nil {+ rr, actionRes, recoveryErr := h.Recovery.Attempt(tCtx, resolvedTabID, req.Ref, req.Kind,- semantic.ClassifyFailure(err),func(ctx context.Context, kind string, nodeID int64) (map[string]any, error) {req.NodeID = nodeIDreturn h.Bridge.ExecuteAction(ctx, kind, req)@@ -170,18 +164,51 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {recoveryResult = &rrif recoveryErr == nil {result = actionRes- err = nil+ } else {+ actionErr = fmt.Errorf("ref %s not found and recovery failed: %w", req.Ref, recoveryErr)+ }+ } else if refMissing {+ web.Error(w, 404, fmt.Errorf("ref %s not found - take a /snapshot first", req.Ref))+ return+ } else {+ result, actionErr = h.Bridge.ExecuteAction(tCtx, req.Kind, req)+ if actionErr != nil && req.Ref != "" && shouldRetryStaleRef(actionErr) {+ recordStaleRefRetry()+ h.refreshRefCache(tCtx, resolvedTabID)+ if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {+ if nid, ok := cache.Refs[req.Ref]; ok {+ req.NodeID = nid+ result, actionErr = h.Bridge.ExecuteAction(tCtx, req.Kind, req)+ }+ }+ }+ // Semantic self-healing: if stale-ref retry still failed, attempt+ // recovery via the semantic matcher.+ if actionErr != nil && req.Ref != "" && h.Recovery != nil && h.Recovery.ShouldAttempt(actionErr, req.Ref) {+ rr, actionRes, recoveryErr := h.Recovery.AttemptWithClassification(+ tCtx, resolvedTabID, req.Ref, req.Kind,+ semantic.ClassifyFailure(actionErr),+ func(ctx context.Context, kind string, nodeID int64) (map[string]any, error) {+ req.NodeID = nodeID+ return h.Bridge.ExecuteAction(ctx, kind, req)+ },+ )+ recoveryResult = &rr+ if recoveryErr == nil {+ result = actionRes+ actionErr = nil+ }}}- if err != nil {- if strings.HasPrefix(err.Error(), "unknown action") {+ if actionErr != nil {+ if strings.HasPrefix(actionErr.Error(), "unknown action") {kinds := h.Bridge.AvailableActions()web.JSON(w, 400, map[string]string{- "error": fmt.Sprintf("%s - valid values: %s", err.Error(), strings.Join(kinds, ", ")),+ "error": fmt.Sprintf("%s - valid values: %s", actionErr.Error(), strings.Join(kinds, ", ")),})return}- web.ErrorCode(w, 500, "action_failed", fmt.Sprintf("action %s: %v", req.Kind, err), true, nil)+ web.ErrorCode(w, 500, "action_failed", fmt.Sprintf("action %s: %v", req.Kind, actionErr), true, nil)return}@@ -346,19 +373,10 @@ func (h *Handlers) handleActionsBatch(w http.ResponseWriter, r *http.Request, reaction.NodeID = nid}}- if action.NodeID == 0 {- tCancel()- results = append(results, actionResult{- Index: i, Success: false,- Error: fmt.Sprintf("ref %s not found - take a /snapshot first", action.Ref),- })- if req.StopOnError {- break- }- continue- }}+ refMissing := action.Ref != "" && action.NodeID == 0 && action.Selector == ""+if action.Kind == "" {tCancel()results = append(results, actionResult{@@ -371,35 +389,68 @@ func (h *Handlers) handleActionsBatch(w http.ResponseWriter, r *http.Request, re}// Cache intent before execution so recovery can reconstruct the query.- if action.Ref != "" && h.Recovery != nil {+ // Only cache when the ref IS in the snapshot to avoid overwriting+ // the richer /find-cached entry (which has the Query).+ if action.Ref != "" && h.Recovery != nil && !refMissing {h.cacheActionIntent(resolvedTabID, action)}- actionRes, err := h.Bridge.ExecuteAction(tCtx, action.Kind, action)- if err != nil && action.Ref != "" && shouldRetryStaleRef(err) {- recordStaleRefRetry()- h.refreshRefCache(tCtx, resolvedTabID)- if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {- if nid, ok := cache.Refs[action.Ref]; ok {- action.NodeID = nid- actionRes, err = h.Bridge.ExecuteAction(tCtx, action.Kind, action)- }- }- }- // Semantic self-healing for batched actions.- if err != nil && action.Ref != "" && h.Recovery != nil && h.Recovery.ShouldAttempt(err, action.Ref) {- rr, recRes, recErr := h.Recovery.AttemptWithClassification(+ var actionRes map[string]any+ var err error++ if refMissing && h.Recovery != nil {+ // Ref not in snapshot cache but we may have a cached intent —+ // attempt semantic recovery (refresh snapshot + re-match).+ rr, recRes, recErr := h.Recovery.Attempt(tCtx, resolvedTabID, action.Ref, action.Kind,- semantic.ClassifyFailure(err),func(ctx context.Context, kind string, nodeID int64) (map[string]any, error) {action.NodeID = nodeIDreturn h.Bridge.ExecuteAction(ctx, kind, action)},)- _ = rr // recovery metadata not surfaced per-action in batch+ _ = rrif recErr == nil {actionRes = recRes- err = nil+ } else {+ err = fmt.Errorf("ref %s not found and recovery failed: %w", action.Ref, recErr)+ }+ } else if refMissing {+ tCancel()+ results = append(results, actionResult{+ Index: i, Success: false,+ Error: fmt.Sprintf("ref %s not found - take a /snapshot first", action.Ref),+ })+ if req.StopOnError {+ break+ }+ continue+ } else {+ actionRes, err = h.Bridge.ExecuteAction(tCtx, action.Kind, action)+ if err != nil && action.Ref != "" && shouldRetryStaleRef(err) {+ recordStaleRefRetry()+ h.refreshRefCache(tCtx, resolvedTabID)+ if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {+ if nid, ok := cache.Refs[action.Ref]; ok {+ action.NodeID = nid+ actionRes, err = h.Bridge.ExecuteAction(tCtx, action.Kind, action)+ }+ }+ }+ // Semantic self-healing for batched actions.+ if err != nil && action.Ref != "" && h.Recovery != nil && h.Recovery.ShouldAttempt(err, action.Ref) {+ rr, recRes, recErr := h.Recovery.AttemptWithClassification(+ tCtx, resolvedTabID, action.Ref, action.Kind,+ semantic.ClassifyFailure(err),+ func(ctx context.Context, kind string, nodeID int64) (map[string]any, error) {+ action.NodeID = nodeID+ return h.Bridge.ExecuteAction(ctx, kind, action)+ },+ )+ _ = rr // recovery metadata not surfaced per-action in batch+ if recErr == nil {+ actionRes = recRes+ err = nil+ }}}tCancel()@@ -468,28 +519,36 @@ func (h *Handlers) HandleMacro(w http.ResponseWriter, r *http.Request) {if step.TabID == "" {step.TabID = resolvedTabID}- // Cache intent before execution so recovery can reconstruct the query.- if step.Ref != "" && h.Recovery != nil {- h.cacheActionIntent(resolvedTabID, step)- }-- tCtx, cancel := context.WithTimeout(ctx, stepTimeout)- res, err := h.Bridge.ExecuteAction(tCtx, step.Kind, step)- if err != nil && step.Ref != "" && shouldRetryStaleRef(err) {- recordStaleRefRetry()- h.refreshRefCache(tCtx, resolvedTabID)- if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {+ // Resolve ref → nodeID from snapshot cache (mirrors HandleAction).+ stepRefMissing := false+ if step.Ref != "" && step.NodeID == 0 && step.Selector == "" {+ cache := h.Bridge.GetRefCache(resolvedTabID)+ if cache != nil {if nid, ok := cache.Refs[step.Ref]; ok {step.NodeID = nid- res, err = h.Bridge.ExecuteAction(tCtx, step.Kind, step)}}+ if step.NodeID == 0 {+ stepRefMissing = true+ }}- // Semantic self-healing for macro steps.- if err != nil && step.Ref != "" && h.Recovery != nil && h.Recovery.ShouldAttempt(err, step.Ref) {- rr, recRes, recErr := h.Recovery.AttemptWithClassification(++ // Cache intent before execution so recovery can reconstruct the query.+ // Only cache when the ref IS in the snapshot to avoid overwriting+ // the richer /find-cached entry (which has the Query).+ if step.Ref != "" && h.Recovery != nil && !stepRefMissing {+ h.cacheActionIntent(resolvedTabID, step)+ }++ tCtx, cancel := context.WithTimeout(ctx, stepTimeout)++ var res map[string]any+ var err error++ if stepRefMissing && h.Recovery != nil {+ // Ref not in snapshot cache — attempt semantic recovery.+ rr, recRes, recErr := h.Recovery.Attempt(tCtx, resolvedTabID, step.Ref, step.Kind,- semantic.ClassifyFailure(err),func(ctx context.Context, kind string, nodeID int64) (map[string]any, error) {step.NodeID = nodeIDreturn h.Bridge.ExecuteAction(ctx, kind, step)@@ -498,7 +557,46 @@ func (h *Handlers) HandleMacro(w http.ResponseWriter, r *http.Request) {_ = rrif recErr == nil {res = recRes- err = nil+ } else {+ err = fmt.Errorf("ref %s not found and recovery failed: %w", step.Ref, recErr)+ }+ } else if stepRefMissing {+ cancel()+ results = append(results, actionResult{+ Index: i, Success: false,+ Error: fmt.Sprintf("ref %s not found - take a /snapshot first", step.Ref),+ })+ if req.StopOnError {+ break+ }+ continue+ } else {+ res, err = h.Bridge.ExecuteAction(tCtx, step.Kind, step)+ if err != nil && step.Ref != "" && shouldRetryStaleRef(err) {+ recordStaleRefRetry()+ h.refreshRefCache(tCtx, resolvedTabID)+ if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {+ if nid, ok := cache.Refs[step.Ref]; ok {+ step.NodeID = nid+ res, err = h.Bridge.ExecuteAction(tCtx, step.Kind, step)+ }+ }+ }+ // Semantic self-healing for macro steps.+ if err != nil && step.Ref != "" && h.Recovery != nil && h.Recovery.ShouldAttempt(err, step.Ref) {+ rr, recRes, recErr := h.Recovery.AttemptWithClassification(+ tCtx, resolvedTabID, step.Ref, step.Kind,+ semantic.ClassifyFailure(err),+ func(ctx context.Context, kind string, nodeID int64) (map[string]any, error) {+ step.NodeID = nodeID+ return h.Bridge.ExecuteAction(ctx, kind, step)+ },+ )+ _ = rr+ if recErr == nil {+ res = recRes+ err = nil+ }}}cancel()@@ -538,6 +636,11 @@ func (h *Handlers) cacheActionIntent(tabID string, req bridge.ActionRequest) {if h.Recovery == nil || req.Ref == "" {return}+ // Don't overwrite an existing entry that has a real Query (from /find)+ // with a descriptor-only entry.+ if existing, ok := h.Recovery.IntentCache.Lookup(tabID, req.Ref); ok && existing.Query != "" {+ return+ }desc := semantic.ElementDescriptor{Ref: req.Ref}// Try to enrich from the current snapshot cache.if cache := h.Bridge.GetRefCache(tabID); cache != nil {diff --git a/internal/orchestrator/process_windows.go b/internal/orchestrator/process_windows.goindex d95896c6..1b6f2be0 100644--- a/internal/orchestrator/process_windows.go+++ b/internal/orchestrator/process_windows.go@@ -6,6 +6,7 @@ import ("os""os/exec""syscall"+ "unsafe")func setProcGroup(cmd *exec.Cmd) {}@@ -18,14 +19,41 @@ func killProcessGroup(pid int, _ syscall.Signal) error {return p.Kill()}+var (+ modkernel32 = syscall.NewLazyDLL("kernel32.dll")+ procOpenProcess = modkernel32.NewProc("OpenProcess")+ procGetExitCodeProcess = modkernel32.NewProc("GetExitCodeProcess")+)++const (+ processQueryLimitedInfo = 0x1000+ stillActive = 259 // STATUS_PENDING — process has not exited+)+func processAlive(pid int) bool {- p, err := os.FindProcess(pid)- if err != nil {+ // On Windows, os.Process.Signal(0) always fails because Go does not+ // support signals on Windows (only os.Kill via TerminateProcess).+ // Instead, use OpenProcess + GetExitCodeProcess to check liveness.+ handle, _, err := procOpenProcess.Call(+ uintptr(processQueryLimitedInfo),+ 0, // bInheritHandle = false+ uintptr(pid),+ )+ if handle == 0 {+ // Cannot open process — either it doesn't exist or access denied.+ // If access denied the process is alive, but we can't tell; assume dead+ // to match the previous (broken) behaviour conservatively.+ _ = errreturn false}- // On Windows, FindProcess always succeeds; we try to signal 0 equivalent.- err = p.Signal(syscall.Signal(0))- return err == nil+ defer func() { _ = syscall.CloseHandle(syscall.Handle(handle)) }()++ var exitCode uint32+ ret, _, _ := procGetExitCodeProcess.Call(handle, uintptr(unsafe.Pointer(&exitCode)))+ if ret == 0 {+ return false // API call failed+ }+ return exitCode == stillActive}const sigTERM = syscall.Signal(0xf)diff --git a/internal/semantic/combined_matcher.go b/internal/semantic/combined_matcher.goindex b58e4ade..4a7e915e 100644--- a/internal/semantic/combined_matcher.go+++ b/internal/semantic/combined_matcher.go@@ -145,8 +145,8 @@ func (c *CombinedMatcher) Find(ctx context.Context, query string, elements []Eleel: refToElem[ref],}if opts.Explain {- s.lexScore = ls- s.embScore = es+ s.lexScore = lexW * ls+ s.embScore = embW * es}candidates = append(candidates, s)}diff --git a/tests/manual/test-recovery.ps1 b/tests/manual/test-recovery.ps1new file mode 100644index 00000000..985856eb--- /dev/null+++ b/tests/manual/test-recovery.ps1@@ -0,0 +1,879 @@+# =============================================================================+# PinchTab - Self-Healing Recovery System Manual Test Suite+# =============================================================================+# Tests: /find enhancements + self-healing recovery engine (PR #2)+#+# What is tested:+# - POST /find : TrimSpace, per-request weights, explain field+# - POST /action: recovery field in response, stale-ref self-healing+# - POST /actions: batch recovery+# - POST /macro : macro-step recovery+# - Failure classification edge cases via known error patterns+# - Real-world recovery scenarios on live websites+#+# How recovery is triggered in these tests:+# 1. Navigate to a page - Chrome assigns fresh nodeIDs+# 2. POST /find - intent is cached in the RecoveryEngine+# 3. Navigate to the SAME page (reload) - Chrome rebuilds the DOM;+# all nodeIDs are now different, but our snapshot still holds+# the OLD refs -> nodeID mapping+# 4. POST /action with the old ref - ExecuteAction fails with+# "could not find node" since the nodeID is stale -> recovery+# re-matches semantically and re-executes -> response.recovery.recovered = true+#+# Prerequisites:+# - PinchTab server running on $BASE (default: http://localhost:9867)+# - jq.exe on PATH OR PowerShell 7+ (ConvertFrom-Json used instead)+# - Internet access for real-site tests+#+# Usage:+# # Default (localhost:9867)+# .\tests\manual\test-recovery.ps1+#+# # Custom port+# .\tests\manual\test-recovery.ps1 -Port 9868+#+# # Skip slow website tests+# .\tests\manual\test-recovery.ps1 -SkipRealSites+#+# # Stop on first failure+# .\tests\manual\test-recovery.ps1 -FailFast+# =============================================================================++param(+ [int] $Port = 9867,+ [switch]$SkipRealSites,+ [switch]$FailFast,+ [switch]$Verbose+)++$BASE = "http://localhost:$Port"+$PASS = 0+$FAIL = 0+$SKIP = 0+$TAB = $null # active tab ID, set after each navigate++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------+function Write-Header($text) {+ Write-Host ""+ Write-Host ("=" * 72) -ForegroundColor Cyan+ Write-Host " $text" -ForegroundColor Cyan+ Write-Host ("=" * 72) -ForegroundColor Cyan+}++function Write-Section($text) {+ Write-Host ""+ Write-Host "--- $text ---" -ForegroundColor Yellow+}++function Pass($name) {+ $script:PASS+++ Write-Host " [PASS] $name" -ForegroundColor Green+}++function Fail($name, $detail = "") {+ $script:FAIL+++ Write-Host " [FAIL] $name" -ForegroundColor Red+ if ($detail) { Write-Host " $detail" -ForegroundColor DarkRed }+ if ($FailFast) { throw "FailFast: $name" }+}++function Skip($name, $reason = "") {+ $script:SKIP+++ $suffix = if ($reason) { " ($reason)" } else { "" }+ Write-Host " [SKIP] $name$suffix" -ForegroundColor DarkYellow+}++function Invoke-Api($method, $path, $body = $null, $quiet = $false) {+ $uri = "$BASE$path"+ $params = @{ Method = $method; Uri = $uri; ContentType = "application/json"; UseBasicParsing = $true }+ if ($body) { $params.Body = ($body | ConvertTo-Json -Depth 10) }+ try {+ $resp = Invoke-WebRequest @params -ErrorAction Stop+ return ($resp.Content | ConvertFrom-Json)+ } catch {+ $code = $_.Exception.Response.StatusCode.value__+ try {+ $errBody = $_.ErrorDetails.Message | ConvertFrom-Json+ if (-not $quiet) { Write-Host " HTTP $code - $($errBody.error)" -ForegroundColor DarkGray }+ $errBody | Add-Member -NotePropertyName "__status" -NotePropertyValue $code+ return $errBody+ } catch {+ if (-not $quiet) { Write-Host " HTTP $code - (no JSON body)" -ForegroundColor DarkGray }+ $e = [pscustomobject]@{ __status = $code; error = $_.Exception.Message }+ return $e+ }+ }+}++function Navigate($url, $sleepSec = 3) {+ $r = Invoke-Api POST "/navigate" @{ url = $url }+ if ($r.tabId) {+ $script:TAB = $r.tabId+ if ($Verbose) { Write-Host " Navigated -> tabId=$($r.tabId)" -ForegroundColor DarkGray }+ }+ Start-Sleep -Seconds $sleepSec+ return $r+}++function Find-Element($query, $extra = @{}) {+ $body = @{ query = $query; tabId = $TAB } + $extra+ return Invoke-Api POST "/find" $body+}++function Do-Action($ref, $kind = "click", $extra = @{}) {+ $body = @{ ref = $ref; kind = $kind; tabId = $TAB } + $extra+ return Invoke-Api POST "/action" $body+}++# ---------------------------------------------------------------------------+# Preflight+# ---------------------------------------------------------------------------+Write-Header "PinchTab Recovery System - Manual Test Suite"+Write-Host " Server : $BASE"+Write-Host " Date : $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"++$health = Invoke-Api GET "/health" -quiet $true+if (-not $health -or $health.__status -ge 400) {+ Write-Host ""+ Write-Host " ERROR: Server not reachable at $BASE - start PinchTab first." -ForegroundColor Red+ exit 1+}+Write-Host " Health : OK" -ForegroundColor Green++# ---------------------------------------------------------------------------+# Detect whether a bridge instance is running (dashboard proxies need one)+# ---------------------------------------------------------------------------+$HAS_INSTANCE = $false+try {+ $instances = Invoke-RestMethod -Uri "$BASE/instances" -UseBasicParsing -ErrorAction SilentlyContinue+ if ($instances -and $instances.Count -gt 0) {+ $running = $instances | Where-Object { $_.status -eq "running" }+ if ($running) { $HAS_INSTANCE = $true }+ }+} catch {}+if ($HAS_INSTANCE) {+ Write-Host " Instance: running (bridge proxy active)" -ForegroundColor Green+} else {+ Write-Host " Instance: none running (Sections 1-2 require a running instance for validation tests)" -ForegroundColor DarkYellow+}++# =============================================================================+# SECTION 1 - /find endpoint enhancements (requires running instance)+# =============================================================================+Write-Header "Section 1: /find Endpoint Enhancements"++if (-not $HAS_INSTANCE) {+ Write-Host " No running instance - /find and /macro validation tests require a running bridge instance." -ForegroundColor DarkYellow+ Write-Host " Launch a profile from the dashboard, then re-run this test." -ForegroundColor DarkYellow+ Skip "Empty query returns 400" "no running instance"+ Skip "Whitespace-only query returns 400" "no running instance"+ Skip "Tab/newline-only query returns 400" "no running instance"+ Skip "Missing query field returns 400" "no running instance"+ Skip "Unknown tabId returns 404" "no running instance"+ Skip "Malformed JSON body returns 400" "no running instance"+ Skip "Non-string query returns 400" "no running instance"+ Skip "Negative threshold clamped to 0.3" "no running instance"+ Skip "Zero topK clamped to 3" "no running instance"+} else {++# 1.1 Empty query -> 400+Write-Section "1.1 Input Validation"+$r = Invoke-Api POST "/find" @{ query = "" } -quiet $true+if ($r.__status -eq 400) { Pass "Empty query returns 400" }+else { Fail "Empty query should return 400" "got $($r.__status)" }++# 1.2 Whitespace-only query -> 400 (TrimSpace fix)+$r = Invoke-Api POST "/find" @{ query = " " } -quiet $true+if ($r.__status -eq 400) { Pass "Whitespace-only query returns 400 (TrimSpace)" }+else { Fail "Whitespace-only query should return 400" "got $($r.__status)" }++# 1.3 Tab-and-newline whitespace -> 400+$r = Invoke-Api POST "/find" @{ query = "`t`n `r" } -quiet $true+if ($r.__status -eq 400) { Pass "Tab/newline-only query returns 400 (TrimSpace)" }+else { Fail "Tab/newline-only query should return 400" "got $($r.__status)" }++# 1.4 Missing query field entirely -> 400+$r = Invoke-Api POST "/find" @{ tabId = "someTab" } -quiet $true+if ($r.__status -eq 400) { Pass "Missing query field returns 400" }+else { Fail "Missing query field should return 400" "got $($r.__status)" }++# 1.5 Unknown tabId -> 404+$r = Invoke-Api POST "/find" @{ query = "submit button"; tabId = "tab_doesnotexist" } -quiet $true+if ($r.__status -eq 404) { Pass "Unknown tabId returns 404" }+else { Fail "Unknown tabId should return 404" "got $($r.__status)" }++# 1.6 Bad JSON body -> 400+Write-Section "1.2 Bad Request Bodies"+try {+ $uri = "$BASE/find"+ $resp = Invoke-WebRequest -Method POST -Uri $uri -ContentType "application/json" -Body "{ bad json }" -UseBasicParsing -ErrorAction Stop+ Fail "Malformed JSON should return 400" "got $($resp.StatusCode)"+} catch {+ $code = $_.Exception.Response.StatusCode.value__+ if ($code -eq 400) { Pass "Malformed JSON body returns 400" }+ else { Fail "Malformed JSON should return 400" "got $code" }+}++# 1.7 Integer passed for query field -> 400+try {+ $uri = "$BASE/find"+ $resp = Invoke-WebRequest -Method POST -Uri $uri -ContentType "application/json" -Body '{"query":12345}' -UseBasicParsing -ErrorAction Stop+ Fail "Non-string query should not succeed silently"+} catch {+ $code = $_.Exception.Response.StatusCode.value__+ if ($code -eq 400) { Pass "Non-string query returns 400" }+ else { Fail "Non-string query should return 400" "got $code" }+}++# 1.8 Negative threshold -> treated as default (0.3), not an error+Write-Section "1.3 Threshold & TopK Boundary Values"+# These are tested on live pages in Section 3 (Google).+# Here we just verify the server doesn't reject them.+$r = Invoke-Api POST "/find" @{ query = "test"; threshold = -5; topK = 0 } -quiet $true+if (-not $r.__status -or $r.__status -lt 500) { Pass "Negative threshold + zero topK accepted without server error" }+else { Fail "Negative threshold/zero topK should not cause 500" "got $($r.__status)" }++} # end if ($HAS_INSTANCE) for Section 1++# =============================================================================+# SECTION 2 - Recovery System: Edge Cases (requires running instance)+# =============================================================================+Write-Header "Section 2: Recovery Edge Cases"++if (-not $HAS_INSTANCE) {+ Write-Host " No running instance - action/batch/macro validation tests require a running bridge instance." -ForegroundColor DarkYellow+ Skip "Unknown ref with unknown tab returns 404" "no running instance"+ Skip "Missing kind returns 4xx" "no running instance"+ Skip "Empty actions array returns 400" "no running instance"+ Skip "Empty macro steps returns 400" "no running instance"+ Skip "All actions with unknown tab returns 404" "no running instance"+} else {++Write-Section "2.1 Action with unknown ref - triggers 404 before recovery"+# When ref is not in snapshot cache at all, system returns 404 immediately+# (recovery is only attempted when CDP execution fails, not on cache miss)+$r = Invoke-Api POST "/action" @{ kind = "click"; ref = "e99999"; tabId = "tab_fake" } -quiet $true+if ($r.__status -eq 404) { Pass "Unknown ref with unknown tab returns 404" }+else { Fail "Unknown ref + unknown tab should return 404" "got $($r.__status): $($r.error)" }++Write-Section "2.2 Action missing kind -> 400"+$r = Invoke-Api POST "/action" @{ ref = "e1" } -quiet $true+if ($r.__status -ge 400 -and $r.__status -lt 500) { Pass "Missing kind returns 4xx" }+else { Fail "Missing kind should return 4xx" "got $($r.__status)" }++Write-Section "2.3 Batch actions - empty array -> 400"+$r = Invoke-Api POST "/actions" @{ actions = @() } -quiet $true+if ($r.__status -eq 400) { Pass "Empty actions array returns 400" }+else { Fail "Empty actions array should return 400" "got $($r.__status)" }++Write-Section "2.4 Macro - empty steps -> 400"+$r = Invoke-Api POST "/macro" @{ steps = @() } -quiet $true+if ($r.__status -eq 400) { Pass "Empty macro steps returns 400" }+else { Fail "Empty macro steps should return 400" "got $($r.__status)" }++Write-Section "2.5 Batch actions - unknown tabId for all actions"+$r = Invoke-Api POST "/actions" @{+ tabId = "tab_ghost"+ actions = @(+ @{ kind = "click"; ref = "e1" },+ @{ kind = "click"; ref = "e2" }+ )+} -quiet $true+if ($r.__status -eq 404) { Pass "All actions with unknown tab returns 404" }+else { Fail "Unknown tab for batch should return 404" "got $($r.__status)" }++} # end if ($HAS_INSTANCE) for Section 2++# =============================================================================+# SECTION 3 - Real Websites (requires Chrome + internet)+# =============================================================================+if ($SkipRealSites) {+ Write-Header "Section 3-8: Real Website Tests (SKIPPED - use without -SkipRealSites)"+ $SKIP += 30+} else {++# =============================================================================+# SECTION 3 - Google.com: Basic /find + explain + weights+# =============================================================================+Write-Header "Section 3: Google.com - /find Enhancements"++Write-Section "3.1 Navigate to Google"+$r = Navigate "https://www.google.com" 3+if ($r.tabId) { Pass "Navigated to Google" }+else { Fail "Navigate to Google failed" "$($r.error)" }++Write-Section "3.2 Exact match - Google Search button"+$r = Find-Element "Google Search button"+if ($r.best_ref -and $r.score -gt 0) {+ Pass "Found 'Google Search button' (ref=$($r.best_ref), score=$([math]::Round($r.score,3)), conf=$($r.confidence))"+} else { Fail "Should find Google Search button" "score=$($r.score) ref=$($r.best_ref)" }++Write-Section "3.3 Explain field - lexical vs embedding breakdown"+$r = Find-Element "search input field" @{ explain = $true; topK = 3 }+if ($r.best_ref) {+ $hasExplain = $r.matches | Where-Object { $_.explain -ne $null }+ if ($hasExplain) { Pass "explain field populated with lexical_score + embedding_score" }+ else { Fail "explain field should be present when explain=true" }+ if ($Verbose) {+ $r.matches | ForEach-Object {+ Write-Host " ref=$($_.ref) score=$($_.score) lex=$($_.explain.lexical_score) emb=$($_.explain.embedding_score)" -ForegroundColor DarkGray+ }+ }+} else { Fail "explain test: should find elements on Google" }++Write-Section "3.4 Per-request weights - lexical-heavy (1.0 / 0.0)"+$r = Find-Element "I'm Feeling Lucky" @{ lexicalWeight = 1.0; embeddingWeight = 0.0; explain = $true }+if ($r.best_ref) {+ # All embedding_score should be 0 when embeddingWeight=0+ $nonZeroEmb = $r.matches | Where-Object { $_.explain -and $_.explain.embedding_score -ne 0 }+ if (-not $nonZeroEmb) { Pass "lexicalWeight=1.0/embeddingWeight=0.0 -> embedding_score=0 in explain" }+ else { Fail "embeddingWeight=0 should produce embedding_score=0" }+} else { Fail "Should find elements with lexical-only weights" }++Write-Section "3.5 Per-request weights - embedding-heavy (0.0 / 1.0)"+$r = Find-Element "I'm Feeling Lucky" @{ lexicalWeight = 0.0; embeddingWeight = 1.0; explain = $true }+if ($r.best_ref) {+ $nonZeroLex = $r.matches | Where-Object { $_.explain -and $_.explain.lexical_score -ne 0 }+ if (-not $nonZeroLex) { Pass "lexicalWeight=0.0/embeddingWeight=1.0 -> lexical_score=0 in explain" }+ else { Fail "lexicalWeight=0 should produce lexical_score=0" }+} else { Fail "Should find elements with embedding-only weights" }++Write-Section "3.6 Threshold boundary - topK=1 returns exactly 1 match"+$r = Find-Element "search" @{ topK = 1 }+if ($r.matches.Count -le 1) { Pass "topK=1 returns at most 1 match (got $($r.matches.Count))" }+else { Fail "topK=1 should return at most 1 match" "got $($r.matches.Count)" }++Write-Section "3.7 Very high threshold - returns empty matches"+$r = Find-Element "some obscure query unlikely to match X7Z9" @{ threshold = 0.99 }+if ($r.best_ref -eq "" -or $r.best_ref -eq $null) {+ Pass "Threshold=0.99 returns no match for non-existent element"+} else { Fail "Threshold=0.99 should not match low-score elements" "got ref=$($r.best_ref) score=$($r.score)" }++Write-Section "3.8 Low threshold - returns more candidates"+$r = Find-Element "click" @{ threshold = 0.01; topK = 10 }+if ($r.matches.Count -gt 1) { Pass "threshold=0.01 returns multiple matches (got $($r.matches.Count))" }+else { Fail "threshold=0.01 should return many matches" "got $($r.matches.Count)" }++Write-Section "3.9 Response structure validation"+$r = Find-Element "Google Search"+$fields = @("best_ref","confidence","score","matches","strategy","threshold","latency_ms","element_count")+$missing = $fields | Where-Object { $r.PSObject.Properties.Name -notcontains $_ }+if ($missing) { Fail "Response missing fields: $($missing -join ', ')" }+else { Pass "All expected fields present in /find response" }+if ($r.latency_ms -ge 0) { Pass "latency_ms is non-negative ($($r.latency_ms)ms)" }+else { Fail "latency_ms should be >= 0" }+if ($r.element_count -gt 0) { Pass "element_count > 0 ($($r.element_count) elements)" }+else { Fail "element_count should be > 0" }++Write-Section "3.10 TrimSpace - leading/trailing whitespace stripped"+$r1 = Find-Element "Google Search"+$r2 = Find-Element " Google Search "+$r3 = Find-Element "`t`tGoogle Search`n"+if ($r1.best_ref -and $r1.best_ref -eq $r2.best_ref -and $r2.best_ref -eq $r3.best_ref) {+ Pass "TrimSpace: queries with leading/trailing whitespace return same result"+} else {+ Fail "TrimSpace: trimmed queries should produce equal results" "r1=$($r1.best_ref) r2=$($r2.best_ref) r3=$($r3.best_ref)"+}++# =============================================================================+# SECTION 4 - Google.com: Recovery Simulation (Stale Ref)+# =============================================================================+Write-Header "Section 4: Google.com - Stale-Ref Recovery Simulation"+Write-Host " NOTE: Recovery is triggered by navigating to the SAME page after" -ForegroundColor DarkCyan+Write-Host " caching intent via /find. The second navigate resets Chrome's" -ForegroundColor DarkCyan+Write-Host " nodeIDs but our snapshot cache still holds OLD refs -> stale." -ForegroundColor DarkCyan++Write-Section "4.1 Cache intent for Google Search button via /find"+$r = Find-Element "Google Search button"+$googleSearchRef = $r.best_ref+if ($googleSearchRef) {+ Pass "Intent cached: ref=$googleSearchRef (score=$([math]::Round($r.score,3)))"+} else {+ Skip "4.2-4.4 (could not find Google Search button)" "skipping recovery tests"+ $googleSearchRef = $null+}++if ($googleSearchRef) {+ Write-Section "4.2 Reload page - Chrome assigns new nodeIDs (snapshot cache now stale)"+ # Reload by navigating to the same URL - Chrome rebuilds the DOM+ Invoke-Api POST "/navigate" @{ url = "https://www.google.com"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2+ Pass "Page reloaded - old snapshot refs are now stale"++ Write-Section "4.3 Click old ref WITHOUT refreshing snapshot (triggers recovery)"+ # DO NOT call /snapshot - use old snapshot so nodeID is stale+ $r = Do-Action $googleSearchRef "click"+ if ($r.success -eq $true) {+ if ($r.recovery -and $r.recovery.recovered -eq $true) {+ Pass "RECOVERY TRIGGERED: action succeeded via semantic re-match"+ Pass " original_ref=$($r.recovery.original_ref) -> new_ref=$($r.recovery.new_ref)"+ Pass " confidence=$($r.recovery.confidence) score=$([math]::Round($r.recovery.score,3)) latency=$($r.recovery.latency_ms)ms"+ } else {+ # nodeID was still valid (Chrome sometimes keeps IDs across soft reload)+ Pass "Action succeeded without recovery (nodeID still valid - soft reload)"+ }+ } else {+ # Recovery attempted but failed, or ref was not in cache+ if ($r.recovery) {+ Fail "Recovery attempted but could not heal" "error=$($r.recovery.error)"+ } else {+ Fail "Action failed and no recovery attempted" "error=$($r.error)"+ }+ }++ Write-Section "4.4 Verify recovery response structure"+ # Do another find + stale-ref click to inspect response shape+ $r2 = Find-Element "I'm Feeling Lucky button"+ $luckyRef = $r2.best_ref+ if ($luckyRef) {+ Invoke-Api POST "/navigate" @{ url = "https://www.google.com"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2+ $r3 = Do-Action $luckyRef "click"+ if ($r3.recovery -ne $null) {+ $rfields = @("recovered","original_ref","failure_type","attempts","latency_ms")+ $rmissing = $rfields | Where-Object { $r3.recovery.PSObject.Properties.Name -notcontains $_ }+ if ($rmissing) { Fail "Recovery object missing fields: $($rmissing -join ', ')" }+ else { Pass "Recovery response contains all expected fields" }+ if ($r3.recovery.attempts -ge 1) { Pass "attempts >= 1 ($($r3.recovery.attempts))" }+ else { Fail "attempts should be >= 1" }+ if ($r3.recovery.latency_ms -ge 0) { Pass "recovery.latency_ms >= 0 ($($r3.recovery.latency_ms)ms)" }+ else { Fail "recovery.latency_ms should be >= 0" }+ } else {+ Skip "Recovery response structure" "action did not trigger recovery (nodeID still valid)"+ }+ } else {+ Skip "4.4 recovery structure" "could not find Feeling Lucky button"+ }+}++# =============================================================================+# SECTION 5 - GitHub.com: Login page stale-ref recovery+# =============================================================================+Write-Header "Section 5: GitHub.com - Login Form Recovery"++Write-Section "5.1 Navigate to GitHub login"+$r = Navigate "https://github.com/login" 3+if ($r.tabId) { Pass "Navigated to GitHub login" }+else { Fail "Navigate to GitHub login failed" "$($r.error)" }++Write-Section "5.2 Find password field"+$passFind = Find-Element "password input"+if ($passFind.best_ref) {+ Pass "Found password field (ref=$($passFind.best_ref), conf=$($passFind.confidence))"+} else { Fail "Should find password field on GitHub login" }++Write-Section "5.3 Find Sign In button"+$signinFind = Find-Element "sign in button"+$signinRef = $signinFind.best_ref+if ($signinRef) {+ Pass "Found Sign In button (ref=$signinRef, score=$([math]::Round($signinFind.score,3)))"+} else {+ Fail "Should find Sign In button on GitHub"+ $signinRef = $null+}++Write-Section "5.4 Find username field"+$userFind = Find-Element "username or email field"+if ($userFind.best_ref) {+ Pass "Found username/email field (ref=$($userFind.best_ref))"+} else { Fail "Should find username field on GitHub" }++Write-Section "5.5 Reload page to make refs stale, then recover Sign In click"+if ($signinRef) {+ Invoke-Api POST "/navigate" @{ url = "https://github.com/login"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2+ Pass "GitHub login page reloaded - old refs are stale"++ $r = Do-Action $signinRef "click"+ if ($r.success -eq $true) {+ if ($r.recovery -and $r.recovery.recovered) {+ Pass "RECOVERY: GitHub Sign In button re-found after DOM rebuild"+ Pass " new_ref=$($r.recovery.new_ref) confidence=$($r.recovery.confidence)"+ } else {+ Pass "Click succeeded (nodeID still valid - no full rebuild)"+ }+ } else {+ if ($r.recovery) {+ Skip "Sign In recovery" "recovery attempted but failed (may need credentials - action correct)"+ } else {+ Fail "Sign In action failed without recovery attempt" "$($r.error)"+ }+ }+} else {+ Skip "5.5 stale-ref recovery" "Sign In ref not found in 5.3"+}++Write-Section "5.6 Explain + weights on login form"+$r = Find-Element "Sign in with passkey" @{ explain = $true; topK = 5; lexicalWeight = 0.7; embeddingWeight = 0.3 }+if ($r.best_ref) {+ Pass "Custom weights (0.7/0.3) returned result on GitHub login"+ $withExplain = $r.matches | Where-Object { $_.explain -ne $null }+ if ($withExplain) { Pass "explain field present with custom weights" }+ else { Fail "explain field missing with explain=true" }+} else { Pass "No match above threshold - acceptable for 'passkey' on this page" }++# =============================================================================+# SECTION 6 - Wikipedia: Text/link elements + edge-case queries+# =============================================================================+Write-Header "Section 6: Wikipedia - Edge-Case Queries"++Write-Section "6.1 Navigate to Wikipedia"+$r = Navigate "https://en.wikipedia.org/wiki/React_(JavaScript_library)" 3+if ($r.tabId) { Pass "Navigated to Wikipedia (React article)" }+else { Fail "Navigate to Wikipedia failed" "$($r.error)" }++Write-Section "6.2 Long query - sentence-length natural language"+$longQ = "click the link that takes me to the contents table of this article"+$r = Find-Element $longQ @{ topK = 5 }+if ($r.best_ref -and $r.score -gt 0) {+ Pass "Long sentence query returned a result (ref=$($r.best_ref), score=$([math]::Round($r.score,3)))"+} else { Fail "Long query should find at least one element" }++Write-Section "6.3 Single word query"+$r = Find-Element "history"+if ($r.best_ref) { Pass "Single word query 'history' returned ref=$($r.best_ref)" }+else { Fail "Single word query should return a result" }++Write-Section "6.4 Emoji in query - should not crash, graceful result"+$r = Find-Element "search [magnify] button"+# Not crashing is the main goal; match is a bonus+Pass "Emoji in query did not crash server (status=$($r.__status), ref=$($r.best_ref))"++Write-Section "6.5 Very long query (>200 chars)"+$veryLong = ("the button that will " * 15).TrimEnd()+$r = Find-Element $veryLong+Pass "300-char query did not crash server (ref=$($r.best_ref))"++Write-Section "6.6 Special-character query - SQL injection pattern (should be safe)"+$r = Find-Element "'; DROP TABLE elements; --"+if (-not $r.__status -or $r.__status -lt 500) {+ Pass "SQL-injection-pattern query handled safely"+} else { Fail "Server should not 500 on special-char query" "status=$($r.__status)" }++Write-Section "6.7 Unicode query - Arabic script"+$r = Find-Element "search button in Arabic" # "search button" in Arabic+Pass "Arabic-script query handled gracefully (ref=$($r.best_ref))"++Write-Section "6.8 Batch /find via multiple calls - concurrent intent caching"+$queries = @("search box","logo image","login button","navigation menu","footer link")+$refs = @()+foreach ($q in $queries) {+ $rq = Find-Element $q+ if ($rq.best_ref) { $refs += $rq.best_ref }+}+if ($refs.Count -ge 2) { Pass "Multiple /find calls succeed concurrently ($($refs.Count) intents cached)" }+else { Fail "Should cache multiple intents" "only $($refs.Count) found" }++# =============================================================================+# SECTION 7 - Batch Actions + Macro Recovery Tests+# =============================================================================+Write-Header "Section 7: Batch /actions + /macro Recovery"++Write-Section "7.1 Navigate to example.com"+$r = Navigate "https://example.com" 2+if ($r.tabId) { Pass "Navigated to example.com" }+else { Fail "Navigate to example.com failed" }++Write-Section "7.2 Find elements for batch test"+$linkFind = Find-Element "more information link"+$linkRef = $linkFind.best_ref+if ($linkRef) { Pass "Found 'More information' link (ref=$linkRef)" }+else { Fail "Should find a link on example.com" }++Write-Section "7.3 Batch actions - mixed valid/invalid refs (stopOnError=false)"+$r = Invoke-Api POST "/actions" @{+ tabId = $TAB+ stopOnError = $false+ actions = @(+ @{ kind = "click"; ref = "e99998" }, # nonexistent ref -> 404 item+ @{ kind = "click"; ref = "e99999" } # nonexistent ref -> 404 item+ )+}+if ($r.results) {+ $failures = $r.results | Where-Object { $_.success -eq $false }+ if ($failures.Count -eq 2) { Pass "Batch: both invalid refs fail gracefully (stopOnError=false)" }+ else { Fail "Expected 2 failures in batch" "got $($failures.Count)" }+ if ($r.failed -eq 2) { Pass "Batch summary: failed=2 correct" }+ else { Fail "Batch summary.failed should be 2" "got $($r.failed)" }+} else { Fail "Batch response should have results array" }++Write-Section "7.4 Batch actions - stopOnError=true stops at first failure"+$r = Invoke-Api POST "/actions" @{+ tabId = $TAB+ stopOnError = $true+ actions = @(+ @{ kind = "click"; ref = "e99997" }, # will fail -> should stop+ @{ kind = "click"; ref = "e99996" } # should never run+ )+}+if ($r.results -and $r.results.Count -le 1) {+ Pass "stopOnError=true stops after first failure (processed $($r.results.Count) action(s))"+} else { Fail "stopOnError=true should stop after first failure" }++Write-Section "7.5 Batch actions - stale-ref recovery in batch"+if ($linkRef) {+ # Reload page to stale refs+ Invoke-Api POST "/navigate" @{ url = "https://example.com"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2++ $r = Invoke-Api POST "/actions" @{+ tabId = $TAB+ stopOnError = $false+ actions = @(+ @{ kind = "click"; ref = $linkRef } # stale ref - recovery should kick in+ )+ }+ if ($r.results -and $r.results.Count -ge 1) {+ $a1 = $r.results[0]+ if ($a1.success) { Pass "Batch: action item succeeded (with or without recovery)" }+ else { Fail "Batch: stale-ref action failed" "$($a1.error)" }+ } else { Fail "Batch response missing results" }+}+else { Skip "7.5 batch stale-ref recovery" "link ref not found in 7.2" }++Write-Section "7.6 Macro - steps with invalid refs (stopOnError=false)"+$r = Invoke-Api POST "/macro" @{+ tabId = $TAB+ stopOnError = $false+ steps = @(+ @{ kind = "click"; ref = "e99994"; tabId = $TAB },+ @{ kind = "click"; ref = "e99993"; tabId = $TAB }+ )+}+if ($r.kind -eq "macro" -and $r.results) {+ Pass "Macro returns kind='macro' with results array"+ if ($r.failed -ge 1) { Pass "Macro correctly reports failures (failed=$($r.failed))" }+ else { Fail "Macro should report failed count" "failed=$($r.failed)" }+} else { Fail "Macro response should have kind='macro' and results" }++Write-Section "7.7 Macro - zero steps -> 400"+$r = Invoke-Api POST "/macro" @{ steps = @() } -quiet $true+if ($r.__status -eq 400) { Pass "Macro with empty steps returns 400" }+else { Fail "Empty macro steps should return 400" "got $($r.__status)" }++Write-Section "7.8 Macro - stale-ref recovery"+$r = Navigate "https://example.com" 2+$moreInfoFind = Find-Element "more information"+$moreInfoRef = $moreInfoFind.best_ref+if ($moreInfoRef) {+ Invoke-Api POST "/navigate" @{ url = "https://example.com"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2++ $r = Invoke-Api POST "/macro" @{+ tabId = $TAB+ stopOnError = $false+ steps = @( @{ kind = "click"; ref = $moreInfoRef; tabId = $TAB } )+ }+ if ($r.results -and $r.results[0].success) {+ Pass "Macro: stale-ref step succeeded (recovery may have helped)"+ } else {+ if ($r.results) { Fail "Macro step failed" "$($r.results[0].error)" }+ else { Fail "Macro returned no results" }+ }+}+else { Skip "7.8 macro stale-ref" "more-info link ref not found" }++# =============================================================================+# SECTION 8 - E-commerce + SPA: Realistic Recovery Scenarios+# =============================================================================+Write-Header "Section 8: Realistic Recovery Scenarios"++# --- 8.1 : Hacker News - dynamic voting buttons+Write-Section "8.1 Hacker News - voting arrows (SPA-like interaction)"+$r = Navigate "https://news.ycombinator.com" 3+if ($r.tabId) {+ Pass "Navigated to Hacker News"++ $upvoteFind = Find-Element "upvote button"+ if ($upvoteFind.best_ref) {+ Pass "Found upvote element (ref=$($upvoteFind.best_ref), conf=$($upvoteFind.confidence))"+ } else {+ Pass "No labeled upvote button (aria-label may be absent) - element_count=$($upvoteFind.element_count)"+ }++ $moreFind = Find-Element "more link"+ if ($moreFind.best_ref) {+ Pass "Found 'more' link on HN (ref=$($moreFind.best_ref))"+ } else { Fail "Should find 'more' pagination link on Hacker News" }+} else { Fail "Navigate to HN failed" }++# --- 8.2 : Wikipedia login page - two-step form simulation+Write-Section "8.2 Wikipedia login form - find then stale-ref recovery"+$r = Navigate "https://en.wikipedia.org/w/index.php?title=Special:UserLogin" 3+if ($r.tabId) {+ Pass "Navigated to Wikipedia login"++ $wikiUser = Find-Element "username input"+ $wikiPass = Find-Element "password field"+ $wikiBtn = Find-Element "log in button"++ if ($wikiUser.best_ref -and $wikiPass.best_ref -and $wikiBtn.best_ref) {+ Pass "Found all 3 login form elements: username, password, login button"+ $loginRef = $wikiBtn.best_ref++ # Reload to stale refs+ Invoke-Api POST "/navigate" @{ url = "https://en.wikipedia.org/w/index.php?title=Special:UserLogin"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2++ $r2 = Do-Action $loginRef "click"+ if ($r2.success -or ($r2.recovery -ne $null)) {+ Pass "Wikipedia Login click: action executed (recovery=$($r2.recovery -ne $null))"+ } else {+ Fail "Wikipedia login button action failed" "$($r2.error)"+ }+ } else {+ Fail "Expected username+password+button on Wikipedia login"+ }+} else { Fail "Navigate to Wikipedia login failed" }++# --- 8.3 : DuckDuckGo - search box + button recovery+Write-Section "8.3 DuckDuckGo - search form recovery"+$r = Navigate "https://duckduckgo.com" 3+if ($r.tabId) {+ Pass "Navigated to DuckDuckGo"++ $ddgSearch = Find-Element "search input box"+ $ddgBtn = Find-Element "search button"+ if ($ddgSearch.best_ref -and $ddgBtn.best_ref) {+ Pass "Found search input (ref=$($ddgSearch.best_ref)) + button (ref=$($ddgBtn.best_ref))"+ $ddgBtnRef = $ddgBtn.best_ref++ # Reload + stale ref+ Invoke-Api POST "/navigate" @{ url = "https://duckduckgo.com"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2++ $r2 = Do-Action $ddgBtnRef "click"+ if ($r2.success -or $r2.recovery -ne $null) {+ Pass "DuckDuckGo search button click handled (recovery=$($r2.recovery -ne $null))"+ } else {+ Fail "DuckDuckGo search button action failed" "$($r2.error)"+ }+ } else {+ Pass "Search box found=$($ddgSearch.best_ref -ne '') btn found=$($ddgBtn.best_ref -ne '') - minimal DDG markup is normal"+ }+} else { Fail "Navigate to DuckDuckGo failed" }++# --- 8.4 : GitHub repo page - star button (common agent use-case)+Write-Section "8.4 GitHub pinchtab repo - Star button recovery"+$r = Navigate "https://github.com/pinchtab/pinchtab" 3+if ($r.tabId) {+ Pass "Navigated to GitHub pinchtab repo"++ $starFind = Find-Element "Star this repository button"+ if ($starFind.best_ref) {+ Pass "Found Star button (ref=$($starFind.best_ref), score=$([math]::Round($starFind.score,3)))"+ $starRef = $starFind.best_ref++ Invoke-Api POST "/navigate" @{ url = "https://github.com/pinchtab/pinchtab"; tabId = $TAB } | Out-Null+ Start-Sleep -Seconds 2++ $r2 = Do-Action $starRef "click"+ if ($r2.success -or $r2.recovery -ne $null) {+ Pass "GitHub Star button action executed after reload (recovery=$($r2.recovery -ne $null))"+ } else {+ Skip "Star button click failed - likely needs authentication" "expected for unauthenticated user"+ }+ } else {+ Pass "Star button not accessible (normal when unauthenticated) - score=$($starFind.score)"+ }+} else { Fail "Navigate to GitHub repo failed" }++# --- 8.5 : MDN Web Docs - navigation menu (large, complex DOM)+Write-Section "8.5 MDN Web Docs - complex DOM navigation"+$r = Navigate "https://developer.mozilla.org/en-US/docs/Web/JavaScript" 4+if ($r.tabId) {+ Pass "Navigated to MDN JavaScript docs"++ $menuFind = Find-Element "open navigation menu"+ if ($menuFind.best_ref) {+ Pass "Found navigation menu (ref=$($menuFind.best_ref), conf=$($menuFind.confidence), elements=$($menuFind.element_count))"+ } else {+ $anyFind = Find-Element "search documentation"+ if ($anyFind.best_ref) { Pass "Found search on MDN (ref=$($anyFind.best_ref))" }+ else { Fail "Should find navigation or search on MDN" }+ }++ # Test explain on a complex page+ $r2 = Find-Element "feedback button" @{ explain = $true; topK = 5 }+ $hasExplain = $r2.matches | Where-Object { $_.explain -ne $null }+ if ($hasExplain) { Pass "explain field populated on large-DOM page (MDN)" }+ else { Pass "No match above threshold on MDN - explain not triggered (acceptable)" }+} else { Fail "Navigate to MDN failed" }++} # end if -not $SkipRealSites++# =============================================================================+# SECTION 9 - Recovery Response Contract (Structure Enforcement)+# =============================================================================+Write-Header "Section 9: Recovery Response Contract"++Write-Section "9.1 recovery field is absent when no recovery was tried"+# When ref resolves correctly (fresh snapshot + fresh action), no recovery block+if ($TAB) {+ $r = Navigate "https://example.com" 2+ $lf = Find-Element "more information"+ if ($lf.best_ref) {+ $r2 = Do-Action $lf.best_ref "click"+ if ($r2.success -and -not $r2.recovery) {+ Pass "recovery field absent in response when no recovery needed"+ } elseif ($r2.success -and $r2.recovery) {+ Pass "recovery field present but not null - recovery was attempted (also valid)"+ } else {+ Fail "Action failed on fresh ref" "$($r2.error)"+ }+ } else { Skip "9.1" "no link found on example.com" }+} else { Skip "9.1" "no active tab" }++Write-Section "9.2 /find response: score is float in [0,1]"+if ($TAB) {+ $r = Navigate "https://example.com" 2+ $r2 = Find-Element "example" @{ topK = 3; threshold = 0.01 }+ $badScore = $r2.matches | Where-Object { $_.score -lt 0 -or $_.score -gt 1 }+ if ($badScore) { Fail "Scores out of [0,1] range: $($badScore.score -join ', ')" }+ else { Pass "/find scores all within [0, 1] range" }+} else { Skip "9.2" "no active tab" }++Write-Section "9.3 strategy field is non-empty"+if ($TAB) {+ $r2 = Find-Element "link"+ if ($r2.strategy -and $r2.strategy.Length -gt 0) {+ Pass "strategy field non-empty: '$($r2.strategy)'"+ } else { Fail "strategy field should be non-empty" }+} else { Skip "9.3" "no active tab" }++Write-Section "9.4 matches array always present (even on no-result query)"+if ($TAB) {+ $r2 = Find-Element "ZQ97XNOBODYWILLFINDTHIS" @{ threshold = 0.99 }+ if ($r2.PSObject.Properties.Name -contains "matches") {+ Pass "matches is present even when empty (count=$($r2.matches.Count))"+ } else { Fail "matches array should always be present" }+} else { Skip "9.4" "no active tab" }++# =============================================================================+# SUMMARY+# =============================================================================+Write-Host ""+Write-Host ("=" * 72) -ForegroundColor Cyan+Write-Host " TEST SUMMARY" -ForegroundColor Cyan+Write-Host ("=" * 72) -ForegroundColor Cyan+Write-Host ""+Write-Host (" Total: " + ($PASS + $FAIL + $SKIP))+Write-Host (" PASS : $PASS") -ForegroundColor Green+if ($FAIL -gt 0) {+ Write-Host (" FAIL : $FAIL") -ForegroundColor Red+} else {+ Write-Host (" FAIL : $FAIL") -ForegroundColor Green+}+Write-Host (" SKIP : $SKIP") -ForegroundColor DarkYellow+Write-Host ""++if ($FAIL -gt 0) {+ Write-Host " Result: FAILED ($FAIL test(s) failed)" -ForegroundColor Red+ exit 1+} else {+ Write-Host " Result: PASSED" -ForegroundColor Green+ exit 0+}