Skip to content

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

fix: remove index-based tab resolution (#276)

luigiagente018464e7d61
diff --git a/cmd/pinchtab/cmd_cli.go b/cmd/pinchtab/cmd_cli.go
index f9a1b8b4..f3b7343f 100644
--- a/cmd/pinchtab/cmd_cli.go
+++ b/cmd/pinchtab/cmd_cli.go
@@ -121,9 +121,9 @@ var screenshotCmd = &cobra.Command{
}
var tabsCmd = &cobra.Command{
- Use: "tab [n|id]",
+ Use: "tab [id]",
Aliases: []string{"tabs"},
- Short: "List tabs, or focus a tab by index or ID",
+ Short: "List tabs, or focus a tab by ID",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
@@ -353,8 +353,8 @@ func init() {
},
})
tabsCmd.AddCommand(&cobra.Command{
- Use: "close <n|id>",
- Short: "Close a tab by index or ID",
+ Use: "close <id>",
+ Short: "Close a tab by ID",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Load()
diff --git a/docs/commands.md b/docs/commands.md
index 6dd2ba20..48ddf02c 100644
--- a/docs/commands.md
+++ b/docs/commands.md
@@ -41,11 +41,9 @@ Hidden aliases: `goto`, `navigate`, `open`
```
pinchtab tab # List all tabs
-pinchtab tab <n> # Focus tab by index (1-based)
pinchtab tab <id> # Focus tab by ID
pinchtab tab new # Open a new empty tab
pinchtab tab new <url> # Open a new tab with URL
-pinchtab tab close <n> # Close tab by index (1-based)
pinchtab tab close <id> # Close tab by ID
```
diff --git a/internal/cli/actions/actions_tabs.go b/internal/cli/actions/actions_tabs.go
index c571ce02..1aa8b062 100644
--- a/internal/cli/actions/actions_tabs.go
+++ b/internal/cli/actions/actions_tabs.go
@@ -1,11 +1,9 @@
package actions
import (
- "encoding/json"
"fmt"
"net/http"
"os"
- "strconv"
"github.com/pinchtab/pinchtab/internal/cli"
"github.com/pinchtab/pinchtab/internal/cli/apiclient"
@@ -28,55 +26,19 @@ func TabNew(client *http.Client, base, token string, body map[string]any) {
apiclient.DoPost(client, base, token, "/tab", body)
}
-// TabClose closes a tab by index or ID.
-func TabClose(client *http.Client, base, token string, ref string) {
- tabID := resolveTabRef(client, base, token, ref)
- if tabID == "" {
- return
- }
+// TabClose closes a tab by ID.
+func TabClose(client *http.Client, base, token string, tabID string) {
apiclient.DoPost(client, base, token, "/tab", map[string]any{
"action": "close",
"tabId": tabID,
})
}
-// TabFocus switches to a tab by index or ID, making it the active tab
+// TabFocus switches to a tab by ID, making it the active tab
// for subsequent commands.
-func TabFocus(client *http.Client, base, token string, ref string) {
- tabID := resolveTabRef(client, base, token, ref)
- if tabID == "" {
- return
- }
+func TabFocus(client *http.Client, base, token string, tabID string) {
apiclient.DoPost(client, base, token, "/tab", map[string]any{
"action": "focus",
"tabId": tabID,
})
}
-
-// resolveTabRef resolves a tab reference that can be either a 1-based index
-// (e.g., "1", "2") or a tab ID (e.g., "A1B2C3D4...").
-// Returns the resolved tab ID, or empty string on error (already printed).
-func resolveTabRef(client *http.Client, base, token string, ref string) string {
- n, err := strconv.Atoi(ref)
- if err != nil {
- // Not a number — treat as tab ID directly
- return ref
- }
-
- // Numeric — resolve index to tab ID via /tabs
- body := apiclient.DoGetRaw(client, base, token, "/tabs", nil)
- var resp struct {
- Tabs []struct {
- ID string `json:"id"`
- } `json:"tabs"`
- }
- if err := json.Unmarshal(body, &resp); err != nil {
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle, fmt.Sprintf("Failed to parse tabs: %v", err)))
- return ""
- }
- if n < 1 || n > len(resp.Tabs) {
- fmt.Fprintln(os.Stderr, cli.StyleStderr(cli.ErrorStyle, fmt.Sprintf("Tab index %d out of range (1-%d)", n, len(resp.Tabs))))
- return ""
- }
- return resp.Tabs[n-1].ID
-}
diff --git a/internal/cli/actions/actions_tabs_test.go b/internal/cli/actions/actions_tabs_test.go
deleted file mode 100644
index c701202d..00000000
--- a/internal/cli/actions/actions_tabs_test.go
+++ /dev/null
@@ -1,77 +0,0 @@
-package actions
-
-import (
- "encoding/json"
- "net/http"
- "net/http/httptest"
- "testing"
-)
-
-func TestResolveTabRef(t *testing.T) {
- tabs := []struct {
- ID string `json:"id"`
- }{
- {ID: "AAAA1111"},
- {ID: "BBBB2222"},
- {ID: "CCCC3333"},
- }
- resp, _ := json.Marshal(map[string]any{"tabs": tabs})
-
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-Type", "application/json")
- _, _ = w.Write(resp)
- }))
- defer srv.Close()
-
- client := srv.Client()
-
- t.Run("numeric index 1", func(t *testing.T) {
- id := resolveTabRef(client, srv.URL, "", "1")
- if id != "AAAA1111" {
- t.Errorf("expected AAAA1111, got %s", id)
- }
- })
-
- t.Run("numeric index 3", func(t *testing.T) {
- id := resolveTabRef(client, srv.URL, "", "3")
- if id != "CCCC3333" {
- t.Errorf("expected CCCC3333, got %s", id)
- }
- })
-
- t.Run("index out of range", func(t *testing.T) {
- id := resolveTabRef(client, srv.URL, "", "5")
- if id != "" {
- t.Errorf("expected empty, got %s", id)
- }
- })
-
- t.Run("index zero", func(t *testing.T) {
- id := resolveTabRef(client, srv.URL, "", "0")
- if id != "" {
- t.Errorf("expected empty, got %s", id)
- }
- })
-
- t.Run("negative index", func(t *testing.T) {
- // Negative numbers are valid ints but out of range
- id := resolveTabRef(client, srv.URL, "", "-1")
- if id != "" {
- t.Errorf("expected empty, got %s", id)
- }
- })
-
- t.Run("non-numeric passed through as ID", func(t *testing.T) {
- id := resolveTabRef(client, srv.URL, "", "AAAA1111")
- if id != "AAAA1111" {
- t.Errorf("expected AAAA1111, got %s", id)
- }
- })
-
- t.Run("hex-like ID passed through", func(t *testing.T) {
- id := resolveTabRef(client, srv.URL, "", "A1B2C3D4E5F6")
- if id != "A1B2C3D4E5F6" {
- t.Errorf("expected A1B2C3D4E5F6, got %s", id)
- }
- })
-}
diff --git a/tests/e2e/scenarios-cli/15-tab-focus.sh b/tests/e2e/scenarios-cli/15-tab-focus.sh
index f0daab75..c81b9c35 100755
--- a/tests/e2e/scenarios-cli/15-tab-focus.sh
+++ b/tests/e2e/scenarios-cli/15-tab-focus.sh
@@ -1,5 +1,5 @@
#!/bin/bash
-# 15-tab-focus.sh — CLI tab focus and index resolution commands
+# 15-tab-focus.sh — CLI tab focus and close commands
source "$(dirname "$0")/common.sh"
@@ -13,20 +13,11 @@ assert_output_contains "tabs" "output contains tabs array"
end_test
# ─────────────────────────────────────────────────────────────────
-start_test "pinchtab tab 1 (focus first tab by index)"
+start_test "pinchtab tab <id> (focus by tab ID)"
# Navigate to a page first to ensure there's a tab
pt nav "${FIXTURES_URL}/index.html"
-# Focus on first tab by index
-pt_ok tab 1
-assert_output_contains "focused" "output contains 'focused'"
-
-end_test
-
-# ─────────────────────────────────────────────────────────────────
-start_test "pinchtab tab <id> (focus by tab ID)"
-
# Get list of tabs and extract first tab ID
pt tab
assert_output_json "tab list is valid JSON"
@@ -34,12 +25,8 @@ TAB_ID=$(echo "$PT_OUT" | jq -r '.tabs[0].id // empty')
if [ -n "$TAB_ID" ] && [ "$TAB_ID" != "null" ]; then
echo -e " ${BLUE}→ focusing on tab ID: ${TAB_ID:0:12}...${NC}"
-
- # Focus on that specific tab ID
pt_ok tab "$TAB_ID"
assert_output_contains "focused" "output indicates tab is focused"
-
- ((ASSERTIONS_PASSED++)) || true
else
echo -e " ${YELLOW}⚠${NC} could not extract tab ID, skipping"
((ASSERTIONS_PASSED++)) || true
@@ -48,22 +35,22 @@ fi
end_test
# ─────────────────────────────────────────────────────────────────
-start_test "pinchtab tab close <index> (close by index)"
+start_test "pinchtab tab close <id> (close by tab ID)"
# Open two tabs
pt nav "${FIXTURES_URL}/index.html"
pt nav "${FIXTURES_URL}/form.html"
-# Get tab count before
+# Get tab count and last tab ID
pt tab
BEFORE=$(echo "$PT_OUT" | jq '.tabs | length')
-echo -e " ${MUTED}tab count before: $BEFORE${NC}"
+CLOSE_ID=$(echo "$PT_OUT" | jq -r '.tabs[-1].id // empty')
+echo -e " ${MUTED}tab count before: $BEFORE, closing: ${CLOSE_ID:0:12}...${NC}"
-# Close the second tab by index
-pt_ok tab close 2
-echo -e " ${MUTED}closed tab at index 2${NC}"
+# Close by ID
+pt_ok tab close "$CLOSE_ID"
-# Get tab count after
+# Verify count decreased
pt tab
AFTER=$(echo "$PT_OUT" | jq '.tabs | length')
echo -e " ${MUTED}tab count after: $AFTER${NC}"
@@ -77,12 +64,3 @@ else
fi
end_test
-
-# ─────────────────────────────────────────────────────────────────
-start_test "pinchtab tab 99 (index out of range)"
-
-# Try to focus on a non-existent tab index
-pt tab 99
-assert_exit_code_lte 1 "exit code indicates error or graceful handling"
-
-end_test
diff --git a/tests/e2e/scenarios-recent/43-tab-focus.sh b/tests/e2e/scenarios-recent/43-tab-focus.sh
index fce80d28..a29a8320 100755
--- a/tests/e2e/scenarios-recent/43-tab-focus.sh
+++ b/tests/e2e/scenarios-recent/43-tab-focus.sh
@@ -1,6 +1,6 @@
#!/bin/bash
-# Test: Tab focus and index-based tab resolution
-# Tests for `POST /tab {"action":"focus","tabId":"..."}` and tab index resolution
+# Test: Tab focus
+# Tests for `POST /tab {"action":"focus","tabId":"..."}`
source "$(dirname "$0")/common.sh"
# --- T1: Focus tab by ID ---