File size: 13,335 Bytes
14f6b4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
HuggingFace Spaces 监控系统单元测试
"""

import pytest
import asyncio
from unittest.mock import Mock, AsyncMock, patch
from datetime import datetime
import json
import os

from config import ConfigManager, APIConfig
from data_models import (
    SpaceInfo, SpaceStatus, SpaceStatusInfo, SpaceRuntime, 
    MonitorEvent, EventType, AlertLevel, AlertRule
)
from huggingface_client_v2 import HuggingFaceClient, RetryClient, WebhookHandler
from monitor_engine import MonitorEngine, HealthChecker, SpaceMonitor


class TestConfigManager:
    
    def test_load_default_config(self):
        with patch.dict(os.environ, {'HF_TOKEN': 'test-token'}):
            manager = ConfigManager()
            config = manager.get_config()
            
            assert config.api.token == 'test-token'
            assert config.api.base_url == 'https://huggingface.co/api'
    
    def test_validate_config(self):
        with patch.dict(os.environ, {'HF_TOKEN': 'test-token'}):
            manager = ConfigManager()
            errors = manager.validate_config()
            
            assert len(errors) == 0
    
    def test_validate_missing_token(self):
        manager = ConfigManager()
        manager.config = None
        errors = manager.validate_config()
        
        assert any('HF_TOKEN' in error for error in errors)


class TestHuggingFaceClient:
    
    @pytest.fixture
    def client(self):
        return HuggingFaceClient(token="test-token")
    
    @pytest.fixture
    def mock_session(self):
        session = AsyncMock()
        return session
    
    @pytest.mark.asyncio
    async def test_get_space_info_success(self, client, mock_session):
        mock_response = {
            'id': 'test-space',
            'url': 'https://huggingface.co/spaces/test-space',
            'author': 'test-user',
            'description': 'Test space',
            'sdk': 'gradio',
            'lastModified': '2024-01-01T00:00:00.000Z'
        }
        
        with patch.object(client, '_get_session', return_value=mock_session):
            with patch.object(client, '_make_request', return_value=mock_response):
                space_info = await client.get_space_info('test-space')
                
                assert space_info.space_id == 'test-space'
                assert space_info.author == 'test-user'
                assert space_info.sdk == 'gradio'
    
    @pytest.mark.asyncio
    async def test_get_space_status_success(self, client, mock_session):
        with patch.object(client, '_get_session', return_value=mock_session):
            with patch.object(client, 'get_space_info', return_value=Mock()):
                with patch.object(client, 'get_space_runtime', return_value=SpaceRuntime(
                    stage='RUNNING', state='RUNNING'
                )):
                    status = await client.get_space_status('test-space')
                    
                    assert status.space_id == 'test-space'
                    assert status.status == SpaceStatus.RUNNING
    
    @pytest.mark.asyncio
    async def test_rate_limit(self, client):
        client.config.rate_limit_per_minute = 2
        
        start_time = asyncio.get_event_loop().time()
        
        for i in range(3):
            with patch.object(client, '_get_session', return_value AsyncMock()):
                with patch.object(client, '_make_request', return_value={}):
                    await client.get_space_info('test-space')
        
        elapsed = asyncio.get_event_loop().time() - start_time
        assert elapsed >= 60


class TestRetryClient:
    
    @pytest.fixture
    def retry_client(self):
        base_client = Mock()
        return RetryClient(base_client, max_retries=2, base_delay=0.1)
    
    @pytest.mark.asyncio
    async def test_success_on_first_try(self, retry_client):
        retry_client.client.get_space_status = AsyncMock(return_value=Mock())
        
        result = await retry_client.get_space_status('test-space')
        
        assert result is not None
        retry_client.client.get_space_status.assert_called_once()
    
    @pytest.mark.asyncio
    async def test_retry_on_failure(self, retry_client):
        retry_client.client.get_space_status = AsyncMock(
            side_effect=[Exception("First failure"), Mock(success=True)]
        )
        
        result = await retry_client.get_space_status('test-space')
        
        assert result is not None
        assert retry_client.client.get_space_status.call_count == 2
    
    @pytest.mark.asyncio
    async def test_max_retries_exceeded(self, retry_client):
        retry_client.client.get_space_status = AsyncMock(
            side_effect=Exception("Persistent failure")
        )
        
        with pytest.raises(Exception):
            await retry_client.get_space_status('test-space')
        
        assert retry_client.client.get_space_status.call_count == 3


class TestWebhookHandler:
    
    @pytest.fixture
    def webhook_handler(self):
        client = Mock()
        return WebhookHandler(client, secret="test-secret")
    
    @pytest.mark.asyncio
    async def test_handle_valid_webhook(self, webhook_handler):
        payload = {
            'event': 'space.status_updated',
            'space': {
                'id': 'test-space',
                'runtime': {'stage': 'RUNNING', 'state': 'RUNNING'}
            }
        }
        
        with patch.object(webhook_handler, '_verify_signature'):
            event = await webhook_handler.handle_webhook(payload, {})
            
            assert event.space_id == 'test-space'
            assert event.processed
    
    @pytest.mark.asyncio
    async def test_handle_unknown_event(self, webhook_handler):
        payload = {
            'event': 'unknown.event',
            'space': {'id': 'test-space'}
        }
        
        with patch.object(webhook_handler, '_verify_signature'):
            event = await webhook_handler.handle_webhook(payload, {})
            
            assert not event.processed


class TestMonitorEngine:
    
    @pytest.fixture
    def engine(self):
        return MonitorEngine()
    
    @pytest.mark.asyncio
    async def test_add_space(self, engine):
        with patch.object(engine.client.client, 'get_space_info', return_value=SpaceInfo(
            space_id='test-space', name='test-space'
        )):
            with patch.object(engine.client, 'get_space_status', return_value=SpaceStatusInfo(
                space_id='test-space', status=SpaceStatus.RUNNING, 
                runtime=SpaceRuntime(stage='RUNNING', state='RUNNING'),
                timestamp=datetime.now()
            )):
                with patch.object(engine.db_manager, 'save_space_info'):
                    with patch.object(engine, '_emit_event'):
                        await engine.add_space('test-space')
                        
                        assert 'test-space' in engine.monitored_spaces
    
    @pytest.mark.asyncio
    async def test_remove_space(self, engine):
        monitor = SpaceMonitor(space_id='test-space', config={})
        engine.monitored_spaces['test-space'] = monitor
        
        with patch.object(engine, '_emit_event'):
            await engine.remove_space('test-space')
            
            assert 'test-space' not in engine.monitored_spaces
    
    @pytest.mark.asyncio
    async def test_status_change_event(self, engine):
        monitor = SpaceMonitor(
            space_id='test-space', 
            config={}, 
            last_status=SpaceStatus.BUILDING
        )
        engine.monitored_spaces['test-space'] = monitor
        
        with patch.object(engine.client, 'get_space_status', return_value=SpaceStatusInfo(
            space_id='test-space', status=SpaceStatus.RUNNING,
            runtime=SpaceRuntime(stage='RUNNING', state='RUNNING'),
            timestamp=datetime.now()
        )):
            with patch.object(engine.db_manager, 'save_status_history'):
                with patch.object(engine, '_handle_status_change') as mock_handler:
                    await engine._check_space('test-space', monitor)
                    
                    mock_handler.assert_called_once()
    
    @pytest.mark.asyncio
    async def test_error_threshold_trigger(self, engine):
        monitor = SpaceMonitor(
            space_id='test-space',
            config={'error_threshold': 2},
            consecutive_errors=1
        )
        engine.monitored_spaces['test-space'] = monitor
        
        with patch.object(engine.client, 'get_space_status', side_effect=Exception("API Error")):
            with patch.object(engine, '_trigger_error_alert') as mock_alert:
                await engine._check_space('test-space', monitor)
                
                mock_alert.assert_called_once()
    
    def test_register_event_callback(self, engine):
        callback = Mock()
        
        engine.register_event_callback(EventType.ERROR_DETECTED, callback)
        
        assert callback in engine.event_callbacks[EventType.ERROR_DETECTED]
    
    def test_unregister_event_callback(self, engine):
        callback = Mock()
        engine.event_callbacks[EventType.ERROR_DETECTED].append(callback)
        
        engine.unregister_event_callback(EventType.ERROR_DETECTED, callback)
        
        assert callback not in engine.event_callbacks[EventType.ERROR_DETECTED]


class TestHealthChecker:
    
    @pytest.fixture
    def health_checker(self):
        engine = Mock()
        return HealthChecker(engine)
    
    @pytest.mark.asyncio
    async def test_healthy_status(self, health_checker):
        health_checker.engine.get_stats = AsyncMock(return_value={
            'state': 'running'
        })
        health_checker.engine.client.client.validate_token = AsyncMock(return_value=True)
        
        with patch.object(health_checker.engine.db_manager, '_init_database'):
            status = await health_checker.check_health()
            
            assert status['status'] == 'healthy'
    
    @pytest.mark.asyncio
    async def test_unhealthy_engine(self, health_checker):
        health_checker.engine.get_stats = AsyncMock(return_value={
            'state': 'error'
        })
        
        status = await health_checker.check_health()
        
        assert status['status'] == 'unhealthy'
        assert 'engine' in status['checks']


class TestDataModels:
    
    def test_space_info_creation(self):
        space_info = SpaceInfo(
            space_id='test-space',
            name='Test Space',
            author='test-user',
            sdk='gradio'
        )
        
        assert space_info.space_id == 'test-space'
        assert space_info.author == 'test-user'
        assert space_info.sdk == 'gradio'
    
    def test_monitor_event_creation(self):
        event = MonitorEvent(
            space_id='test-space',
            event_type=EventType.ERROR_DETECTED,
            timestamp=datetime.now(),
            message='Test error',
            severity=AlertLevel.HIGH
        )
        
        assert event.space_id == 'test-space'
        assert event.event_type == EventType.ERROR_DETECTED
        assert event.severity == AlertLevel.HIGH
    
    def test_alert_rule_creation(self):
        rule = AlertRule(
            name='Test Rule',
            condition={'event_type': 'error'},
            severity=AlertLevel.MEDIUM,
            cooldown_minutes=30
        )
        
        assert rule.name == 'Test Rule'
        assert rule.severity == AlertLevel.MEDIUM
        assert rule.cooldown_minutes == 30


class TestIntegration:
    
    @pytest.mark.asyncio
    async def test_full_monitoring_cycle(self):
        with patch('config.get_config') as mock_config:
            mock_config.return_value.monitoring.default_check_interval = 1
            
            engine = MonitorEngine()
            
            with patch.object(engine.client.client, 'validate_token', return_value=True):
                with patch.object(engine.client.client, 'get_space_info', return_value=SpaceInfo(
                    space_id='test-space', name='test-space'
                )):
                    with patch.object(engine.client, 'get_space_status', return_value=SpaceStatusInfo(
                        space_id='test-space', status=SpaceStatus.RUNNING,
                        runtime=SpaceRuntime(stage='RUNNING', state='RUNNING'),
                        timestamp=datetime.now()
                    )):
                        with patch.object(engine.db_manager, 'save_space_info'):
                            with patch.object(engine.db_manager, 'save_status_history'):
                                with patch.object(engine, '_emit_event'):
                                    await engine.start()
                                    await engine.add_space('test-space')
                                    
                                    await asyncio.sleep(2)
                                    
                                    stats = await engine.get_stats()
                                    
                                    assert stats['total_checks'] > 0
                                    
                                    await engine.stop()


if __name__ == "__main__":
    pytest.main([__file__, "-v"])