aistoy Claude Opus 4.7 commited on
Commit
d0051bb
Β·
1 Parent(s): 64f21ed

Exercise full search pipeline via respx mock HTTP layer

Browse files

Adds tests/integration/ driving the full search() β†’ engine.request()
β†’ network β†’ response β†’ classify β†’ health.update chain against
scripted HTTP responses. Fills the gap between parser fixture tests
(which skip request()) and live tests (which depend on real sites).

Six scenarios: happy path (with URL-shape assertion on the request),
timeout, connection error, engine opt-out, rate-limit β†’ cooldown β†’
subsequent skip, and 503 β†’ ANTI_BOT classification.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

CLAUDE.md CHANGED
@@ -90,6 +90,7 @@ Engines are adapted from SearXNG: change `from searx.*` β†’ `from scoutlet.*`, r
90
  tests/
91
  unit/ # Core logic tests (offline, no network)
92
  engines/ # Engine parser fixture tests (offline, uses saved HTML/JSON)
 
93
  fixtures/ # Minimal HTML/JSON fixtures per engine
94
  live/ # Network-dependent tests (gated by pytest.mark.live)
95
  ```
 
90
  tests/
91
  unit/ # Core logic tests (offline, no network)
92
  engines/ # Engine parser fixture tests (offline, uses saved HTML/JSON)
93
+ integration/ # Full-pipeline tests via respx (offline, exercises engine.request β†’ network β†’ response β†’ aggregate)
94
  fixtures/ # Minimal HTML/JSON fixtures per engine
95
  live/ # Network-dependent tests (gated by pytest.mark.live)
96
  ```
