Skip to content

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

fix(bridgekit): GET /tabs leads with the current tab under ghost-chrome, because the adapter forwards the read-only CurrentTabID (PIN-370)

Luigi Agostie8acc0b78738
diff --git a/internal/bridge/tab_lookup.go b/internal/bridge/tab_lookup.go
index 3bf30ae8..0aba28af 100644
--- a/internal/bridge/tab_lookup.go
+++ b/internal/bridge/tab_lookup.go
@@ -26,6 +26,9 @@ func (tm *TabManager) CurrentTabID() string {
}
func (tm *TabManager) TabLastUsed(tabID string) (time.Time, bool) {
+ if tm == nil {
+ return time.Time{}, false
+ }
tm.mu.RLock()
defer tm.mu.RUnlock()
entry, ok := tm.tabs[tabID]
diff --git a/internal/browsers/ghostchrome/bridgekit/bridge_adapter_passthrough.go b/internal/browsers/ghostchrome/bridgekit/bridge_adapter_passthrough.go
index 0b962816..4aefa76e 100644
--- a/internal/browsers/ghostchrome/bridgekit/bridge_adapter_passthrough.go
+++ b/internal/browsers/ghostchrome/bridgekit/bridge_adapter_passthrough.go
@@ -139,3 +139,13 @@ func (a *BridgeAdapter) TabHandoffState(tabID string) (bridge.TabHandoffState, b
}
return bridge.TabHandoffState{}, false
}
+
+func (a *BridgeAdapter) CurrentTabID() string {
+ type currentTabReader interface {
+ CurrentTabID() string
+ }
+ if r, ok := a.BridgeAPI.(currentTabReader); ok {
+ return r.CurrentTabID()
+ }
+ return ""
+}
diff --git a/internal/browsers/ghostchrome/bridgekit/current_tab_listing_test.go b/internal/browsers/ghostchrome/bridgekit/current_tab_listing_test.go
new file mode 100644
index 00000000..c1d96520
--- /dev/null
+++ b/internal/browsers/ghostchrome/bridgekit/current_tab_listing_test.go
@@ -0,0 +1,59 @@
+package bridgekit
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/handlers"
+)
+
+type listedChromeBridge struct {
+ *bridge.Bridge
+ targets []bridge.TabTarget
+}
+
+func (b listedChromeBridge) ListTargets() ([]bridge.TabTarget, error) { return b.targets, nil }
+
+func (b listedChromeBridge) EnsureBrowser(*config.RuntimeConfig) error { return nil }
+
+func TestTheTabListingThroughTheAdapterLeadsWithTheCurrentTab(t *testing.T) {
+ ctx, cancel := chromedp.NewContext(context.Background())
+ t.Cleanup(cancel)
+ cfg := &config.RuntimeConfig{StateDir: t.TempDir()}
+ chrome := listedChromeBridge{Bridge: bridge.New(context.Background(), ctx, cfg)}
+ for _, id := range []string{"tabA", "tabB", "tabC"} {
+ chrome.RegisterTab(id, ctx)
+ chrome.targets = append(chrome.targets, bridge.TabTarget{TargetID: id, URL: "https://example.com/" + id, Type: "page"})
+ }
+ chrome.RegisterTab("tabB", ctx)
+ adapter := NewBridgeAdapter(chrome, cfg)
+
+ if got := adapter.CurrentTabID(); got != "tabB" {
+ t.Errorf("adapter CurrentTabID = %q, want the chrome bridge's current tab tabB", got)
+ }
+ _, resolved, err := adapter.TabContext("")
+ if err != nil || resolved != "tabB" {
+ t.Fatalf("adapter TabContext(\"\") = %q, %v; the reader must agree with it", resolved, err)
+ }
+
+ w := httptest.NewRecorder()
+ handlers.New(adapter, cfg, nil, nil, nil).HandleTabs(w, httptest.NewRequest(http.MethodGet, "/tabs", nil))
+
+ var resp struct {
+ Tabs []struct {
+ ID string `json:"id"`
+ } `json:"tabs"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil || len(resp.Tabs) != 3 {
+ t.Fatalf("GET /tabs = %d %s", w.Code, w.Body.String())
+ }
+ if resp.Tabs[0].ID != "tabB" {
+ t.Fatalf("GET /tabs through the ghost-chrome adapter leads with %s, want the current tab tabB", resp.Tabs[0].ID)
+ }
+}