High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
refactor: give the zero-delta scroll rule one owner and drop the comments restating it (PIN-217)
Luigi Agostia253d64ea91f
diff --git a/cmd/pinchtab/cmd_cli_browser_actions.go b/cmd/pinchtab/cmd_cli_browser_actions.goindex 25a178c5..23446483 100644--- a/cmd/pinchtab/cmd_cli_browser_actions.go+++ b/cmd/pinchtab/cmd_cli_browser_actions.go@@ -168,10 +168,7 @@ func scrollArgs(cmd *cobra.Command, args []string) error {if len(args) == 1 && byFlag {return fmt.Errorf("give either %q or --dy/--dx, not both", args[0])}- // A zero delta is not a scroll, and the refusal is keyed to the DELTA rather than- // to how it was spelled: `scroll 0` and `--dy 0` are the same request. The server- // refuses it too — this is the local echo that saves a round trip, not the rule.- // An explicit zero on ONE axis is a real scroll and passes through.+ // The server owns this rule; the local copy only saves a round trip.if len(args) == 1 {if px, err := strconv.Atoi(args[0]); err == nil && px == 0 {return errZeroScrollDeltadiff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.goindex 96a59794..ea60b359 100644--- a/internal/bridge/action_pointer.go+++ b/internal/bridge/action_pointer.go@@ -560,23 +560,24 @@ func (b *Bridge) actionMouseWheel(ctx context.Context, req ActionRequest) (map[sreturn map[string]any{"wheel": true, "x": x, "y": y, "deltaX": deltaX, "deltaY": deltaY}, nil}-// wheelDelta resolves a wheel delta from either spelling, deltaX/deltaY or the legacy-// scrollX/scrollY. An absent delta means one notch down, which is what a bare wheel has-// always meant. An explicit zero is a different request and cannot share that answer, for-// the reason a zero scroll cannot: a caller computing a delta reaches zero exactly when it-// wants no movement. A zero on one axis only is a real scroll on the other.+const defaultScrollNotch = 120++func resolveScrollDelta(x, y int, explicit bool, spelling string) (int, int, error) {+ if x != 0 || y != 0 {+ return x, y, nil+ }+ if explicit {+ return 0, 0, fmt.Errorf("a zero delta is not a scroll: pass a non-zero %s", spelling)+ }+ return 0, defaultScrollNotch, nil+}+func wheelDelta(req ActionRequest) (int, int, error) {deltaX, deltaY := req.DeltaX, req.DeltaYif deltaX == 0 && deltaY == 0 {deltaX, deltaY = req.ScrollX, req.ScrollY}- if deltaX != 0 || deltaY != 0 {- return deltaX, deltaY, nil- }- if req.HasDelta || req.HasScroll {- return 0, 0, fmt.Errorf("a zero delta is not a scroll: pass a non-zero deltaX/deltaY")- }- return 0, 120, nil+ return resolveScrollDelta(deltaX, deltaY, req.HasDelta || req.HasScroll, "deltaX/deltaY")}func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[string]any, error) {@@ -592,24 +593,14 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strinreturn map[string]any{"scrolled": true}, ScrollByNodeID(ctx, int64(node.BackendNodeID))}- scrollX := req.ScrollX- scrollY := req.ScrollY- if scrollX == 0 && scrollY == 0 {- // An absent delta means "one step", which is what a bare scroll has always- // answered. An explicit zero is a different request and cannot share that- // answer: a caller computing a remaining distance reaches zero exactly when- // it wants no movement, and scrolling a default step there is the wrong- // direction reported as success.- if req.HasScroll {- return nil, fmt.Errorf("a zero delta is not a scroll: pass a non-zero scrollX/scrollY, or a selector to scroll into view")- }- scrollY = 120+ scrollX, scrollY, err := resolveScrollDelta(req.ScrollX, req.ScrollY, req.HasScroll, "scrollX/scrollY, or a selector to scroll into view")+ if err != nil {+ return nil, err}scrollTargetX := req.XscrollTargetY := req.Yif !req.HasXY {- var err errorscrollTargetX, scrollTargetY, err = scrollViewportCenter(ctx)if err != nil {return nil, fmt.Errorf("resolve scroll viewport center: %w", err)diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.goindex fc02e8ba..ca009124 100644--- a/internal/bridge/action_request.go+++ b/internal/bridge/action_request.go@@ -81,15 +81,10 @@ type ActionRequest struct {ScrollX int `json:"scrollX"`ScrollY int `json:"scrollY"`- // HasScroll separates an absent delta, which scroll answers with its default- // step, from an explicit zero, which is not a scroll at all.HasScroll bool `json:"hasScroll,omitempty"`- // DeltaX/DeltaY are explicit mouse-wheel deltas for low-level- // mouse-wheel actions. ScrollX/ScrollY remain for backward compatibility.- DeltaX int `json:"deltaX,omitempty"`- DeltaY int `json:"deltaY,omitempty"`- // HasDelta is the DeltaX/DeltaY counterpart of HasScroll: a bare wheel means one- // notch down, an explicit zero means no movement, and both leave the fields at 0.+ // DeltaX/DeltaY are the wheel spelling; ScrollX/ScrollY remain for compatibility.+ DeltaX int `json:"deltaX,omitempty"`+ DeltaY int `json:"deltaY,omitempty"`HasDelta bool `json:"hasDelta,omitempty"`DragX int `json:"dragX"`DragY int `json:"dragY"`diff --git a/internal/bridge/action_scroll_zero_test.go b/internal/bridge/action_scroll_zero_test.goindex 43a91942..b9fb0571 100644--- a/internal/bridge/action_scroll_zero_test.go+++ b/internal/bridge/action_scroll_zero_test.go@@ -51,6 +51,11 @@ func TestScrollRefusesAnExplicitZeroDeltaButKeepsTheDefaultWhenAbsent(t *testingif !strings.Contains(err.Error(), "zero delta") {t.Errorf("error = %v, want it to name the zero delta", err)}+ // Scroll and wheel share one resolver, so each must still name its OWN spelling:+ // telling a scroll caller to pass deltaX names a field its surface does not accept.+ if !strings.Contains(err.Error(), "scrollX/scrollY") {+ t.Errorf("error = %v, want it to name the spelling scroll accepts", err)+ }// An absent delta must reach the default step. Resolving the viewport needs a browser// this test does not have, so the assertion is that it got PAST the zero-delta branch@@ -87,6 +92,10 @@ func TestWheelDeltaRefusesAnExplicitZeroInEitherSpelling(t *testing.T) {if tc.wantErr {if err == nil {t.Errorf("%s: accepted, resolving to (%d,%d) — a zero delta scrolls a default notch nobody asked for", tc.body, gotX, gotY)+ continue+ }+ if !strings.Contains(err.Error(), "deltaX/deltaY") {+ t.Errorf("%s: error = %v, want it to name the spelling wheel accepts", tc.body, err)}continue}