|
| 1 | +"""Tests for AI video descriptions and v4 media endpoint support.""" |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | +from unittest import IsolatedAsyncioTestCase |
| 5 | +from blinkpy import api |
| 6 | +from blinkpy.blinkpy import Blink |
| 7 | +from blinkpy.helpers.util import BlinkURLHandler |
| 8 | +from blinkpy.sync_module import BlinkSyncModule |
| 9 | +from blinkpy.camera import BlinkCamera |
| 10 | + |
| 11 | +# Sample v4 media entry with AI description and CV detection |
| 12 | +V4_ENTRY_WITH_AI = { |
| 13 | + "device_name": "Front Door", |
| 14 | + "media": "/api/v4/accounts/1234/media/999/video/contents", |
| 15 | + "created_at": "1990-01-01T00:00:00+00:00", |
| 16 | + "ai_vd": { |
| 17 | + "full_description": "A person is walking on the driveway.", |
| 18 | + "short_description": "Person on driveway.", |
| 19 | + }, |
| 20 | + "cv_detection": ["person"], |
| 21 | +} |
| 22 | + |
| 23 | +# Sample v4 media entry without AI description (SVD not enabled) |
| 24 | +V4_ENTRY_NO_AI = { |
| 25 | + "device_name": "Front Door", |
| 26 | + "media": "/api/v4/accounts/1234/media/888/video/contents", |
| 27 | + "created_at": "1990-01-01T00:00:00+00:00", |
| 28 | + "ai_vd": None, |
| 29 | + "cv_detection": None, |
| 30 | +} |
| 31 | + |
| 32 | +# Sample v4 entry with empty ai_vd object |
| 33 | +V4_ENTRY_EMPTY_AI = { |
| 34 | + "device_name": "Front Door", |
| 35 | + "media": "/api/v4/accounts/1234/media/777/video/contents", |
| 36 | + "created_at": "1990-01-01T00:00:00+00:00", |
| 37 | + "ai_vd": {}, |
| 38 | + "cv_detection": ["vehicle"], |
| 39 | +} |
| 40 | + |
| 41 | +# Full v4 response |
| 42 | +V4_RESPONSE = { |
| 43 | + "media": [V4_ENTRY_WITH_AI], |
| 44 | + "moment_gap_time": 25, |
| 45 | + "page_size": 200, |
| 46 | + "pagination_key": None, |
| 47 | + "smart_video_descriptions": True, |
| 48 | +} |
| 49 | + |
| 50 | +# Camera config used by TestCameraAIAttributes |
| 51 | +CAMERA_CFG = { |
| 52 | + "name": "foobar", |
| 53 | + "id": 1234, |
| 54 | + "network_id": 5678, |
| 55 | + "serial": "12345678", |
| 56 | + "enabled": False, |
| 57 | + "battery_state": "ok", |
| 58 | + "battery_voltage": 163, |
| 59 | + "wifi_strength": -38, |
| 60 | + "signals": {"lfr": 5, "wifi": 4, "battery": 3, "temp": 68}, |
| 61 | + "thumbnail": "/thumb", |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +@mock.patch("blinkpy.auth.Auth.query") |
| 66 | +class TestV4MediaEndpoint(IsolatedAsyncioTestCase): |
| 67 | + """Test the v4 media endpoint API function.""" |
| 68 | + |
| 69 | + async def asyncSetUp(self): |
| 70 | + """Set up Blink module.""" |
| 71 | + self.blink = Blink(session=mock.AsyncMock()) |
| 72 | + self.blink.urls = BlinkURLHandler("test") |
| 73 | + self.blink.auth.account_id = 1234 |
| 74 | + |
| 75 | + def tearDown(self): |
| 76 | + """Clean up after test.""" |
| 77 | + self.blink = None |
| 78 | + |
| 79 | + async def test_request_videos_v4(self, mock_resp): |
| 80 | + """Test v4 media endpoint returns expected data.""" |
| 81 | + mock_resp.return_value = V4_RESPONSE |
| 82 | + result = await api.request_videos_v4(self.blink) |
| 83 | + self.assertIn("media", result) |
| 84 | + self.assertEqual(len(result["media"]), 1) |
| 85 | + self.assertIn("ai_vd", result["media"][0]) |
| 86 | + |
| 87 | + async def test_request_videos_v4_empty_response(self, mock_resp): |
| 88 | + """Test v4 endpoint with empty response.""" |
| 89 | + mock_resp.return_value = None |
| 90 | + result = await api.request_videos_v4(self.blink) |
| 91 | + self.assertIsNone(result) |
| 92 | + |
| 93 | + async def test_request_videos_v4_no_media(self, mock_resp): |
| 94 | + """Test v4 endpoint with response missing media key.""" |
| 95 | + mock_resp.return_value = {"error": "something went wrong"} |
| 96 | + result = await api.request_videos_v4(self.blink) |
| 97 | + self.assertNotIn("media", result) |
| 98 | + |
| 99 | + |
| 100 | +@mock.patch("blinkpy.auth.Auth.query") |
| 101 | +class TestAIDescriptionExtraction(IsolatedAsyncioTestCase): |
| 102 | + """Test AI description extraction in check_new_videos.""" |
| 103 | + |
| 104 | + def setUp(self): |
| 105 | + """Set up Blink module.""" |
| 106 | + self.blink = Blink(motion_interval=0, session=mock.AsyncMock()) |
| 107 | + self.blink.last_refresh = 1000 |
| 108 | + self.blink.urls = BlinkURLHandler("test") |
| 109 | + self.blink.sync["test"] = BlinkSyncModule(self.blink, "test", "1234", []) |
| 110 | + self.blink.sync["test"].network_info = {"network": {"armed": True}} |
| 111 | + |
| 112 | + def tearDown(self): |
| 113 | + """Clean up after test.""" |
| 114 | + self.blink = None |
| 115 | + |
| 116 | + async def test_v4_ai_description_extracted(self, mock_resp): |
| 117 | + """Test that ai_vd is extracted from v4 media entries.""" |
| 118 | + mock_resp.return_value = {"media": [V4_ENTRY_WITH_AI]} |
| 119 | + sync_module = self.blink.sync["test"] |
| 120 | + sync_module.cameras = {"Front Door": None} |
| 121 | + self.assertTrue(await sync_module.check_new_videos()) |
| 122 | + records = sync_module.last_records["Front Door"] |
| 123 | + self.assertEqual(len(records), 1) |
| 124 | + self.assertEqual( |
| 125 | + records[0]["ai_description"], |
| 126 | + "A person is walking on the driveway.", |
| 127 | + ) |
| 128 | + self.assertEqual(records[0]["ai_description_short"], "Person on driveway.") |
| 129 | + self.assertEqual(records[0]["cv_detection"], ["person"]) |
| 130 | + |
| 131 | + async def test_v4_no_ai_description(self, mock_resp): |
| 132 | + """Test graceful handling when ai_vd is None.""" |
| 133 | + mock_resp.return_value = {"media": [V4_ENTRY_NO_AI]} |
| 134 | + sync_module = self.blink.sync["test"] |
| 135 | + sync_module.cameras = {"Front Door": None} |
| 136 | + self.assertTrue(await sync_module.check_new_videos()) |
| 137 | + records = sync_module.last_records["Front Door"] |
| 138 | + self.assertEqual(len(records), 1) |
| 139 | + # No ai_description keys should be present |
| 140 | + self.assertNotIn("ai_description", records[0]) |
| 141 | + self.assertNotIn("ai_description_short", records[0]) |
| 142 | + |
| 143 | + async def test_v4_empty_ai_vd_object(self, mock_resp): |
| 144 | + """Test graceful handling when ai_vd is an empty dict.""" |
| 145 | + mock_resp.return_value = {"media": [V4_ENTRY_EMPTY_AI]} |
| 146 | + sync_module = self.blink.sync["test"] |
| 147 | + sync_module.cameras = {"Front Door": None} |
| 148 | + self.assertTrue(await sync_module.check_new_videos()) |
| 149 | + records = sync_module.last_records["Front Door"] |
| 150 | + self.assertEqual(len(records), 1) |
| 151 | + # Empty dict should not produce ai_description keys |
| 152 | + self.assertNotIn("ai_description", records[0]) |
| 153 | + # But cv_detection should still be extracted |
| 154 | + self.assertEqual(records[0]["cv_detection"], ["vehicle"]) |
| 155 | + |
| 156 | + |
| 157 | +@mock.patch("blinkpy.auth.Auth.query", return_value={}) |
| 158 | +class TestCameraAIAttributes(IsolatedAsyncioTestCase): |
| 159 | + """Test AI description attributes on camera objects.""" |
| 160 | + |
| 161 | + def setUp(self): |
| 162 | + """Set up Blink module.""" |
| 163 | + self.blink = Blink(session=mock.AsyncMock()) |
| 164 | + self.blink.urls = BlinkURLHandler("test") |
| 165 | + self.blink.sync["test"] = BlinkSyncModule(self.blink, "test", 1234, []) |
| 166 | + self.camera = BlinkCamera(self.blink.sync["test"]) |
| 167 | + self.camera.name = "foobar" |
| 168 | + self.blink.sync["test"].cameras["foobar"] = self.camera |
| 169 | + |
| 170 | + def tearDown(self): |
| 171 | + """Clean up after test.""" |
| 172 | + self.blink = None |
| 173 | + self.camera = None |
| 174 | + |
| 175 | + async def test_camera_attributes_include_ai_fields(self, mock_resp): |
| 176 | + """Test that camera attributes include AI description fields.""" |
| 177 | + attrs = self.camera.attributes |
| 178 | + self.assertIn("ai_description", attrs) |
| 179 | + self.assertIn("ai_description_short", attrs) |
| 180 | + self.assertIn("cv_detection", attrs) |
| 181 | + # Initially None |
| 182 | + self.assertIsNone(attrs["ai_description"]) |
| 183 | + self.assertIsNone(attrs["ai_description_short"]) |
| 184 | + self.assertIsNone(attrs["cv_detection"]) |
| 185 | + |
| 186 | + async def test_camera_ai_description_from_records(self, mock_resp): |
| 187 | + """Test that camera picks up AI description from last records.""" |
| 188 | + self.camera.sync.last_records["foobar"] = [ |
| 189 | + { |
| 190 | + "clip": "/clip.mp4", |
| 191 | + "time": "2024-01-01T00:00:00+00:00", |
| 192 | + "ai_description": "A cat sitting on the porch.", |
| 193 | + "ai_description_short": "Cat on porch.", |
| 194 | + "cv_detection": ["animal"], |
| 195 | + } |
| 196 | + ] |
| 197 | + self.camera.sync.motion["foobar"] = True |
| 198 | + await self.camera.update(CAMERA_CFG, force_cache=False) |
| 199 | + self.assertEqual(self.camera.ai_description, "A cat sitting on the porch.") |
| 200 | + self.assertEqual(self.camera.ai_description_short, "Cat on porch.") |
| 201 | + self.assertEqual(self.camera.cv_detection, ["animal"]) |
| 202 | + |
| 203 | + async def test_camera_no_ai_description_in_records(self, mock_resp): |
| 204 | + """Test camera handles records without AI description gracefully.""" |
| 205 | + self.camera.sync.last_records["foobar"] = [ |
| 206 | + { |
| 207 | + "clip": "/clip.mp4", |
| 208 | + "time": "2024-01-01T00:00:00+00:00", |
| 209 | + } |
| 210 | + ] |
| 211 | + self.camera.sync.motion["foobar"] = True |
| 212 | + await self.camera.update(CAMERA_CFG, force_cache=False) |
| 213 | + # Should be None since record didn't have these keys |
| 214 | + self.assertIsNone(self.camera.ai_description) |
| 215 | + self.assertIsNone(self.camera.ai_description_short) |
| 216 | + self.assertIsNone(self.camera.cv_detection) |
| 217 | + |
| 218 | + async def test_camera_recent_clips_include_ai_fields(self, mock_resp): |
| 219 | + """Test that recent_clips entries include AI description fields.""" |
| 220 | + self.camera.sync.last_records["foobar"] = [ |
| 221 | + { |
| 222 | + "clip": "/clip.mp4", |
| 223 | + "time": "2024-01-01T00:00:00+00:00", |
| 224 | + "ai_description": "A delivery person at the door.", |
| 225 | + "cv_detection": ["person"], |
| 226 | + } |
| 227 | + ] |
| 228 | + self.camera.sync.motion["foobar"] = True |
| 229 | + await self.camera.update_images(CAMERA_CFG, expire_clips=False) |
| 230 | + self.assertEqual(len(self.camera.recent_clips), 1) |
| 231 | + clip = self.camera.recent_clips[0] |
| 232 | + self.assertEqual(clip["ai_description"], "A delivery person at the door.") |
| 233 | + self.assertEqual(clip["cv_detection"], ["person"]) |
| 234 | + |
| 235 | + async def test_clip_url_absolute_not_doubled(self, mock_resp): |
| 236 | + """Test that absolute clip URLs from v4 are not doubled with base_url.""" |
| 237 | + absolute_url = "https://rest-u009.immedia-semi.com/api/v4/media/123/video" |
| 238 | + self.camera.sync.last_records["foobar"] = [ |
| 239 | + { |
| 240 | + "clip": absolute_url, |
| 241 | + "time": "2024-01-01T00:00:00+00:00", |
| 242 | + } |
| 243 | + ] |
| 244 | + self.camera.sync.motion["foobar"] = False |
| 245 | + await self.camera.update(CAMERA_CFG, force_cache=False) |
| 246 | + # URL should NOT be doubled (no base_url prepended) |
| 247 | + self.assertEqual(self.camera.clip, absolute_url) |
| 248 | + |
| 249 | + async def test_clip_url_relative_gets_base_url(self, mock_resp): |
| 250 | + """Test that relative clip URLs still get base_url prepended.""" |
| 251 | + self.camera.sync.last_records["foobar"] = [ |
| 252 | + { |
| 253 | + "clip": "/api/v1/media/123.mp4", |
| 254 | + "time": "2024-01-01T00:00:00+00:00", |
| 255 | + } |
| 256 | + ] |
| 257 | + self.camera.sync.motion["foobar"] = False |
| 258 | + await self.camera.update(CAMERA_CFG, force_cache=False) |
| 259 | + self.assertEqual( |
| 260 | + self.camera.clip, |
| 261 | + f"{self.blink.urls.base_url}/api/v1/media/123.mp4", |
| 262 | + ) |
0 commit comments