File size: 8,114 Bytes
e44fdef
2e4089d
e44fdef
 
 
 
2e4089d
e44fdef
 
 
 
2e4089d
 
 
 
 
 
 
e44fdef
2e4089d
e44fdef
 
2e4089d
 
 
 
e44fdef
 
 
 
 
2e4089d
e44fdef
 
2e4089d
 
e44fdef
 
2e4089d
 
 
 
 
 
 
 
 
 
 
 
 
 
e44fdef
 
 
 
 
 
2e4089d
 
 
e44fdef
2e4089d
 
 
 
 
 
 
 
e44fdef
 
 
2e4089d
e44fdef
2e4089d
e44fdef
2e4089d
 
 
 
 
 
e44fdef
2e4089d
e44fdef
2e4089d
e44fdef
 
2e4089d
 
 
e44fdef
2e4089d
 
 
 
 
e44fdef
2e4089d
e44fdef
 
2e4089d
e44fdef
2e4089d
 
e44fdef
2e4089d
e44fdef
2e4089d
e44fdef
 
2e4089d
e44fdef
2e4089d
 
e44fdef
2e4089d
 
 
 
 
e44fdef
2e4089d
e44fdef
 
2e4089d
 
 
 
 
e44fdef
 
2e4089d
e44fdef
2e4089d
e44fdef
2e4089d
e44fdef
2e4089d
 
e44fdef
 
 
2e4089d
e44fdef
 
2e4089d
 
e44fdef
2e4089d
 
 
 
e44fdef
 
2e4089d
e44fdef
 
 
 
 
 
 
 
 
 
2e4089d
 
e44fdef
 
 
2e4089d
e44fdef
 
2e4089d
 
e44fdef
2e4089d
e44fdef
2e4089d
e44fdef
 
2e4089d
e44fdef
2e4089d
 
e44fdef
2e4089d
 
e44fdef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e4089d
e44fdef
 
 
2e4089d
e44fdef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e4089d
e44fdef
 
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
"""The Google Patents -> EPO OPS fallback chain.

The policy lives in services.PatentService, so most of this is plain unit
testing with no ASGI client involved. Only the last section goes through
the app, and only to check that the domain errors the service raises map
onto the right status codes.

The distinction these tests exist to protect: a 404 means "this patent does
not exist", and an agent is explicitly told by MCP_INSTRUCTIONS to believe
it and move on without retrying. Anything else - an upstream outage, a
timeout, a blocked scrape - must not be reported that way.
"""

import httpx
import pytest
from httpx import ASGITransport, HTTPStatusError, Request, Response

import app as app_module
import services as services_module
from scrap import PatentScrapResult
from services import (OPSUnconfigured, PatentNotFound, PatentService,
                      UpstreamUnavailable)

PATENT_ID = "US11930446B2"


class FakeOPSTokens:
    """Stand-in for the OPS token manager. Tests only care whether
    credentials are configured, and this avoids assigning to private
    attributes on the real module-level singleton.
    """

    def __init__(self, configured: bool):
        self.configured = configured


def make_service(*, ops_configured: bool) -> PatentService:
    return PatentService(http_client=None, ops_tokens=FakeOPSTokens(ops_configured))


def _http_error(status: int) -> HTTPStatusError:
    request = Request("GET", "https://patents.google.com/")
    return HTTPStatusError(
        str(status), request=request, response=Response(status, request=request))


def _raises(exc):
    async def _fn(*args, **kwargs):
        raise exc
    return _fn


def _returns(value):
    async def _fn(*args, **kwargs):
        return value
    return _fn


# ------------------------------- the happy path -------------------------------


async def test_successful_scrape_never_touches_ops(monkeypatch):
    ops_called = False

    async def fake_ops(*args, **kwargs):
        nonlocal ops_called
        ops_called = True
        return PatentScrapResult(title="from OPS")

    monkeypatch.setattr(
        services_module, "scrap_patent_async",
        _returns(PatentScrapResult(title="from Google Patents")))
    monkeypatch.setattr(services_module, "ops_scrap_patent", fake_ops)

    result = await make_service(ops_configured=True).scrap(PATENT_ID)

    assert result.title == "from Google Patents"
    assert ops_called is False


# ------------------------- genuine miss vs upstream failure -------------------------


async def test_a_real_404_from_google_patents_is_not_found(monkeypatch):
    """The one case where "not found" is the truth."""
    monkeypatch.setattr(services_module, "scrap_patent_async", _raises(_http_error(404)))

    with pytest.raises(PatentNotFound):
        await make_service(ops_configured=False).scrap(PATENT_ID)


