test_camera_api.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. """Integration tests for Camera API endpoints.
  2. Tests the full request/response cycle for /api/v1/printers/{id}/camera/ endpoints.
  3. """
  4. from unittest.mock import AsyncMock, MagicMock, patch
  5. import pytest
  6. from httpx import AsyncClient
  7. class TestCameraAPI:
  8. """Integration tests for /api/v1/printers/{id}/camera/ endpoints."""
  9. # ========================================================================
  10. # Camera Stop Endpoint
  11. # ========================================================================
  12. @pytest.mark.asyncio
  13. @pytest.mark.integration
  14. async def test_stop_camera_stream_get(self, async_client: AsyncClient, printer_factory):
  15. """Verify camera stop endpoint works with GET method."""
  16. printer = await printer_factory()
  17. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/stop")
  18. assert response.status_code == 200
  19. result = response.json()
  20. assert "stopped" in result
  21. assert isinstance(result["stopped"], int)
  22. @pytest.mark.asyncio
  23. @pytest.mark.integration
  24. async def test_stop_camera_stream_post(self, async_client: AsyncClient, printer_factory):
  25. """Verify camera stop endpoint works with POST method (sendBeacon compatibility)."""
  26. printer = await printer_factory()
  27. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/stop")
  28. assert response.status_code == 200
  29. result = response.json()
  30. assert "stopped" in result
  31. assert isinstance(result["stopped"], int)
  32. @pytest.mark.asyncio
  33. @pytest.mark.integration
  34. async def test_stop_camera_stream_no_active_streams(self, async_client: AsyncClient, printer_factory):
  35. """Verify stop returns 0 when no active streams exist."""
  36. printer = await printer_factory()
  37. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/stop")
  38. assert response.status_code == 200
  39. assert response.json()["stopped"] == 0
  40. @pytest.mark.asyncio
  41. @pytest.mark.integration
  42. async def test_stop_camera_stream_with_active_stream(self, async_client: AsyncClient, printer_factory):
  43. """Verify stop terminates active streams for the printer."""
  44. printer = await printer_factory()
  45. # Mock an active stream — wait() must be AsyncMock since it's awaited
  46. mock_process = MagicMock()
  47. mock_process.returncode = None
  48. mock_process.pid = 99999
  49. mock_process.terminate = MagicMock()
  50. mock_process.wait = AsyncMock()
  51. with patch("backend.app.api.routes.camera._active_streams", {f"{printer.id}-abc123": mock_process}):
  52. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/stop")
  53. assert response.status_code == 200
  54. assert response.json()["stopped"] == 1
  55. mock_process.terminate.assert_called_once()
  56. @pytest.mark.asyncio
  57. @pytest.mark.integration
  58. async def test_stop_camera_stream_only_stops_matching_printer(self, async_client: AsyncClient, printer_factory):
  59. """Verify stop only terminates streams for the specified printer."""
  60. printer1 = await printer_factory(name="Printer 1")
  61. printer2 = await printer_factory(name="Printer 2")
  62. # Mock active streams for both printers — wait() must be AsyncMock since it's awaited
  63. mock_process1 = MagicMock()
  64. mock_process1.returncode = None
  65. mock_process1.pid = 99998
  66. mock_process1.terminate = MagicMock()
  67. mock_process1.wait = AsyncMock()
  68. mock_process2 = MagicMock()
  69. mock_process2.returncode = None
  70. mock_process2.pid = 99997
  71. mock_process2.terminate = MagicMock()
  72. mock_process2.wait = AsyncMock()
  73. active_streams = {
  74. f"{printer1.id}-abc123": mock_process1,
  75. f"{printer2.id}-def456": mock_process2,
  76. }
  77. with patch("backend.app.api.routes.camera._active_streams", active_streams):
  78. response = await async_client.post(f"/api/v1/printers/{printer1.id}/camera/stop")
  79. assert response.status_code == 200
  80. assert response.json()["stopped"] == 1
  81. mock_process1.terminate.assert_called_once()
  82. mock_process2.terminate.assert_not_called()
  83. @pytest.mark.asyncio
  84. @pytest.mark.integration
  85. async def test_stop_camera_stream_handles_fanout_stream_id(self, async_client: AsyncClient, printer_factory):
  86. """Stop must terminate streams keyed with the deterministic
  87. ``{printer_id}-fanout`` id used by the fan-out broadcaster (#1089).
  88. Regression guard against the prefix-match drifting away from the
  89. broadcaster's stream-id convention.
  90. """
  91. printer = await printer_factory()
  92. mock_process = MagicMock()
  93. mock_process.returncode = None
  94. mock_process.pid = 99996
  95. mock_process.terminate = MagicMock()
  96. mock_process.wait = AsyncMock()
  97. with patch(
  98. "backend.app.api.routes.camera._active_streams",
  99. {f"{printer.id}-fanout": mock_process},
  100. ):
  101. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/stop")
  102. assert response.status_code == 200
  103. assert response.json()["stopped"] == 1
  104. mock_process.terminate.assert_called_once()
  105. @pytest.mark.asyncio
  106. @pytest.mark.integration
  107. async def test_stop_camera_stream_invokes_broadcaster_shutdown(self, async_client: AsyncClient, printer_factory):
  108. """Stop must call ``shutdown_broadcaster`` so subscribers wake up via
  109. the upstream-gone sentinel rather than stalling on the queue (#1089)."""
  110. printer = await printer_factory()
  111. with patch(
  112. "backend.app.api.routes.camera.shutdown_broadcaster",
  113. AsyncMock(return_value=False),
  114. ) as mock_shutdown:
  115. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/stop")
  116. assert response.status_code == 200
  117. mock_shutdown.assert_awaited_once_with(f"printer-{printer.id}")
  118. # ========================================================================
  119. # Camera Test Endpoint
  120. # ========================================================================
  121. @pytest.mark.asyncio
  122. @pytest.mark.integration
  123. async def test_camera_test_printer_not_found(self, async_client: AsyncClient):
  124. """Verify 404 when testing camera for non-existent printer."""
  125. response = await async_client.get("/api/v1/printers/99999/camera/test")
  126. assert response.status_code == 404
  127. assert "not found" in response.json()["detail"].lower()
  128. @pytest.mark.asyncio
  129. @pytest.mark.integration
  130. async def test_camera_test_success(self, async_client: AsyncClient, printer_factory):
  131. """Verify camera test returns success when camera is accessible."""
  132. printer = await printer_factory()
  133. with patch("backend.app.api.routes.camera.test_camera_connection", new_callable=AsyncMock) as mock_test:
  134. mock_test.return_value = {"success": True, "message": "Camera connected"}
  135. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/test")
  136. assert response.status_code == 200
  137. result = response.json()
  138. assert result["success"] is True
  139. @pytest.mark.asyncio
  140. @pytest.mark.integration
  141. async def test_camera_test_failure(self, async_client: AsyncClient, printer_factory):
  142. """Verify camera test returns failure when camera is not accessible."""
  143. printer = await printer_factory()
  144. with patch("backend.app.api.routes.camera.test_camera_connection", new_callable=AsyncMock) as mock_test:
  145. mock_test.return_value = {"success": False, "message": "Connection timeout"}
  146. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/test")
  147. assert response.status_code == 200
  148. result = response.json()
  149. assert result["success"] is False
  150. # ========================================================================
  151. # Camera Diagnose Endpoint (#1395 follow-up)
  152. # ========================================================================
  153. @pytest.mark.asyncio
  154. @pytest.mark.integration
  155. async def test_camera_diagnose_printer_not_found(self, async_client: AsyncClient):
  156. response = await async_client.post("/api/v1/printers/99999/camera/diagnose")
  157. assert response.status_code == 404
  158. @pytest.mark.asyncio
  159. @pytest.mark.integration
  160. async def test_camera_diagnose_returns_structured_result(self, async_client: AsyncClient, printer_factory):
  161. """Endpoint returns the per-stage shape the frontend modal renders."""
  162. from backend.app.services.camera_diagnose import (
  163. CameraDiagnoseResult,
  164. CameraDiagnoseStage,
  165. )
  166. printer = await printer_factory()
  167. fake = CameraDiagnoseResult(
  168. printer_id=printer.id,
  169. protocol="rtsp",
  170. port=322,
  171. profile="P2S",
  172. overall_status="failed",
  173. stages=[
  174. CameraDiagnoseStage(name="tcp_reachable", status="ok", duration_ms=12),
  175. CameraDiagnoseStage(name="first_frame", status="failed", duration_ms=15123, code="no_frame"),
  176. ],
  177. summary_code="no_frame",
  178. )
  179. with patch(
  180. "backend.app.services.camera_diagnose.diagnose_camera",
  181. new_callable=AsyncMock,
  182. return_value=fake,
  183. ):
  184. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/diagnose")
  185. assert response.status_code == 200
  186. body = response.json()
  187. assert body["printer_id"] == printer.id
  188. assert body["protocol"] == "rtsp"
  189. assert body["profile"] == "P2S"
  190. assert body["overall_status"] == "failed"
  191. assert body["summary_code"] == "no_frame"
  192. assert [s["name"] for s in body["stages"]] == ["tcp_reachable", "first_frame"]
  193. assert body["stages"][1]["code"] == "no_frame"
  194. # ========================================================================
  195. # Camera Snapshot Endpoint
  196. # ========================================================================
  197. @pytest.mark.asyncio
  198. @pytest.mark.integration
  199. async def test_camera_snapshot_printer_not_found(self, async_client: AsyncClient):
  200. """Verify 404 when capturing snapshot for non-existent printer."""
  201. response = await async_client.get("/api/v1/printers/99999/camera/snapshot")
  202. assert response.status_code == 404
  203. @pytest.mark.asyncio
  204. @pytest.mark.integration
  205. async def test_camera_snapshot_success(self, async_client: AsyncClient, printer_factory):
  206. """Verify snapshot returns JPEG image when successful."""
  207. printer = await printer_factory()
  208. # Create a fake JPEG (starts with FFD8)
  209. fake_jpeg = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
  210. with patch("backend.app.api.routes.camera.capture_camera_frame", new_callable=AsyncMock) as mock_capture:
  211. mock_capture.return_value = True
  212. # Mock the file read
  213. with patch("builtins.open", create=True) as mock_open:
  214. mock_open.return_value.__enter__.return_value.read.return_value = fake_jpeg
  215. with patch("pathlib.Path.exists", return_value=True), patch("pathlib.Path.unlink"):
  216. _response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
  217. # Note: The actual test might fail due to file operations, but this tests the endpoint structure
  218. # In production tests, we'd mock more comprehensively
  219. @pytest.mark.asyncio
  220. @pytest.mark.integration
  221. async def test_camera_snapshot_failure(self, async_client: AsyncClient, printer_factory):
  222. """Verify 503 when camera capture fails."""
  223. printer = await printer_factory()
  224. with patch("backend.app.api.routes.camera.capture_camera_frame", new_callable=AsyncMock) as mock_capture:
  225. mock_capture.return_value = False
  226. with patch("pathlib.Path.exists", return_value=False), patch("pathlib.Path.unlink"):
  227. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
  228. assert response.status_code == 503
  229. assert "Failed to capture" in response.json()["detail"]
  230. @pytest.mark.asyncio
  231. @pytest.mark.integration
  232. async def test_camera_snapshot_reuses_buffered_frame_when_stream_active(
  233. self, async_client: AsyncClient, printer_factory
  234. ):
  235. """#1271: /camera/snapshot must reuse the broadcaster's buffered frame
  236. when a live stream is running, instead of opening a second concurrent
  237. RTSP socket. On printers with strict single-connection enforcement (e.g.
  238. X2D firmware 01.01.00.00) opening a second socket kicks the live stream.
  239. """
  240. printer = await printer_factory()
  241. fake_jpeg = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
  242. # Simulate a running broadcaster: one active stream entry + buffered frame.
  243. active_streams = {f"{printer.id}-fanout": MagicMock()}
  244. last_frames = {printer.id: fake_jpeg}
  245. with (
  246. patch("backend.app.api.routes.camera._active_streams", active_streams),
  247. patch("backend.app.api.routes.camera._last_frames", last_frames),
  248. patch("backend.app.api.routes.camera.capture_camera_frame", new_callable=AsyncMock) as mock_capture,
  249. ):
  250. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
  251. assert response.status_code == 200
  252. assert response.content == fake_jpeg
  253. # The fresh-capture path must NOT have been taken — that's the whole point.
  254. mock_capture.assert_not_called()
  255. @pytest.mark.asyncio
  256. @pytest.mark.integration
  257. async def test_camera_snapshot_external_camera_success(self, async_client: AsyncClient, printer_factory):
  258. """Verify snapshot uses external camera when configured."""
  259. printer = await printer_factory(
  260. external_camera_enabled=True,
  261. external_camera_url="http://192.168.1.50/mjpeg",
  262. external_camera_type="mjpeg",
  263. )
  264. fake_jpeg = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00"
  265. with patch(
  266. "backend.app.services.external_camera.capture_frame",
  267. new_callable=AsyncMock,
  268. return_value=fake_jpeg,
  269. ):
  270. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
  271. assert response.status_code == 200
  272. assert response.headers["content-type"] == "image/jpeg"
  273. assert response.content == fake_jpeg
  274. @pytest.mark.asyncio
  275. @pytest.mark.integration
  276. async def test_camera_snapshot_external_camera_failure(self, async_client: AsyncClient, printer_factory):
  277. """Verify 503 when external camera capture fails."""
  278. printer = await printer_factory(
  279. external_camera_enabled=True,
  280. external_camera_url="http://192.168.1.50/mjpeg",
  281. external_camera_type="mjpeg",
  282. )
  283. with patch(
  284. "backend.app.services.external_camera.capture_frame",
  285. new_callable=AsyncMock,
  286. return_value=None,
  287. ):
  288. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/snapshot")
  289. assert response.status_code == 503
  290. assert "external camera" in response.json()["detail"].lower()
  291. # ========================================================================
  292. # Camera Stream Endpoint
  293. # ========================================================================
  294. @pytest.mark.asyncio
  295. @pytest.mark.integration
  296. async def test_camera_stream_printer_not_found(self, async_client: AsyncClient):
  297. """Verify 404 when streaming camera for non-existent printer."""
  298. response = await async_client.get("/api/v1/printers/99999/camera/stream")
  299. assert response.status_code == 404
  300. @pytest.mark.asyncio
  301. @pytest.mark.integration
  302. async def test_camera_stream_fps_validation(self, async_client: AsyncClient, printer_factory):
  303. """Verify FPS parameter is validated and clamped."""
  304. printer = await printer_factory()
  305. # FPS should be clamped between 1 and 30
  306. # Testing that the endpoint accepts various FPS values without error
  307. # (actual streaming would require mocking ffmpeg)
  308. with patch("backend.app.api.routes.camera.get_ffmpeg_path", return_value=None):
  309. # With no ffmpeg, stream should return error message but not crash
  310. response = await async_client.get(
  311. f"/api/v1/printers/{printer.id}/camera/stream",
  312. params={"fps": 100}, # Should be clamped to 30
  313. )
  314. # Response will be a streaming response with error
  315. assert response.status_code == 200
  316. # ========================================================================
  317. # Plate Detection Endpoints
  318. # ========================================================================
  319. @pytest.mark.asyncio
  320. @pytest.mark.integration
  321. async def test_plate_detection_status_printer_not_found(self, async_client: AsyncClient):
  322. """Verify 404 when checking plate detection status for non-existent printer."""
  323. response = await async_client.get("/api/v1/printers/99999/camera/plate-detection/status")
  324. assert response.status_code == 404
  325. @pytest.mark.asyncio
  326. @pytest.mark.integration
  327. async def test_plate_detection_status_opencv_not_available(self, async_client: AsyncClient, printer_factory):
  328. """Verify plate detection status returns unavailable when OpenCV not installed."""
  329. printer = await printer_factory()
  330. with patch("backend.app.services.plate_detection.OPENCV_AVAILABLE", False):
  331. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/plate-detection/status")
  332. assert response.status_code == 200
  333. result = response.json()
  334. assert result["available"] is False
  335. assert result["calibrated"] is False
  336. @pytest.mark.asyncio
  337. @pytest.mark.integration
  338. async def test_plate_detection_status_success(self, async_client: AsyncClient, printer_factory):
  339. """Verify plate detection status returns correctly when OpenCV available."""
  340. printer = await printer_factory()
  341. # OpenCV is available in test environment, just check the response structure
  342. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/plate-detection/status")
  343. assert response.status_code == 200
  344. result = response.json()
  345. assert "available" in result
  346. assert "calibrated" in result
  347. @pytest.mark.asyncio
  348. @pytest.mark.integration
  349. async def test_check_plate_empty_printer_not_found(self, async_client: AsyncClient):
  350. """Verify 404 when checking plate for non-existent printer."""
  351. response = await async_client.get("/api/v1/printers/99999/camera/check-plate")
  352. assert response.status_code == 404
  353. @pytest.mark.asyncio
  354. @pytest.mark.integration
  355. async def test_check_plate_empty_success_structure(self, async_client: AsyncClient, printer_factory):
  356. """Verify check plate returns proper structure when OpenCV available."""
  357. printer = await printer_factory()
  358. # Mock PlateDetectionResult to avoid camera timeout
  359. mock_result = MagicMock()
  360. mock_result.is_empty = True
  361. mock_result.confidence = 0.95
  362. mock_result.difference_percent = 0.5
  363. mock_result.message = "Plate appears empty"
  364. mock_result.needs_calibration = False
  365. mock_result.debug_image = None
  366. mock_result.to_dict.return_value = {
  367. "is_empty": True,
  368. "confidence": 0.95,
  369. "difference_percent": 0.5,
  370. "message": "Plate appears empty",
  371. "has_debug_image": False,
  372. "needs_calibration": False,
  373. }
  374. # Mock PlateDetector for reference count
  375. mock_detector = MagicMock()
  376. mock_detector.get_calibration_count.return_value = 0
  377. mock_detector.MAX_REFERENCES = 5
  378. with (
  379. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  380. patch("backend.app.services.plate_detection.check_plate_empty", new_callable=AsyncMock) as mock_check,
  381. patch("backend.app.services.plate_detection.PlateDetector", return_value=mock_detector),
  382. ):
  383. mock_check.return_value = mock_result
  384. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/check-plate")
  385. assert response.status_code == 200
  386. result = response.json()
  387. assert "is_empty" in result
  388. assert "confidence" in result
  389. assert "message" in result
  390. @pytest.mark.asyncio
  391. @pytest.mark.integration
  392. async def test_calibrate_plate_printer_not_found(self, async_client: AsyncClient):
  393. """Verify 404 when calibrating plate for non-existent printer."""
  394. response = await async_client.post("/api/v1/printers/99999/camera/plate-detection/calibrate")
  395. assert response.status_code == 404
  396. @pytest.mark.asyncio
  397. @pytest.mark.integration
  398. async def test_calibrate_plate_success_structure(self, async_client: AsyncClient, printer_factory):
  399. """Verify calibrate endpoint responds with proper structure."""
  400. printer = await printer_factory()
  401. # Mock calibrate_plate at the source module to avoid camera timeout
  402. with (
  403. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  404. patch("backend.app.services.plate_detection.calibrate_plate", new_callable=AsyncMock) as mock_calibrate,
  405. ):
  406. mock_calibrate.return_value = (True, "Calibration saved (1/5 references)", 0)
  407. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/plate-detection/calibrate")
  408. assert response.status_code == 200
  409. result = response.json()
  410. assert result["success"] is True
  411. assert "index" in result
  412. # ------------------------------------------------------------------
  413. # Regression: #1359 — the manual UI check/calibrate routes must derive
  414. # use_external from the printer's external_camera_enabled setting when
  415. # the caller omits the flag. Otherwise the UI calibrates against the
  416. # built-in camera while the runtime auto-check at print start uses the
  417. # external one, producing a permanent "build plate not empty".
  418. # ------------------------------------------------------------------
  419. @pytest.mark.asyncio
  420. @pytest.mark.integration
  421. async def test_check_plate_defaults_use_external_when_external_camera_enabled(
  422. self, async_client: AsyncClient, printer_factory
  423. ):
  424. """Omitting use_external on a printer with external camera enabled
  425. must call the service with use_external=True."""
  426. printer = await printer_factory(
  427. external_camera_enabled=True,
  428. external_camera_url="http://192.168.1.50/mjpeg",
  429. external_camera_type="mjpeg",
  430. )
  431. mock_result = MagicMock()
  432. mock_result.to_dict.return_value = {
  433. "is_empty": True,
  434. "confidence": 0.95,
  435. "difference_percent": 0.5,
  436. "message": "Plate appears empty",
  437. "has_debug_image": False,
  438. "needs_calibration": False,
  439. }
  440. mock_result.debug_image = None
  441. mock_detector = MagicMock()
  442. mock_detector.get_calibration_count.return_value = 0
  443. mock_detector.MAX_REFERENCES = 5
  444. with (
  445. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  446. patch("backend.app.services.plate_detection.check_plate_empty", new_callable=AsyncMock) as mock_check,
  447. patch("backend.app.services.plate_detection.PlateDetector", return_value=mock_detector),
  448. ):
  449. mock_check.return_value = mock_result
  450. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/check-plate")
  451. assert response.status_code == 200
  452. assert mock_check.await_args.kwargs["use_external"] is True
  453. @pytest.mark.asyncio
  454. @pytest.mark.integration
  455. async def test_check_plate_defaults_use_external_false_when_external_camera_disabled(
  456. self, async_client: AsyncClient, printer_factory
  457. ):
  458. """Omitting use_external on a printer without an external camera
  459. must call the service with use_external=False (built-in)."""
  460. printer = await printer_factory() # external_camera_enabled defaults to False
  461. mock_result = MagicMock()
  462. mock_result.to_dict.return_value = {
  463. "is_empty": True,
  464. "confidence": 0.95,
  465. "difference_percent": 0.5,
  466. "message": "Plate appears empty",
  467. "has_debug_image": False,
  468. "needs_calibration": False,
  469. }
  470. mock_result.debug_image = None
  471. mock_detector = MagicMock()
  472. mock_detector.get_calibration_count.return_value = 0
  473. mock_detector.MAX_REFERENCES = 5
  474. with (
  475. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  476. patch("backend.app.services.plate_detection.check_plate_empty", new_callable=AsyncMock) as mock_check,
  477. patch("backend.app.services.plate_detection.PlateDetector", return_value=mock_detector),
  478. ):
  479. mock_check.return_value = mock_result
  480. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/check-plate")
  481. assert response.status_code == 200
  482. assert mock_check.await_args.kwargs["use_external"] is False
  483. @pytest.mark.asyncio
  484. @pytest.mark.integration
  485. async def test_calibrate_plate_defaults_use_external_when_external_camera_enabled(
  486. self, async_client: AsyncClient, printer_factory
  487. ):
  488. """Calibrating with use_external omitted on an external-camera-enabled
  489. printer captures the reference from the external camera — matching
  490. what the runtime check at print start will compare against (#1359)."""
  491. printer = await printer_factory(
  492. external_camera_enabled=True,
  493. external_camera_url="http://192.168.1.50/mjpeg",
  494. external_camera_type="mjpeg",
  495. )
  496. with (
  497. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  498. patch("backend.app.services.plate_detection.calibrate_plate", new_callable=AsyncMock) as mock_calibrate,
  499. ):
  500. mock_calibrate.return_value = (True, "Calibration saved (1/5 references)", 0)
  501. response = await async_client.post(f"/api/v1/printers/{printer.id}/camera/plate-detection/calibrate")
  502. assert response.status_code == 200
  503. assert mock_calibrate.await_args.kwargs["use_external"] is True
  504. @pytest.mark.asyncio
  505. @pytest.mark.integration
  506. async def test_calibrate_plate_explicit_use_external_false_overrides_default(
  507. self, async_client: AsyncClient, printer_factory
  508. ):
  509. """An explicit use_external=false from the caller still wins even
  510. when the printer has an external camera configured, so power users
  511. can force a built-in-camera reference if they ever need to."""
  512. printer = await printer_factory(
  513. external_camera_enabled=True,
  514. external_camera_url="http://192.168.1.50/mjpeg",
  515. external_camera_type="mjpeg",
  516. )
  517. with (
  518. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  519. patch("backend.app.services.plate_detection.calibrate_plate", new_callable=AsyncMock) as mock_calibrate,
  520. ):
  521. mock_calibrate.return_value = (True, "Calibration saved (1/5 references)", 0)
  522. response = await async_client.post(
  523. f"/api/v1/printers/{printer.id}/camera/plate-detection/calibrate?use_external=false"
  524. )
  525. assert response.status_code == 200
  526. assert mock_calibrate.await_args.kwargs["use_external"] is False
  527. @pytest.mark.asyncio
  528. @pytest.mark.integration
  529. async def test_delete_calibration_printer_not_found(self, async_client: AsyncClient):
  530. """Verify 404 when deleting calibration for non-existent printer."""
  531. response = await async_client.delete("/api/v1/printers/99999/camera/plate-detection/calibrate")
  532. assert response.status_code == 404
  533. @pytest.mark.asyncio
  534. @pytest.mark.integration
  535. async def test_delete_calibration_success(self, async_client: AsyncClient, printer_factory):
  536. """Verify delete calibration returns proper structure."""
  537. printer = await printer_factory()
  538. with patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True):
  539. response = await async_client.delete(f"/api/v1/printers/{printer.id}/camera/plate-detection/calibrate")
  540. assert response.status_code == 200
  541. result = response.json()
  542. assert "success" in result
  543. assert "message" in result
  544. @pytest.mark.asyncio
  545. @pytest.mark.integration
  546. async def test_get_references_printer_not_found(self, async_client: AsyncClient):
  547. """Verify 404 when getting references for non-existent printer."""
  548. response = await async_client.get("/api/v1/printers/99999/camera/plate-detection/references")
  549. assert response.status_code == 404
  550. @pytest.mark.asyncio
  551. @pytest.mark.integration
  552. async def test_get_references_opencv_not_available(self, async_client: AsyncClient, printer_factory):
  553. """Verify get references returns unavailable when OpenCV not installed."""
  554. printer = await printer_factory()
  555. with patch("backend.app.services.plate_detection.OPENCV_AVAILABLE", False):
  556. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/plate-detection/references")
  557. assert response.status_code == 503
  558. @pytest.mark.asyncio
  559. @pytest.mark.integration
  560. async def test_get_references_success(self, async_client: AsyncClient, printer_factory):
  561. """Verify get references returns proper structure."""
  562. printer = await printer_factory()
  563. # Mock OpenCV availability and PlateDetector
  564. mock_detector = MagicMock()
  565. mock_detector.get_references.return_value = []
  566. mock_detector.MAX_REFERENCES = 5
  567. with (
  568. patch("backend.app.services.plate_detection.is_plate_detection_available", return_value=True),
  569. patch("backend.app.services.plate_detection.PlateDetector", return_value=mock_detector),
  570. ):
  571. response = await async_client.get(f"/api/v1/printers/{printer.id}/camera/plate-detection/references")
  572. assert response.status_code == 200
  573. result = response.json()
  574. assert "references" in result
  575. assert "max_references" in result
  576. assert isinstance(result["references"], list)
  577. @pytest.mark.asyncio
  578. @pytest.mark.integration
  579. async def test_update_reference_label_printer_not_found(self, async_client: AsyncClient):
  580. """Verify 404 when updating reference label for non-existent printer."""
  581. response = await async_client.put(
  582. "/api/v1/printers/99999/camera/plate-detection/references/0", params={"label": "New Label"}
  583. )
  584. assert response.status_code == 404
  585. @pytest.mark.asyncio
  586. @pytest.mark.integration
  587. async def test_delete_reference_printer_not_found(self, async_client: AsyncClient):
  588. """Verify 404 when deleting reference for non-existent printer."""
  589. response = await async_client.delete("/api/v1/printers/99999/camera/plate-detection/references/0")
  590. assert response.status_code == 404
  591. @pytest.mark.asyncio
  592. @pytest.mark.integration
  593. async def test_get_reference_thumbnail_printer_not_found(self, async_client: AsyncClient):
  594. """Verify 404 when getting reference thumbnail for non-existent printer."""
  595. response = await async_client.get("/api/v1/printers/99999/camera/plate-detection/references/0/thumbnail")
  596. assert response.status_code == 404
  597. # ========================================================================
  598. # USB Camera Endpoint
  599. # ========================================================================
  600. @pytest.mark.asyncio
  601. @pytest.mark.integration
  602. async def test_list_usb_cameras_returns_list(self, async_client: AsyncClient):
  603. """Verify USB cameras endpoint returns a list of cameras."""
  604. response = await async_client.get("/api/v1/printers/usb-cameras")
  605. assert response.status_code == 200
  606. result = response.json()
  607. assert "cameras" in result
  608. assert isinstance(result["cameras"], list)
  609. @pytest.mark.asyncio
  610. @pytest.mark.integration
  611. async def test_list_usb_cameras_structure(self, async_client: AsyncClient):
  612. """Verify USB cameras endpoint returns proper structure for each camera."""
  613. with patch("backend.app.services.external_camera.list_usb_cameras") as mock_list:
  614. mock_list.return_value = [
  615. {"device": "/dev/video0", "name": "Logitech Webcam C920", "index": 0},
  616. {"device": "/dev/video2", "name": "USB Camera", "index": 2},
  617. ]
  618. response = await async_client.get("/api/v1/printers/usb-cameras")
  619. assert response.status_code == 200
  620. result = response.json()
  621. assert len(result["cameras"]) == 2
  622. assert result["cameras"][0]["device"] == "/dev/video0"
  623. assert result["cameras"][0]["name"] == "Logitech Webcam C920"
  624. assert result["cameras"][1]["device"] == "/dev/video2"
  625. @pytest.mark.asyncio
  626. @pytest.mark.integration
  627. async def test_list_usb_cameras_empty_on_non_linux(self, async_client: AsyncClient):
  628. """Verify USB cameras endpoint returns empty list on non-Linux systems."""
  629. with patch("backend.app.services.external_camera.list_usb_cameras") as mock_list:
  630. # Simulate non-Linux system (no /dev/video* devices)
  631. mock_list.return_value = []
  632. response = await async_client.get("/api/v1/printers/usb-cameras")
  633. assert response.status_code == 200
  634. result = response.json()
  635. assert result["cameras"] == []