11import pytest
22from unittest .mock import patch , MagicMock
3+ import numpy as np
4+ import tempfile
5+ import base64
6+ from io import BytesIO
7+ from PIL import Image as PILImage
38from mbodied .types .sense .vision import Image
49from mbodied .agents .sense .depth_estimation_agent import DepthEstimationAgent
5- from mbodied .agents .sense .object_detection_agent import ObjectDetectionAgent
610from mbodied .agents .backends .gradio_backend import GradioBackend
711
812
13+ def get_dummy_base64_image ():
14+ """Create a minimal valid base64 image for testing."""
15+ img = PILImage .new ("RGB" , (10 , 10 ), color = "red" )
16+ buffered = BytesIO ()
17+ img .save (buffered , format = "JPEG" )
18+ return base64 .b64encode (buffered .getvalue ()).decode ()
19+
20+
921@pytest .fixture
1022def mock_gradio_backend ():
23+ # Create a temp file that would be a valid location for a numpy file
24+ temp_file = tempfile .NamedTemporaryFile (suffix = ".npy" , delete = False )
25+ temp_path = temp_file .name
26+ temp_file .close ()
27+ dummy_image_b64 = get_dummy_base64_image ()
28+
1129 with patch .object (GradioBackend , "__init__" , lambda x , model_src = None , ** kwargs : None ):
12- with patch .object (GradioBackend , "predict" , return_value = Image (size = (224 , 224 ))):
13- yield GradioBackend (endpoint = "http://1.2.3.4:1234" )
30+ # Return a valid base64 image string and a path
31+ with patch .object (GradioBackend , "predict" , return_value = (dummy_image_b64 , temp_path )):
32+ # Intercept np.load calls to avoid actual file system access
33+ with patch ("numpy.load" , return_value = np .zeros ((224 , 224 ))):
34+ yield GradioBackend (endpoint = "http://1.2.3.4:1234" )
1435
1536
1637@pytest .fixture
@@ -29,14 +50,16 @@ def test_depth_agent_act(depth_agent):
2950 mock_image = MagicMock (spec = Image )
3051 mock_image .base64 = "base64encodedimage"
3152
32- result = depth_agent .act (mock_image )
53+ result_img , depth_array = depth_agent .act (mock_image )
3354
34- assert isinstance (result , Image )
55+ assert isinstance (result_img , Image )
56+ assert isinstance (depth_array , np .ndarray )
3557
3658
3759@pytest .mark .network
3860def test_real_depth_agent_act ():
3961 # Make real network call.
4062 agent = DepthEstimationAgent (model_src = "https://api.mbodi.ai/sense/" )
41- result = agent .act (image = Image ("resources/xarm.jpeg" , size = (224 , 224 )))
42- assert isinstance (result , Image )
63+ result_img , depth_array = agent .act (image = Image ("resources/xarm.jpeg" , size = (224 , 224 )))
64+ assert isinstance (result_img , Image )
65+ assert isinstance (depth_array , np .ndarray )
0 commit comments