File size: 23,316 Bytes
cc036ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
"""
Tests for Agent Status API Endpoints

Coverage Targets:
- Status retrieval (GET /agent/status/{task_id})
- Status updates (PUT /agent/{agent_id}/status)
- Status filtering (GET /agents, /agent/status)
- Status metrics (GET /agent/metrics)
- Error handling (404, 400, 500)
"""

import pytest
import json
from datetime import datetime
from pathlib import Path
from fastapi.testclient import TestClient
from fastapi import FastAPI
from unittest.mock import Mock, AsyncMock, patch, MagicMock
from typing import Dict, Any

from api.agent_status_endpoints import (
    router,
    load_agent_status,
    save_agent_status,
    AGENT_STATUS_FILE,
    AgentTask,
    AgentInfo
)

# ============================================================================
# Fixtures
# ============================================================================

@pytest.fixture
def client():
    """Create test client with router"""
    app = FastAPI()
    app.include_router(router)
    return TestClient(app)

@pytest.fixture
def temp_status_file(tmp_path):
    """Create temporary status file"""
    status_file = tmp_path / "test_agent_status.json"
    with patch("api.agent_status_endpoints.AGENT_STATUS_FILE", status_file):
        yield status_file

@pytest.fixture
def sample_agent_data():
    """Sample agent data"""
    return {
        "agents": {
            "agent_001": {
                "agent_id": "agent_001",
                "name": "Test Agent 1",
                "type": "general",
                "status": "idle",
                "last_active": datetime.now().isoformat(),
                "current_task": None,
                "capabilities": ["text_processing", "analysis"],
                "health_score": 1.0
            },
            "agent_002": {
                "agent_id": "agent_002",
                "name": "Test Agent 2",
                "type": "specialized",
                "status": "busy",
                "last_active": datetime.now().isoformat(),
                "current_task": "task_001",
                "capabilities": ["data_processing"],
                "health_score": 0.9
            }
        },
        "tasks": {
            "task_001": {
                "task_id": "task_001",
                "agent_id": "agent_002",
                "status": "running",
                "progress": 0.5,
                "started_at": datetime.now().isoformat(),
                "completed_at": None,
                "error_message": None,
                "result": None,
                "metadata": {"test": "data"}
            },
            "task_002": {
                "task_id": "task_002",
                "agent_id": "agent_001",
                "status": "completed",
                "progress": 1.0,
                "started_at": datetime.now().isoformat(),
                "completed_at": datetime.now().isoformat(),
                "error_message": None,
                "result": {"output": "success"},
                "metadata": {}
            }
        }
    }

@pytest.fixture
def sample_tasks(sample_agent_data):
    """Extract sample tasks from agent data"""
    return sample_agent_data["tasks"]

# ============================================================================
# GET /agent/status/{task_id} - Status Retrieval
# ============================================================================

def test_get_agent_status_success(client, temp_status_file, sample_agent_data):
    """Test successful status retrieval for existing task"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    response = client.get("/api/agent-status/agent/status/task_001")

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["task_id"] == "task_001"
    assert data["agent_id"] == "agent_002"
    assert data["status"] == "running"
    assert data["progress"] == 0.5


def test_get_agent_status_not_found(client, temp_status_file):
    """Test status retrieval for non-existent task returns default"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    response = client.get("/api/agent-status/agent/status/nonexistent_task")

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["task_id"] == "nonexistent_task"
    assert data["agent_id"] == "unknown"
    assert data["status"] == "not_found"
    assert data["error_message"] == "Task not found"


def test_get_agent_status_empty_file(client, temp_status_file):
    """Test status retrieval with empty status file"""
    # Test
    response = client.get("/api/agent-status/agent/status/task_any")

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["status"] == "not_found"


# ============================================================================
# GET /agent/status - All Tasks
# ============================================================================

def test_get_all_agent_tasks(client, temp_status_file, sample_agent_data):
    """Test retrieving all agent tasks"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    response = client.get("/api/agent-status/agent/status")

    # Verify
    assert response.status_code == 200
    tasks = response.json()
    assert len(tasks) == 2
    assert any(t["task_id"] == "task_001" for t in tasks)
    assert any(t["task_id"] == "task_002" for t in tasks)


def test_get_all_agent_tasks_empty(client, temp_status_file):
    """Test retrieving all tasks when no tasks exist"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    response = client.get("/api/agent-status/agent/status")

    # Verify
    assert response.status_code == 200
    tasks = response.json()
    assert len(tasks) == 0


# ============================================================================
# GET /agents - All Agents
# ============================================================================

