Skip to content

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

test: add handler + config tests for error paths

Mariobc7d50bbcaf8
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 4603440f..062ca14e 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -150,3 +150,15 @@ func TestLoadConfigFile(t *testing.T) {
t.Errorf("file ActionTimeout = %v, want 60s", cfg.ActionTimeout)
}
}
+
+func TestListenAddr(t *testing.T) {
+ cfg := &RuntimeConfig{Bind: "127.0.0.1", Port: "9867"}
+ if got := cfg.ListenAddr(); got != "127.0.0.1:9867" {
+ t.Errorf("expected 127.0.0.1:9867, got %s", got)
+ }
+
+ cfg = &RuntimeConfig{Bind: "0.0.0.0", Port: "8080"}
+ if got := cfg.ListenAddr(); got != "0.0.0.0:8080" {
+ t.Errorf("expected 0.0.0.0:8080, got %s", got)
+ }
+}
diff --git a/internal/handlers/handler_download_test.go b/internal/handlers/handler_download_test.go
new file mode 100644
index 00000000..3a6b6f61
--- /dev/null
+++ b/internal/handlers/handler_download_test.go
@@ -0,0 +1,29 @@
+package handlers
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+func TestHandleDownload_MissingURL(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/download", nil)
+ w := httptest.NewRecorder()
+ h.HandleDownload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
+ }
+}
+
+func TestHandleDownload_EmptyURL(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/download?url=", nil)
+ w := httptest.NewRecorder()
+ h.HandleDownload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400 for empty URL, got %d", w.Code)
+ }
+}
diff --git a/internal/handlers/handler_media_test.go b/internal/handlers/handler_media_test.go
new file mode 100644
index 00000000..b37b663e
--- /dev/null
+++ b/internal/handlers/handler_media_test.go
@@ -0,0 +1,60 @@
+package handlers
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+func TestHandleScreenshot_NoTab(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/screenshot", nil)
+ w := httptest.NewRecorder()
+ h.HandleScreenshot(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleScreenshot_InvalidQuality(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/screenshot?quality=abc", nil)
+ w := httptest.NewRecorder()
+ h.HandleScreenshot(w, req)
+ // Should still return 404 (no tab), not crash on bad quality
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleText_NoTab(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/text", nil)
+ w := httptest.NewRecorder()
+ h.HandleText(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleText_WithTabId(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/text?tabId=nonexistent", nil)
+ w := httptest.NewRecorder()
+ h.HandleText(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleText_RawMode(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/text?mode=raw", nil)
+ w := httptest.NewRecorder()
+ h.HandleText(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
diff --git a/internal/handlers/handler_snapshot_test.go b/internal/handlers/handler_snapshot_test.go
new file mode 100644
index 00000000..35e98e7c
--- /dev/null
+++ b/internal/handlers/handler_snapshot_test.go
@@ -0,0 +1,50 @@
+package handlers
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+func TestHandleSnapshot_NoTab(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/snapshot", nil)
+ w := httptest.NewRecorder()
+ h.HandleSnapshot(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleSnapshot_WithFilter(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/snapshot?filter=interactive&format=compact", nil)
+ w := httptest.NewRecorder()
+ h.HandleSnapshot(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleSnapshot_FileOutput_NoTab(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{StateDir: "/tmp"}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/snapshot?output=file", nil)
+ w := httptest.NewRecorder()
+ h.HandleSnapshot(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Errorf("expected 404, got %d", w.Code)
+ }
+}
+
+func TestHandleSnapshot_PathTraversal(t *testing.T) {
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{StateDir: "/tmp/pinchtab-test"}, nil, nil, nil)
+ req := httptest.NewRequest("GET", "/snapshot?output=file&path=../../etc/passwd", nil)
+ w := httptest.NewRecorder()
+ h.HandleSnapshot(w, req)
+ // Should fail — either 404 (no tab) or 400 (path traversal)
+ if w.Code == http.StatusOK {
+ t.Error("expected error for path traversal")
+ }
+}
diff --git a/internal/handlers/handler_upload_test.go b/internal/handlers/handler_upload_test.go
new file mode 100644
index 00000000..1051b3da
--- /dev/null
+++ b/internal/handlers/handler_upload_test.go
@@ -0,0 +1,45 @@
+package handlers
+
+import (
+ "bytes"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+func TestHandleUpload_BadJSON(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ req := httptest.NewRequest("POST", "/upload", bytes.NewReader([]byte("not json")))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ h.HandleUpload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400 for bad JSON, got %d", w.Code)
+ }
+}
+
+func TestHandleUpload_EmptyPaths(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ body := `{"selector": "input[type=file]"}`
+ req := httptest.NewRequest("POST", "/upload", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ h.HandleUpload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400 for empty paths, got %d", w.Code)
+ }
+}
+
+func TestHandleUpload_NonexistentPath(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ body := `{"selector": "input[type=file]", "paths": ["/tmp/nonexistent-file-12345.jpg"]}`
+ req := httptest.NewRequest("POST", "/upload", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ h.HandleUpload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400 for nonexistent path, got %d", w.Code)
+ }
+}