High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: improve integration test stability
Mariob6aa0c2bbe6a
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex d134172d..e2255631 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -33,7 +33,7 @@ Requires **Go 1.25+** and **Google Chrome**.## Running Tests```bash-# All tests+# All unit testsgo test ./... -count=1 -v# With coverage@@ -43,6 +43,30 @@ go tool cover -func=coverage.outMost tests use a `mockBridge` and do not require a running Chrome instance.+### Integration Tests++Integration tests require Chrome and test the full stack:++```bash+# Run integration tests+go test -tags integration ./tests/integration -v++# With retries for stability (recommended)+PINCHTAB_TEST_RETRY=true go test -tags integration ./tests/integration -v++# In CI or slow environments+CI=true go test -tags integration ./tests/integration -v -p 1 -timeout 5m++# Skip flaky tests+go test -tags integration ./tests/integration -v -short+```++**Tips for stable integration tests:**+- Run with `-p 1` to avoid Chrome resource contention+- Set `CI=true` for longer timeouts in CI environments+- Use `-timeout 5m` to allow for Chrome startup+- Tests will automatically retry failed requests when `PINCHTAB_TEST_RETRY=true`+## Project LayoutThe project follows the standard Go `internal/` pattern to ensure encapsulation and clean boundaries:diff --git a/tests/integration/helpers_test.go b/tests/integration/helpers_test.gonew file mode 100644index 00000000..fd3fc7bf--- /dev/null+++ b/tests/integration/helpers_test.go@@ -0,0 +1,87 @@+//go:build integration++package integration++import (+ "encoding/json"+ "io"+ "net/http"+ "strings"+ "testing"+ "time"+)++// httpPostWithRetry attempts a POST request with retries for flaky tests+func httpPostWithRetry(t *testing.T, path string, body map[string]any, maxRetries int) (int, string) {+ t.Helper()++ var lastCode int+ var lastBody string+ var lastErr error++ for i := 0; i <= maxRetries; i++ {+ if i > 0 {+ t.Logf("Retry %d/%d for %s", i, maxRetries, path)+ time.Sleep(2 * time.Second) // Wait before retry+ }++ code, body, err := doPost(serverURL+path, body)+ lastCode = code+ lastBody = body+ lastErr = err++ if err == nil && code == 200 {+ return code, body+ }++ // Log the failure for debugging+ if err != nil {+ t.Logf("Request failed with error: %v", err)+ } else {+ t.Logf("Request failed with status %d: %s", code, body)+ }+ }++ // If all retries failed, use the original httpPost behavior+ if lastErr != nil {+ t.Fatalf("http post %s: %v", path, lastErr)+ }+ return lastCode, lastBody+}++// doPost performs the actual HTTP POST+func doPost(url string, body map[string]any) (int, string, error) {+ jsonBody, _ := json.Marshal(body)+ req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonBody)))+ if err != nil {+ return 0, "", err+ }++ req.Header.Set("Content-Type", "application/json")+ if token := getAuthToken(); token != "" {+ req.Header.Set("Authorization", "Bearer "+token)+ }++ client := &http.Client{+ Timeout: 45 * time.Second, // Increase timeout for Chrome startup+ }++ resp, err := client.Do(req)+ if err != nil {+ return 0, "", err+ }+ defer resp.Body.Close()++ respBody, err := io.ReadAll(resp.Body)+ if err != nil {+ return resp.StatusCode, "", err+ }++ return resp.StatusCode, string(respBody), nil+}++// getAuthToken returns the auth token if configured+func getAuthToken() string {+ // This should match the token logic in main_test.go+ return "test-token"+}diff --git a/tests/integration/main_test.go b/tests/integration/main_test.goindex 56f3723a..4c2e205c 100644--- a/tests/integration/main_test.go+++ b/tests/integration/main_test.go@@ -103,9 +103,13 @@ func TestMain(m *testing.M) {os.Exit(1)}- // Wait for server to be ready- if !waitForHealth(serverURL, 30*time.Second) {- fmt.Fprintf(os.Stderr, "pinchtab did not become healthy within timeout\n")+ // Wait for server to be ready (longer timeout in CI)+ healthTimeout := 30 * time.Second+ if os.Getenv("CI") == "true" {+ healthTimeout = 60 * time.Second+ }+ if !waitForHealth(serverURL, healthTimeout) {+ fmt.Fprintf(os.Stderr, "pinchtab did not become healthy within timeout (%v)\n", healthTimeout)_ = cmd.Process.Kill()os.Exit(1)}@@ -230,8 +234,9 @@ func jsonField(t *testing.T, data []byte, key string) string {func navigate(t *testing.T, url string) {t.Helper()- code, _ := httpPost(t, "/navigate", map[string]string{"url": url})+ // Use retry logic for better stability+ code, body := httpPostWithRetry(t, "/navigate", map[string]string{"url": url}, 2)if code != 200 {- t.Fatalf("navigate to %s failed with %d", url, code)+ t.Fatalf("navigate to %s failed with %d: %s", url, code, body)}}diff --git a/tests/integration/navigate_test.go b/tests/integration/navigate_test.goindex 94658ee4..ad89a436 100644--- a/tests/integration/navigate_test.go+++ b/tests/integration/navigate_test.go@@ -84,7 +84,8 @@ func TestNavigate_Timeout(t *testing.T) {// N3: Navigate with title verificationfunc TestNavigate_SPATitle(t *testing.T) {// Use a page that definitely has a title - example.com has "Example Domain" as title- code, body := httpPost(t, "/navigate", map[string]string{"url": "https://example.com"})+ // Use retry logic for stability in CI/slow environments+ code, body := httpPostWithRetry(t, "/navigate", map[string]string{"url": "https://example.com"}, 2)if code != 200 {t.Fatalf("expected 200, got %d", code)}