def test_get_all_agents(client, temp_status_file, sample_agent_data):
    """Test retrieving all agents"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    response = client.get("/api/agent-status/agents")

    # Verify
    assert response.status_code == 200
    agents = response.json()
    assert len(agents) == 2
    assert any(a["agent_id"] == "agent_001" for a in agents)
    assert any(a["agent_id"] == "agent_002" for a in agents)


def test_get_all_agents_empty(client, temp_status_file):
    """Test retrieving all agents when no agents exist"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    response = client.get("/api/agent-status/agents")

    # Verify
    assert response.status_code == 200
    agents = response.json()
    assert len(agents) == 0


# ============================================================================
# GET /agents/{agent_id} - Specific Agent
# ============================================================================

def test_get_agent_info_success(client, temp_status_file, sample_agent_data):
    """Test successful retrieval of specific agent info"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    response = client.get("/api/agent-status/agents/agent_001")

    # Verify
    assert response.status_code == 200
    agent = response.json()
    assert agent["agent_id"] == "agent_001"
    assert agent["name"] == "Test Agent 1"
    assert agent["type"] == "general"
    assert agent["status"] == "idle"


def test_get_agent_info_not_found_creates_default(client, temp_status_file):
    """Test agent info retrieval creates default agent if not found"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    response = client.get("/api/agent-status/agents/new_agent")

    # Verify
    assert response.status_code == 200
    agent = response.json()
    assert agent["agent_id"] == "new_agent"
    assert agent["name"] == "Agent new_agent"
    assert agent["type"] == "general"
    assert agent["status"] == "idle"
    assert "last_active" in agent

    # Verify default agent was saved
    data = load_agent_status()
    assert "new_agent" in data["agents"]


# ============================================================================
# POST /agent/{agent_id}/heartbeat - Heartbeat Update
# ============================================================================

def test_agent_heartbeat_success(client, temp_status_file):
    """Test successful agent heartbeat update"""
    # Setup
    status_data = {
        "agents": {
            "agent_001": {
                "agent_id": "agent_001",
                "name": "Test Agent",
                "type": "general",
                "status": "idle",
                "last_active": datetime.now().isoformat(),
                "current_task": None,
                "capabilities": [],
                "health_score": 1.0
            }
        },
        "tasks": {}
    }
    save_agent_status(status_data)

    # Test
    heartbeat_data = {
        "name": "Updated Agent",
        "status": "busy",
        "current_task": "task_123",
        "health_score": 0.95
    }
    response = client.post("/api/agent-status/agent/agent_001/heartbeat", json=heartbeat_data)

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert "timestamp" in data["data"]

    # Verify agent was updated
    agent_status = load_agent_status()
    agent = agent_status["agents"]["agent_001"]
    assert agent["name"] == "Updated Agent"
    assert agent["status"] == "busy"
    assert agent["current_task"] == "task_123"
    assert agent["health_score"] == 0.95


def test_agent_heartbeat_new_agent(client, temp_status_file):
    """Test heartbeat creates new agent if not exists"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    heartbeat_data = {
        "name": "New Agent",
        "type": "specialized",
        "capabilities": ["analysis", "reporting"]
    }
    response = client.post("/api/agent-status/agent/agent_new/heartbeat", json=heartbeat_data)

    # Verify
    assert response.status_code == 200

    # Verify agent was created
    agent_status = load_agent_status()
    assert "agent_new" in agent_status["agents"]
    assert agent_status["agents"]["agent_new"]["name"] == "New Agent"


# ============================================================================
# POST /agent/task/{task_id}/update - Task Status Update
# ============================================================================

def test_update_task_status_success(client, temp_status_file, sample_agent_data):
    """Test successful task status update"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    update_data = {
        "status": "completed",
        "progress": 1.0,
        "result": {"final_output": "done"}
    }
    response = client.post("/api/agent-status/agent/task/task_001/update", json=update_data)

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True

    # Verify task was updated
    agent_status = load_agent_status()
    task = agent_status["tasks"]["task_001"]
    assert task["status"] == "completed"
    assert task["progress"] == 1.0
    assert task["result"] == {"final_output": "done"}
    assert "completed_at" in task


