FastAPI + Celery + Next.js + Postgres/Redis app with company monitoring, source collection, LLM-based change analysis, enrichment, and account security (Turnstile, escalating lockout, email verification).
79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
"""SearchProvider: Mock (deterministic, honest about having no real
|
|
evidence) and Brave (respx-mocked REST call), plus the factory's routing."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
|
|
from app.core.config import Settings
|
|
from app.search.factory import get_search_provider
|
|
from app.search.mock import MockSearchProvider
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_mock_provider_guesses_a_domain_for_official_website_queries():
|
|
provider = MockSearchProvider()
|
|
results = await provider.search("Acme Mobility official website")
|
|
|
|
assert len(results) == 1
|
|
assert results[0].url == "https://acmemobility.com"
|
|
assert "Acme Mobility" in results[0].title
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_mock_provider_is_honest_about_no_evidence_for_other_queries():
|
|
provider = MockSearchProvider()
|
|
results = await provider.search("Acme Mobility competitors")
|
|
|
|
assert len(results) == 1
|
|
assert "no information available" in results[0].snippet.lower()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_mock_provider_respects_count():
|
|
provider = MockSearchProvider()
|
|
results = await provider.search("Acme official website", count=0)
|
|
assert results == []
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_brave_provider_maps_api_response_to_search_results():
|
|
from app.search.brave import BraveSearchProvider
|
|
|
|
settings = Settings(search_provider="brave", brave_search_api_key="test-key")
|
|
provider = BraveSearchProvider(settings)
|
|
|
|
with respx.mock:
|
|
route = respx.get("https://api.search.brave.com/res/v1/web/search").mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={
|
|
"web": {
|
|
"results": [
|
|
{
|
|
"title": "Acme Mobility - Official Site",
|
|
"url": "https://acmemobility.com",
|
|
"description": "Acme Mobility builds electric scooters.",
|
|
}
|
|
]
|
|
}
|
|
},
|
|
)
|
|
)
|
|
results = await provider.search("Acme Mobility official website", count=3)
|
|
|
|
assert route.calls.last.request.headers["X-Subscription-Token"] == "test-key"
|
|
assert len(results) == 1
|
|
assert results[0].url == "https://acmemobility.com"
|
|
assert results[0].snippet == "Acme Mobility builds electric scooters."
|
|
|
|
|
|
def test_factory_routes_by_search_provider_setting():
|
|
mock_provider = get_search_provider(Settings(search_provider="mock"))
|
|
assert mock_provider.provider_name == "mock"
|
|
|
|
brave_provider = get_search_provider(Settings(search_provider="brave"))
|
|
assert brave_provider.provider_name == "brave"
|