File size: 10,970 Bytes
3020394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

import numpy as np
import cv2
from fastapi.testclient import TestClient

from app import main
from monitoring import feedback as feedback_mod
from monitoring import metrics as metrics_mod


class TestFeedbackRecording(unittest.TestCase):
    def setUp(self):
        self._tmpdir = tempfile.TemporaryDirectory()
        self.tmp_path = Path(self._tmpdir.name)
        self.feedback_path = self.tmp_path / "feedback.jsonl"

    def tearDown(self):
        self._tmpdir.cleanup()

    def test_record_feedback_appends_entry(self):
        entry = feedback_mod.record_feedback(
            "abc.jpg", {"prix": 10000, "volume": 14.28},
            corrected_by="pompiste-7", feedback_path=self.feedback_path,
        )
        self.assertEqual(entry["photo_reference"], "abc.jpg")
        self.assertFalse(entry["converted"])

        entries = feedback_mod.load_feedback_entries(self.feedback_path)
        self.assertEqual(len(entries), 1)
        self.assertEqual(entries[0]["corrected_fields"]["prix"], 10000)

    def test_record_feedback_rejects_unknown_field(self):
        with self.assertRaises(ValueError):
            feedback_mod.record_feedback(
                "abc.jpg", {"montant_total": 10000}, feedback_path=self.feedback_path,
            )

    def test_record_feedback_rejects_empty_corrections(self):
        with self.assertRaises(ValueError):
            feedback_mod.record_feedback("abc.jpg", {}, feedback_path=self.feedback_path)

    def test_load_feedback_entries_missing_file_returns_empty(self):
        entries = feedback_mod.load_feedback_entries(self.tmp_path / "nope.jsonl")
        self.assertEqual(entries, [])


class TestFeedbackConversion(unittest.TestCase):
    def setUp(self):
        self._tmpdir = tempfile.TemporaryDirectory()
        self.tmp_path = Path(self._tmpdir.name)
        self.photos_dir = self.tmp_path / "photos"
        self.photos_dir.mkdir()
        self.feedback_path = self.tmp_path / "feedback.jsonl"
        self.annotations_path = self.tmp_path / "annotations.json"
        self.crops_dir = self.tmp_path / "crops"

        img = np.full((200, 400, 3), 30, dtype=np.uint8)
        cv2.imwrite(str(self.photos_dir / "photo1.jpg"), img)

    def tearDown(self):
        self._tmpdir.cleanup()

    def test_convert_creates_pending_review_annotation(self):
        feedback_mod.record_feedback(
            "photo1.jpg", {"prix": 10000, "volume": 14.28, "prix_litre": 700},
            corrected_by="pompiste-7", feedback_path=self.feedback_path,
        )

        count = feedback_mod.convert_feedback_to_annotations(
            photos_dir=self.photos_dir, feedback_path=self.feedback_path,
            annotations_path=self.annotations_path, crops_dir=self.crops_dir,
        )
        self.assertEqual(count, 1)

        with open(self.annotations_path, "r", encoding="utf-8") as f:
            annotations = json.load(f)
        self.assertIn("photo1.jpg", annotations)
        entry = annotations["photo1.jpg"]
        self.assertEqual(entry["status"], "pending_review")
        self.assertEqual(entry["fields"]["prix"], "10000")
        self.assertEqual(entry["fields"]["volume"], "14.28")

        entries = feedback_mod.load_feedback_entries(self.feedback_path)
        self.assertTrue(entries[0]["converted"])

    def test_convert_is_idempotent_for_already_converted_entries(self):
        feedback_mod.record_feedback(
            "photo1.jpg", {"prix": 10000}, feedback_path=self.feedback_path,
        )
        feedback_mod.convert_feedback_to_annotations(
            photos_dir=self.photos_dir, feedback_path=self.feedback_path,
            annotations_path=self.annotations_path, crops_dir=self.crops_dir,
        )
        second_count = feedback_mod.convert_feedback_to_annotations(
            photos_dir=self.photos_dir, feedback_path=self.feedback_path,
            annotations_path=self.annotations_path, crops_dir=self.crops_dir,
        )
        self.assertEqual(second_count, 0)

    def test_convert_skips_missing_photo(self):
        feedback_mod.record_feedback(
            "ghost.jpg", {"prix": 10000}, feedback_path=self.feedback_path,
        )
        count = feedback_mod.convert_feedback_to_annotations(
            photos_dir=self.photos_dir, feedback_path=self.feedback_path,
            annotations_path=self.annotations_path, crops_dir=self.crops_dir,
        )
        self.assertEqual(count, 0)


