File size: 4,438 Bytes
4e75170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""tests/test_api.py β€” FastAPI endpoint integration tests."""
from __future__ import annotations

import io
import pytest
import numpy as np
from PIL import Image
from fastapi.testclient import TestClient


@pytest.fixture(scope="module")
def client():
    from src.api.main import app
    return TestClient(app)


@pytest.fixture
def jpeg_bytes():
    arr = (np.random.rand(224, 224, 3) * 255).astype(np.uint8)
    buf = io.BytesIO()
    Image.fromarray(arr).save(buf, format="JPEG")
    return buf.getvalue()


# ── GET /health ───────────────────────────────────────────────────────────────

def test_health_returns_200(client):
    r = client.get("/health")
    assert r.status_code == 200


def test_health_has_required_fields(client):
    data = client.get("/health").json()
    assert data["status"] == "ok"
    assert "version" in data
    assert "engines" in data
    assert "inference_backend" in data
    assert "runpod_configured" in data
    assert set(data["engines"]) == {"fingerprint", "coherence", "sstgnn"}


def test_health_models_returns_inventory(client):
    data = client.get("/health/models").json()
    assert "fingerprint" in data
    assert "coherence" in data
    assert "sstgnn" in data
    assert "generator_labels" in data
    assert "stable_diffusion" in data["generator_labels"]


# ── GET / ─────────────────────────────────────────────────────────────────────

def test_root_returns_html(client):
    r = client.get("/")
    assert r.status_code == 200
    assert "text/html" in r.headers["content-type"]


# ── POST /detect/image ────────────────────────────────────────────────────────

def test_detect_image_returns_200(client, jpeg_bytes):
    r = client.post(
        "/detect/image",
        files={"file": ("test.jpg", jpeg_bytes, "image/jpeg")},
    )
    assert r.status_code == 200


def test_detect_image_response_schema(client, jpeg_bytes):
    data = client.post(
        "/detect/image",
        files={"file": ("test.jpg", jpeg_bytes, "image/jpeg")},
    ).json()

    assert data["verdict"] in ("FAKE", "REAL")
    assert 0.0 <= data["confidence"] <= 1.0
    assert "attributed_generator" in data
    assert "explanation" in data
    assert "engine_breakdown" in data
    assert len(data["engine_breakdown"]) == 3


def test_detect_image_engine_names(client, jpeg_bytes):
    data = client.post(
        "/detect/image",
        files={"file": ("test.jpg", jpeg_bytes, "image/jpeg")},
    ).json()

    engine_names = {e["engine"] for e in data["engine_breakdown"]}
    assert engine_names == {"fingerprint", "coherence", "sstgnn"}


def test_detect_image_engine_confidence_range(client, jpeg_bytes):
    data = client.post(
        "/detect/image",
        files={"file": ("test.jpg", jpeg_bytes, "image/jpeg")},
    ).json()

    for engine in data["engine_breakdown"]:
        assert 0.0 <= engine["confidence"] <= 1.0
        assert engine["verdict"] in ("FAKE", "REAL")


def test_detect_image_too_large_returns_413(client):
    big = b"x" * (21 * 1024 * 1024)  # 21MB > 20MB limit
    r   = client.post(
        "/detect/image",
        files={"file": ("big.jpg", big, "image/jpeg")},
    )
    assert r.status_code == 413


def test_detect_image_wrong_type_returns_415(client, jpeg_bytes):
    r = client.post(
        "/detect/image",
        files={"file": ("test.mp4", jpeg_bytes, "video/mp4")},
    )
    assert r.status_code == 415


def test_detect_image_processing_time_positive(client, jpeg_bytes):
    data = client.post(
        "/detect/image",
        files={"file": ("test.jpg", jpeg_bytes, "image/jpeg")},
    ).json()
    assert data["processing_time_ms"] >= 0


# ── POST /detect/video ────────────────────────────────────────────────────────

def test_detect_video_wrong_type_returns_415(client, jpeg_bytes):
    r = client.post(
        "/detect/video",
        files={"file": ("test.jpg", jpeg_bytes, "image/jpeg")},
    )
    assert r.status_code == 415