File size: 9,641 Bytes
240c946
 
 
 
 
 
 
 
bbbfba8
240c946
 
 
 
 
bbbfba8
 
 
240c946
 
 
 
 
 
 
bbbfba8
 
 
 
 
240c946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbbfba8
 
 
 
 
 
 
240c946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbbfba8
 
 
 
 
 
 
240c946
 
 
 
 
 
 
 
 
 
 
 
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""Integration tests for the FastAPI routes.

Tests the full API surface: providers, jobs, exports, viewer.
"""

from __future__ import annotations

import io
from typing import TYPE_CHECKING

import pytest
from fastapi.testclient import TestClient
from src.app.main import app

if TYPE_CHECKING:
    from pathlib import Path


@pytest.fixture
def client(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> TestClient:
    """TestClient with lifespan — ensures DB/FileStore/JobService are initialized."""
    monkeypatch.setenv("STORAGE_ROOT", str(tmp_path / "data"))
    # Force re-creation of settings
    from src.app import settings as settings_mod
    monkeypatch.setattr(
        settings_mod,
        "get_settings",
        lambda: settings_mod.Settings(storage_root=tmp_path / "data"),
    )
    with TestClient(app) as c:
        yield c


@pytest.fixture
def paddle_payload_bytes(fixtures_dir: Path) -> bytes:
    with open(fixtures_dir / "paddle_ocr_sample.json", "rb") as f:
        return f.read()


# -- Health ------------------------------------------------------------------


class TestHealth:
    def test_health(self, client: TestClient) -> None:
        r = client.get("/health")
        assert r.status_code == 200
        assert r.json()["status"] == "ok"


# -- Providers ---------------------------------------------------------------


class TestProviders:
    def test_register_and_list(self, client: TestClient) -> None:
        r = client.post("/providers", json={
            "provider_id": "test_paddle",
            "display_name": "PaddleOCR Test",
            "runtime_type": "local",
            "model_id_or_path": "/models/paddle",
            "family": "word_box_json",
        })
        assert r.status_code == 201
        data = r.json()
        assert data["provider_id"] == "test_paddle"

        # List
        r = client.get("/providers")
        assert r.status_code == 200
        providers = r.json()
        assert any(p.get("provider_id") == "test_paddle" for p in providers)

    def test_get_provider(self, client: TestClient) -> None:
        client.post("/providers", json={
            "provider_id": "get_test",
            "display_name": "Get Test",
            "runtime_type": "local",
            "model_id_or_path": "/models/test",
            "family": "word_box_json",
        })
        r = client.get("/providers/get_test")
        assert r.status_code == 200
        assert r.json()["provider_id"] == "get_test"

    def test_get_nonexistent_provider(self, client: TestClient) -> None:
        r = client.get("/providers/nonexistent")
        assert r.status_code == 404

    def test_delete_provider(self, client: TestClient) -> None:
        client.post("/providers", json={
            "provider_id": "del_test",
            "display_name": "Del Test",
            "runtime_type": "local",
            "model_id_or_path": "/models/test",
            "family": "word_box_json",
        })
        r = client.delete("/providers/del_test")
        assert r.status_code == 204

        r = client.get("/providers/del_test")
        assert r.status_code == 404

    def test_delete_nonexistent_provider(self, client: TestClient) -> None:
        r = client.delete("/providers/nonexistent")
        assert r.status_code == 404

    def test_register_invalid(self, client: TestClient) -> None:
        r = client.post("/providers", json={
            "provider_id": "",  # invalid: empty
            "display_name": "Bad",
            "runtime_type": "local",
            "model_id_or_path": "/x",
            "family": "word_box_json",
        })
        assert r.status_code == 422


# -- Jobs --------------------------------------------------------------------


class TestJobs:
    def _create_job(self, client: TestClient, payload_bytes: bytes) -> dict:
        r = client.post(
            "/jobs",
            params={
                "provider_id": "paddleocr",
                "provider_family": "word_box_json",
                "image_width": 2480,
                "image_height": 3508,
            },
            files={
                "raw_payload_file": (
                    "payload.json",
                    io.BytesIO(payload_bytes),
                    "application/json",
                ),
            },
        )
        assert r.status_code == 201
        return r.json()

    def test_create_and_run_job(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        data = self._create_job(client, paddle_payload_bytes)
        assert data["status"] == "succeeded"
        assert data["has_alto"] is True
        assert data["has_page_xml"] is True
        assert data["error"] is None

    def test_list_jobs(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        self._create_job(client, paddle_payload_bytes)
        r = client.get("/jobs")
        assert r.status_code == 200
        jobs = r.json()
        assert len(jobs) >= 1

    def test_get_job(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        created = self._create_job(client, paddle_payload_bytes)
        job_id = created["job_id"]

        r = client.get(f"/jobs/{job_id}")
        assert r.status_code == 200
        data = r.json()
        assert data["job_id"] == job_id
        assert data["status"] == "succeeded"

    def test_get_nonexistent_job(self, client: TestClient) -> None:
        r = client.get("/jobs/nonexistent")
        assert r.status_code == 404

    def test_get_job_logs(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        created = self._create_job(client, paddle_payload_bytes)
        job_id = created["job_id"]

        r = client.get(f"/jobs/{job_id}/logs")
        assert r.status_code == 200
        events = r.json()
        assert len(events) > 0
        steps = [e["step"] for e in events]
        assert "normalize" in steps
        assert "export_alto" in steps

    def test_invalid_payload(self, client: TestClient) -> None:
        r = client.post(
            "/jobs",
            params={
                "provider_id": "test",
                "provider_family": "word_box_json",
                "image_width": 100,
                "image_height": 100,
            },
            files={"raw_payload_file": ("bad.json", io.BytesIO(b"not json"), "application/json")},
        )
        assert r.status_code == 422


# -- Exports -----------------------------------------------------------------


class TestExports:
    def _create_job(self, client: TestClient, payload_bytes: bytes) -> str:
        r = client.post(
            "/jobs",
            params={
                "provider_id": "paddleocr",
                "provider_family": "word_box_json",
                "image_width": 2480,
                "image_height": 3508,
            },
            files={"raw_payload_file": ("p.json", io.BytesIO(payload_bytes), "application/json")},
        )
        return r.json()["job_id"]

    def test_get_raw_payload(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        job_id = self._create_job(client, paddle_payload_bytes)
        r = client.get(f"/jobs/{job_id}/raw")
        assert r.status_code == 200
        data = r.json()
        assert "provider_id" in data

    def test_get_canonical(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        job_id = self._create_job(client, paddle_payload_bytes)
        r = client.get(f"/jobs/{job_id}/canonical")
        assert r.status_code == 200
        data = r.json()
        assert "document_id" in data
        assert "pages" in data

    def test_get_alto(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        job_id = self._create_job(client, paddle_payload_bytes)
        r = client.get(f"/jobs/{job_id}/alto")
        assert r.status_code == 200
        assert r.headers["content-type"] == "application/xml"
        assert b"<alto" in r.content or b"alto" in r.content

    def test_get_page_xml(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        job_id = self._create_job(client, paddle_payload_bytes)
        r = client.get(f"/jobs/{job_id}/pagexml")
        assert r.status_code == 200
        assert r.headers["content-type"] == "application/xml"
        assert b"PcGts" in r.content

    def test_nonexistent_export(self, client: TestClient) -> None:
        r = client.get("/jobs/nonexistent/alto")
        assert r.status_code == 404

    def test_nonexistent_raw(self, client: TestClient) -> None:
        r = client.get("/jobs/nonexistent/raw")
        assert r.status_code == 404


# -- Viewer ------------------------------------------------------------------


class TestViewer:
    def test_viewer_fallback(self, client: TestClient, paddle_payload_bytes: bytes) -> None:
        r = client.post(
            "/jobs",
            params={
                "provider_id": "paddleocr",
                "provider_family": "word_box_json",
                "image_width": 2480,
                "image_height": 3508,
            },
            files={
                "raw_payload_file": (
                    "p.json",
                    io.BytesIO(paddle_payload_bytes),
                    "application/json",
                ),
            },
        )
        job_id = r.json()["job_id"]

        r = client.get(f"/jobs/{job_id}/viewer")
        assert r.status_code == 200
        data = r.json()
        assert "image_width" in data
        assert "image_height" in data

    def test_viewer_nonexistent(self, client: TestClient) -> None:
        r = client.get("/jobs/nonexistent/viewer")
        assert r.status_code == 404