High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: a zero scroll delta scrolls down 120px instead of not scrolling — the refusal was keyed to the flag spelling and lived in the CLI, so scroll 0 and every HTTP caller still hit it (PIN-217)
Luigi Agosti9d191542f975
diff --git a/cmd/pinchtab/cmd_cli_browser_actions.go b/cmd/pinchtab/cmd_cli_browser_actions.goindex 2cb80307..25a178c5 100644--- a/cmd/pinchtab/cmd_cli_browser_actions.go+++ b/cmd/pinchtab/cmd_cli_browser_actions.go@@ -1,7 +1,9 @@package mainimport (+ "errors""fmt"+ "strconv""strings""github.com/pinchtab/pinchtab/internal/bridge"@@ -166,17 +168,22 @@ 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 delta of zero on both axes is indistinguishable from sending no delta at- // all, and the server reads that as "scroll down by the default" — so- // `--dy 0` would scroll 120px instead of nothing. Dropping the field client- // side lands in the same default, so the only honest client-side answer is to- // refuse. An explicit zero on ONE axis is fine and passes through.- if byFlag && scrollFlagDelta(cmd, "dy") == 0 && scrollFlagDelta(cmd, "dx") == 0 {- return fmt.Errorf("a zero delta is not a scroll: the server reads no-delta as its default and would scroll down 120px; pass a non-zero --dy/--dx")+ // 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.+ if len(args) == 1 {+ if px, err := strconv.Atoi(args[0]); err == nil && px == 0 {+ return errZeroScrollDelta+ }+ } else if byFlag && scrollFlagDelta(cmd, "dy") == 0 && scrollFlagDelta(cmd, "dx") == 0 {+ return errZeroScrollDelta}return nil}+var errZeroScrollDelta = errors.New("a zero delta is not a scroll: pass a non-zero count, a direction, or a selector to scroll into view")+// scrollFlagDelta is the value of an int scroll flag, 0 when unset or unreadable.func scrollFlagDelta(cmd *cobra.Command, name string) int {if !cmd.Flags().Changed(name) {diff --git a/cmd/pinchtab/cmd_cli_scroll_test.go b/cmd/pinchtab/cmd_cli_scroll_test.goindex c0a4d127..cf8639a1 100644--- a/cmd/pinchtab/cmd_cli_scroll_test.go+++ b/cmd/pinchtab/cmd_cli_scroll_test.go@@ -75,6 +75,18 @@ func TestScrollRefusesArgumentsItCannotHonour(t *testing.T) {args: nil,flags: map[string]string{"dy": "0", "dx": "500"},},+ {+ // The same request as --dy 0, differently spelled. Keying the refusal to the+ // flag rather than to the delta let this one through to the default step.+ name: "a zero positional",+ args: []string{"0"},+ wantErr: "zero delta",+ },+ {+ name: "a signed zero positional",+ args: []string{"-0"},+ wantErr: "zero delta",+ },{name: "a positional alone",args: []string{"800"},diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.goindex 9b418643..3cc9944c 100644--- a/internal/bridge/action_pointer.go+++ b/internal/bridge/action_pointer.go@@ -579,6 +579,14 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strinscrollX := req.ScrollXscrollY := req.ScrollYif 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}diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.goindex 2c49a156..b6265cd2 100644--- a/internal/bridge/action_request.go+++ b/internal/bridge/action_request.go@@ -80,6 +80,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"`@@ -159,6 +163,7 @@ func (r *ActionRequest) UnmarshalJSON(data []byte) error {r.Kind = CanonicalActionKind(r.Kind)r.HasXY = r.HasXY || hasJSONKey(raw, "x") || hasJSONKey(raw, "y")r.HasToXY = r.HasToXY || hasJSONKey(raw, "toX") || hasJSONKey(raw, "toY")+ r.HasScroll = r.HasScroll || hasJSONKey(raw, "scrollX") || hasJSONKey(raw, "scrollY")r.HasText = r.HasText || hasJSONKey(raw, "text") || hasJSONKey(raw, "value")if hasJSONKey(raw, "deltaX") {if err := json.Unmarshal(raw["deltaX"], &r.DeltaX); err != nil {diff --git a/internal/bridge/action_scroll_zero_test.go b/internal/bridge/action_scroll_zero_test.gonew file mode 100644index 00000000..3d311dd9--- /dev/null+++ b/internal/bridge/action_scroll_zero_test.go@@ -0,0 +1,62 @@+package bridge++import (+ "context"+ "encoding/json"+ "strings"+ "testing"+)++func decodeScrollRequest(t *testing.T, body string) ActionRequest {+ t.Helper()++ var req ActionRequest+ if err := json.Unmarshal([]byte(body), &req); err != nil {+ t.Fatalf("decode %s: %v", body, err)+ }+ return req+}++// An absent delta and an explicit zero are different requests, and only the JSON key+// presence can tell them apart — both leave ScrollX and ScrollY at 0.+func TestHasScrollSeparatesAnAbsentDeltaFromAnExplicitZero(t *testing.T) {+ for _, tc := range []struct {+ body string+ want bool+ }{+ {`{"kind":"scroll"}`, false},+ {`{"kind":"scroll","selector":"#footer"}`, false},+ {`{"kind":"scroll","scrollY":0}`, true},+ {`{"kind":"scroll","scrollX":0}`, true},+ {`{"kind":"scroll","scrollY":800}`, true},+ } {+ if got := decodeScrollRequest(t, tc.body).HasScroll; got != tc.want {+ t.Errorf("%s: HasScroll = %v, want %v", tc.body, got, tc.want)+ }+ }+}++// The defect this pins: scrollX==0 && scrollY==0 was answered with a default 120px step+// whatever the caller meant, so an explicit zero scrolled DOWN. A caller computing a+// remaining distance reaches zero exactly when it wants no movement, so that is the wrong+// direction reported as success. The refusal must not fire for an absent delta, which has+// always meant one step.+func TestScrollRefusesAnExplicitZeroDeltaButKeepsTheDefaultWhenAbsent(t *testing.T) {+ b := &Bridge{}++ _, err := b.actionScroll(context.Background(), decodeScrollRequest(t, `{"kind":"scroll","scrollY":0}`))+ if err == nil {+ t.Fatal("an explicit zero delta was accepted, so it scrolled a default step in a direction nobody asked for")+ }+ if !strings.Contains(err.Error(), "zero delta") {+ t.Errorf("error = %v, want it to name the zero delta", 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+ // and failed later — a refusal here would mean the default step was lost.+ _, err = b.actionScroll(context.Background(), decodeScrollRequest(t, `{"kind":"scroll"}`))+ if err != nil && strings.Contains(err.Error(), "zero delta") {+ t.Errorf("a bare scroll was refused as a zero delta: %v", err)+ }+}