Spaces:
Sleeping

File size: 3,301 Bytes
7065d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f32872
 
 
 
 
 
7065d8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Smoke tests for the PaddleOCR sidecar.

Avoid invoking the real PaddleOCR singleton in CI -- monkeypatch
``_get_ocr`` so the unit tests run in a few hundred ms without
loading a 800MB model. Integration coverage with real PaddleOCR
lives behind the ``CARD_OCR_INTEGRATION=1`` env var so the developer
can run it locally on demand.
"""
from __future__ import annotations

import io
import os

import pytest
from fastapi.testclient import TestClient
from PIL import Image


@pytest.fixture
def client(monkeypatch):
    # Ensure auth check passes without needing the real key in CI.
    monkeypatch.setenv("OCR_INTERNAL_API_KEY", "test-key")

    # Reload main so it picks up the env var.
    import importlib
    import main as ocr_main
    importlib.reload(ocr_main)

    # Stub the OCR singleton so tests don't need PaddlePaddle installed.
    class _FakeOCR:
        def ocr(self, _arr, cls=True):  # noqa: ARG002
            return [[
                [[[0, 0], [0, 0], [0, 0], [0, 0]], ("Aanya Patel", 0.97)],
                [[[0, 0], [0, 0], [0, 0], [0, 0]], ("aanya@sunrise.example", 0.92)],
            ]]

    monkeypatch.setattr(ocr_main, "_ocr_singleton", _FakeOCR())
    return TestClient(ocr_main.app)


def _png_bytes() -> bytes:
    img = Image.new("RGB", (50, 30), (255, 255, 255))
    buf = io.BytesIO()
    img.save(buf, format="PNG")
    return buf.getvalue()


def test_health_200(client):
    res = client.get("/health")
    assert res.status_code == 200
    assert res.json() == {"ok": True}


def test_extract_requires_internal_key(client):
    res = client.post(
        "/extract",
        files={"front_image": ("f.png", _png_bytes(), "image/png")},
    )
    assert res.status_code == 401


def test_extract_rejects_wrong_key(client):
    res = client.post(
        "/extract",
        files={"front_image": ("f.png", _png_bytes(), "image/png")},
        headers={"X-Internal-API-Key": "wrong"},
    )
    assert res.status_code == 401


def test_extract_returns_text_and_confidence(client):
    res = client.post(
        "/extract",
        files={
            "front_image": ("f.png", _png_bytes(), "image/png"),
            "back_image": ("b.png", _png_bytes(), "image/png"),
        },
        headers={"X-Internal-API-Key": "test-key"},
    )
    assert res.status_code == 200, res.text
    body = res.json()
    assert "Aanya Patel" in body["front_text"]
    assert "aanya@sunrise.example" in body["front_text"]
    assert body["confidence"]["front"] > 0
    # New per-block envelope -- enables per-field confidence aggregation
    # in the Django side.
    assert "blocks" in body
    front_blocks = body["blocks"]["front"]
    assert any(b["text"] == "Aanya Patel" for b in front_blocks)
    assert all("confidence" in b for b in front_blocks)


def test_extract_rejects_empty_request(client):
    res = client.post(
        "/extract",
        headers={"X-Internal-API-Key": "test-key"},
    )
    # FastAPI may return 400 (no-images-provided) or 422 (validation).
    assert res.status_code in (400, 422)


def test_extract_rejects_invalid_image(client):
    res = client.post(
        "/extract",
        files={"front_image": ("f.png", b"not-an-image", "image/png")},
        headers={"X-Internal-API-Key": "test-key"},
    )
    assert res.status_code == 400