class TestMetrics(unittest.TestCase):
    def setUp(self):
        self._tmpdir = tempfile.TemporaryDirectory()
        self.log_path = Path(self._tmpdir.name) / "api.log"

    def tearDown(self):
        self._tmpdir.cleanup()

    def _write_log_line(self, payload):
        with open(self.log_path, "a", encoding="utf-8") as f:
            f.write(f"2026-07-07 10:00:00,000 | INFO | {json.dumps(payload, ensure_ascii=False)}\n")

    def test_compute_metrics_on_missing_log_returns_zero(self):
        m = metrics_mod.compute_metrics(self.log_path)
        self.assertEqual(m["total_requests"], 0)
        self.assertIsNone(m["success_rate"])

    def test_compute_metrics_aggregates_success_and_blocking(self):
        self._write_log_line({
            "model_version": "v2",
            "response": {"success": True, "confidence_score": 0.9, "image_quality": "valid", "message": "ok"},
        })
        self._write_log_line({
            "model_version": "v2",
            "response": {"success": False, "confidence_score": 0.2, "image_quality": "blurry", "message": "Photo floue, veuillez reprendre la photo."},
        })
        with open(self.log_path, "a", encoding="utf-8") as f:
            f.write("2026-07-07 10:00:01,000 | INFO | Modèle CRNN chargé sur cpu.\n")

        m = metrics_mod.compute_metrics(self.log_path)
        self.assertEqual(m["total_requests"], 2)
        self.assertEqual(m["success_rate"], 0.5)
        self.assertAlmostEqual(m["avg_confidence_score"], 0.55)
        self.assertEqual(m["blocking_causes"]["Photo floue, veuillez reprendre la photo."], 1)
        self.assertEqual(m["image_quality_distribution"]["valid"], 1)

    def test_list_failed_requests_returns_only_failures_most_recent_first(self):
        self._write_log_line({
            "response": {"success": True, "photo_reference": "ok.jpg", "message": "ok"},
        })
        self._write_log_line({
            "response": {"success": False, "photo_reference": "first-fail.jpg",
                        "message": "Photo floue, veuillez reprendre la photo.",
                        "image_quality": "blurry", "confidence_score": 0.1},
        })
        self._write_log_line({
            "response": {"success": False, "photo_reference": "second-fail.jpg",
                        "message": "Incohérence détectée entre montant, litres et prix.",
                        "image_quality": "valid", "confidence_score": 0.4},
        })

        failures = metrics_mod.list_failed_requests(self.log_path)
        self.assertEqual(len(failures), 2)
        self.assertEqual(failures[0]["photo_reference"], "second-fail.jpg")
        self.assertEqual(failures[1]["photo_reference"], "first-fail.jpg")
        self.assertNotIn("filename", failures[0])

    def test_list_failed_requests_respects_limit(self):
        for i in range(5):
            self._write_log_line({
                "response": {"success": False, "photo_reference": f"fail{i}.jpg", "message": "x"},
            })
        failures = metrics_mod.list_failed_requests(self.log_path, limit=2)
        self.assertEqual(len(failures), 2)


class TestFeedbackAndMetricsEndpoints(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(main.app)
        self._tmpdir = tempfile.TemporaryDirectory()
        self.fake_photos_dir = Path(self._tmpdir.name)
        (self.fake_photos_dir / "photo.jpg").write_bytes(b"fake")
        self._patch_photos_dir = patch.object(main, "PHOTOS_DIR", self.fake_photos_dir)
        self._patch_photos_dir.start()

    def tearDown(self):
        self._patch_photos_dir.stop()
        self._tmpdir.cleanup()

    def test_feedback_rejects_unknown_photo_reference(self):
        resp = self.client.post(
            "/feedback",
            data={"photo_reference": "does-not-exist.jpg", "corrected_prix": "10000"},
        )
        self.assertEqual(resp.status_code, 404)

    def test_feedback_rejects_no_corrections(self):
        resp = self.client.post("/feedback", data={"photo_reference": "photo.jpg"})
        self.assertEqual(resp.status_code, 400)

    def test_feedback_success_records_entry(self):
        fake_entry = {"feedback_id": "fake-id"}
        with patch.object(main, "record_feedback", return_value=fake_entry) as mock_record:
            resp = self.client.post(
                "/feedback",
                data={"photo_reference": "photo.jpg", "corrected_prix": "10000", "corrected_by": "pompiste-7"},
            )
        self.assertEqual(resp.status_code, 200)
        body = resp.json()
        self.assertTrue(body["success"])
        self.assertEqual(body["feedback_id"], "fake-id")
        mock_record.assert_called_once()
        args, kwargs = mock_record.call_args
        self.assertEqual(args[0], "photo.jpg")
        self.assertEqual(args[1], {"prix": 10000.0})

    def test_metrics_endpoint_returns_aggregate(self):
        fake_metrics = {"total_requests": 0, "success_rate": None,
                        "avg_confidence_score": None, "blocking_causes": {},
                        "image_quality_distribution": {}}
        with patch.object(main, "compute_metrics", return_value=fake_metrics):
            resp = self.client.get("/metrics")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(), fake_metrics)

    def test_failures_endpoint_returns_list(self):
        fake_failures = [{"photo_reference": "photo.jpg", "message": "Photo floue."}]
        with patch.object(main, "list_failed_requests", return_value=fake_failures):
            resp = self.client.get("/failures")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json(), fake_failures)

    def test_get_photo_returns_file(self):
        resp = self.client.get("/photos/photo.jpg")
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b"fake")

    def test_get_photo_unknown_reference_returns_404(self):
        resp = self.client.get("/photos/does-not-exist.jpg")
        self.assertEqual(resp.status_code, 404)

    def test_get_photo_rejects_path_traversal(self):
        outside_file = Path(self._tmpdir.name).parent / "secret.txt"
        outside_file.write_text("should not be servable")
        try:
            resp = self.client.get("/photos/..%2Fsecret.txt")
            self.assertEqual(resp.status_code, 404)
        finally:
            outside_file.unlink(missing_ok=True)


if __name__ == "__main__":
    unittest.main()