Skip to content

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

fix: integration tests and bug fixes

Luigi Agostic27778f35781
diff --git a/cmd/pinchtab/cmd_bridge.go b/cmd/pinchtab/cmd_bridge.go
index eb0466bf..f2a2e20a 100644
--- a/cmd/pinchtab/cmd_bridge.go
+++ b/cmd/pinchtab/cmd_bridge.go
@@ -10,6 +10,7 @@ import (
"syscall"
"time"
+ "github.com/pinchtab/pinchtab/internal/assets"
"github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/handlers"
@@ -24,6 +25,7 @@ func runBridgeServer(cfg *config.RuntimeConfig) {
// Create a bridge instance with lazy initialization
// Chrome will be initialized on first request via ensureChrome()
bridgeInstance := bridge.New(context.Background(), nil, cfg)
+ bridgeInstance.StealthScript = assets.StealthScript
mux := http.NewServeMux()
diff --git a/internal/dashboard/dashboard/profiles.js b/internal/dashboard/dashboard/profiles.js
index d4c6c379..5c9bcb70 100644
--- a/internal/dashboard/dashboard/profiles.js
+++ b/internal/dashboard/dashboard/profiles.js
@@ -184,7 +184,7 @@ async function doCreateProfile() {
body: JSON.stringify(body)
});
} else {
- res = await fetch('/profiles/create', {
+ res = await fetch('/profiles', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
@@ -252,12 +252,12 @@ function updateLaunchCommand() {
const port = document.getElementById('launch-port').value.trim();
const headless = !!(document.getElementById('launch-headless') && document.getElementById('launch-headless').checked);
- // Get profile ID
- const profileId = profileByName[name] ? profileByName[name].id : '';
+ // Prefer profile ID, fall back to name if ID is unavailable.
+ const profileId = profileByName[name] ? profileByName[name].id : name;
- // Modern API: POST /instances/launch with profileId + mode + port
+ // Canonical API: POST /instances/start with profileId + mode + port
const mode = headless ? 'headless' : 'headed';
- const curlCmd = 'curl -X POST http://localhost:9867/instances/launch '
+ const curlCmd = 'curl -X POST http://localhost:9867/instances/start '
+ '-H "Content-Type: application/json" '
+ '-d \'{\"profileId\":\"' + profileId + '\",\"mode\":\"' + mode + '\",\"port\":\"' + (port || 'auto') + '"}\'';
@@ -310,20 +310,19 @@ async function doLaunch() {
return;
}
- // Get profile ID from profileByName map
- const profileId = profileByName[name] ? profileByName[name].id : '';
+ // Prefer profile ID, fall back to name if ID is unavailable.
+ const profileId = profileByName[name] ? profileByName[name].id : name;
saveProfilePort(name, port);
setLaunchHeadlessPref(headless);
closeLaunchModal();
try {
- const res = await fetch('/instances/launch', {
+ const res = await fetch('/instances/start', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
profileId: profileId,
- name: name,
mode: headless ? 'headless' : 'headed',
port: port
})
@@ -341,7 +340,7 @@ async function doLaunch() {
async function deleteProfile(name) {
if (!await appConfirm('Delete profile "' + name + '"? This removes all data.', '🗑️ Delete Profile')) return;
- await fetch('/profiles/' + name, { method: 'DELETE' });
+ await fetch('/profiles/' + encodeURIComponent(name), { method: 'DELETE' });
loadProfiles();
}
@@ -373,7 +372,7 @@ function pollInstanceStatus(id) {
async function stopInstance(id) {
if (!await appConfirm('Stop instance ' + id + '?', '⏹ Stop Instance')) return;
- await fetch('/instances/' + id + '/stop', { method: 'POST' });
+ await fetch('/instances/' + encodeURIComponent(id) + '/stop', { method: 'POST' });
setTimeout(loadProfiles, 1000);
}
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 26345323..820ad37f 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -148,6 +148,16 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
_ = bridge.SetResourceBlocking(tCtx, blockPatterns)
}
+ if err := bridge.NavigatePage(tCtx, req.URL); err != nil {
+ code := 500
+ errMsg := err.Error()
+ if strings.Contains(errMsg, "invalid URL") || strings.Contains(errMsg, "Cannot navigate to invalid URL") || strings.Contains(errMsg, "ERR_INVALID_URL") {
+ code = 400
+ }
+ web.Error(w, code, fmt.Errorf("navigate: %w", err))
+ return
+ }
+
var url string
_ = chromedp.Run(tCtx, chromedp.Location(&url))
title := bridge.WaitForTitle(tCtx, titleWait)
@@ -351,6 +361,16 @@ func (h *Handlers) HandleTab(w http.ResponseWriter, r *http.Request) {
return
}
+ if req.URL != "" && req.URL != "about:blank" {
+ tCtx, tCancel := context.WithTimeout(ctx, h.Config.NavigateTimeout)
+ defer tCancel()
+ if err := bridge.NavigatePage(tCtx, req.URL); err != nil {
+ _ = h.Bridge.CloseTab(newTargetID)
+ web.Error(w, 500, fmt.Errorf("navigate: %w", err))
+ return
+ }
+ }
+
var curURL, title string
_ = chromedp.Run(ctx, chromedp.Location(&curURL), chromedp.Title(&title))
diff --git a/internal/orchestrator/ports.go b/internal/orchestrator/ports.go
index 3bb84b52..ff751f8f 100644
--- a/internal/orchestrator/ports.go
+++ b/internal/orchestrator/ports.go
@@ -69,6 +69,9 @@ func (pa *PortAllocator) ReleasePort(port int) {
defer pa.mu.Unlock()
delete(pa.allocated, port)
+ if port >= pa.start && port <= pa.end {
+ pa.nextCandidate = port
+ }
slog.Debug("released port", "port", port)
}
diff --git a/scripts/check.sh b/scripts/check.sh
index ece5a29a..c74c44aa 100755
--- a/scripts/check.sh
+++ b/scripts/check.sh
@@ -55,6 +55,12 @@ echo -e "\n${YELLOW}📊 Coverage Summary:${NC}"
go tool cover -func=coverage.out | tail -1
success_step "Tests with Coverage"
+check_step "Integration Tests"
+if ! go test -tags integration -v -timeout 10m ./tests/integration/; then
+ error_step "Integration Tests"
+fi
+success_step "Integration Tests"
+
check_step "Lint Check"
LINT_CMD=""
if command -v golangci-lint >/dev/null 2>&1; then
diff --git a/tests/integration/eval_test.go b/tests/integration/eval_test.go
index 692f1bfc..711fb925 100644
--- a/tests/integration/eval_test.go
+++ b/tests/integration/eval_test.go
@@ -9,7 +9,10 @@ import (
// E1: Simple eval
func TestEval_Simple(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpPost(t, "/evaluate", map[string]string{"expression": "1+1"})
+ code, body := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
+ "expression": "1+1",
+ })
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -22,7 +25,10 @@ func TestEval_Simple(t *testing.T) {
// E2: DOM eval
func TestEval_DOM(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpPost(t, "/evaluate", map[string]string{"expression": "document.title"})
+ code, body := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
+ "expression": "document.title",
+ })
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
diff --git a/tests/integration/snapshot_test.go b/tests/integration/snapshot_test.go
index 4ab32800..071103e4 100644
--- a/tests/integration/snapshot_test.go
+++ b/tests/integration/snapshot_test.go
@@ -4,15 +4,27 @@ package integration
import (
"encoding/json"
+ "net/url"
"os"
"strings"
"testing"
)
+func snapshotPath(path string) string {
+ if currentTabID == "" {
+ return path
+ }
+ sep := "?"
+ if strings.Contains(path, "?") {
+ sep = "&"
+ }
+ return path + sep + "tabId=" + url.QueryEscape(currentTabID)
+}
+
// S1: Basic snapshot
func TestSnapshot_Basic(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/snapshot")
+ code, body := httpGet(t, snapshotPath("/snapshot"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -29,7 +41,7 @@ func TestSnapshot_Basic(t *testing.T) {
// S2: Interactive filter
func TestSnapshot_Interactive(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/snapshot?filter=interactive")
+ code, body := httpGet(t, snapshotPath("/snapshot?filter=interactive"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -43,7 +55,7 @@ func TestSnapshot_Interactive(t *testing.T) {
// S4: Text format
func TestSnapshot_TextFormat(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/snapshot?format=text")
+ code, body := httpGet(t, snapshotPath("/snapshot?format=text"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -60,7 +72,7 @@ func TestSnapshot_TextFormat(t *testing.T) {
// S5: YAML format
func TestSnapshot_YAMLFormat(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/snapshot?format=yaml")
+ code, body := httpGet(t, snapshotPath("/snapshot?format=yaml"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -73,7 +85,7 @@ func TestSnapshot_YAMLFormat(t *testing.T) {
// S3: Depth filter
func TestSnapshot_Depth(t *testing.T) {
navigate(t, "https://example.com")
- code, _ := httpGet(t, "/snapshot?depth=2")
+ code, _ := httpGet(t, snapshotPath("/snapshot?depth=2"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -82,7 +94,7 @@ func TestSnapshot_Depth(t *testing.T) {
// S5 (compact): maxTokens
func TestSnapshot_MaxTokens(t *testing.T) {
navigate(t, "https://example.com")
- code, _ := httpGet(t, "/snapshot?maxTokens=500")
+ code, _ := httpGet(t, snapshotPath("/snapshot?maxTokens=500"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -148,7 +160,7 @@ func TestSnapshot_NoTab(t *testing.T) {
// S8: Snapshot file output
func TestSnapshot_FileOutput(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/snapshot?output=file")
+ code, body := httpGet(t, snapshotPath("/snapshot?output=file"))
if code != 200 {
t.Fatalf("expected 200, got %d (body: %s)", code, body)
}
@@ -173,7 +185,7 @@ func TestSnapshot_RefStability(t *testing.T) {
navigate(t, "https://example.com")
// Get initial snapshot with interactive filter
- code, body := httpGet(t, "/snapshot?filter=interactive")
+ code, body := httpGet(t, snapshotPath("/snapshot?filter=interactive"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -181,13 +193,14 @@ func TestSnapshot_RefStability(t *testing.T) {
// Perform an action (press a key) to trigger potential ref changes
_, _ = httpPost(t, "/action", map[string]string{
- "kind": "press",
- "key": "Escape",
+ "tabId": currentTabID,
+ "kind": "press",
+ "key": "Escape",
})
// Ignore result - just testing snapshot stability
// Get snapshot again after action
- code, body = httpGet(t, "/snapshot?filter=interactive")
+ code, body = httpGet(t, snapshotPath("/snapshot?filter=interactive"))
if code != 200 {
t.Fatalf("expected 200 after action, got %d", code)
}
@@ -211,7 +224,7 @@ func TestSnapshot_DiffMode(t *testing.T) {
navigate(t, "https://example.com")
// First snapshot call - stores state
- code, body1 := httpGet(t, "/snapshot")
+ code, body1 := httpGet(t, snapshotPath("/snapshot"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -223,7 +236,7 @@ func TestSnapshot_DiffMode(t *testing.T) {
}
// Second snapshot call with diff=true
- code, body2 := httpGet(t, "/snapshot?diff=true")
+ code, body2 := httpGet(t, snapshotPath("/snapshot?diff=true"))
if code != 200 {
t.Fatalf("expected 200 for diff snapshot, got %d", code)
}
@@ -251,7 +264,7 @@ func TestSnapshot_DiffFirstCall(t *testing.T) {
navigate(t, "https://example.com")
// Call /snapshot?diff=true immediately (first call with diff=true)
- code, body := httpGet(t, "/snapshot?diff=true")
+ code, body := httpGet(t, snapshotPath("/snapshot?diff=true"))
if code != 200 {
t.Fatalf("expected 200 for first diff call, got %d", code)
}
@@ -276,7 +289,7 @@ func TestSnapshot_LargePage(t *testing.T) {
navigate(t, url)
// Call /snapshot on the large page
- code, body := httpGet(t, "/snapshot")
+ code, body := httpGet(t, snapshotPath("/snapshot"))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
diff --git a/tests/integration/stealth_test.go b/tests/integration/stealth_test.go
index 9b349b8d..bc2602aa 100644
--- a/tests/integration/stealth_test.go
+++ b/tests/integration/stealth_test.go
@@ -15,6 +15,7 @@ func TestStealth_WebdriverUndefined(t *testing.T) {
// This is more reliable than a fixed wait across different CI runners
for retry := 0; retry < 10; retry++ {
code, body := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
"expression": "navigator.webdriver === undefined",
})
if code != 200 {
@@ -43,6 +44,7 @@ func TestStealth_PluginsPresent(t *testing.T) {
// Poll for stealth injection instead of fixed sleep
for retry := 0; retry < 10; retry++ {
code, body := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
"expression": "navigator.plugins.length > 0",
})
if code != 200 {
@@ -66,6 +68,7 @@ func TestStealth_ChromeRuntimePresent(t *testing.T) {
// Poll for stealth injection instead of fixed sleep
for retry := 0; retry < 10; retry++ {
code, body := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
"expression": "!!window.chrome.runtime",
})
if code != 200 {
@@ -89,6 +92,7 @@ func TestStealth_FingerprintRotate(t *testing.T) {
// Get initial user agent
code1, body1 := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
"expression": "navigator.userAgent",
})
if code1 != 200 {
@@ -98,7 +102,8 @@ func TestStealth_FingerprintRotate(t *testing.T) {
// Rotate fingerprint with OS specified
code2, body2 := httpPost(t, "/fingerprint/rotate", map[string]string{
- "os": "windows",
+ "os": "windows",
+ "tabId": currentTabID,
})
if code2 != 200 {
t.Fatalf("expected 200 for fingerprint rotate, got %d (body: %s)", code2, body2)
@@ -106,6 +111,7 @@ func TestStealth_FingerprintRotate(t *testing.T) {
// Get new user agent after rotation
code3, body3 := httpPost(t, "/evaluate", map[string]string{
+ "tabId": currentTabID,
"expression": "navigator.userAgent",
})
if code3 != 200 {
@@ -131,7 +137,7 @@ func TestStealth_FingerprintRotateRandom(t *testing.T) {
navigate(t, "https://example.com")
// Rotate fingerprint with empty body (random OS)
- code, body := httpPost(t, "/fingerprint/rotate", map[string]string{})
+ code, body := httpPost(t, "/fingerprint/rotate", map[string]string{"tabId": currentTabID})
if code != 200 {
t.Fatalf("expected 200 for fingerprint rotate (random), got %d (body: %s)", code, body)
}
diff --git a/tests/integration/text_test.go b/tests/integration/text_test.go
index 08fd0e68..ac9ea8e2 100644
--- a/tests/integration/text_test.go
+++ b/tests/integration/text_test.go
@@ -4,6 +4,7 @@ package integration
import (
"encoding/json"
+ "net/url"
"strings"
"testing"
)
@@ -11,7 +12,7 @@ import (
// T1: Readability mode
func TestText_Readability(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/text")
+ code, body := httpGet(t, "/text?tabId="+url.QueryEscape(currentTabID))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -24,7 +25,7 @@ func TestText_Readability(t *testing.T) {
// T2: Raw mode
func TestText_Raw(t *testing.T) {
navigate(t, "https://example.com")
- code, body := httpGet(t, "/text?mode=raw")
+ code, body := httpGet(t, "/text?mode=raw&tabId="+url.QueryEscape(currentTabID))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}
@@ -103,7 +104,7 @@ func TestText_NoTab(t *testing.T) {
// T5: Token efficiency (real-world content)
func TestText_TokenEfficiency(t *testing.T) {
navigate(t, "https://google.com")
- code, body := httpGet(t, "/text")
+ code, body := httpGet(t, "/text?tabId="+url.QueryEscape(currentTabID))
if code != 200 {
t.Fatalf("expected 200, got %d", code)
}