Skip to content

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

fix: nav and reload hide the landed URL the server already returned (PIN-064)

Luigi Agosti58c00e353fe2
diff --git a/cmd/pinchtab/cmd_cli_register.go b/cmd/pinchtab/cmd_cli_register.go
index 1f6f34bf..ea912ed0 100644
--- a/cmd/pinchtab/cmd_cli_register.go
+++ b/cmd/pinchtab/cmd_cli_register.go
@@ -193,7 +193,7 @@ func configureBrowserFlags() {
navCmd.Flags().Bool("new-tab", false, "Open in new tab")
navCmd.Flags().Bool("block-images", false, "Block image loading")
navCmd.Flags().Bool("block-ads", false, "Block ads")
- addPostActionFlags(navCmd, "navigation", false)
+ addPostActionFlags(navCmd, "navigation", true)
navCmd.Flags().Bool("dismiss-banners", false, "After landing, click any visible cookie/consent dismissal button or remove obvious overlay containers")
addPostActionFlags(backCmd, "navigation", true)
diff --git a/cmd/pinchtab/cmd_cli_register_test.go b/cmd/pinchtab/cmd_cli_register_test.go
index 40306f26..3cf03b93 100644
--- a/cmd/pinchtab/cmd_cli_register_test.go
+++ b/cmd/pinchtab/cmd_cli_register_test.go
@@ -79,7 +79,7 @@ func TestCaptureCommandRegistersTabFlag(t *testing.T) {
// TestPostActionFlagsBundle pins the exact usage strings the shared
// addPostActionFlags helper interpolates per verb, so a future verb edit cannot
-// silently drift the --help text, and verifies the no-text commands omit --text.
+// silently drift the --help text, and verifies the one no-text command omits it.
func TestPostActionFlagsBundle(t *testing.T) {
wantUsage := func(cmd *cobra.Command, flag, want string) {
f := cmd.Flags().Lookup(flag)
@@ -103,11 +103,12 @@ func TestPostActionFlagsBundle(t *testing.T) {
wantUsage(navCmd, "snap", "Output interactive snapshot after navigation")
wantUsage(navCmd, "snap-diff", "Output snapshot diff after navigation (changes only)")
+ // nav has --text because landing on a page is exactly when reading it is
+ // useful, and reload — which lands on a page the same way — always had it.
+ wantUsage(navCmd, "text", "Output page text after navigation (for verification)")
- // nav and scroll have no post-action --text flag.
- if navCmd.Flags().Lookup("text") != nil {
- t.Error("navCmd should not register a post-action --text flag")
- }
+ // scroll stays excluded: scrolling does not change which document is loaded,
+ // so post-scroll text answers nothing --snap-diff does not answer better.
if scrollCmd.Flags().Lookup("text") != nil {
t.Error("scrollCmd should not register a post-action --text flag")
}
diff --git a/docs/commands.md b/docs/commands.md
index 6a3daad1..d599d265 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -26,12 +26,20 @@ pinchtab nav <url> --new-tab # Explicitly force a new tab
pinchtab nav <url> --block-images # Block images for this navigation
pinchtab nav <url> --block-ads # Block ads for this navigation
pinchtab nav <url> --snap # Navigate and output interactive snapshot
+pinchtab nav <url> --text # Navigate and output page text
+pinchtab nav <url> --print-tab-id # Print only the tab ID, whatever stdout is
pinchtab back # Go back in the active tab
pinchtab back --tab <id> # Go back in a specific tab
pinchtab forward # Go forward in the active tab
pinchtab reload # Reload the active tab
```
+`nav`, `back`, `forward` and `reload` report the URL the page actually landed on,
+so a redirect, a login wall or an error page is visible without `--snap`. `nav`
+prints the tab ID first and the landed URL after it, and prints the tab ID alone
+when stdout is not a terminal or when `--print-tab-id` is passed — so
+`TAB=$(pinchtab nav <url>)` still captures a usable tab ID.
+
## Tabs
The `tab` command only lists, focuses, and closes tabs. It does not proxy the rest of the browser command set.
diff --git a/internal/cli/actions/actions_navigate.go b/internal/cli/actions/actions_navigate.go
index 76f47347..b5c189b5 100644
--- a/internal/cli/actions/actions_navigate.go
+++ b/internal/cli/actions/actions_navigate.go
@@ -14,43 +14,28 @@ import (
// Back navigates the current (or specified) tab back in history.
func Back(client *http.Client, base, token string, cmd *cobra.Command) {
- tabID, _ := cmd.Flags().GetString("tab")
- path := "/back"
- if tabID != "" {
- path = "/tabs/" + tabID + "/back"
- }
- path = appendDismissBannersQuery(path, cmd)
-
- jsonOutput, _ := cmd.Flags().GetBool("json")
- if jsonOutput {
- apiclient.DoPost(client, base, token, path, nil)
- return
- }
-
- result := apiclient.DoPostQuiet(client, base, token, path, nil)
- if url, ok := result["url"].(string); ok {
- output.Value(url)
- } else {
- output.Success()
- }
-
- snap, _ := cmd.Flags().GetBool("snap")
- snapDiff, _ := cmd.Flags().GetBool("snap-diff")
- if snap || snapDiff {
- fetchAndPrintSnapshot(client, base, token, tabID, snapDiff)
- }
- text, _ := cmd.Flags().GetBool("text")
- if text {
- fetchAndPrintText(client, base, token, tabID)
- }
+ historyNav(client, base, token, "back", cmd)
}
// Forward navigates the current (or specified) tab forward in history.
func Forward(client *http.Client, base, token string, cmd *cobra.Command) {
+ historyNav(client, base, token, "forward", cmd)
+}
+
+// Reload reloads the current (or specified) tab.
+func Reload(client *http.Client, base, token string, cmd *cobra.Command) {
+ historyNav(client, base, token, "reload", cmd)
+}
+
+// historyNav is the one body behind back, forward and reload. The server answers
+// all three from a single handler returning {"tabId","url"}, so the CLI reports
+// the landed URL the same way for each rather than keeping three copies that
+// drift — reload used to discard the response and print a bare OK.
+func historyNav(client *http.Client, base, token, action string, cmd *cobra.Command) {
tabID, _ := cmd.Flags().GetString("tab")
- path := "/forward"
+ path := "/" + action
if tabID != "" {
- path = "/tabs/" + tabID + "/forward"
+ path = "/tabs/" + tabID + "/" + action
}
path = appendDismissBannersQuery(path, cmd)
@@ -61,52 +46,36 @@ func Forward(client *http.Client, base, token string, cmd *cobra.Command) {
}
result := apiclient.DoPostQuiet(client, base, token, path, nil)
- if url, ok := result["url"].(string); ok {
- output.Value(url)
+ if landed := landedURL(result); landed != "" {
+ output.Value(landed)
} else {
output.Success()
}
- snap, _ := cmd.Flags().GetBool("snap")
- snapDiff, _ := cmd.Flags().GetBool("snap-diff")
- if snap || snapDiff {
- fetchAndPrintSnapshot(client, base, token, tabID, snapDiff)
- }
- text, _ := cmd.Flags().GetBool("text")
- if text {
- fetchAndPrintText(client, base, token, tabID)
- }
+ printPostActionOutput(client, base, token, tabID, cmd)
}
-// Reload reloads the current (or specified) tab.
-func Reload(client *http.Client, base, token string, cmd *cobra.Command) {
- tabID, _ := cmd.Flags().GetString("tab")
- path := "/reload"
- if tabID != "" {
- path = "/tabs/" + tabID + "/reload"
- }
- path = appendDismissBannersQuery(path, cmd)
-
- jsonOutput, _ := cmd.Flags().GetBool("json")
- if jsonOutput {
- apiclient.DoPost(client, base, token, path, nil)
- return
- }
-
- apiclient.DoPostQuiet(client, base, token, path, nil)
- output.Success()
-
+// printPostActionOutput runs the shared --snap / --snap-diff / --text tail.
+func printPostActionOutput(client *http.Client, base, token, tabID string, cmd *cobra.Command) {
snap, _ := cmd.Flags().GetBool("snap")
snapDiff, _ := cmd.Flags().GetBool("snap-diff")
if snap || snapDiff {
fetchAndPrintSnapshot(client, base, token, tabID, snapDiff)
}
- text, _ := cmd.Flags().GetBool("text")
- if text {
+ if text, _ := cmd.Flags().GetBool("text"); text {
fetchAndPrintText(client, base, token, tabID)
}
}
+// landedURL is the URL the navigation actually ended on, which is not the
+// requested one whenever a redirect, login wall or error page intervened. Empty
+// when the response carries none, so callers can stay terse instead of printing
+// a blank line.
+func landedURL(result map[string]any) string {
+ landed, _ := result["url"].(string)
+ return landed
+}
+
func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Command) string {
req := buildNavigateRequest(url, cmd)
@@ -123,19 +92,42 @@ func Navigate(client *http.Client, base, token string, url string, cmd *cobra.Co
fmt.Println(resultTabID)
}
+ // The landed URL is the cheap signal that the page is not the one asked for,
+ // but a second line would break `TAB=$(pinchtab nav URL)` — command
+ // substitution captures every line. So it is printed only for a human at a
+ // terminal, which is what --print-tab-id already promised.
+ if !tabIDOnly(cmd) {
+ if landed := landedURL(result); landed != "" {
+ output.Value(landed)
+ }
+ }
+
if !isIdentifiedCaller() {
output.Hint(cli.NoSessionHint)
}
- snap, _ := cmd.Flags().GetBool("snap")
- snapDiff, _ := cmd.Flags().GetBool("snap-diff")
- if snap || snapDiff {
- fetchAndPrintSnapshot(client, base, token, resultTabID, snapDiff)
- }
+ printPostActionOutput(client, base, token, resultTabID, cmd)
return resultTabID
}
+// tabIDOnly reports whether stdout must carry nothing but the tab ID: either
+// --print-tab-id was passed, or stdout is not a terminal, which is the shape a
+// capture or a pipeline has.
+func tabIDOnly(cmd *cobra.Command) bool {
+ if only, _ := cmd.Flags().GetBool("print-tab-id"); only {
+ return true
+ }
+ return !stdoutIsTerminal()
+}
+
+// stdoutIsTerminal stands in for an environment a test cannot set, so it is a
+// var; production reads its result on every non-JSON navigate.
+var stdoutIsTerminal = func() bool {
+ info, err := os.Stdout.Stat()
+ return err == nil && (info.Mode()&os.ModeCharDevice) != 0
+}
+
type navigateRequest struct {
path string
body map[string]any
diff --git a/internal/cli/actions/actions_navigate_test.go b/internal/cli/actions/actions_navigate_test.go
index f0886e27..df60dd95 100644
--- a/internal/cli/actions/actions_navigate_test.go
+++ b/internal/cli/actions/actions_navigate_test.go
@@ -292,3 +292,222 @@ func TestNavigateIdentifiedCallerPrintsNoSessionHint(t *testing.T) {
t.Fatalf("stderr = %q, want no session hint for an identified caller", stderr)
}
}
+
+// atTerminal makes the stdout check answer true for the duration of a test, so
+// the interactive branch is reachable — under `go test` stdout is a pipe.
+func atTerminal(t *testing.T) {
+ t.Helper()
+ old := stdoutIsTerminal
+ stdoutIsTerminal = func() bool { return true }
+ t.Cleanup(func() { stdoutIsTerminal = old })
+}
+
+// The landed URL is the cheap signal that a redirect, login wall or error page
+// intervened. The server already returns it; nav used to print only the tab ID.
+func TestNavigateReportsTheLandedURLAtATerminal(t *testing.T) {
+ atTerminal(t)
+ m := newMockServer()
+ m.response = `{"tabId":"ABC123","title":"Example Domain","url":"https://example.com/"}`
+ defer m.close()
+
+ out := captureStdout(t, func() {
+ Navigate(m.server.Client(), m.base(), "", "https://httpbin.org/redirect-to?url=https://example.com/", newNavigateCmd())
+ })
+
+ lines := strings.Split(strings.TrimSpace(out), "\n")
+ if len(lines) != 2 {
+ t.Fatalf("stdout = %q, want the tab ID and the landed URL", out)
+ }
+ if lines[0] != "ABC123" {
+ t.Errorf("first line = %q, want the tab ID", lines[0])
+ }
+ if lines[1] != "https://example.com/" {
+ t.Errorf("second line = %q, want the landed URL, not the requested one", lines[1])
+ }
+}
+
+// TAB=$(pinchtab nav URL) captures every line, so a second line would break it.
+// Both the explicit flag and a non-terminal stdout must stay single-line.
+func TestNavigatePrintsOnlyTheTabIDWhenCaptured(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ terminal bool
+ flag bool
+ }{
+ {name: "stdout is not a character device", terminal: false},
+ {name: "print-tab-id at a terminal", terminal: true, flag: true},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ old := stdoutIsTerminal
+ stdoutIsTerminal = func() bool { return tc.terminal }
+ t.Cleanup(func() { stdoutIsTerminal = old })
+
+ m := newMockServer()
+ m.response = `{"tabId":"ABC123","url":"https://example.com/"}`
+ defer m.close()
+
+ cmd := newNavigateCmd()
+ if tc.flag {
+ _ = cmd.Flags().Set("print-tab-id", "true")
+ }
+ out := captureStdout(t, func() {
+ Navigate(m.server.Client(), m.base(), "", "https://example.com", cmd)
+ })
+
+ if got := strings.TrimSpace(out); got != "ABC123" {
+ t.Errorf("stdout = %q, want exactly the tab ID so $(pinchtab nav URL) stays usable", got)
+ }
+ })
+ }
+}
+
+// back, forward and reload all come from one server handler that returns
+// {"tabId","url"}; reload used to discard it and print a bare OK.
+func TestHistoryNavigationPrintsTheLandedURL(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ run func(*http.Client, string, string, *cobra.Command)
+ path string
+ }{
+ {name: "back", run: Back, path: "/back"},
+ {name: "forward", run: Forward, path: "/forward"},
+ {name: "reload", run: Reload, path: "/reload"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ m := newMockServer()
+ m.response = `{"tabId":"ABC123","url":"https://example.com/landed"}`
+ defer m.close()
+
+ out := captureStdout(t, func() {
+ tc.run(m.server.Client(), m.base(), "", newHistoryCmd())
+ })
+
+ if m.lastPath != tc.path {
+ t.Fatalf("path = %q, want %q", m.lastPath, tc.path)
+ }
+ if got := strings.TrimSpace(out); got != "https://example.com/landed" {
+ t.Errorf("stdout = %q, want the landed URL", got)
+ }
+ })
+ }
+}
+
+// A response without a url must stay terse rather than print a blank line: OK
+// for the history commands, the bare tab ID for nav.
+func TestLandingReportDegradesWithoutAURL(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ run func(*http.Client, string, string, *cobra.Command)
+ }{
+ {name: "back", run: Back},
+ {name: "forward", run: Forward},
+ {name: "reload", run: Reload},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ m := newMockServer()
+ m.response = `{"tabId":"ABC123"}`
+ defer m.close()
+
+ out := captureStdout(t, func() {
+ tc.run(m.server.Client(), m.base(), "", newHistoryCmd())
+ })
+ if got := strings.TrimSpace(out); got != "OK" {
+ t.Errorf("stdout = %q, want the terse OK", got)
+ }
+ if strings.Contains(out, "\n\n") {
+ t.Errorf("stdout = %q, want no blank line", out)
+ }
+ })
+ }
+
+ t.Run("navigate", func(t *testing.T) {
+ atTerminal(t)
+ m := newMockServer()
+ m.response = `{"tabId":"ABC123"}`
+ defer m.close()
+
+ out := captureStdout(t, func() {
+ Navigate(m.server.Client(), m.base(), "", "https://example.com", newNavigateCmd())
+ })
+ if got := strings.TrimSpace(out); got != "ABC123" {
+ t.Errorf("stdout = %q, want just the tab ID", got)
+ }
+ if strings.Contains(out, "\n\n") {
+ t.Errorf("stdout = %q, want no blank line", out)
+ }
+ })
+}
+
+// --json is the machine contract and predates this change: it must stay the raw
+// response body for all four commands, with no landed-URL line added.
+func TestJSONOutputIsTheRawResponseForAllFour(t *testing.T) {
+ const response = `{"tabId":"ABC123","url":"https://example.com/landed"}`
+ // DoPost pretty-prints the decoded body; that is the pre-existing contract and
+ // must not gain a landed-URL line.
+ const want = "{\n \"tabId\": \"ABC123\",\n \"url\": \"https://example.com/landed\"\n}"
+
+ t.Run("navigate", func(t *testing.T) {
+ atTerminal(t)
+ m := newMockServer()
+ m.response = response
+ defer m.close()
+
+ cmd := newNavigateCmd()
+ cmd.Flags().Bool("json", false, "")
+ _ = cmd.Flags().Set("json", "true")
+ out := captureStdout(t, func() {
+ Navigate(m.server.Client(), m.base(), "", "https://example.com", cmd)
+ })
+ if got := strings.TrimSpace(out); got != want {
+ t.Errorf("stdout = %q, want the response body alone %q", got, want)
+ }
+ })
+
+ for _, tc := range []struct {
+ name string
+ run func(*http.Client, string, string, *cobra.Command)
+ }{
+ {name: "back", run: Back},
+ {name: "forward", run: Forward},
+ {name: "reload", run: Reload},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ atTerminal(t)
+ m := newMockServer()
+ m.response = response
+ defer m.close()
+
+ cmd := newHistoryCmd()
+ _ = cmd.Flags().Set("json", "true")
+ out := captureStdout(t, func() {
+ tc.run(m.server.Client(), m.base(), "", cmd)
+ })
+ if got := strings.TrimSpace(out); got != want {
+ t.Errorf("stdout = %q, want the response body alone %q", got, want)
+ }
+ })
+ }
+}
+
+// nav gained --text; the shared tail must actually run it.
+func TestNavigateTextFetchesPageText(t *testing.T) {
+ atTerminal(t)
+ m := newMockServer()
+ m.response = `{"tabId":"ABC123","url":"https://example.com/"}`
+ m.responses["GET /tabs/ABC123/text"] = mockResponse{statusCode: 200, body: `{"text":"PAGE TEXT"}`}
+ defer m.close()
+
+ cmd := newNavigateCmd()
+ cmd.Flags().Bool("snap", false, "")
+ cmd.Flags().Bool("snap-diff", false, "")
+ cmd.Flags().Bool("text", false, "")
+ _ = cmd.Flags().Set("text", "true")
+
+ out := captureStdout(t, func() {
+ Navigate(m.server.Client(), m.base(), "", "https://example.com", cmd)
+ })
+
+ if !strings.Contains(out, "PAGE TEXT") {
+ t.Errorf("stdout = %q, want the page text after navigation", out)
+ }
+}