Files
light-heart-labs--dreamserver/ods/extensions/services/dashboard-api/tests/test_agents.py
T
wehub-resource-sync 9e8f1bbeed
Dashboard / frontend (push) Failing after 0s
Dashboard / api (push) Failing after 0s
Lint PowerShell / powershell-lint (ubuntu-latest) (push) Failing after 1s
Python Lint / Lint Python with Ruff (push) Failing after 1s
ShellCheck / Lint shell scripts (push) Failing after 1s
Matrix Smoke / linux-smoke (push) Failing after 1s
Matrix Smoke / distro: cachyos (push) Failing after 15s
Matrix Smoke / distro: linux-mint-21.3 (push) Failing after 15s
Matrix Smoke / distro: debian-12 (push) Failing after 5m21s
Matrix Smoke / distro: fedora-41 (push) Failing after 4m56s
Matrix Smoke / distro: ubuntu-24.04 (push) Failing after 2m13s
Matrix Smoke / distro: rocky-9 (push) Failing after 10m39s
Matrix Smoke / distro: manjaro (push) Failing after 12m11s
Matrix Smoke / distro: opensuse-tw (push) Failing after 11m53s
Matrix Smoke / distro: archlinux (push) Failing after 20m3s
Matrix Smoke / distro: ubuntu-22.04 (push) Failing after 13m49s
Validate .env Schema / tier-1-env-validation (push) Successful in 52s
Validate .env Schema / tier-2-env-validation (push) Successful in 44s
Validate .env Schema / tier-3-env-validation (push) Successful in 52s
Validate .env Schema / tier-4-env-validation (push) Successful in 51s
Validate Extensions Catalog / Check catalog is up-to-date (push) Failing after 9m47s
Secret Scan / Scan for secrets (push) Failing after 21m4s
Validate Docker Compose / Validate Docker Compose files (push) Has been cancelled
Python Type Check / Type check with mypy (push) Has been cancelled
Validate .env Schema / tier-0-env-validation (push) Has been cancelled
Test Linux / integration-smoke (push) Has been cancelled
Lint PowerShell / powershell-lint (windows-latest) (push) Has been cancelled
Matrix Smoke / macos-smoke (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:31:33 +08:00

92 lines
2.9 KiB
Python

"""Tests for routers/agents.py — agent monitoring endpoints."""
from unittest.mock import AsyncMock
# --- GET /api/agents/metrics ---
class TestGetAgentMetrics:
def test_returns_metrics_structure(self, test_client):
resp = test_client.get("/api/agents/metrics", headers=test_client.auth_headers)
assert resp.status_code == 200
data = resp.json()
assert "timestamp" in data
assert "agent" in data
assert "cluster" in data
assert "throughput" in data
assert "session_count" in data["agent"]
assert "tokens_per_second" in data["agent"]
def test_requires_auth(self, test_client):
resp = test_client.get("/api/agents/metrics")
assert resp.status_code == 401
# --- GET /api/agents/metrics.html ---
class TestGetAgentMetricsHtml:
def test_returns_html_fragment(self, test_client):
resp = test_client.get("/api/agents/metrics.html", headers=test_client.auth_headers)
assert resp.status_code == 200
assert "text/html" in resp.headers["content-type"]
body = resp.text
assert "<div" in body
assert "Cluster Status" in body
assert "Active Sessions" in body
assert "Throughput" in body
def test_escapes_html_special_chars(self, test_client, monkeypatch):
"""HTML content should be escaped to prevent XSS."""
from agent_monitor import agent_metrics
from datetime import datetime
# Inject XSS-like data into agent metrics
original_last_update = agent_metrics.last_update
agent_metrics.last_update = datetime.fromisoformat("2026-01-01T12:00:00")
resp = test_client.get("/api/agents/metrics.html", headers=test_client.auth_headers)
assert resp.status_code == 200
body = resp.text
# The output should contain safely rendered content, no raw script tags
assert "<script>" not in body
# Restore original
agent_metrics.last_update = original_last_update
# --- GET /api/agents/cluster ---
class TestGetClusterStatus:
def test_returns_cluster_data(self, test_client, monkeypatch):
from agent_monitor import cluster_status
monkeypatch.setattr(cluster_status, "refresh", AsyncMock())
resp = test_client.get("/api/agents/cluster", headers=test_client.auth_headers)
assert resp.status_code == 200
data = resp.json()
assert "nodes" in data
assert "total_gpus" in data
assert "active_gpus" in data
assert "failover_ready" in data
# --- GET /api/agents/throughput ---
class TestGetThroughput:
def test_returns_throughput_stats(self, test_client):
resp = test_client.get("/api/agents/throughput", headers=test_client.auth_headers)
assert resp.status_code == 200
data = resp.json()
assert "current" in data
assert "average" in data
assert "peak" in data
assert "history" in data