| 1 | from pathlib import Path |
| 2 | from urllib.parse import parse_qs, urlparse |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from lib import competitors, dates, grounding, resolve |
| 7 | from tests.skill_contract import reference_text |
| 8 | |
| 9 | |
| 10 | @pytest.fixture(autouse=True) |
| 11 | def _first_party_modules(): |
| 12 | scripts = Path(__file__).resolve().parents[1] / "skills" / "last30days" / "scripts" |
| 13 | assert Path(grounding.__file__).resolve().is_relative_to(scripts) |
| 14 | assert Path(resolve.__file__).resolve().is_relative_to(scripts) |
| 15 | |
| 16 | |
| 17 | @pytest.mark.parametrize("key", ["OPENROUTER_API_KEY", "PERPLEXITY_API_KEY"]) |
| 18 | @pytest.mark.parametrize("native_search", ["0", "1"]) |
| 19 | def test_unsupported_search_credentials_do_not_claim_autoresolve( |
| 20 | monkeypatch, capsys, key, native_search, |
| 21 | ): |
| 22 | config = {key: "dummy-key", "LAST30DAYS_NATIVE_SEARCH": native_search} |
| 23 | |
| 24 | def unexpected_request(*args, **kwargs): |
| 25 | pytest.fail("Unsupported search credentials must not trigger requests") |
| 26 | |
| 27 | monkeypatch.setattr(grounding.http, "request", unexpected_request) |
| 28 | monkeypatch.setattr(grounding.http, "get_text", unexpected_request) |
| 29 | result = resolve.auto_resolve("Widgetworks", config) |
| 30 | |
| 31 | assert not resolve._has_backend(config) |
| 32 | assert result["searches_run"] == 0 |
| 33 | assert result["subreddits"] == [] |
| 34 | assert "No web search backend available" in capsys.readouterr().err |
| 35 | assert competitors.discover_competitors("Widgetworks", 2, config) == [] |
| 36 | |
| 37 | |
| 38 | @pytest.mark.parametrize("backend,key", [ |
| 39 | ("brave", "BRAVE_API_KEY"), |
| 40 | ("exa", "EXA_API_KEY"), |
| 41 | ("serper", "SERPER_API_KEY"), |
| 42 | ("parallel", "PARALLEL_API_KEY"), |
| 43 | ]) |
| 44 | def test_supported_search_credentials_resolve_through_real_dispatcher( |
| 45 | monkeypatch, backend, key, |
| 46 | ): |
| 47 | calls = [] |
| 48 | |
| 49 | def request(method, url, **kwargs): |
| 50 | calls.append((method, url)) |
| 51 | body = kwargs.get("json_data", {}) |
| 52 | query = ( |
| 53 | parse_qs(urlparse(url).query).get("q", [""])[0] |
| 54 | or body.get("query") |
| 55 | or body.get("q") |
| 56 | or (body.get("search_queries") or [""])[0] |
| 57 | ) |
| 58 | if "subreddit" in query: |
| 59 | title, snippet = "r/widgetworks discussion", "Community updates" |
| 60 | elif "handle" in query: |
| 61 | title, snippet = "Widgetworks on X", "Follow @widgetworks" |
| 62 | else: |
| 63 | title, snippet = "Widgetworks update", "Widgetworks announced a new product." |
| 64 | item = { |
| 65 | "title": title, |
| 66 | "url": "https://example.org/article", |
| 67 | "link": "https://example.org/article", |
| 68 | "description": snippet, |
| 69 | "snippet": snippet, |
| 70 | "text": snippet, |
| 71 | "excerpts": [snippet], |
| 72 | } |
| 73 | return {"web": {"results": [item]}, "results": [item], "organic": [item]} |
| 74 | |
| 75 | monkeypatch.setattr(grounding.http, "request", request) |
| 76 | config = {key: "dummy-key", "LAST30DAYS_NATIVE_SEARCH": "1"} |
| 77 | result = resolve.auto_resolve("Widgetworks", config) |
| 78 | |
| 79 | assert resolve._has_backend(config) |
| 80 | assert result["searches_run"] == 4 |
| 81 | assert result["subreddits"] == ["widgetworks"] |
| 82 | assert result["x_handle"] == "widgetworks" |
| 83 | assert "announced a new product" in result["context"] |
| 84 | assert len(calls) == 4 |
| 85 | assert all(backend in url for _, url in calls) |
| 86 | |
| 87 | |
| 88 | def test_non_latin_auto_search_uses_available_exa_without_brave(monkeypatch): |
| 89 | calls = [] |
| 90 | |
| 91 | def request(method, url, **kwargs): |
| 92 | calls.append((method, url, kwargs)) |
| 93 | return {"results": [{ |
| 94 | "title": "קפה עלית", |
| 95 | "url": "https://example.org/coffee", |
| 96 | "text": "עדכונים על קפה עלית", |
| 97 | }]} |
| 98 | |
| 99 | monkeypatch.setattr(grounding.http, "request", request) |
| 100 | topic = "קפה עלית" |
| 101 | items, artifact = grounding.web_search( |
| 102 | topic, dates.get_date_range(30), {"EXA_API_KEY": "dummy-key"}, |
| 103 | ) |
| 104 | |
| 105 | assert artifact["label"] == "exa" |
| 106 | assert items[0]["title"] == topic |
| 107 | assert calls[0][1] == "https://api.exa.ai/search" |
| 108 | assert calls[0][2]["json_data"]["query"] == topic |
| 109 | |
| 110 | |
| 111 | def test_non_latin_auto_search_keeps_keyless_floor(monkeypatch): |
| 112 | html = ( |
| 113 | '<a class="result__a" href="https://example.org/coffee">קפה עלית</a>' |
| 114 | '<a class="result__snippet">עדכונים על קפה עלית</a>' |
| 115 | ) |
| 116 | monkeypatch.setattr(grounding.http, "get_text", lambda *args, **kwargs: html) |
| 117 | items, artifact = grounding.web_search("קפה עלית", dates.get_date_range(30), {}) |
| 118 | assert items[0]["title"] == "קפה עלית" |
| 119 | assert artifact["keyless_backend"] == "ddg" |
| 120 | |
| 121 | |
| 122 | def test_native_search_host_does_not_start_keyless_search(monkeypatch): |
| 123 | def unexpected_request(*args, **kwargs): |
| 124 | pytest.fail("Native-search hosts supply their own web results") |
| 125 | |
| 126 | monkeypatch.setattr(grounding.http, "get_text", unexpected_request) |
| 127 | assert grounding.web_search( |
| 128 | "קפה עלית", dates.get_date_range(30), {"LAST30DAYS_NATIVE_SEARCH": "1"}, |
| 129 | ) == ([], {}) |
| 130 | |
| 131 | |
| 132 | def test_explicit_unavailable_backend_still_reports_configuration_error(): |
| 133 | with pytest.raises(RuntimeError, match="BRAVE_API_KEY is required"): |
| 134 | grounding.web_search( |
| 135 | "קפה עלית", dates.get_date_range(30), {"EXA_API_KEY": "dummy-key"}, |
| 136 | backend="brave", |
| 137 | ) |
| 138 | |
| 139 | |
| 140 | def test_non_latin_runbook_preserves_available_backend_selection(): |
| 141 | runbook = reference_text("research-runbook") |
| 142 | section = runbook.split("**Class 5:", 1)[1].split("**Pre-Flight decision flow", 1)[0] |
| 143 | assert "Leave `--web-backend` unset" in section |
| 144 | assert "host's native web search" in section |
| 145 | assert "Never force `--web-backend brave`" in section |
| 146 | assert "Force `--web-backend brave`" not in section |
| 147 | assert "| `--web-backend brave` | Step 0.45 Class 5 |" not in runbook |
| 148 | |
| 149 | |
| 150 | def test_competitor_instructions_only_claim_supported_search_backends(): |
| 151 | text = reference_text("competitors") |
| 152 | capability = text.split("**Engine-internal auto-resolve:**", 1)[1].split("\n", 1)[0] |
| 153 | for key in ("BRAVE_API_KEY", "EXA_API_KEY", "SERPER_API_KEY", "PARALLEL_API_KEY"): |
| 154 | assert key in capability |
| 155 | assert "PERPLEXITY_API_KEY" not in capability |
| 156 | assert "OPENROUTER_API_KEY" not in capability |
| 157 |