Spaces:
Running on Zero
Running on Zero
File size: 9,491 Bytes
56f6a56 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | """Unit tests for the fixed-source local web-search parser."""
from __future__ import annotations
import unittest
from unittest.mock import patch
import httpx
import web_search
from web_search import (
SearchUnavailable,
parse_bing_rss,
parse_duckduckgo_lite,
parse_google_news_rss,
search_web,
)
class FakeClient:
def __enter__(self) -> "FakeClient":
return self
def __exit__(self, *_args: object) -> None:
return None
class WebSearchParserTests(unittest.TestCase):
def test_duckduckgo_lite_results_and_redirects(self) -> None:
payload = """
<html><body>
<a class="result-link"
href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fexample.com%2Fdocs&rut=x">
Example & docs
</a>
<td class="result-snippet">A <b>useful</b> result.</td>
</body></html>
"""
self.assertEqual(
parse_duckduckgo_lite(payload),
[
{
"title": "Example & docs",
"url": "https://example.com/docs",
"description": "A useful result.",
"source": "example.com",
}
],
)
def test_bing_rss_results(self) -> None:
payload = """<?xml version="1.0"?>
<rss><channel><item>
<title>Projeto</title>
<link>https://example.org/project</link>
<description>Uma <b>descrição</b>.</description>
</item></channel></rss>
"""
self.assertEqual(
parse_bing_rss(payload),
[
{
"title": "Projeto",
"url": "https://example.org/project",
"description": "Uma descrição.",
"source": "example.org",
}
],
)
def test_google_news_rss_parses_date_and_source(self) -> None:
payload = """<?xml version="1.0" encoding="UTF-8"?>
<rss><channel><item>
<title>Operação acontece no Rio de Janeiro</title>
<link>https://news.google.com/rss/articles/example</link>
<description><![CDATA[
<a href="https://example.com/rj">Operação acontece no Rio de Janeiro</a>
<p>Forças de segurança divulgaram o balanço.</p>
<font>O GLOBO</font>
]]></description>
<pubDate>Tue, 04 Aug 2026 17:11:11 GMT</pubDate>
<source url="https://oglobo.globo.com">O GLOBO</source>
</item></channel></rss>
"""
self.assertEqual(
parse_google_news_rss(payload),
[
{
"title": "Operação acontece no Rio de Janeiro",
"url": "https://news.google.com/rss/articles/example",
"description": (
"Publicado em 04/08/2026 17:11 UTC — Fonte: O GLOBO. "
"Forças de segurança divulgaram o balanço."
),
"source": "O GLOBO",
}
],
)
def test_google_news_rss_rejects_unsafe_link(self) -> None:
payload = """<rss><channel><item>
<title>Resultado inseguro</title>
<link>javascript:alert(1)</link>
<pubDate>Tue, 04 Aug 2026 17:11:11 GMT</pubDate>
<source>Fonte</source>
</item></channel></rss>"""
self.assertEqual(parse_google_news_rss(payload), [])
def test_invalid_result_scheme_is_rejected(self) -> None:
payload = """
<a class="result-link" href="javascript:alert(1)">Inseguro</a>
<td class="result-snippet">Não deve aparecer.</td>
"""
self.assertEqual(parse_duckduckgo_lite(payload), [])
def test_invalid_result_does_not_replace_previous_snippet(self) -> None:
payload = """
<a class="result-link" href="https://example.com/valid">Valido</a>
<td class="result-snippet">Descricao valida.</td>
<a class="result-link" href="javascript:alert(1)">Inseguro</a>
<td class="result-snippet">Descricao insegura.</td>
"""
self.assertEqual(
parse_duckduckgo_lite(payload),
[
{
"title": "Valido",
"url": "https://example.com/valid",
"description": "Descricao valida.",
"source": "example.com",
}
],
)
class WebSearchAggregationTests(unittest.TestCase):
def test_recent_news_aggregates_and_prioritizes_rio_de_janeiro(self) -> None:
google_results = [
{
"title": "Agenda cultural em Porto Alegre, RS",
"url": "https://gauchazh.clicrbs.com.br/porto-alegre/noticia",
"description": "Publicado em 04/08/2026 17:00 UTC — Fonte: GZH.",
"source": "GZH",
},
{
"title": "Prefeitura do Rio de Janeiro anuncia medida",
"url": "https://example.com/rj/prefeitura",
"description": "Publicado em 04/08/2026 16:00 UTC — Fonte: Jornal RJ.",
"source": "Jornal RJ",
},
]
duck_results = [
{
"title": "Prefeitura do Rio de Janeiro anuncia medida",
"url": "https://duplicate.example/noticia",
"description": "Resultado duplicado vindo de outro provedor.",
"source": "duplicate.example",
},
{
"title": "Trânsito no RJ tem alteração nesta terça",
"url": "https://example.org/noticias/rj/transito",
"description": "Mudança afeta vias da capital fluminense.",
"source": "example.org",
},
]
with (
patch("web_search.httpx.Client", return_value=FakeClient()),
patch(
"web_search._google_news_rss",
return_value=google_results,
) as google,
patch(
"web_search._duckduckgo_lite",
return_value=duck_results,
) as duck,
patch("web_search._bing_rss") as bing,
):
result = search_web("últimas notícias do RJ hoje")
self.assertEqual(result["provider"], "google-news+duckduckgo-lite")
self.assertEqual(
[item["title"] for item in result["results"]],
[
"Prefeitura do Rio de Janeiro anuncia medida",
"Trânsito no RJ tem alteração nesta terça",
"Agenda cultural em Porto Alegre, RS",
],
)
google.assert_called_once()
duck.assert_called_once()
bing.assert_not_called()
def test_recent_news_falls_back_when_primary_providers_fail(self) -> None:
bing_result = {
"title": "Notícia do Rio de Janeiro",
"url": "https://example.net/rj/noticia",
"description": "Informação atualizada.",
"source": "example.net",
}
network_error = httpx.ConnectError("offline")
with (
patch("web_search.httpx.Client", return_value=FakeClient()),
patch("web_search._google_news_rss", side_effect=network_error),
patch("web_search._duckduckgo_lite", return_value=[]),
patch("web_search._bing_rss", return_value=[bing_result]),
):
result = search_web("notícias recentes do Rio de Janeiro")
self.assertEqual(result["provider"], "bing-rss")
self.assertEqual(result["results"], [bing_result])
def test_general_search_aggregates_existing_providers(self) -> None:
duck_result = {
"title": "Documentação do projeto",
"url": "https://example.com/docs",
"description": "Guia principal.",
"source": "example.com",
}
bing_result = {
"title": "Repositório do projeto",
"url": "https://github.com/example/project",
"description": "Código fonte.",
"source": "github.com",
}
with (
patch("web_search.httpx.Client", return_value=FakeClient()),
patch("web_search._duckduckgo_lite", return_value=[duck_result]),
patch("web_search._bing_rss", return_value=[bing_result]),
patch("web_search._wikipedia") as wikipedia,
patch("web_search._google_news_rss") as google_news,
):
result = search_web("documentação projeto")
self.assertEqual(result["provider"], "duckduckgo-lite+bing-rss")
self.assertEqual(len(result["results"]), 2)
wikipedia.assert_not_called()
google_news.assert_not_called()
def test_all_providers_unavailable_preserves_search_error(self) -> None:
network_error = httpx.ConnectError("offline")
with (
patch("web_search.httpx.Client", return_value=FakeClient()),
patch("web_search._google_news_rss", side_effect=network_error),
patch("web_search._duckduckgo_lite", return_value=[]),
patch("web_search._bing_rss", side_effect=network_error),
):
with self.assertRaisesRegex(SearchUnavailable, "google-news"):
search_web("últimas notícias")
if __name__ == "__main__":
unittest.main()
|