import pytest from app.services.scholar_service import fetch_author_works, fetch_ranked_papers, resolve_author class FakeOpenAlex: def __init__(self, authors, works=None): self.authors = authors self.works = works or [] self.last_params = {} async def __call__(self, path, params): self.last_params = dict(params) if path == "authors": return {"results": self.authors} if path == "works": return {"results": self.works} raise AssertionError(path) @pytest.mark.asyncio async def test_resolve_author_returns_exact_provider_identity(): provider = FakeOpenAlex([ {"id": "https://openalex.org/A5023888391", "display_name": "Jonas Gehring", "works_count": 42, "cited_by_count": 9000}, {"id": "https://openalex.org/A2", "display_name": "Jon Gehring", "works_count": 3, "cited_by_count": 1}, ]) result = await resolve_author("Jonas Gehring", request_json=provider) assert result.status == "resolved" assert result.candidates[0].openalex_id == "A5023888391" @pytest.mark.asyncio async def test_resolve_author_preserves_ambiguous_exact_people(): provider = FakeOpenAlex([ {"id": "https://openalex.org/A1", "display_name": "Alex Kim", "works_count": 12, "cited_by_count": 100}, {"id": "https://openalex.org/A2", "display_name": "Alex Kim", "works_count": 8, "cited_by_count": 80}, ]) result = await resolve_author("Alex Kim", request_json=provider) assert result.status == "ambiguous" assert [candidate.openalex_id for candidate in result.candidates] == ["A1", "A2"] @pytest.mark.asyncio async def test_fetch_author_works_filters_by_author_and_sorts_latest(): provider = FakeOpenAlex([], [{ "id": "https://openalex.org/W1", "display_name": "Newest verified work", "publication_year": 2026, "publication_date": "2026-06-01", "cited_by_count": 4, "authorships": [{"author": {"display_name": "Jonas Gehring"}}], "primary_location": {}, }]) papers = await fetch_author_works( "A5023888391", n=1, ranking="publication_date", request_json=provider, ) assert len(papers) == 1 assert papers[0]["year"] == 2026 assert provider.last_params["filter"] == "authorships.author.id:A5023888391" assert provider.last_params["sort"] == "publication_date:desc" @pytest.mark.asyncio async def test_fetch_ranked_papers_can_rank_an_entire_year_by_citations(): provider = FakeOpenAlex([], [{ "id": "https://openalex.org/W2", "display_name": "Most cited paper of the year", "publication_year": 2026, "cited_by_count": 99, "authorships": [], "primary_location": {}, }]) papers = await fetch_ranked_papers( n=5, year_start=2026, year_end=2026, ranking="citation_count", request_json=provider, ) assert papers[0]["title"] == "Most cited paper of the year" assert provider.last_params["filter"] == "from_publication_date:2026-01-01,to_publication_date:2026-12-31" assert provider.last_params["sort"] == "cited_by_count:desc" assert "search" not in provider.last_params