Skip to content

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

refactor: delete the stale box-model comment and the dead GetElementCenter (PIN-058)

Luigi Agostie62ec4886303
diff --git a/internal/bridge/cdp_geometry_test.go b/internal/bridge/cdp_geometry_test.go
index 9911791d..8a9ae926 100644
--- a/internal/bridge/cdp_geometry_test.go
+++ b/internal/bridge/cdp_geometry_test.go
@@ -2,28 +2,13 @@ package bridge
import (
"context"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "strings"
"testing"
)
-func TestGetElementCenter_ParsesBoxModel(t *testing.T) {
- // Test the box model parsing logic
- // Content quad: [x1,y1, x2,y2, x3,y3, x4,y4]
- // For a 100x50 box at position (200, 100):
- // corners: (200,100), (300,100), (300,150), (200,150)
- content := []float64{200, 100, 300, 100, 300, 150, 200, 150}
-
- // Calculate expected center
- expectedX := (content[0] + content[2] + content[4] + content[6]) / 4 // (200+300+300+200)/4 = 250
- expectedY := (content[1] + content[3] + content[5] + content[7]) / 4 // (100+100+150+150)/4 = 125
-
- if expectedX != 250 {
- t.Errorf("expected X=250, got %f", expectedX)
- }
- if expectedY != 125 {
- t.Errorf("expected Y=125, got %f", expectedY)
- }
-}
-
// TestGetElementCenterJS_ContextCancelled verifies that getElementCenterJS
// returns an error when the context is already cancelled (no browser panic).
func TestGetElementCenterJS_ContextCancelled(t *testing.T) {
@@ -45,3 +30,68 @@ func TestPointerPointForNode_ContextCancelled(t *testing.T) {
t.Error("expected error for cancelled context, got nil")
}
}
+
+const censusFile = "cdp_geometry_test.go"
+
+// DOM.getBoxModel quads were measured to be VIEWPORT-relative. A comment in cdpops
+// still said the shared box-model path was document-relative, one call boundary above
+// the facade comment that says the opposite — and a corrected claim next to a stale one
+// is how the disproven premise gets reconstructed and a scroll transform reintroduced.
+// Deleting it once is not enough; this is what keeps it deleted.
+func TestNoCdpopsCommentClaimsTheBoxModelIsDocumentRelative(t *testing.T) {
+ const claim = "document-relative"
+
+ var scanned, offenders int
+ err := filepath.WalkDir("cdpops", func(path string, d fs.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") {
+ return err
+ }
+ body, readErr := os.ReadFile(path) // #nosec G304 -- files walked from this repo's own tree.
+ if readErr != nil {
+ return readErr
+ }
+ scanned++
+ if strings.Contains(string(body), claim) {
+ offenders++
+ t.Errorf("%s claims %q; getBoxModel quads are viewport-relative, and the caller already states the space once", path, claim)
+ }
+ return nil
+ })
+ if err != nil {
+ t.Fatalf("cannot walk cdpops, so this census guards nothing: %v", err)
+ }
+ if scanned == 0 {
+ t.Fatal("scanned no cdpops files; this census would pass vacuously")
+ }
+ if _, err := os.Stat(censusFile); err != nil {
+ t.Fatalf("censusFile %q does not exist, so the floor excludes the wrong file: %v", censusFile, err)
+ }
+ if offenders == 0 && !phraseStillUsedInBridge(t, claim) {
+ t.Errorf("%q appears nowhere under internal/bridge, so this ban no longer bans a phrase anyone writes; re-point it at the wording actually in use", claim)
+ }
+}
+
+// phraseStillUsedInBridge is the floor: the ban is only meaningful while the phrase is
+// still the one the codebase uses for real document space. If a rename retires it, the
+// ban silently passes over nothing.
+//
+// This file is excluded, and that exclusion is the whole point: the census names the
+// banned phrase to ban it, so without this it finds its own literal and every floor
+// passes — a floor that counts itself is not a floor.
+func phraseStillUsedInBridge(t *testing.T, phrase string) bool {
+ t.Helper()
+ self := filepath.Base(censusFile)
+ var found bool
+ _ = filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
+ if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") ||
+ strings.HasPrefix(path, "cdpops/") || filepath.Base(path) == self {
+ return nil //nolint:nilerr // an unreadable sibling must not decide the floor
+ }
+ body, readErr := os.ReadFile(path) // #nosec G304 -- files walked from this repo's own tree.
+ if readErr == nil && strings.Contains(string(body), phrase) {
+ found = true
+ }
+ return nil
+ })
+ return found
+}
diff --git a/internal/bridge/cdpops/geometry.go b/internal/bridge/cdpops/geometry.go
index f0db509c..a7e8ecac 100644
--- a/internal/bridge/cdpops/geometry.go
+++ b/internal/bridge/cdpops/geometry.go
@@ -8,41 +8,6 @@ import (
"github.com/chromedp/chromedp"
)
-// GetElementCenter returns the center coordinates of an element using DOM.getBoxModel.
-func GetElementCenter(ctx context.Context, backendNodeID int64) (x, y float64, err error) {
- var result json.RawMessage
- err = chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
- return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.getBoxModel", map[string]any{
- "backendNodeId": backendNodeID,
- }, &result)
- }))
- if err != nil {
- return 0, 0, err
- }
-
- var box struct {
- Model struct {
- Content []float64 `json:"content"`
- } `json:"model"`
- }
- if err = json.Unmarshal(result, &box); err != nil {
- return 0, 0, err
- }
-
- if len(box.Model.Content) < 4 {
- return 0, 0, fmt.Errorf("invalid box model: expected at least 4 coordinates")
- }
-
- x = (box.Model.Content[0] + box.Model.Content[2] + box.Model.Content[4] + box.Model.Content[6]) / 4
- y = (box.Model.Content[1] + box.Model.Content[3] + box.Model.Content[5] + box.Model.Content[7]) / 4
-
- if x == 0 && y == 0 {
- return GetElementCenterJS(ctx, backendNodeID)
- }
-
- return x, y, nil
-}
-
// GetElementCenterJS resolves the DOM node and evaluates getBoundingClientRect().
func GetElementCenterJS(ctx context.Context, backendNodeID int64) (float64, float64, error) {
var resolveResult json.RawMessage
@@ -97,10 +62,6 @@ func GetElementCenterJS(ctx context.Context, backendNodeID int64) (float64, floa
return callRes.Result.Value.X, callRes.Result.Value.Y, nil
}
-// ScrollIntoViewIfNeeded scrolls the element into view. Measuring the resulting
-// box is deliberately not done here: getBoundingClientRect in the element's own
-// context is frame-relative, so the caller measures through the shared
-// document-relative box-model path instead.
func ScrollIntoViewIfNeeded(ctx context.Context, nodeID int64) error {
if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.scrollIntoViewIfNeeded", map[string]any{"backendNodeId": nodeID}, nil)
diff --git a/internal/bridge/cdpops_facade.go b/internal/bridge/cdpops_facade.go
index 22c43cac..851946b4 100644
--- a/internal/bridge/cdpops_facade.go
+++ b/internal/bridge/cdpops_facade.go
@@ -51,9 +51,6 @@ func getElementCenterJS(ctx context.Context, backendNodeID int64) (float64, floa
// ScrollIntoViewAndGetBox scrolls the element into view and reports the box it
// ended up at, in the same top-level viewport space /box and /capture report.
-// It shared /box's defect until now: measuring with getBoundingClientRect in the
-// element's own context returns frame-relative coordinates for anything inside
-// an iframe.
func ScrollIntoViewAndGetBox(ctx context.Context, nodeID int64) (map[string]any, error) {
if err := bridgecdpops.ScrollIntoViewIfNeeded(ctx, nodeID); err != nil {
return nil, err
diff --git a/internal/bridge/isolated_world_test.go b/internal/bridge/isolated_world_test.go
index 4eee575b..3e87f61c 100644
--- a/internal/bridge/isolated_world_test.go
+++ b/internal/bridge/isolated_world_test.go
@@ -185,7 +185,7 @@ var mainWorldResolvers = map[string]string{
"internal/bridge/cdpops/element_ops.go": "element ops act on an already-chosen node",
"internal/bridge/cdpops/frame_dom.go": "callFunctionOn helper for an already-chosen node",
"internal/bridge/cdpops/pointer.go": "pointer actions act on an already-chosen node",
- "internal/bridge/cdpops/geometry.go": "dead pending deletion: GetElementCenter has no callers at all and GetElementCenterJS only a test-reached facade wrapper; the live click-point path is PointerPointForNode, which is isolated",
+ "internal/bridge/cdpops/geometry.go": "dead pending deletion: GetElementCenterJS reaches production through no caller — only the unexported facade wrapper and its cancellation test; the live click-point path is PointerPointForNode, which is isolated",
}
// resolveNodeSpellings pairs each way of issuing DOM.resolveNode with the token