def test_update_task_status_not_found(client, temp_status_file):
    """Test updating non-existent task returns 404"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    update_data = {"status": "running"}
    response = client.post("/api/agent-status/agent/task/nonexistent/update", json=update_data)

    # Verify
    assert response.status_code == 404


def test_update_task_status_running_sets_timestamp(client, temp_status_file, sample_agent_data):
    """Test updating to running status sets started_at timestamp"""
    # Setup
    task_data = sample_agent_data.copy()
    task_data["tasks"]["task_003"] = {
        "task_id": "task_003",
        "agent_id": "agent_001",
        "status": "pending",
        "progress": 0.0,
        "started_at": None,
        "completed_at": None,
        "error_message": None,
        "result": None,
        "metadata": {}
    }
    save_agent_status(task_data)

    # Test
    update_data = {"status": "running"}
    response = client.post("/api/agent-status/agent/task/task_003/update", json=update_data)

    # Verify
    assert response.status_code == 200
    agent_status = load_agent_status()
    task = agent_status["tasks"]["task_003"]
    assert task["started_at"] is not None


def test_update_task_status_with_error(client, temp_status_file, sample_agent_data):
    """Test updating task with error message"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    update_data = {
        "status": "failed",
        "error_message": "Connection timeout",
        "progress": 0.3
    }
    response = client.post("/api/agent-status/agent/task/task_001/update", json=update_data)

    # Verify
    assert response.status_code == 200
    agent_status = load_agent_status()
    task = agent_status["tasks"]["task_001"]
    assert task["status"] == "failed"
    assert task["error_message"] == "Connection timeout"
    assert task["progress"] == 0.3


# ============================================================================
# POST /agent/task - Create Task
# ============================================================================

def test_create_task_success(client, temp_status_file):
    """Test successful task creation"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    task_data = {
        "task_id": "task_new",
        "agent_id": "agent_001",
        "status": "pending",
        "progress": 0.0,
        "metadata": {"source": "test"}
    }
    response = client.post("/api/agent-status/agent/task", json=task_data)

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True
    assert data["data"]["task_id"] == "task_new"

    # Verify task was created
    agent_status = load_agent_status()
    assert "task_new" in agent_status["tasks"]
    assert agent_status["tasks"]["task_new"]["agent_id"] == "agent_001"


def test_create_task_running_sets_timestamp(client, temp_status_file):
    """Test creating running task sets started_at"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    task_data = {
        "task_id": "task_running",
        "agent_id": "agent_001",
        "status": "running",
        "progress": 0.0
    }
    response = client.post("/api/agent-status/agent/task", json=task_data)

    # Verify
    assert response.status_code == 200
    agent_status = load_agent_status()
    task = agent_status["tasks"]["task_running"]
    assert task["started_at"] is not None


# ============================================================================
# DELETE /agent/task/{task_id} - Delete Task
# ============================================================================

def test_delete_task_success(client, temp_status_file, sample_agent_data):
    """Test successful task deletion"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    response = client.delete("/api/agent-status/agent/task/task_001")

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert data["success"] is True

    # Verify task was deleted
    agent_status = load_agent_status()
    assert "task_001" not in agent_status["tasks"]
    assert "task_002" in agent_status["tasks"]  # Other task still exists


def test_delete_task_not_found(client, temp_status_file):
    """Test deleting non-existent task returns 404"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    response = client.delete("/api/agent-status/agent/task/nonexistent")

    # Verify
    assert response.status_code == 404


# ============================================================================
# GET /agent/metrics - Performance Metrics
# ============================================================================

def test_get_agent_metrics(client, temp_status_file, sample_agent_data):
    """Test retrieving agent metrics"""
    # Setup
    save_agent_status(sample_agent_data)

    # Test
    response = client.get("/api/agent-status/agent/metrics")

    # Verify
    assert response.status_code == 200
    data = response.json()
    assert "data" in data

    metrics = data["data"]
    assert "agents" in metrics
    assert "tasks" in metrics

    # Check agent metrics
    assert metrics["agents"]["total"] == 2
    assert metrics["agents"]["active"] == 1  # agent_002 is busy
    assert metrics["agents"]["idle"] == 1     # agent_001 is idle

    # Check task metrics
    assert metrics["tasks"]["total"] == 2
    assert metrics["tasks"]["completed"] == 1  # task_002
    assert metrics["tasks"]["failed"] == 0
    assert metrics["tasks"]["pending"] == 1   # task_001 is running

    # Check success rate
    assert "success_rate" in metrics
    assert metrics["success_rate"] == 0.5  # 1 completed out of 2


def test_get_agent_metrics_empty(client, temp_status_file):
    """Test metrics with no agents or tasks"""
    # Setup
    save_agent_status({"agents": {}, "tasks": {}})

    # Test
    response = client.get("/api/agent-status/agent/metrics")

    # Verify
    assert response.status_code == 200
    data = response.json()
    metrics = data["data"]

    assert metrics["agents"]["total"] == 0
    assert metrics["agents"]["active"] == 0
    assert metrics["tasks"]["total"] == 0
    assert metrics["tasks"]["completed"] == 0
    assert metrics["success_rate"] == 0.0  # Division by zero protection


# ============================================================================
# Error Handling
# ============================================================================

def test_load_agent_status_corrupted_file(temp_status_file):
    """Test loading from corrupted file returns empty dict"""
    # Setup
    temp_status_file.write_text("invalid json content")

    # Test
    data = load_agent_status()

    # Verify
    assert data == {"agents": {}, "tasks": {}}


