package app import ( "context" "errors" "fmt" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync/atomic" "testing" "time" ) func TestBrowserWaitKeepsPhaseAndCallerDeadlinesSeparate(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() err := browserWait(ctx, 5*time.Millisecond, time.Millisecond, func(ctx context.Context) (bool, error) { <-ctx.Done() return false, ctx.Err() }) if err != nil || ctx.Err() != nil { t.Fatalf("phase timeout killed parent: %v, %v", err, ctx.Err()) } cancel() if err := browserWait(ctx, time.Second, time.Millisecond, func(ctx context.Context) (bool, error) { return true, nil }); !errors.Is(err, context.Canceled) { t.Fatalf("ignored caller cancellation: %v", err) } want := errors.New("browser disconnected") if err := browserWait(context.Background(), time.Second, time.Millisecond, func(context.Context) (bool, error) { return false, want }); !errors.Is(err, want) { t.Fatalf("hid browser error: %v", err) } } func TestBrowserQueueCancellation(t *testing.T) { n := newNetwork(true) defer n.closeIdle() b := &Browser{path: "not launched", network: n, slots: make(chan struct{}, 1)} b.slots <- struct{}{} ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond) defer cancel() _, _, err := b.render(ctx, "https://example.com", false, "", "") if !errors.Is(err, context.DeadlineExceeded) || len(b.slots) != 1 { t.Fatalf("queue cancellation changed occupied slot: %v", err) } } func TestKagiMissingResultsDiagnosis(t *testing.T) { for _, tc := range []struct{ html, final, tag string }{ {`sign in to kagi`, "https://kagi.com/signin", "SEARCH_SESSION_REJECTED"}, {`
`, "https://kagi.com/search", "SEARCH_SESSION_REJECTED"}, {`

no results; sign in

`, "https://kagi.com/search", "SEARCH_UPSTREAM_ERROR"}, {`

unrecognized search markup

`, "https://kagi.com/search", "SEARCH_UPSTREAM_ERROR"}, } { var api *APIError if err := kagiPageError(tc.html, tc.final); !errors.As(err, &api) || api.Tag != tc.tag { t.Errorf("%s: %v", tc.html, err) } } } func TestChallengeContentIsNotEvidence(t *testing.T) { for _, html := range []string{ `Just a moment...`, `Attention Required`, `
checking your browser
`, `
checking your browser
`, ``, } { _, err := htmlDocument("https://example.com", "https://example.com", []byte(html)) var api *APIError if !errors.As(err, &api) || api.Tag != "CRAWL_CHALLENGE" { t.Errorf("challenge accepted as evidence: %s, %v", html, err) } } if isAccessChallenge("cloudflare documentation", "

how access challenges work

") { t.Fatal("ordinary content classified as a challenge") } } // These cases exercise real Chrome and the guarded proxy, without external // requests or model calls. Keep opt-in consistent with TestLiveBrowser. func TestLiveBrowserRecovery(t *testing.T) { if os.Getenv("LIVE_BROWSER") != "1" { t.Skip("set LIVE_BROWSER=1 and BROWSER_PATH to exercise chrome") } if os.Getenv("BROWSER_PATH") == "" { t.Fatal("BROWSER_PATH is required") } profiles := t.TempDir() t.Setenv("TMPDIR", profiles) var blockedHits, scriptHits atomic.Int32 page := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") switch r.URL.Path { case "/never": <-r.Context().Done() case "/hung", "/empty": body := "" if r.URL.Path == "/hung" { body = "usable evidence despite an unfinished third-party iframe" } fmt.Fprintf(w, `
%s
`, body) case "/stream": fmt.Fprint(w, `
`) case "/missing": fmt.Fprint(w, `sign in to kagi
`) case "/hydrate": fmt.Fprint(w, `
loading
`) case "/challenge", "/resolved": w.WriteHeader(http.StatusForbidden) fmt.Fprint(w, `just a moment
checking browser
`) if r.URL.Path == "/resolved" { fmt.Fprint(w, ``) } case "/resources": fmt.Fprint(w, `
rendering
`) case "/tracker.avif": blockedHits.Add(1) case "/necessary.js": scriptHits.Add(1) w.Header().Set("Content-Type", "text/javascript") fmt.Fprint(w, `document.querySelector('main').textContent='necessary javascript ran'`) case "/http-error": w.WriteHeader(http.StatusBadGateway) fmt.Fprint(w, `upstream unavailable; this is not source evidence`) default: fmt.Fprint(w, `
ordinary source evidence
`) } })) defer page.Close() n := newNetwork(true) defer n.transport.CloseIdleConnections() b := &Browser{path: os.Getenv("BROWSER_PATH"), network: n, slots: make(chan struct{}, 2)} for _, tc := range []struct { path, want, tag string kagi bool max time.Duration }{ {"/hung", "
usable evidence despite an unfinished third-party iframe
", "", false, 20 * time.Second}, {"/empty", "", "REQUEST_TIMEOUT", false, 20 * time.Second}, {"/stream", `class="search-result"`, "", true, 6 * time.Second}, {"/missing", `type="password"`, "", true, 15 * time.Second}, {"/hydrate", "
hydration stage 4....
", "", false, 6 * time.Second}, {"/resolved", "
resolved source evidence
", "", false, 8 * time.Second}, {"/challenge", "", "CRAWL_CHALLENGE", false, 18 * time.Second}, {"/resources", "
necessary javascript ran
", "", false, 6 * time.Second}, {"/http-error", "", "CRAWL_HTTP_ERROR", false, 6 * time.Second}, } { t.Run(tc.path, func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), tc.max+5*time.Second) defer cancel() start := time.Now() html, final, err := b.render(ctx, page.URL+tc.path, tc.kagi, "", "") elapsed := time.Since(start) if tc.tag != "" { var api *APIError if !errors.As(err, &api) || api.Tag != tc.tag || html != "" { t.Fatalf("want %s without content, got %q, %v", tc.tag, html, err) } } else if err != nil || !strings.Contains(html, tc.want) || final != page.URL+tc.path { t.Fatalf("render mismatch: url=%s error=%v html=%s", final, err, html) } if elapsed > tc.max { t.Errorf("phase wait was not bounded: %s", elapsed) } if len(b.slots) != 0 { t.Fatal("browser slot leaked") } left, _ := filepath.Glob(filepath.Join(profiles, "chromedp-runner*")) if len(left) != 0 { t.Fatalf("temporary browser profiles leaked: %v", left) } t.Logf("elapsed=%s", elapsed.Round(time.Millisecond)) }) } if blockedHits.Load() != 0 || scriptHits.Load() != 1 { t.Errorf("resource blocking: image=%d necessary script=%d", blockedHits.Load(), scriptHits.Load()) } t.Run("caller cancellation", func(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() _, _, err := b.render(ctx, page.URL+"/hung", false, "", "") if !errors.Is(err, context.DeadlineExceeded) || len(b.slots) != 0 { t.Fatalf("caller cancellation did not terminate browser: %v", err) } }) t.Run("navigation failure", func(t *testing.T) { dead := httptest.NewServer(http.NotFoundHandler()) dead.Close() // HTTPS forces a failed CONNECT/navigation, rather than the proxy's // ordinary HTTP 502 document (covered separately above). _, _, err := b.render(context.Background(), strings.Replace(dead.URL, "http:", "https:", 1), false, "", "") if err == nil || errors.Is(err, context.DeadlineExceeded) { t.Fatalf("navigation failure was hidden: %v", err) } }) }