@pytest.mark.parametrize("status", [429, 500, 502, 503])
async def test_an_upstream_failure_is_not_reported_as_not_found(monkeypatch, status):
    """MCP_INSTRUCTIONS tells the model a "not found" patent is genuinely
    absent everywhere and to move on rather than retrying. Reporting a
    transient upstream failure that way teaches an agent - with the
    server's explicit encouragement - that a real patent does not exist.
    """
    monkeypatch.setattr(services_module, "scrap_patent_async", _raises(_http_error(status)))

    with pytest.raises(UpstreamUnavailable) as exc_info:
        await make_service(ops_configured=False).scrap(PATENT_ID)

    assert exc_info.value.timeout is False


async def test_a_timeout_is_flagged_as_a_timeout(monkeypatch):
    monkeypatch.setattr(
        services_module, "scrap_patent_async", _raises(httpx.ConnectTimeout("timed out")))

    with pytest.raises(UpstreamUnavailable) as exc_info:
        await make_service(ops_configured=False).scrap(PATENT_ID)

    assert exc_info.value.timeout is True


async def test_an_unparseable_page_is_an_upstream_failure(monkeypatch):
    """parse_patent_html raises ValueError when the page isn't a patent page
    (an interstitial, or a markup change). That is our problem or theirs,
    but it is not evidence the patent doesn't exist.
    """
    monkeypatch.setattr(
        services_module, "scrap_patent_async", _raises(ValueError("no DC.title")))

    with pytest.raises(UpstreamUnavailable):
        await make_service(ops_configured=False).scrap(PATENT_ID)


# --------------------------------- the OPS fallback ---------------------------------


async def test_falls_back_to_ops_when_google_patents_fails(monkeypatch):
    monkeypatch.setattr(services_module, "scrap_patent_async", _raises(_http_error(503)))
    monkeypatch.setattr(
        services_module, "ops_scrap_patent", _returns(PatentScrapResult(title="from OPS")))

    result = await make_service(ops_configured=True).scrap(PATENT_ID)

    assert result.title == "from OPS"


async def test_missing_from_both_backends_is_not_found(monkeypatch):
    monkeypatch.setattr(services_module, "scrap_patent_async", _raises(_http_error(404)))
    monkeypatch.setattr(services_module, "ops_scrap_patent", _raises(_http_error(404)))

    with pytest.raises(PatentNotFound):
        await make_service(ops_configured=True).scrap(PATENT_ID)


async def test_ops_failing_after_a_google_patents_404_is_not_a_miss(monkeypatch):
    """Google Patents says the patent is missing, but OPS - the backend that
    covers what Google Patents doesn't - never answered. That is unknown,
    not absent.
    """
    monkeypatch.setattr(services_module, "scrap_patent_async", _raises(_http_error(404)))
    monkeypatch.setattr(services_module, "ops_scrap_patent", _raises(_http_error(500)))

    with pytest.raises(UpstreamUnavailable):
        await make_service(ops_configured=True).scrap(PATENT_ID)


# ------------------------------ the OPS-only path ------------------------------


async def test_ops_only_retrieval_requires_credentials():
    with pytest.raises(OPSUnconfigured):
        await make_service(ops_configured=False).ops_scrap(PATENT_ID)


async def test_ops_only_retrieval_reports_a_real_miss(monkeypatch):
    """Here OPS is the only backend asked, so its 404 is the whole answer."""
    monkeypatch.setattr(services_module, "ops_scrap_patent", _raises(_http_error(404)))

    with pytest.raises(PatentNotFound):
        await make_service(ops_configured=True).ops_scrap(PATENT_ID)


async def test_ops_only_retrieval_flags_a_timeout(monkeypatch):
    monkeypatch.setattr(
        services_module, "ops_scrap_patent", _raises(httpx.ConnectTimeout("timed out")))

    with pytest.raises(UpstreamUnavailable) as exc_info:
        await make_service(ops_configured=True).ops_scrap(PATENT_ID)

    assert exc_info.value.timeout is True


# ---------------------------- domain error -> status code ----------------------------


@pytest.fixture
async def client():
    transport = ASGITransport(app=app_module.app)
    async with httpx.AsyncClient(transport=transport, base_url="http://test") as c:
        yield c


@pytest.fixture
def override_patent_service():
    """Swap the app's patent service for one the test controls, then put the
    real provider back."""
    def _install(service):
        app_module.app.dependency_overrides[app_module.get_patent_service] = lambda: service

    yield _install
    app_module.app.dependency_overrides.clear()


class _StubService:
    def __init__(self, error):
        self._error = error

    async def scrap(self, patent_id):
        raise self._error

    ops_scrap = scrap


@pytest.mark.parametrize("error, expected_status", [
    (PatentNotFound("gone"), 404),
    (UpstreamUnavailable("upstream broke"), 502),
    (UpstreamUnavailable("slow", timeout=True), 504),
    (OPSUnconfigured("no credentials"), 503),
])
async def test_domain_errors_map_onto_status_codes(
        client, override_patent_service, error, expected_status):
    override_patent_service(_StubService(error))

    resp = await client.get(f"/scrap/scrap_patent/{PATENT_ID}")

    assert resp.status_code == expected_status
    assert resp.json()["detail"] == str(error)