@@ -79,3 +79,59 @@ def test_stop_sets_should_exit():
7979 ui ._server = dummy_server
8080 ui .stop ()
8181 assert dummy_server .should_exit is True
82+
83+
84+ def test_expose_camera_starts_camera_if_not_started ():
85+ from unittest .mock import Mock , patch
86+ import numpy as np
87+
88+ ui = WebUI ()
89+ mock_camera = Mock ()
90+ mock_camera .is_started = False
91+ mock_camera .capture = Mock (side_effect = [np .zeros ((2 , 2 , 3 ), dtype = np .uint8 ), RuntimeError ("end" )])
92+
93+ with patch ("arduino.app_utils.image.compress_to_jpeg" , return_value = np .array ([0 ], dtype = np .uint8 )):
94+ ui .expose_camera ("/stream" , mock_camera )
95+ TestClient (ui .app , raise_server_exceptions = False ).get ("/stream" )
96+
97+ mock_camera .start .assert_called_once ()
98+
99+
100+ def test_expose_camera_streams_mjpeg_response ():
101+ from unittest .mock import Mock , patch
102+ import numpy as np
103+
104+ ui = WebUI ()
105+ mock_camera = Mock ()
106+ mock_camera .is_started = True
107+
108+ fake_frame = np .zeros ((2 , 2 , 3 ), dtype = np .uint8 )
109+ mock_camera .capture = Mock (side_effect = [fake_frame , RuntimeError ("end" )])
110+
111+ fake_jpeg = b"\xff \xd8 jpeg"
112+
113+ with patch ("arduino.app_utils.image.compress_to_jpeg" , return_value = np .frombuffer (fake_jpeg , dtype = np .uint8 )):
114+ ui .expose_camera ("/stream" , mock_camera )
115+ response = TestClient (ui .app ).get ("/stream" )
116+
117+ assert response .status_code == 200
118+ assert "multipart/x-mixed-replace" in response .headers ["content-type" ]
119+ assert fake_jpeg in response .content
120+
121+
122+ def test_expose_camera_passes_quality_to_compress ():
123+ from unittest .mock import Mock , patch
124+ import numpy as np
125+
126+ ui = WebUI ()
127+ mock_camera = Mock ()
128+ mock_camera .is_started = True
129+
130+ fake_frame = np .zeros ((2 , 2 , 3 ), dtype = np .uint8 )
131+ mock_camera .capture = Mock (side_effect = [fake_frame , RuntimeError ("end" )])
132+
133+ with patch ("arduino.app_utils.image.compress_to_jpeg" , return_value = np .array ([0 ], dtype = np .uint8 )) as mock_compress :
134+ ui .expose_camera ("/stream" , mock_camera , quality = 95 )
135+ TestClient (ui .app ).get ("/stream" )
136+
137+ mock_compress .assert_called_with (fake_frame , quality = 95 )
0 commit comments