tests/integration/__init__.py ADDED
File without changes
tests/integration/conftest.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Shared fixtures for mock-HTTP integration tests.
2
+
3
+ These tests drive the full ``search()`` pipeline against scripted HTTP
4
+ responses via respx. The autouse ``reset_health`` fixture clears the
5
+ module-level health registry between tests so cooldowns don't leak.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from pathlib import Path
11
+
12
+ import pytest
13
+ import respx
14
+
15
+ from scoutlet.health import reset_default_registry
16
+ from scoutlet import network
17
+
18
+ FIXTURES = Path(__file__).parent.parent / "fixtures" / "engines"
19
+
20
+
21
+ @pytest.fixture
22
+ def respx_mock():
23
+ """Active respx mock scope. Real httpx calls inside raise."""
24
+ with respx.mock(assert_all_called=False) as mock:
25
+ yield mock
26
+
27
+
28
+ @pytest.fixture(autouse=True)
29
+ def reset_health():
30
+ """Clear health registry + close cached adapters before each test.
31
+
32
+ Health registry is module-level; without this, a cooldown from one
33
+ test bleeds into the next. Cached httpx clients are also reset so
34
+ respx's transport patches apply to a fresh client each test.
35
+ """
36
+ reset_default_registry()
37
+ network.close()
38
+ yield
39
+ network.close()
40
+
41
+
42
+ @pytest.fixture
43
+ def bing_success_html() -> str:
44
+ return (FIXTURES / "bing" / "success_minimal.html").read_text()
45
+
46
+
47
+ @pytest.fixture
48
+ def bing_captcha_html() -> str:
49
+ return (FIXTURES / "bing" / "captcha_minimal.html").read_text()
tests/integration/test_pipeline.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Mock-HTTP integration tests for the search pipeline.
2
+
3
+ Each test drives ``search_sync()`` through the full
4
+ ``engine.request() β†’ network.get() β†’ engine.response() β†’ classify β†’ health.update``
5
+ chain, with respx scripting the HTTP layer. This is the middle layer
6
+ between parser fixture tests (which skip ``request()``) and live tests
7
+ (which depend on real sites).
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import httpx
13
+ import pytest
14
+
15
+ from scoutlet import engine_loader
16
+ from scoutlet.health import get_default_registry
17
+ from scoutlet.outcome import FailureKind
18
+ from scoutlet.search import search_sync
19
+
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Happy path: verifies engine.request() builds a real URL and the full
23
+ # chain (fetch + parse + aggregate) works end-to-end.
24
+ # ---------------------------------------------------------------------------
25
+
26
+
27
+ def test_happy_path_returns_results(respx_mock, bing_success_html):
28
+ respx_mock.route(url__startswith="https://www.bing.com/search").mock(
29
+ return_value=httpx.Response(200, text=bing_success_html)
30
+ )
31
+
32
+ response = search_sync("python tutorial", engines=["bing"])
33
+
34
+ assert response.engines[0].status == FailureKind.SUCCESS
35
+ assert len(response.results) >= 1
36
+ for r in response.results:
37
+ assert r.url
38
+ assert r.title
39
+ assert r.score > 0
40
+
41
+ # Coverage gap this closes: engine.request() actually built a URL
42
+ # with the query in it. No other test in the repo verifies this.
43
+ assert len(respx_mock.calls) == 1
44
+ request_url = respx_mock.calls[0].request.url
45
+ assert "bing.com/search" in str(request_url)
46
+ assert request_url.params["q"] == "python tutorial"
47
+
48
+
49
+ # ---------------------------------------------------------------------------
50
+ # Exception classification: verifies _run_engine's try/except wiring and
51
+ # classify_failure() mapping for transport-layer failures.
52
+ # ---------------------------------------------------------------------------
53
+
54
+
55
+ def test_timeout_classified(respx_mock):
56
+ respx_mock.route(url__startswith="https://www.bing.com/search").mock(
57
+ side_effect=httpx.TimeoutException("read timed out")
58
+ )
59
+
60
+ response = search_sync("python tutorial", engines=["bing"])
61
+
62
+ assert response.engines[0].status == FailureKind.TIMEOUT
63
+ assert response.results == []
64
+
65
+
66
+ def test_connection_error_classified(respx_mock):
67
+ respx_mock.route(url__startswith="https://www.bing.com/search").mock(
68
+ side_effect=httpx.ConnectError("connection refused")
69
+ )
70
+
71
+ response = search_sync("python tutorial", engines=["bing"])
72
+
73
+ assert response.engines[0].status == FailureKind.HTTP_ERROR
74
+ assert response.results == []
75
+
76
+
77
+ # ---------------------------------------------------------------------------
78
+ # Engine opt-out: engine.request() returns None β†’ _run_engine sees no URL
79
+ # and returns EMPTY without ever hitting the network.
80
+ # ---------------------------------------------------------------------------
81
+
82
+
83
+ def test_engine_opt_out_returns_empty(respx_mock, monkeypatch):
84
+ engine_loader.load_engines(["bing"])
85
+ bing = engine_loader.engines["bing"]
86
+ monkeypatch.setattr(bing, "request", lambda query, params: None)
87
+
88
+ response = search_sync("python tutorial", engines=["bing"])
89
+
90
+ assert response.engines[0].status == FailureKind.EMPTY
91
+ assert len(respx_mock.calls) == 0
92
+
93
+
94
+ # ---------------------------------------------------------------------------
95
+ # Rate limit β†’ cooldown β†’ subsequent skip. This is the marquee test:
96
+ # verifies the health registry, the skip-filter in search(), and the
97
+ # SkippedEngine wiring all compose correctly end-to-end.
98
+ #
99
+ # Uses Wikipedia because it sets raise_for_httperror=True; Bing does not,
100
+ # so Bing's failure path stops at EMPTY (parsed 0 results).
101
+ # ---------------------------------------------------------------------------
102
+
103
+
104
+ def test_rate_limit_triggers_cooldown_and_skip(respx_mock):
105
+ respx_mock.route(url__startswith="https://en.wikipedia.org/w/api.php").mock(
106
+ return_value=httpx.Response(429)
107
+ )
108
+
109
+ first = search_sync("python", engines=["wikipedia"])
110
+ assert first.engines[0].status == FailureKind.RATE_LIMIT
111
+ assert get_default_registry().is_available("wikipedia") is False
112
+
113
+ # Second call: Wikipedia should be filtered out before dispatch.
114
+ second = search_sync("python", engines=["wikipedia"])
115
+ assert second.skipped, "expected Wikipedia in skipped list"
116
+ assert second.skipped[0].name == "wikipedia"
117
+ assert second.skipped[0].reason == "cooldown"
118
+ assert second.engines == []
119
+
120
+
121
+ def test_access_denied_503_classified(respx_mock):
122
+ # network.raise_for_httperror maps 503 β†’ AccessDenied β†’ ANTI_BOT.
123
+ respx_mock.route(url__startswith="https://en.wikipedia.org/w/api.php").mock(
124
+ return_value=httpx.Response(503)
125
+ )
126
+
127
+ response = search_sync("python", engines=["wikipedia"])
128
+
129
+ assert response.engines[0].status == FailureKind.ANTI_BOT
130
+ assert get_default_registry().is_available("wikipedia") is False