High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
test: coverage to 30.1% — cookie, stealth, handler, state tests
L90fef7c02dcd
diff --git a/handler_cookies_test.go b/handler_cookies_test.gonew file mode 100644index 00000000..b2549425--- /dev/null+++ b/handler_cookies_test.go@@ -0,0 +1,53 @@+package main++import (+ "bytes"+ "encoding/json"+ "net/http/httptest"+ "testing"+)++func TestHandleSetCookies_InvalidJSON(t *testing.T) {+ b := &Bridge{}+ req := httptest.NewRequest("POST", "/cookies", bytes.NewReader([]byte(`not json`)))+ w := httptest.NewRecorder()++ b.handleSetCookies(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestHandleSetCookies_NoTab(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ body := `{"url":"https://example.com","cookies":[{"name":"a","value":"b"}],"tabId":"nonexistent"}`+ req := httptest.NewRequest("POST", "/cookies", bytes.NewReader([]byte(body)))+ w := httptest.NewRecorder()++ b.handleSetCookies(w, req)++ if w.Code != 404 {+ t.Errorf("expected 404, got %d", w.Code)+ }+}++func TestHandleGetCookies_NameFilter(t *testing.T) {+ // Verify the name query param is parsed (can't test actual filtering without Chrome)+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ req := httptest.NewRequest("GET", "/cookies?name=session_id&tabId=nonexistent", nil)+ w := httptest.NewRecorder()++ b.handleGetCookies(w, req)++ if w.Code != 404 {+ t.Errorf("expected 404, got %d", w.Code)+ }+ var resp map[string]any+ json.NewDecoder(w.Body).Decode(&resp)+ if resp["error"] == nil {+ t.Error("expected error in response")+ }+}diff --git a/handler_stealth_test.go b/handler_stealth_test.gonew file mode 100644index 00000000..5b1a9d5d--- /dev/null+++ b/handler_stealth_test.go@@ -0,0 +1,104 @@+package main++import (+ "bytes"+ "net/http/httptest"+ "testing"+)++func TestHandleStealthStatus_NoTab_Returns200(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ req := httptest.NewRequest("GET", "/stealth/status", nil)+ w := httptest.NewRecorder()++ b.handleStealthStatus(w, req)++ // With no tab, returns static analysis (200)+ if w.Code != 200 {+ t.Errorf("expected 200, got %d", w.Code)+ }+}++func TestHandleStealthStatus_NonexistentTab(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ req := httptest.NewRequest("GET", "/stealth/status?tabId=nonexistent", nil)+ w := httptest.NewRecorder()++ b.handleStealthStatus(w, req)++ // With explicit nonexistent tabId, still returns static (no tab found falls through)+ if w.Code != 200 {+ t.Errorf("expected 200, got %d", w.Code)+ }+}++func TestHandleFingerprintRotate_InvalidJSON(t *testing.T) {+ b := &Bridge{}+ req := httptest.NewRequest("POST", "/fingerprint/rotate", bytes.NewReader([]byte(`not json`)))+ w := httptest.NewRecorder()++ b.handleFingerprintRotate(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestGenerateFingerprint_Windows(t *testing.T) {+ fp := generateFingerprint(fingerprintRequest{OS: "windows"})+ if fp.Platform != "Win32" {+ t.Errorf("expected Win32, got %q", fp.Platform)+ }+ if fp.UserAgent == "" {+ t.Error("expected non-empty user agent")+ }+ if fp.ScreenWidth == 0 {+ t.Error("expected non-zero screen width")+ }+}++func TestStaticStealthFeatures_HasEntries(t *testing.T) {+ features := staticStealthFeatures()+ if len(features) == 0 {+ t.Error("expected non-empty stealth features map")+ }+}++func TestGetStealthRecommendations_AllGood(t *testing.T) {+ features := map[string]bool{+ "navigator.webdriver": true,+ "navigator.plugins": true,+ "navigator.languages": true,+ "chrome.runtime": true,+ "permission.notification": true,+ }+ recs := getStealthRecommendations(features)+ // Recommendations include general hardening tips even when features pass+ if recs == nil {+ t.Error("expected non-nil recommendations slice")+ }+}++func TestGetStealthRecommendations_MissingFeatures(t *testing.T) {+ features := map[string]bool{+ "navigator.webdriver": false,+ }+ recs := getStealthRecommendations(features)+ if len(recs) == 0 {+ t.Error("expected recommendations for missing features")+ }+}++func TestSendStealthResponse(t *testing.T) {+ b := &Bridge{}+ features := map[string]bool{"navigator.webdriver": true}+ w := httptest.NewRecorder()++ b.sendStealthResponse(w, features, "TestAgent/1.0")++ if w.Code != 200 {+ t.Errorf("expected 200, got %d", w.Code)+ }+}diff --git a/handlers_extra_test.go b/handlers_extra_test.gonew file mode 100644index 00000000..52c3a3b5--- /dev/null+++ b/handlers_extra_test.go@@ -0,0 +1,109 @@+package main++import (+ "bytes"+ "net/http/httptest"+ "testing"+ "time"+)++func TestHandleHealth_Response(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ req := httptest.NewRequest("GET", "/health", nil)+ w := httptest.NewRecorder()++ b.handleHealth(w, req)++ if w.Code != 200 {+ t.Errorf("expected 200, got %d", w.Code)+ }+ if ct := w.Header().Get("Content-Type"); ct != "application/json" {+ t.Errorf("expected application/json, got %s", ct)+ }+}++func TestHandleNavigate_InvalidJSON(t *testing.T) {+ b := &Bridge{}+ req := httptest.NewRequest("POST", "/navigate", bytes.NewReader([]byte(`not json`)))+ w := httptest.NewRecorder()++ b.handleNavigate(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestHandleEvaluate_InvalidJSON(t *testing.T) {+ b := &Bridge{}+ req := httptest.NewRequest("POST", "/evaluate", bytes.NewReader([]byte(`not json`)))+ w := httptest.NewRecorder()++ b.handleEvaluate(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestHandleTab_InvalidJSON(t *testing.T) {+ b := &Bridge{}+ req := httptest.NewRequest("POST", "/tab", bytes.NewReader([]byte(`not json`)))+ w := httptest.NewRecorder()++ b.handleTab(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestHandleTab_InvalidAction(t *testing.T) {+ b := &Bridge{}+ body := `{"action":"invalid"}`+ req := httptest.NewRequest("POST", "/tab", bytes.NewReader([]byte(body)))+ w := httptest.NewRecorder()++ b.handleTab(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestHandleAction_InvalidJSON(t *testing.T) {+ b := &Bridge{}+ req := httptest.NewRequest("POST", "/action", bytes.NewReader([]byte(`not json`)))+ w := httptest.NewRecorder()++ b.handleAction(w, req)++ if w.Code != 400 {+ t.Errorf("expected 400, got %d", w.Code)+ }+}++func TestHandleShutdown_CallsFunc(t *testing.T) {+ called := make(chan bool, 1)+ doShutdown := func() { called <- true }+ b := &Bridge{}+ handler := b.handleShutdown(doShutdown)++ req := httptest.NewRequest("POST", "/shutdown", nil)+ w := httptest.NewRecorder()++ handler(w, req)++ if w.Code != 200 {+ t.Errorf("expected 200, got %d", w.Code)+ }++ // Shutdown runs in a goroutine after 100ms+ select {+ case <-called:+ // ok+ case <-time.After(500 * time.Millisecond):+ t.Error("expected doShutdown to be called within 500ms")+ }+}diff --git a/snapshot_extra_test.go b/snapshot_extra_test.gonew file mode 100644index 00000000..e89699a3--- /dev/null+++ b/snapshot_extra_test.go@@ -0,0 +1,77 @@+package main++import (+ "bytes"+ "net/http/httptest"+ "testing"+)++func TestHandleSnapshot_InvalidFilter(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ req := httptest.NewRequest("GET", "/snapshot?filter=bogus", nil)+ w := httptest.NewRecorder()++ b.handleSnapshot(w, req)++ if w.Code == 0 {+ t.Error("expected a response code")+ }+}++func TestHandleScreenshot_RawParam(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ req := httptest.NewRequest("GET", "/screenshot?raw=true&tabId=nonexistent", nil)+ w := httptest.NewRecorder()++ b.handleScreenshot(w, req)++ if w.Code != 404 {+ t.Errorf("expected 404, got %d", w.Code)+ }+}++func TestHandleNavigate_WithTimeout(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ body := `{"url":"https://example.com","timeout":5,"tabId":"nonexistent"}`+ req := httptest.NewRequest("POST", "/navigate", bytes.NewReader([]byte(body)))+ w := httptest.NewRecorder()++ b.handleNavigate(w, req)++ if w.Code != 404 {+ t.Errorf("expected 404, got %d", w.Code)+ }+}++func TestHandleNavigate_WithBlockImages(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ tr := true+ _ = tr+ body := `{"url":"https://example.com","blockImages":true,"tabId":"nonexistent"}`+ req := httptest.NewRequest("POST", "/navigate", bytes.NewReader([]byte(body)))+ w := httptest.NewRecorder()++ b.handleNavigate(w, req)++ if w.Code != 404 {+ t.Errorf("expected 404, got %d", w.Code)+ }+}++func TestHandleNavigate_WaitTitle(t *testing.T) {+ b := &Bridge{}+ b.tabs = make(map[string]*TabEntry)+ body := `{"url":"https://example.com","waitTitle":true,"tabId":"nonexistent"}`+ req := httptest.NewRequest("POST", "/navigate", bytes.NewReader([]byte(body)))+ w := httptest.NewRecorder()++ b.handleNavigate(w, req)++ if w.Code != 404 && w.Code != 400 {+ t.Errorf("expected 404 or 400, got %d", w.Code)+ }+}diff --git a/state_test.go b/state_test.goindex 14f8f38a..64a93343 100644--- a/state_test.go+++ b/state_test.go@@ -128,3 +128,73 @@ func TestRestoreState_InvalidJSON(t *testing.T) {// Should not panic on invalid JSONb.RestoreState()}++func TestWasUncleanExit_Crashed(t *testing.T) {+ tmp := t.TempDir()+ origProfile := profileDir+ profileDir = tmp+ defer func() { profileDir = origProfile }()++ defaultDir := filepath.Join(tmp, "Default")+ os.MkdirAll(defaultDir, 0755)+ os.WriteFile(filepath.Join(defaultDir, "Preferences"),+ []byte(`{"profile":{"exit_type":"Crashed","exited_cleanly":false}}`), 0644)++ if !wasUncleanExit() {+ t.Error("expected wasUncleanExit to return true for Crashed exit_type")+ }+}++func TestWasUncleanExit_Normal(t *testing.T) {+ tmp := t.TempDir()+ origProfile := profileDir+ profileDir = tmp+ defer func() { profileDir = origProfile }()++ defaultDir := filepath.Join(tmp, "Default")+ os.MkdirAll(defaultDir, 0755)+ os.WriteFile(filepath.Join(defaultDir, "Preferences"),+ []byte(`{"profile":{"exit_type":"Normal","exited_cleanly":true}}`), 0644)++ if wasUncleanExit() {+ t.Error("expected wasUncleanExit to return false for Normal exit_type")+ }+}++func TestWasUncleanExit_NoFile(t *testing.T) {+ tmp := t.TempDir()+ origProfile := profileDir+ profileDir = tmp+ defer func() { profileDir = origProfile }()++ if wasUncleanExit() {+ t.Error("expected wasUncleanExit to return false when no Preferences file exists")+ }+}++func TestClearChromeSessions(t *testing.T) {+ tmp := t.TempDir()+ origProfile := profileDir+ profileDir = tmp+ defer func() { profileDir = origProfile }()++ sessionsDir := filepath.Join(tmp, "Default", "Sessions")+ os.MkdirAll(sessionsDir, 0755)+ os.WriteFile(filepath.Join(sessionsDir, "Session_1"), []byte("data"), 0644)++ clearChromeSessions()++ if _, err := os.Stat(sessionsDir); !os.IsNotExist(err) {+ t.Error("expected Sessions dir to be removed")+ }+}++func TestClearChromeSessions_NoDir(t *testing.T) {+ tmp := t.TempDir()+ origProfile := profileDir+ profileDir = tmp+ defer func() { profileDir = origProfile }()++ // Should not panic when dir doesn't exist+ clearChromeSessions()+}