def test_save_agent_status_error_handling(temp_status_file):
    """Test saving error is logged but doesn't crash"""
    # Setup - make file read-only
    temp_status_file.write_text("{}")
    temp_status_file.chmod(0o444)

    # Test - should not raise exception
    try:
        save_agent_status({"agents": {}, "tasks": {}})
    except Exception:
        pass  # Expected

    # Cleanup
    temp_status_file.chmod(0o644)


# ============================================================================
# Governance Integration
# ============================================================================

def test_active_agent_counting(client, temp_status_file):
    """Test active agent counting considers different states"""
    # Setup
    status_data = {
        "agents": {
            "agent_running": {"agent_id": "agent_running", "status": "running", "last_active": datetime.now().isoformat(), "name": "Running", "type": "general", "current_task": None, "capabilities": [], "health_score": 1.0},
            "agent_busy": {"agent_id": "agent_busy", "status": "busy", "last_active": datetime.now().isoformat(), "name": "Busy", "type": "general", "current_task": None, "capabilities": [], "health_score": 1.0},
            "agent_idle": {"agent_id": "agent_idle", "status": "idle", "last_active": datetime.now().isoformat(), "name": "Idle", "type": "general", "current_task": None, "capabilities": [], "health_score": 1.0}
        },
        "tasks": {}
    }
    save_agent_status(status_data)

    # Test
    response = client.get("/api/agent-status/agent/metrics")

    # Verify
    assert response.status_code == 200
    metrics = response.json()["data"]
    assert metrics["agents"]["active"] == 2  # running + busy
    assert metrics["agents"]["idle"] == 1    # idle agent


def test_task_status_filtering(client, temp_status_file):
    """Test task metrics correctly filter by status"""
    # Setup
    status_data = {
        "agents": {},
        "tasks": {
            "task_1": {"task_id": "task_1", "agent_id": "agent_1", "status": "completed", "progress": 1.0, "started_at": None, "completed_at": None, "error_message": None, "result": None, "metadata": {}},
            "task_2": {"task_id": "task_2", "agent_id": "agent_1", "status": "failed", "progress": 0.5, "started_at": None, "completed_at": None, "error_message": None, "result": None, "metadata": {}},
            "task_3": {"task_id": "task_3", "agent_id": "agent_1", "status": "running", "progress": 0.2, "started_at": None, "completed_at": None, "error_message": None, "result": None, "metadata": {}},
            "task_4": {"task_id": "task_4", "agent_id": "agent_1", "status": "pending", "progress": 0.0, "started_at": None, "completed_at": None, "error_message": None, "result": None, "metadata": {}}
        }
    }
    save_agent_status(status_data)

    # Test
    response = client.get("/api/agent-status/agent/metrics")

    # Verify
    assert response.status_code == 200
    metrics = response.json()["data"]
    assert metrics["tasks"]["total"] == 4
    assert metrics["tasks"]["completed"] == 1
    assert metrics["tasks"]["failed"] == 1
    assert metrics["tasks"]["pending"] == 2  # running + pending


# ============================================================================
# Pydantic Model Validation
# ============================================================================

def test_agent_task_model_validation():
    """Test AgentTask Pydantic model validation"""
    # Valid task
    task = AgentTask(
        task_id="task_001",
        agent_id="agent_001",
        status="running",
        progress=0.5,
        metadata={"key": "value"}
    )
    assert task.task_id == "task_001"
    assert task.status == "running"
    assert task.progress == 0.5

    # Task with all fields
    task_full = AgentTask(
        task_id="task_002",
        agent_id="agent_002",
        status="completed",
        progress=1.0,
        started_at=datetime.now(),
        completed_at=datetime.now(),
        error_message=None,
        result={"output": "success"},
        metadata={}
    )
    assert task_full.status == "completed"
    assert task_full.result == {"output": "success"}


def test_agent_info_model_validation():
    """Test AgentInfo Pydantic model validation"""
    # Valid agent
    agent = AgentInfo(
        agent_id="agent_001",
        name="Test Agent",
        type="general",
        status="idle",
        last_active=datetime.now(),
        current_task=None,
        capabilities=["text_processing"],
        health_score=0.95
    )
    assert agent.agent_id == "agent_001"
    assert agent.status == "idle"
    assert agent.health_score == 0.95

    # Agent with current task
    agent_busy = AgentInfo(
        agent_id="agent_002",
        name="Busy Agent",
        type="specialized",
        status="busy",
        last_active=datetime.now(),
        current_task="task_001",
        capabilities=["data_processing"],
        health_score=1.0
    )
    assert agent_busy.current_task == "task_001"
    assert agent_busy.status == "busy"