Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

test: pin that dialog-scoped wrapper forms get the node scope, not the frame (PIN-038)

Luigi Agostib28e11dd2df4
diff --git a/internal/bridge/action_resolve_scope_test.go b/internal/bridge/action_resolve_scope_test.go
index 2ca7ed36..1caef365 100644
--- a/internal/bridge/action_resolve_scope_test.go
+++ b/internal/bridge/action_resolve_scope_test.go
@@ -2,11 +2,15 @@ package bridge
import (
"context"
+ "encoding/base64"
"errors"
"strings"
"testing"
+ "time"
+ "github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/selector"
+ "github.com/pinchtab/pinchtab/internal/testbrowser"
)
// stubScope records what the shared recursion asks of a scope, so the grammar
@@ -149,3 +153,82 @@ func TestScopeRefBehaviourDiffersByDesign(t *testing.T) {
t.Errorf("node scope zero-id ref = %v, want ErrSelectorNoMatch as before", err)
}
}
+
+// The stub table above proves what the recursion asks of a scope, but both real
+// scopes answer in the same shape, so it is blind to WHICH scope a wrapper form
+// is given. That is the one mistake this refactor made cheap: the scope is now a
+// value at two call sites, so handing the node entry a frameScope is a one-token
+// edit. This is the browser-backed guard for it — the dialog twin must win over
+// the background twins that bracket it in document order, which is exactly what
+// a frame-rooted search would return instead.
+func TestDialogScopedWrapperFormsStayInsideTheScope(t *testing.T) {
+ ctx := newScopedWrapperFixture(t)
+
+ modalNodeID, open, err := TopmostModalNodeID(ctx, "")
+ if err != nil || !open {
+ t.Fatalf("topmost dialog = (%d, %v, %v), want a visible dialog", modalNodeID, open, err)
+ }
+
+ for _, tc := range []struct {
+ selector string
+ wantID string
+ }{
+ {selector: "first:text:Save", wantID: "dialog-first"},
+ {selector: "last:text:Save", wantID: "dialog-last"},
+ {selector: "nth:1:text:Save", wantID: "dialog-last"},
+ {selector: "first:css:button", wantID: "dialog-first"},
+ {selector: "last:css:button", wantID: "dialog-last"},
+ } {
+ t.Run(tc.selector, func(t *testing.T) {
+ nodeID, err := ResolveUnifiedSelectorWithinNode(ctx, selector.Parse(tc.selector), nil, modalNodeID)
+ if err != nil {
+ t.Fatalf("resolve %s within the dialog: %v", tc.selector, err)
+ }
+ var gotID string
+ if err := callFunctionOnNodeForTest(ctx, nodeID, `function() { return this.id; }`, &gotID); err != nil {
+ t.Fatal(err)
+ }
+ if gotID != tc.wantID {
+ t.Errorf("%s resolved %q, want %q — the wrapper form escaped the dialog scope", tc.selector, gotID, tc.wantID)
+ }
+ })
+ }
+}
+
+// Background twins bracket the dialog so that first: and last: each discriminate:
+// a frame-rooted first: returns background-first and a frame-rooted last: returns
+// background-last, while both correct answers are inside the dialog.
+func newScopedWrapperFixture(t *testing.T) context.Context {
+ t.Helper()
+ chromePath := testbrowser.Path(t)
+ alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(
+ chromedp.DefaultExecAllocatorOptions[:],
+ chromedp.ExecPath(chromePath),
+ chromedp.UserDataDir(t.TempDir()),
+ chromedp.Flag("headless", true),
+ chromedp.Flag("no-sandbox", true),
+ )...)
+ ctx, cancelBrowser := chromedp.NewContext(alloc)
+ ctx, cancelTimeout := context.WithTimeout(ctx, 20*time.Second)
+ t.Cleanup(func() {
+ cancelTimeout()
+ cancelBrowser()
+ cancelAlloc()
+ })
+
+ html := `<style>[role=dialog] { position: fixed; inset: 20px; background: white; }</style>
+ <button id="background-first">Save</button>
+ <div id="dlg" role="dialog" aria-modal="true">
+ <button id="dialog-first">Save</button>
+ <button id="dialog-last">Save</button>
+ </div>
+ <button id="background-last">Save</button>`
+ dataURL := "data:text/html;base64," + base64.StdEncoding.EncodeToString([]byte(html))
+ if err := chromedp.Run(ctx,
+ chromedp.Navigate(dataURL),
+ chromedp.WaitVisible("#dialog-first", chromedp.ByID),
+ ); err != nil {
+ t.Fatal(err)
+ }
+ return ctx
+}