返回 last30days-skill
test_backend_descriptors.py
根目录 / tests / test_backend_descriptors.py
1 """U2: backend-chain descriptors with predicted selection (lib/backends.py).
2
3 Chained sources declare their routing once — imported from the definitions
4 lib/env.py already owns — and ``backends.resolve`` produces a truthful
5 "will use" prediction for alternative chains (X, YouTube, web search) plus
6 honest conditional wording for Reddit.
7
8 Covers the plan's U2 scenarios:
9 1. X with cookies present, bird healthy, no XAI key -> predicted ``bird``.
10 2. Pin var set to a later backend -> pin honored and marked pinned.
11 3. Preferred backend installed-but-unauthenticated does not shadow a
12 fully-usable fallback (collect-then-pick).
13 4. No backend usable -> tier error; prescription from the highest-priority
14 backend.
15 5. Paid lanes probe key presence ONLY — no network, no subprocess.
16 6. Reddit renders conditional wording (default + backfill), never a
17 computed winner; a scrapecreators pin renders as pinned.
18 7. Parity: descriptor prediction == pipeline's pre-failover X selection
19 (env.x_backend_chain()[0]) across three config permutations.
20 """
21
22 from pathlib import Path
23 from unittest import mock
24
25 import pytest
26
27 from lib import backends, env, grok_x, health, xurl_x
28
29
30 # ---------------------------------------------------------------------------
31 # Helpers
32 # ---------------------------------------------------------------------------
33
34 def _probe_dep(status_map=None, default_status=health.OK):
35 """Build a fake health.probe_dependency honoring a per-name status map."""
36 status_map = status_map or {}
37
38 def fake(name, timeout=health.PROBE_TIMEOUT):
39 status = status_map.get(name, default_status)
40 if status == health.OK:
41 return health.DependencyProbe(name=name, status=health.OK, detail=f"{name} 1.0.0")
42 return health.DependencyProbe(
43 name=name,
44 status=status,
45 detail=f"{name} probe simulated {status}",
46 prescription=f"reinstall {name}" if status != health.MISSING else f"install {name}",
47 owner_pkg_manager="brew",
48 )
49
50 return fake
51
52
53 def _x_env(
54 bird_installed=False,
55 xurl_installed=False,
56 xurl_authed=False,
57 node_status=health.OK,
58 grok_installed=False,
59 grok_authed=False,
60 grok_expired=False,
61 grok_off_path=None,
62 ):
63 """Context managers configuring the X-chain probe environment.
64
65 ``xurl_authed`` drives BOTH auth surfaces consistently: the research-time
66 network check (``is_available``) and the doctor-path local evidence
67 (``stored_auth_status``/``has_stored_auth``) — a real machine where the
68 user logged in has both.
69
70 ``grok_expired`` simulates an expired session: AUTH_EXPIRED status, but
71 has_stored_auth/is_available still return True (refresh may work).
72
73 ``grok_off_path`` drives the off-PATH branch of the grok probe. It must be
74 patched rather than left to the real filesystem: ``_off_path_binary``
75 scans installer dirs like ~/.local/bin, so a developer who has grok
76 installed there would otherwise take that branch on every
77 ``grok_installed=False`` case and see failures CI never reproduces.
78 """
79 from datetime import datetime, timezone, timedelta
80 stored = (
81 (xurl_x.AUTH_OK, "stored OAuth credentials found in ~/.xurl")
82 if xurl_authed
83 else (xurl_x.AUTH_MISSING, "no token store at ~/.xurl")
84 )
85 if grok_expired:
86 past = datetime.now(timezone.utc) - timedelta(hours=2)
87 grok_stored = (
88 grok_x.AUTH_EXPIRED,
89 f"Grok session expired at {past.isoformat()}",
90 past,
91 )
92 grok_available = grok_installed
93 elif grok_authed:
94 grok_stored = (
95 grok_x.AUTH_OK,
96 "stored Grok credentials found in ~/.grok/auth.json",
97 None,
98 )
99 grok_available = grok_installed
100 else:
101 grok_stored = (
102 grok_x.AUTH_MISSING,
103 "no Grok credential store at ~/.grok/auth.json",
104 None,
105 )
106 grok_available = False
107 return (
108 mock.patch("lib.bird_x.is_bird_installed", return_value=bird_installed),
109 mock.patch("lib.bird_x.set_credentials", lambda *a, **k: None),
110 mock.patch("lib.xurl_x.is_available", return_value=xurl_authed),
111 mock.patch(
112 "lib.backends.which",
113 lambda name: (
114 "/usr/local/bin/xurl" if (name == "xurl" and xurl_installed)
115 else "/usr/local/bin/grok" if (name == "grok" and grok_installed)
116 else None
117 ),
118 ),
119 mock.patch("lib.grok_x.stored_auth_status", return_value=grok_stored),
120 mock.patch(
121 "lib.grok_x.has_stored_auth",
122 return_value=grok_installed and (grok_authed or grok_expired),
123 ),
124 mock.patch("lib.grok_x.is_available", return_value=grok_available),
125 mock.patch(
126 "lib.health._off_path_binary",
127 lambda name: (
128 Path(grok_off_path) if name == "grok" and grok_off_path else None
129 ),
130 ),
131 mock.patch("lib.health.probe_dependency", _probe_dep({"node": node_status})),
132 mock.patch("lib.xurl_x.stored_auth_status", return_value=stored),
133 mock.patch(
134 "lib.xurl_x.has_stored_auth",
135 return_value=xurl_installed and xurl_authed,
136 ),
137 )
138
139
140 def _resolve_x(config, **envkw):
141 with _stack(_x_env(**envkw)):
142 return backends.resolve("x", config)
143
144
145 class _stack:
146 """Enter/exit a tuple of context managers (contextlib.ExitStack, terse)."""
147
148 def __init__(self, ctxs):
149 self._ctxs = ctxs
150
151 def __enter__(self):
152 for c in self._ctxs:
153 c.__enter__()
154 return self
155
156 def __exit__(self, *exc):
157 for c in reversed(self._ctxs):
158 c.__exit__(*exc)
159 return False
160
161
162 # ---------------------------------------------------------------------------
163 # Descriptor registry: routing declared once, imported from env.py (KTD 6)
164 # ---------------------------------------------------------------------------
165
166 class TestDescriptorRegistry:
167 def test_x_chain_comes_from_env_definitions(self):
168 d = backends.get_descriptor("x")
169 assert d.mode == backends.MODE_ALTERNATIVE
170 # Auto chain order: bird first, grok excluded (opt-in only).
171 assert env.X_BACKEND_ORDER == ("bird", "xai", "xurl", "xquik")
172 # Grok and xapi are opt-in only off Grok Bot, not in the auto chain.
173 assert env.X_BACKEND_OPT_IN == ("grok", "xapi")
174 # All known backends (auto + opt-in) for pin validation.
175 assert env.X_BACKEND_KNOWN == ("bird", "xai", "xurl", "xquik", "grok", "xapi")
176 # Descriptor includes all backends (auto + opt-in) for doctor visibility.
177 assert tuple(s.name for s in d.backends) == env.X_BACKEND_ORDER + env.X_BACKEND_OPT_IN
178 # Grok and xapi are marked opt-in in the descriptor.
179 grok_spec = next(s for s in d.backends if s.name == "grok")
180 assert grok_spec.opt_in is True
181 xapi_spec = next(s for s in d.backends if s.name == "xapi")
182 assert xapi_spec.opt_in is True
183 assert xapi_spec.paid is True
184 assert xapi_spec.requires == "X_BEARER_TOKEN (X API v2)"
185 # Auto chain backends are NOT marked opt-in.
186 for name in env.X_BACKEND_ORDER:
187 spec = next(s for s in d.backends if s.name == name)
188 assert spec.opt_in is False
189 assert d.pin_var == env.X_BACKEND_PIN_VAR == "LAST30DAYS_X_BACKEND"
190
191 def test_env_exposes_reddit_pin_constants(self):
192 assert env.REDDIT_BACKEND_PIN_VAR == "LAST30DAYS_REDDIT_BACKEND"
193 assert env.REDDIT_SC_MIN_ITEMS_VAR == "LAST30DAYS_REDDIT_SC_MIN_ITEMS"
194
195 def test_youtube_and_web_chains_declared_in_order(self):
196 yt = backends.get_descriptor("youtube")
197 assert tuple(s.name for s in yt.backends) == ("yt-dlp", "scrapecreators")
198 web = backends.get_descriptor("web")
199 assert tuple(s.name for s in web.backends) == (
200 "brave", "exa", "serper", "parallel", "keyless", "parallel-mcp",
201 )
202 assert web.pin_flag == "--web-backend"
203
204 def test_reddit_is_conditional_and_lanes_are_not_chain_entries(self):
205 d = backends.get_descriptor("reddit")
206 assert d.mode == backends.MODE_CONDITIONAL
207 names = [s.name for s in d.backends]
208 # Internal keyless lanes are sub-probe detail, never chain entries.
209 for lane in ("search", "listing", "arctic", "shreddit", "rss"):
210 assert lane not in names
211 assert names == ["public", "scrapecreators"]
212
213 def test_unknown_source_raises(self):
214 with pytest.raises(KeyError):
215 backends.get_descriptor("nope")
216 with pytest.raises(KeyError):
217 backends.resolve("nope", {})
218
219
220 # ---------------------------------------------------------------------------
221 # Scenario 1: cookies present, bird healthy, no XAI key -> bird predicted
222 # ---------------------------------------------------------------------------
223
224 class TestXPrediction:
225 def test_bird_predicted_with_cookies_and_no_xai_key(self):
226 config = {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"}
227 res = _resolve_x(config, bird_installed=True)
228 assert res.active_backend == "bird"
229 assert res.tier == backends.TIER_OK
230 assert res.pinned is False
231 # Chain includes all backends (auto + opt-in) for doctor visibility.
232 expected_chain = list(env.X_BACKEND_ORDER + env.X_BACKEND_OPT_IN)
233 assert res.chain == expected_chain
234 assert [f.name for f in res.findings] == expected_chain
235 assert "will use: bird" in res.summary
236
237 def test_bird_predicted_even_when_xai_key_present(self):
238 """Cookies beat XAI_API_KEY when both are present (bird-first chain)."""
239 config = {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0", "XAI_API_KEY": "dummy-key"}
240 res = _resolve_x(config, bird_installed=True)
241 assert res.active_backend == "bird"
242 assert res.tier == backends.TIER_OK
243 assert "will use: bird" in res.summary
244
245 def test_grok_is_never_auto_selected_unpinned(self):
246 """Grok is opt-in only: even if grok is the only configured backend, X is unconfigured unpinned."""
247 config = {}
248 res = _resolve_x(config, grok_installed=True, grok_authed=True)
249 # Grok is available but opt-in - should NOT be auto-selected.
250 grok = next(f for f in res.findings if f.name == "grok")
251 assert grok.status == health.OK
252 # But it should not be the active backend.
253 assert res.active_backend is None
254 assert res.tier == backends.TIER_ERROR
255
256 def test_grok_selected_when_pinned(self):
257 """Pin grok to enable it explicitly."""
258 config = {"LAST30DAYS_X_BACKEND": "grok"}
259 res = _resolve_x(config, grok_installed=True, grok_authed=True)
260 assert res.active_backend == "grok"
261 assert res.pinned is True
262 assert res.pin == "grok"
263 assert res.tier == backends.TIER_OK
264
265 def test_grok_pin_with_no_store_is_error(self):
266 """Pin grok without a valid store -> error with grok login prescription."""
267 config = {"LAST30DAYS_X_BACKEND": "grok"}
268 res = _resolve_x(config, grok_installed=True, grok_authed=False)
269 assert res.active_backend is None
270 assert res.pinned is True
271 assert res.tier == backends.TIER_ERROR
272 assert "grok login" in res.prescription.lower()
273
274 # Scenario 2: pin var set to a later backend -> honored + marked pinned.
275 def test_pin_to_later_backend_honored_and_marked(self):
276 config = {
277 "AUTH_TOKEN": "dummy-token",
278 "CT0": "dummy-ct0",
279 "XQUIK_API_KEY": "dummy-key",
280 "LAST30DAYS_X_BACKEND": "xquik",
281 }
282 res = _resolve_x(config, bird_installed=True)
283 assert res.active_backend == "xquik"
284 assert res.pinned is True
285 assert res.pin == "xquik"
286 assert res.tier == backends.TIER_OK
287 assert "pinned" in res.summary
288
289 # Scenario 3: installed-but-unauthenticated preferred backend must not
290 # shadow a fully usable fallback (collect-then-pick).
291 def test_unauthenticated_preferred_does_not_shadow_usable_fallback(self):
292 config = {"XQUIK_API_KEY": "dummy-key"}
293 res = _resolve_x(config, xurl_installed=True, xurl_authed=False)
294 assert res.active_backend == "xquik"
295 assert res.tier == backends.TIER_OK
296 xurl = next(f for f in res.findings if f.name == "xurl")
297 assert not xurl.usable
298 assert "auth" in (xurl.detail + xurl.prescription).lower()
299
300 # Scenario 4: nothing usable -> error tier, highest-priority prescription.
301 def test_no_backend_usable_is_error_with_top_priority_prescription(self):
302 res = _resolve_x({})
303 assert res.active_backend is None
304 assert res.tier == backends.TIER_ERROR
305 # bird (cookies) is first in the chain, so the prescription is about
306 # browser cookies, not XAI_API_KEY.
307 assert "browser-cookie" in res.prescription or "cookies" in res.prescription.lower()
308
309 def test_pinned_but_unusable_backend_is_error_with_its_prescription(self):
310 # Pin bird without cookies: env.x_backend_chain returns [] (pipeline
311 # raises); resolution mirrors that as an error carrying bird's fix.
312 config = {"LAST30DAYS_X_BACKEND": "bird"}
313 res = _resolve_x(config, bird_installed=True)
314 assert res.active_backend is None
315 assert res.pinned is True
316 assert res.tier == backends.TIER_ERROR
317 assert res.prescription # bird's cookie prescription, not xai's
318 assert "XAI_API_KEY" not in res.prescription
319
320 def test_broken_node_shim_makes_bird_unusable_and_falls_back(self):
321 # U1 integration: a stale node shim (BROKEN, not missing) must not
322 # let bird read as usable — the #692 class applied to chains.
323 config = {
324 "AUTH_TOKEN": "dummy-token",
325 "CT0": "dummy-ct0",
326 "XQUIK_API_KEY": "dummy-key",
327 }
328 res = _resolve_x(config, bird_installed=True, node_status=health.BROKEN)
329 assert res.active_backend == "xquik"
330 bird = next(f for f in res.findings if f.name == "bird")
331 assert bird.status == health.BROKEN
332 assert not bird.usable
333
334 def test_unconfigured_x_with_broken_node_is_unconfigured_not_node_error(self):
335 # F9: cookie presence is checked BEFORE the node runtime. With no X
336 # configuration at all, a broken node must not turn bird into a
337 # BROKEN finding carrying a node prescription — the honest state is
338 # "unconfigured, here is the cookie fix" (which doctor rolls up to
339 # tier off, since every finding is MISSING).
340 res = _resolve_x({}, node_status=health.BROKEN)
341 bird = next(f for f in res.findings if f.name == "bird")
342 assert bird.status == health.MISSING
343 assert "AUTH_TOKEN/CT0" in bird.detail
344 assert "cookie" in bird.prescription.lower()
345 assert "node" not in bird.prescription.lower()
346 # Doctor's off/unconfigured rollup keys on all-findings-MISSING.
347 assert all(f.status == health.MISSING for f in res.findings)
348
349 def test_cookies_present_broken_node_still_reads_broken(self):
350 # The inverse guard: once cookies ARE configured, a broken node is a
351 # real configured-but-broken state and must keep the node fix.
352 config = {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"}
353 res = _resolve_x(config, bird_installed=True, node_status=health.BROKEN)
354 bird = next(f for f in res.findings if f.name == "bird")
355 assert bird.status == health.BROKEN
356 assert "node" in bird.prescription.lower()
357
358 def test_grok_expired_is_degraded_not_ok(self):
359 """Expired grok session -> DEGRADED tier (warn), not OK."""
360 # Grok is opt-in: needs explicit pin to be selected.
361 config = {"LAST30DAYS_X_BACKEND": "grok"}
362 res = _resolve_x(config, grok_installed=True, grok_expired=True)
363 grok = next(f for f in res.findings if f.name == "grok")
364 assert grok.status == health.DEGRADED
365 assert grok.usable # DEGRADED is still usable (refresh may work)
366 assert "expired" in grok.detail.lower()
367 assert "grok login" in grok.prescription.lower()
368 # With pin, grok is selected (degraded is usable).
369 assert res.active_backend == "grok"
370 assert res.tier == backends.TIER_WARN
371
372 def test_grok_expired_unpinned_not_selected(self):
373 """Expired grok without pin: X unconfigured, grok not auto-selected."""
374 res = _resolve_x({}, grok_installed=True, grok_expired=True)
375 grok = next(f for f in res.findings if f.name == "grok")
376 assert grok.status == health.DEGRADED
377 # Grok is opt-in, so even though it's usable (degraded), it's not selected.
378 assert res.active_backend is None
379 assert res.tier == backends.TIER_ERROR
380
381 def test_grok_expired_with_fallback_picks_fallback(self):
382 """When grok is expired AND a better auto-chain backend is OK, pick the OK one."""
383 config = {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"}
384 res = _resolve_x(config, grok_installed=True, grok_expired=True, bird_installed=True)
385 # Bird is OK and in the auto chain. Grok is not considered (opt-in).
386 assert res.active_backend == "bird"
387 assert res.tier == backends.TIER_OK
388
389 def test_grok_future_expires_at_is_ok(self):
390 """Grok with future expires_at reports OK."""
391 res = _resolve_x({}, grok_installed=True, grok_authed=True)
392 grok = next(f for f in res.findings if f.name == "grok")
393 assert grok.status == health.OK
394 assert "not live-verified" in grok.detail
395
396
397 # ---------------------------------------------------------------------------
398 # Grok session expiry: three states (grok is opt-in only)
399 # ---------------------------------------------------------------------------
400
401 class TestGrokExpiryStates:
402 """Test the three grok auth states from the plan:
403 1. No grok CLI -> silent fallback (opt-in only)
404 2. CLI installed, never logged in -> silent fallback (opt-in only)
405 3. CLI installed, WAS logged in, session dead -> DEGRADED with expiry info (opt-in only)
406
407 Note: Grok is opt-in only. These tests verify the finding status, but grok
408 is never auto-selected unpinned.
409 """
410
411 def test_no_grok_cli_is_missing(self):
412 """No grok CLI -> MISSING status, no extra failure noise."""
413 res = _resolve_x({}, grok_installed=False)
414 grok = next(f for f in res.findings if f.name == "grok")
415 assert grok.status == health.MISSING
416 assert "not found on PATH" in grok.detail
417 # Grok is opt-in, so even MISSING doesn't affect the resolution.
418 # X is unconfigured (no auto-chain backends available).
419 assert res.active_backend is None
420
421 def test_grok_off_path_prescribes_a_path_edit_not_an_install(self, tmp_path):
422 """grok present in an installer dir but absent from PATH -> still
423 MISSING, but the prescription must be a PATH edit: installing again
424 would fix nothing. This branch had no coverage, so it only ever ran
425 on developer machines that happened to have grok in ~/.local/bin."""
426 grok_path = tmp_path / ".local" / "bin" / "grok"
427 res = _resolve_x({}, grok_installed=False, grok_off_path=str(grok_path))
428 grok = next(f for f in res.findings if f.name == "grok")
429 assert grok.status == health.MISSING
430 assert "not on this process's PATH" in grok.detail
431 assert str(grok_path) in grok.detail
432 assert str(grok_path.parent) in grok.prescription
433 assert "npm install" not in grok.prescription.lower()
434 assert res.active_backend is None
435
436 def test_grok_installed_never_logged_in_is_missing(self):
437 """CLI installed but never logged in -> MISSING with login hint."""
438 res = _resolve_x({}, grok_installed=True, grok_authed=False)
439 grok = next(f for f in res.findings if f.name == "grok")
440 assert grok.status == health.MISSING
441 assert "not signed in" in grok.detail
442 assert "grok login" in grok.prescription
443 # Grok is opt-in, so X is unconfigured.
444 assert res.active_backend is None
445
446 def test_grok_session_expired_is_degraded_with_expiry(self):
447 """Session expired -> DEGRADED with timestamp and refresh hint."""
448 res = _resolve_x({}, grok_installed=True, grok_expired=True)
449 grok = next(f for f in res.findings if f.name == "grok")
450 assert grok.status == health.DEGRADED
451 assert "expired" in grok.detail.lower()
452 # The detail should include the expiry timestamp and hint
453 assert "refresh" in grok.detail.lower() or "login" in grok.prescription.lower()
454 # Grok is opt-in, so X is unconfigured even with degraded grok.
455 assert res.active_backend is None
456
457 def test_grok_healthy_session_is_ok(self):
458 """Non-expired credentials -> OK, but still opt-in only."""
459 res = _resolve_x({}, grok_installed=True, grok_authed=True)
460 grok = next(f for f in res.findings if f.name == "grok")
461 assert grok.status == health.OK
462 # Grok is opt-in, so X is unconfigured unpinned.
463 assert res.active_backend is None
464
465
466 # ---------------------------------------------------------------------------
467 # Scenario 5: paid lanes probe key presence only — never network/subprocess
468 # ---------------------------------------------------------------------------
469
470 def _forbid_io():
471 def boom(*a, **k):
472 raise AssertionError("paid-lane probe attempted I/O")
473
474 return (
475 mock.patch("socket.socket", boom),
476 mock.patch("socket.create_connection", boom),
477 mock.patch("urllib.request.urlopen", boom),
478 mock.patch("subprocess.run", boom),
479 mock.patch("subprocess.Popen", boom),
480 )
481
482
483 class TestPaidLaneProbes:
484 PAID = [
485 ("x", "xai", "XAI_API_KEY"),
486 ("x", "xquik", "XQUIK_API_KEY"),
487 ("web", "serper", "SERPER_API_KEY"),
488 ("youtube", "scrapecreators", "SCRAPECREATORS_API_KEY"),
489 ("reddit", "scrapecreators", "SCRAPECREATORS_API_KEY"),
490 ]
491
492 def test_paid_lanes_are_flagged_paid(self):
493 for source, name, _key in self.PAID:
494 spec = next(
495 s for s in backends.get_descriptor(source).backends if s.name == name
496 )
497 assert spec.paid is True, f"{source}/{name} must be a paid (key-only) lane"
498
499 def test_key_presence_probe_makes_no_network_or_subprocess_calls(self):
500 ctxs = _forbid_io()
501 with ctxs[0], ctxs[1], ctxs[2], ctxs[3], ctxs[4]:
502 for source, name, key in self.PAID:
503 spec = next(
504 s for s in backends.get_descriptor(source).backends if s.name == name
505 )
506 present = spec.probe({key: "dummy-key"})
507 assert present.status == health.OK
508 absent = spec.probe({})
509 assert absent.status == health.MISSING
510 assert key in absent.prescription
511
512
513 # ---------------------------------------------------------------------------
514 # F1 + F10: the doctor-path xurl probe is LOCAL-ONLY (stored-token evidence,
515 # never a live `xurl whoami` — doctor's no-network guarantee) and typed.
516 # ---------------------------------------------------------------------------
517
518 class TestXurlLocalProbe:
519 def _spec(self):
520 return next(
521 s for s in backends.get_descriptor("x").backends if s.name == "xurl"
522 )
523
524 def _finding(self, stored, installed=True):
525 """Run the xurl probe under the forbid-all-I/O harness."""
526 ctxs = _forbid_io()
527 with ctxs[0], ctxs[1], ctxs[2], ctxs[3], ctxs[4], \
528 mock.patch(
529 "lib.backends.which",
530 lambda n: "/usr/local/bin/xurl" if installed else None,
531 ), \
532 mock.patch("lib.xurl_x.stored_auth_status", return_value=stored):
533 return self._spec().probe({})
534
535 def test_token_store_present_is_ok_without_network(self):
536 finding = self._finding(
537 (xurl_x.AUTH_OK, "stored OAuth credentials found in ~/.xurl")
538 )
539 assert finding.status == health.OK
540 assert "not live-verified" in finding.detail
541
542 def test_no_token_store_is_missing_with_auth_prescription(self):
543 finding = self._finding((xurl_x.AUTH_MISSING, "no token store at ~/.xurl"))
544 assert finding.status == health.MISSING
545 assert "not authenticated" in finding.detail
546 assert "xurl auth app-only <bearer-token>" in finding.prescription
547
548 def test_unreadable_token_store_is_error_tier(self):
549 # F10: binary resolvable but the token-store read fails -> typed
550 # ERROR (doctor's error tier), never "unconfigured".
551 finding = self._finding(
552 (
553 xurl_x.AUTH_ERROR,
554 "token store ~/.xurl unreadable: PermissionError: denied",
555 )
556 )
557 assert finding.status == health.ERROR
558 assert not finding.usable
559 assert "unreadable" in finding.detail
560
561 def test_binary_absent_stays_not_installed(self):
562 finding = self._finding((xurl_x.AUTH_OK, "irrelevant"), installed=False)
563 assert finding.status == health.MISSING
564 assert "not found on PATH" in finding.detail
565
566 def test_whole_doctor_path_x_probe_makes_no_network_or_subprocess(self, tmp_path):
567 """The full X chain resolution plus the safe get_x_source_status —
568 the exact X probes doctor runs — under the forbid-everything
569 harness. The token store is a REAL file so the genuine
570 stored_auth_status code path (filesystem only) is exercised."""
571 store = tmp_path / ".xurl"
572 store.write_text(
573 "apps:\n app:\n bearer_token:\n bearer: dummy-not-real\n",
574 encoding="utf-8",
575 )
576 config = {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"}
577 bird_status = {
578 "installed": True,
579 "authenticated": True,
580 "username": "env AUTH_TOKEN",
581 "can_install": True,
582 }
583 ctxs = _forbid_io()
584 with ctxs[0], ctxs[1], ctxs[2], ctxs[3], ctxs[4], \
585 mock.patch(
586 "lib.xurl_x.is_available",
587 side_effect=AssertionError(
588 "doctor path ran the live `xurl whoami` network check"
589 ),
590 ), \
591 mock.patch("lib.xurl_x.token_store_path", return_value=store), \
592 mock.patch("lib.backends.which", lambda n: f"/usr/local/bin/{n}"), \
593 mock.patch(
594 "lib.xurl_x.shutil.which", lambda n: f"/usr/local/bin/{n}"
595 ), \
596 mock.patch("lib.health.probe_dependency", _probe_dep()), \
597 mock.patch("lib.bird_x.is_bird_installed", return_value=True), \
598 mock.patch("lib.bird_x.set_credentials", lambda *a, **k: None), \
599 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status):
600 res = backends.resolve("x", config)
601 status = env.get_x_source_status(config, probe=False)
602 xurl_finding = next(f for f in res.findings if f.name == "xurl")
603 assert xurl_finding.status == health.OK
604 assert "not live-verified" in xurl_finding.detail
605 assert status["xurl_available"] is True
606
607
608 # ---------------------------------------------------------------------------
609 # Scenario 6: Reddit conditional wording, never a computed winner
610 # ---------------------------------------------------------------------------
611
612 class TestRedditConditional:
613 def test_sc_key_present_renders_default_plus_backfill_no_winner(self):
614 res = backends.resolve("reddit", {"SCRAPECREATORS_API_KEY": "dummy-key"})
615 assert res.mode == backends.MODE_CONDITIONAL
616 assert res.active_backend is None # never a single computed winner
617 assert "will use" not in res.summary
618 low = res.conditional.lower()
619 assert "public keyless" in low
620 assert "default" in low
621 assert "scrapecreators backfill" in low
622 assert res.tier == backends.TIER_OK
623
624 def test_thinness_floor_appears_in_wording(self):
625 res = backends.resolve(
626 "reddit",
627 {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_REDDIT_SC_MIN_ITEMS": "5"},
628 )
629 assert "5" in res.conditional
630 assert "floor" in res.conditional.lower()
631
632 def test_default_floor_names_five_item_floor(self):
633 res = backends.resolve("reddit", {"SCRAPECREATORS_API_KEY": "dummy-key"})
634 low = res.conditional.lower()
635 assert "5-item floor" in low
636 assert "nothing" not in low
637
638 def test_empty_floor_treated_as_default(self):
639 res = backends.resolve(
640 "reddit",
641 {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_REDDIT_SC_MIN_ITEMS": ""},
642 )
643 assert "5-item floor" in res.conditional.lower()
644
645 def test_explicit_zero_floor_means_empty_only_wording(self):
646 res = backends.resolve(
647 "reddit",
648 {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_REDDIT_SC_MIN_ITEMS": "0"},
649 )
650 low = res.conditional.lower()
651 assert "nothing" in low
652 assert "floor" not in low
653
654 def test_malformed_floor_spends_nothing_extra(self):
655 # Malformed parses to 0 (empty-only), matching the pipeline.
656 res = backends.resolve(
657 "reddit",
658 {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_REDDIT_SC_MIN_ITEMS": "lots"},
659 )
660 low = res.conditional.lower()
661 assert "nothing" in low
662 assert "floor" not in low
663
664 def test_pinned_scrapecreators_renders_pin(self):
665 res = backends.resolve(
666 "reddit",
667 {
668 "SCRAPECREATORS_API_KEY": "dummy-key",
669 "LAST30DAYS_REDDIT_BACKEND": "scrapecreators",
670 },
671 )
672 assert res.pinned is True
673 assert res.pin == "scrapecreators"
674 low = res.conditional.lower()
675 assert "pinned" in low
676 assert "primary" in low
677 assert res.active_backend is None # still conditional, not a winner
678
679 def test_no_key_means_no_backfill_wording(self):
680 res = backends.resolve("reddit", {})
681 low = res.conditional.lower()
682 assert "public keyless" in low
683 assert "backfill" not in low or "no scrapecreators" in low
684 assert res.tier == backends.TIER_OK # public composite always reachable
685
686 def test_pin_without_key_is_ignored_like_the_pipeline(self):
687 # pipeline gates sc_first on has_sc_key; the pin alone changes nothing.
688 res = backends.resolve(
689 "reddit", {"LAST30DAYS_REDDIT_BACKEND": "scrapecreators"},
690 )
691 assert res.pinned is False
692 assert "primary" not in res.conditional.lower().split("pin ignored")[0]
693
694 def test_keyless_paths_are_sub_probe_detail(self):
695 # The public finding is the keyless floor (Brave and the other web
696 # rungs, Arctic /posts/ids), not the dead anonymous search.
697 res = backends.resolve("reddit", {})
698 public = next(f for f in res.findings if f.name == "public")
699 assert public.status == health.OK
700 assert public.detail.endswith(
701 "(paths: brave, ddg, startpage web search; arctic /posts/ids)"
702 )
703 assert "rss" not in public.detail
704 assert "shreddit" not in public.detail
705
706 def test_cached_skips_show_in_the_public_finding_without_a_request(self):
707 from lib import reddit_health
708
709 for rung in reddit_health.WEB_RUNGS:
710 reddit_health.record(rung, reddit_health.BLOCKED, "HTTP 403")
711 with mock.patch("lib.http.get_text") as get_text, mock.patch("lib.http.get") as get:
712 res = backends.resolve("reddit", {})
713 get_text.assert_not_called()
714 get.assert_not_called()
715 public = next(f for f in res.findings if f.name == "public")
716 assert public.status == health.DEGRADED
717 assert "skipped after a refusal: brave, ddg, startpage" in public.detail
718 assert "doctor" in public.prescription
719
720 def test_one_skipped_rung_keeps_the_floor_ok(self):
721 from lib import reddit_health
722
723 reddit_health.record("brave", reddit_health.BLOCKED, "HTTP 403")
724 res = backends.resolve("reddit", {})
725 public = next(f for f in res.findings if f.name == "public")
726 assert public.status == health.OK
727 assert "brave" in public.detail.split("skipped after a refusal:")[1]
728
729
730 # ---------------------------------------------------------------------------
731 # Scenario 7: parity with the pipeline's pre-failover X selection
732 # ---------------------------------------------------------------------------
733
734 class TestXParityWithPipeline:
735 """Descriptor prediction must equal env.x_backend_chain(config)[0] — the
736 exact expression pipeline._retrieve_stream uses as its pre-failover
737 primary (lib/pipeline.py, `chain = env.x_backend_chain(config)`)."""
738
739 def _assert_parity(self, config, **envkw):
740 with _stack(_x_env(**envkw)):
741 chain = env.x_backend_chain(config)
742 predicted = backends.resolve("x", config).active_backend
743 expected = chain[0] if chain else None
744 assert predicted == expected, (
745 f"prediction {predicted!r} != pipeline pre-failover {expected!r} "
746 f"for config keys {sorted(config)}"
747 )
748
749 def test_parity_xai_key_only(self):
750 self._assert_parity({"XAI_API_KEY": "dummy-key"})
751
752 def test_parity_cookies_and_bird_installed(self):
753 self._assert_parity(
754 {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"},
755 bird_installed=True,
756 )
757
758 def test_parity_pin_forces_xquik(self):
759 self._assert_parity(
760 {"XQUIK_API_KEY": "dummy-key", "LAST30DAYS_X_BACKEND": "xquik"},
761 )
762
763 def test_parity_nothing_configured(self):
764 self._assert_parity({})
765
766 def test_parity_grok_only_unpinned_is_unconfigured(self):
767 """Grok-only with no pin: X unconfigured (parity with env.x_backend_chain)."""
768 self._assert_parity({}, grok_installed=True, grok_authed=True)
769
770 def test_parity_grok_pinned(self):
771 """Grok pinned: grok is selected (parity with env.x_backend_chain)."""
772 self._assert_parity(
773 {"LAST30DAYS_X_BACKEND": "grok"},
774 grok_installed=True,
775 grok_authed=True,
776 )
777
778 def test_parity_cookies_beat_xai_key(self):
779 """Cookies beat XAI_API_KEY when both present (bird-first chain)."""
780 self._assert_parity(
781 {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0", "XAI_API_KEY": "dummy-key"},
782 bird_installed=True,
783 )
784
785 # Host rows (U1): the policy shapes both sides identically.
786
787 def test_parity_grok_bot_bearer(self):
788 self._assert_parity(
789 {"LAST30DAYS_HOST": "grok-bot", "X_BEARER_TOKEN": "dummy-bearer",
790 "AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"},
791 bird_installed=True,
792 )
793
794 def test_parity_grok_bot_xai_key(self):
795 self._assert_parity(
796 {"LAST30DAYS_HOST": "grok-bot", "XAI_API_KEY": "dummy-key",
797 "AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"},
798 bird_installed=True,
799 )
800
801 def test_parity_grok_bot_cookies_only_unpinned(self):
802 self._assert_parity(
803 {"LAST30DAYS_HOST": "grok-bot", "AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0",
804 "XQUIK_API_KEY": "dummy-key"},
805 bird_installed=True,
806 )
807
808 def test_parity_grok_bot_bird_pin(self):
809 self._assert_parity(
810 {"LAST30DAYS_HOST": "grok-bot", "LAST30DAYS_X_BACKEND": "bird",
811 "AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0"},
812 bird_installed=True,
813 )
814
815 def test_parity_grok_bot_grok_pin(self):
816 self._assert_parity(
817 {"LAST30DAYS_HOST": "grok-bot", "LAST30DAYS_X_BACKEND": "grok"},
818 grok_installed=True,
819 grok_authed=True,
820 )
821
822 def test_parity_non_grok_linux_cookies_and_bearer(self):
823 with mock.patch("platform.system", return_value="Linux"):
824 self._assert_parity(
825 {"AUTH_TOKEN": "dummy-token", "CT0": "dummy-ct0",
826 "X_BEARER_TOKEN": "dummy-bearer"},
827 bird_installed=True,
828 )
829
830 def test_parity_macbook_xai_key(self):
831 with (
832 mock.patch("platform.system", return_value="Darwin"),
833 mock.patch.object(env, "_mac_model", return_value="MacBookPro18,2"),
834 ):
835 self._assert_parity({"XAI_API_KEY": "dummy-key"})
836
837
838 # ---------------------------------------------------------------------------
839 # get_x_source_status pin semantics (R4): grok pin forces grok source
840 # ---------------------------------------------------------------------------
841
842 class TestGetXSourceStatusGrokPin:
843 """get_x_source_status must respect LAST30DAYS_X_BACKEND=grok pin."""
844
845 def test_pin_grok_with_store_returns_grok_source(self):
846 """Pin grok + valid store -> get_x_source_status source is 'grok'."""
847 config = {"LAST30DAYS_X_BACKEND": "grok"}
848 bird_status = {
849 "installed": False,
850 "authenticated": False,
851 "username": "",
852 "can_install": False,
853 }
854 with (
855 mock.patch("lib.grok_x.has_stored_auth", return_value=True),
856 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status),
857 ):
858 status = env.get_x_source_status(config, probe=False)
859 assert status["source"] == "grok"
860 assert status["grok_available"] is True
861
862 def test_unpinned_with_store_does_not_return_grok_source(self):
863 """Unpinned + valid grok store -> source is NOT 'grok' (opt-in only)."""
864 config = {} # No pin
865 bird_status = {
866 "installed": False,
867 "authenticated": False,
868 "username": "",
869 "can_install": False,
870 }
871 with (
872 mock.patch("lib.grok_x.has_stored_auth", return_value=True),
873 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status),
874 ):
875 status = env.get_x_source_status(config, probe=False)
876 # Grok is available but NOT the source (opt-in only)
877 assert status["source"] != "grok"
878 assert status["source"] is None # No other backend configured
879 assert status["grok_available"] is True
880
881 def test_pin_grok_with_cookies_still_returns_grok(self):
882 """Pin grok with cookies present -> grok (pin forces single backend)."""
883 config = {
884 "LAST30DAYS_X_BACKEND": "grok",
885 "AUTH_TOKEN": "dummy-token",
886 "CT0": "dummy-ct0",
887 }
888 bird_status = {
889 "installed": True,
890 "authenticated": True,
891 "username": "test",
892 "can_install": True,
893 }
894 with (
895 mock.patch("lib.grok_x.has_stored_auth", return_value=True),
896 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status),
897 ):
898 status = env.get_x_source_status(config, probe=False)
899 # Pin forces grok even when bird is available
900 assert status["source"] == "grok"
901
902 def test_pin_grok_no_store_with_other_creds_returns_none(self):
903 """Pin grok + no store + other creds present -> source is None (exclusive pin)."""
904 config = {
905 "LAST30DAYS_X_BACKEND": "grok",
906 "AUTH_TOKEN": "dummy-token",
907 "CT0": "dummy-ct0",
908 "XAI_API_KEY": "dummy-key",
909 }
910 bird_status = {
911 "installed": True,
912 "authenticated": True,
913 "username": "test",
914 "can_install": True,
915 }
916 with (
917 mock.patch("lib.grok_x.has_stored_auth", return_value=False),
918 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status),
919 ):
920 status = env.get_x_source_status(config, probe=False)
921 # Pin is exclusive: grok unavailable -> None, NOT fallback to bird/xai
922 assert status["source"] is None
923 assert status["grok_available"] is False
924 # Other backends ARE available, but pin blocks fallback
925 assert status["bird_authenticated"] is True
926 assert status["xai_available"] is True
927
928 def test_pin_xai_with_cookies_returns_xai(self):
929 """Pin xai + cookies + XAI_API_KEY -> source is xai, not bird."""
930 config = {
931 "LAST30DAYS_X_BACKEND": "xai",
932 "AUTH_TOKEN": "dummy-token",
933 "CT0": "dummy-ct0",
934 "XAI_API_KEY": "dummy-key",
935 }
936 bird_status = {
937 "installed": True,
938 "authenticated": True,
939 "username": "test",
940 "can_install": True,
941 }
942 with (
943 mock.patch("lib.grok_x.has_stored_auth", return_value=False),
944 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status),
945 ):
946 status = env.get_x_source_status(config, probe=False)
947 # Pin is exclusive: xai pinned + available -> xai (not bird)
948 assert status["source"] == "xai"
949 # Bird is also available, but pin forces xai
950 assert status["bird_authenticated"] is True
951
952 def test_pin_xai_no_key_with_cookies_returns_none(self):
953 """Pin xai + no XAI_API_KEY + cookies -> source is None (exclusive pin)."""
954 config = {
955 "LAST30DAYS_X_BACKEND": "xai",
956 "AUTH_TOKEN": "dummy-token",
957 "CT0": "dummy-ct0",
958 # No XAI_API_KEY
959 }
960 bird_status = {
961 "installed": True,
962 "authenticated": True,
963 "username": "test",
964 "can_install": True,
965 }
966 with (
967 mock.patch("lib.grok_x.has_stored_auth", return_value=False),
968 mock.patch("lib.bird_x.get_bird_status", return_value=bird_status),
969 ):
970 status = env.get_x_source_status(config, probe=False)
971 # Pin is exclusive: xai pinned but unavailable -> None (no fallback)
972 assert status["source"] is None
973 assert status["xai_available"] is False
974 # Bird is available but pin blocks fallback
975 assert status["bird_authenticated"] is True
976
977
978 # ---------------------------------------------------------------------------
979 # Runtime X backend pin (x_backend_chain / _resolve_x_backend)
980 # ---------------------------------------------------------------------------
981
982 class TestRuntimeXBackendPin:
983 """Runtime fetch path must honor any known pin exclusively, including grok."""
984
985 def test_pin_grok_with_store_and_cookies_returns_grok(self):
986 """Pin grok + grok store + cookies -> runtime returns grok, not bird."""
987 from lib import grok_x, providers
988
989 config = {
990 "LAST30DAYS_X_BACKEND": "grok",
991 "AUTH_TOKEN": "dummy-token",
992 "CT0": "dummy-ct0",
993 "XAI_API_KEY": "dummy-key",
994 }
995 with (
996 mock.patch.object(grok_x, "has_stored_auth", return_value=True),
997 mock.patch("lib.bird_x.is_bird_installed", return_value=True),
998 ):
999 # x_backend_chain is the authoritative runtime path
1000 chain = env.x_backend_chain(config)
1001 # _resolve_x_backend delegates to get_x_source (wraps x_backend_chain)
1002 resolved = providers._resolve_x_backend(config)
1003 # Pin grok + available -> grok (not bird/xai)
1004 assert chain == ["grok"]
1005 assert resolved == "grok"
1006
1007 def test_pin_grok_no_store_with_cookies_returns_none(self):
1008 """Pin grok + no store + cookies -> runtime returns None (exclusive pin)."""
1009 from lib import grok_x, providers
1010
1011 config = {
1012 "LAST30DAYS_X_BACKEND": "grok",
1013 "AUTH_TOKEN": "dummy-token",
1014 "CT0": "dummy-ct0",
1015 }
1016 with (
1017 mock.patch.object(grok_x, "has_stored_auth", return_value=False),
1018 mock.patch("lib.bird_x.is_bird_installed", return_value=True),
1019 ):
1020 chain = env.x_backend_chain(config)
1021 resolved = providers._resolve_x_backend(config)
1022 # Pin grok + unavailable -> [] / None (no fallthrough to bird)
1023 assert chain == []
1024 assert resolved is None
1025
1026 def test_unpinned_with_grok_store_and_cookies_returns_bird(self):
1027 """Unpinned + grok store + cookies -> runtime returns bird, never grok."""
1028 from lib import grok_x, providers
1029
1030 config = {
1031 "AUTH_TOKEN": "dummy-token",
1032 "CT0": "dummy-ct0",
1033 }
1034 with (
1035 mock.patch.object(grok_x, "has_stored_auth", return_value=True),
1036 mock.patch("lib.bird_x.is_bird_installed", return_value=True),
1037 ):
1038 chain = env.x_backend_chain(config)
1039 resolved = providers._resolve_x_backend(config)
1040 # Unpinned -> auto-chain (bird first), grok never auto-selected
1041 assert chain[0] == "bird"
1042 assert "grok" not in chain
1043 assert resolved == "bird"
1044
1045
1046 # ---------------------------------------------------------------------------
1047 # YouTube chain: yt-dlp -> ScrapeCreators
1048 # ---------------------------------------------------------------------------
1049
1050 class TestYouTubeChain:
1051 def test_ytdlp_healthy_wins(self):
1052 with mock.patch("lib.health.probe_dependency", _probe_dep()):
1053 res = backends.resolve("youtube", {"SCRAPECREATORS_API_KEY": "dummy-key"})
1054 assert res.active_backend == "yt-dlp"
1055 assert res.tier == backends.TIER_OK
1056
1057 def test_missing_ytdlp_falls_back_to_sc_key(self):
1058 with mock.patch(
1059 "lib.health.probe_dependency", _probe_dep({"yt-dlp": health.MISSING}),
1060 ):
1061 res = backends.resolve("youtube", {"SCRAPECREATORS_API_KEY": "dummy-key"})
1062 assert res.active_backend == "scrapecreators"
1063 assert res.tier == backends.TIER_OK
1064
1065 def test_ytdlp_pin_never_fails_over_to_sc(self):
1066 """#1316: a pinned yt-dlp lane reports its own failure, not SC."""
1067 config = {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_YOUTUBE_BACKEND": "yt-dlp"}
1068 with mock.patch(
1069 "lib.health.probe_dependency", _probe_dep({"yt-dlp": health.MISSING}),
1070 ):
1071 res = backends.resolve("youtube", config)
1072 assert res.pinned is True
1073 assert res.pin == "yt-dlp"
1074 assert res.active_backend is None
1075 assert res.tier == backends.TIER_ERROR
1076
1077 def test_ytdlp_pin_spelling_matches_runtime_gate(self):
1078 """Doctor reads " YTDLP " as the same pin the runtime honors."""
1079 config = {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_YOUTUBE_BACKEND": " YTDLP "}
1080 with mock.patch("lib.health.probe_dependency", _probe_dep()):
1081 res = backends.resolve("youtube", config)
1082 assert res.pinned is True
1083 assert res.pin == "yt-dlp"
1084
1085 def test_scrapecreators_is_not_a_youtube_pin_value(self):
1086 config = {"SCRAPECREATORS_API_KEY": "dummy-key", "LAST30DAYS_YOUTUBE_BACKEND": "scrapecreators"}
1087 with mock.patch("lib.health.probe_dependency", _probe_dep()):
1088 res = backends.resolve("youtube", config)
1089 assert res.pinned is False
1090 assert res.active_backend == "yt-dlp"
1091
1092 def test_neither_available_error_carries_ytdlp_prescription(self):
1093 with mock.patch(
1094 "lib.health.probe_dependency", _probe_dep({"yt-dlp": health.MISSING}),
1095 ):
1096 res = backends.resolve("youtube", {})
1097 assert res.active_backend is None
1098 assert res.tier == backends.TIER_ERROR
1099 assert "yt-dlp" in res.prescription
1100
1101
1102 # ---------------------------------------------------------------------------
1103 # Web search chain: brave -> exa -> serper -> parallel -> keyless floor
1104 # ---------------------------------------------------------------------------
1105
1106 class TestWebChain:
1107 def test_brave_key_predicted_first(self):
1108 res = backends.resolve(
1109 "web", {"BRAVE_API_KEY": "dummy-key", "EXA_API_KEY": "dummy-key"},
1110 )
1111 assert res.active_backend == "brave"
1112 assert res.tier == backends.TIER_OK
1113
1114 def test_keyless_floor_is_degraded_warn(self):
1115 res = backends.resolve("web", {})
1116 assert res.active_backend == "keyless"
1117 assert res.tier == backends.TIER_WARN
1118
1119 def test_native_search_suppresses_keyless_floor(self):
1120 res = backends.resolve("web", {"LAST30DAYS_NATIVE_SEARCH": "1"})
1121 keyless = next(f for f in res.findings if f.name == "keyless")
1122 assert not keyless.usable
1123 assert res.active_backend is None
1124
1125 def test_pin_via_web_backend_flag(self):
1126 res = backends.resolve(
1127 "web", {"BRAVE_API_KEY": "dummy-key", "EXA_API_KEY": "dummy-key"}, pin="exa",
1128 )
1129 assert res.active_backend == "exa"
1130 assert res.pinned is True
1131 assert "pinned" in res.summary
1132
1133 def test_parity_with_grounding_auto_dispatch(self):
1134 """resolve('web').active_backend must match the backend grounding's
1135 auto branch actually dispatches to, per config permutation."""
1136 from lib import grounding
1137
1138 def _auto_pick(config):
1139 picked = {}
1140
1141 def rec(label):
1142 def f(query, date_range, key, count=5):
1143 picked["backend"] = label
1144 return [], {"label": label}
1145 return f
1146
1147 with mock.patch.object(grounding, "brave_search", rec("brave")), \
1148 mock.patch.object(grounding, "exa_search", rec("exa")), \
1149 mock.patch.object(grounding, "serper_search", rec("serper")), \
1150 mock.patch.object(grounding, "parallel_search", rec("parallel")), \
1151 mock.patch(
1152 "lib.web_search_keyless.keyless_search",
1153 lambda q, dr, cfg: (picked.__setitem__("backend", "keyless") or ([], {})),
1154 ):
1155 grounding.web_search("q", ("2026-06-04", "2026-07-04"), config, backend="auto")
1156 return picked.get("backend")
1157
1158 for config in (
1159 {"BRAVE_API_KEY": "dummy-key"},
1160 {"SERPER_API_KEY": "dummy-key"},
1161 {},
1162 ):
1163 assert backends.resolve("web", config).active_backend == _auto_pick(config)
1164
1165
1166 # ---------------------------------------------------------------------------
1167 # Rendering: prediction reads as will-use, never as past observation
1168 # ---------------------------------------------------------------------------
1169
1170 class TestSummaryWording:
1171 def test_alternative_summary_is_will_use(self):
1172 res = backends.resolve("web", {"BRAVE_API_KEY": "dummy-key"})
1173 assert res.summary.startswith("will use: brave")
1174 assert "used" not in res.summary.split("will use")[1]
1175
1176 def test_error_summary_names_no_backend(self):
1177 with mock.patch(
1178 "lib.health.probe_dependency", _probe_dep({"yt-dlp": health.MISSING}),
1179 ):
1180 res = backends.resolve("youtube", {})
1181 assert "will use" not in res.summary
1182 assert "no usable backend" in res.summary.lower()
1183
1184 def test_conditional_summary_is_the_conditional_wording(self):
1185 res = backends.resolve("reddit", {"SCRAPECREATORS_API_KEY": "dummy-key"})
1186 assert res.summary == res.conditional
1187
1187 lines PYTHON