File size: 3,250 Bytes
2e818da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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