Skip to content

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

test: mock chromedp evaluation in storage handler unit tests to fix CI sandbox failures

Yash Jadhav6f70607dc3d2
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index 75f9c86d..9da296ad 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -8,6 +8,7 @@ import (
"strconv"
"time"
+ "github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/dashboard"
@@ -33,6 +34,9 @@ type Handlers struct {
IDPIGuard idpi.Guard
Version string // build version injected at startup
clipboard clipboardStore
+
+ // Optional dependency injection (for unit testing)
+ evalJS func(ctx context.Context, expression string, out *string) error
}
func New(b bridge.BridgeAPI, cfg *config.RuntimeConfig, p bridge.ProfileService, d *dashboard.Dashboard, o bridge.OrchestratorService) *Handlers {
@@ -87,6 +91,11 @@ func New(b bridge.BridgeAPI, cfg *config.RuntimeConfig, p bridge.ProfileService,
},
)
+ // Default evalJS backed by chromedp for production
+ h.evalJS = func(ctx context.Context, expression string, out *string) error {
+ return chromedp.Run(ctx, chromedp.Evaluate(expression, out))
+ }
+
// Clean up .tmp export files orphaned by a previous crash.
go CleanupStaleTmpExports(cfg.StateDir)
diff --git a/internal/handlers/storage.go b/internal/handlers/storage.go
index 12c4bc32..421c061b 100644
--- a/internal/handlers/storage.go
+++ b/internal/handlers/storage.go
@@ -9,7 +9,6 @@ import (
"net/http"
"time"
- "github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/httpx"
)
@@ -64,7 +63,7 @@ func (h *Handlers) handleStorageGet(w http.ResponseWriter, r *http.Request) {
script := buildStorageGetScript(storageType, key)
var resultJSON string
- if err := chromedp.Run(tCtx, chromedp.Evaluate(script, &resultJSON)); err != nil {
+ if err := h.evalJS(tCtx, script, &resultJSON); err != nil {
httpx.Error(w, 500, fmt.Errorf("evaluate storage get: %w", err))
return
}
@@ -143,7 +142,7 @@ func (h *Handlers) handleStorageSet(w http.ResponseWriter, r *http.Request) {
`, storageObj, string(keyJSON), string(valueJSON))
var resultJSON string
- if err := chromedp.Run(tCtx, chromedp.Evaluate(script, &resultJSON)); err != nil {
+ if err := h.evalJS(tCtx, script, &resultJSON); err != nil {
httpx.Error(w, 500, fmt.Errorf("evaluate storage set: %w", err))
return
}
@@ -217,7 +216,7 @@ func (h *Handlers) handleStorageDelete(w http.ResponseWriter, r *http.Request) {
script := buildStorageDeleteScript(req.Type, req.Key)
var resultJSON string
- if err := chromedp.Run(tCtx, chromedp.Evaluate(script, &resultJSON)); err != nil {
+ if err := h.evalJS(tCtx, script, &resultJSON); err != nil {
httpx.Error(w, 500, fmt.Errorf("evaluate storage delete: %w", err))
return
}
diff --git a/internal/handlers/storage_test.go b/internal/handlers/storage_test.go
index c64a7a44..7681bb49 100644
--- a/internal/handlers/storage_test.go
+++ b/internal/handlers/storage_test.go
@@ -2,6 +2,8 @@ package handlers
import (
"bytes"
+ "context"
+
"github.com/pinchtab/pinchtab/internal/config"
"net/http/httptest"
"testing"
@@ -11,6 +13,10 @@ func TestHandleStorageDelete_EmptyBody(t *testing.T) {
// Setup handlers with a mock bridge
m := &mockBridge{}
h := New(m, &config.RuntimeConfig{}, nil, nil, nil)
+ h.evalJS = func(ctx context.Context, expr string, out *string) error {
+ *out = `{"success":true,"origin":"http://example.com"}`
+ return nil
+ }
// Create a DELETE request with NO body
req := httptest.NewRequest("DELETE", "/storage", nil)
@@ -28,14 +34,18 @@ func TestHandleStorageDelete_EmptyBody(t *testing.T) {
// If it got past decode, it should try to find a tab.
// Our mockBridge return a context for any tabID, so it should proceed to domain check.
// We expect 200 if the mock allows it.
- if w.Code != 200 && w.Code != 404 {
- t.Errorf("unexpected status code: %d, body: %s", w.Code, w.Body.String())
+ if w.Code != 200 {
+ t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
}
func TestHandleStorageDelete_WithBody(t *testing.T) {
m := &mockBridge{}
h := New(m, &config.RuntimeConfig{}, nil, nil, nil)
+ h.evalJS = func(ctx context.Context, expr string, out *string) error {
+ *out = `{"success":true,"origin":"http://example.com"}`
+ return nil
+ }
// Valid body
body := `{"type": "local", "key": "test"}`
@@ -44,7 +54,7 @@ func TestHandleStorageDelete_WithBody(t *testing.T) {
h.handleStorageDelete(w, req)
- if w.Code == 400 {
- t.Fatalf("expected handleStorageDelete to handle valid body, got 400: %s", w.Body.String())
+ if w.Code != 200 {
+ t.Fatalf("expected handleStorageDelete to handle valid body and return 200, got %d: %s", w.Code, w.Body.String())
}
}