Files
pocketpaw/tests/test_api_analytics.py
Amritesh 63074f0a68 feat(traces): Implement trace storage utilities for request-level observability
- Added TraceStore class for managing trace data with daily JSONL partitioning.
- Implemented methods for appending, retrieving, and cleaning up traces.
- Introduced helper functions for parsing timestamps and calculating trace costs.
- Created API endpoints for accessing trace data and analytics.

feat(api): Add budget and analytics API endpoints

- Implemented budget status and override management routes.
- Added analytics endpoints for cost, performance, usage, and health metrics.
- Created tests for budget and analytics API functionality.

test(traces): Add comprehensive tests for trace storage and API

- Developed unit tests for trace storage helpers and integration tests for trace propagation.
- Added tests for budget and analytics API endpoints to ensure correct behavior.
- Included tests for trace collector event aggregation and lifecycle management.
2026-04-21 14:50:56 +05:30

76 lines
2.1 KiB
Python

"""Tests for analytics API endpoints."""
from __future__ import annotations
from unittest.mock import AsyncMock, patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pocketpaw.api.v1.analytics import router
def _build_client() -> TestClient:
app = FastAPI()
app.include_router(router, prefix="/api/v1")
return TestClient(app)
def test_analytics_router_registered_in_v1() -> None:
from pocketpaw.api.v1 import _V1_ROUTERS
modules = [item[0] for item in _V1_ROUTERS]
assert "pocketpaw.api.v1.analytics" in modules
def test_analytics_cost_endpoint() -> None:
client = _build_client()
with patch(
"pocketpaw.api.v1.analytics.get_cost_analytics",
new=AsyncMock(return_value={"period": "day", "totals": {"cost_usd": 1.23}}),
):
resp = client.get("/api/v1/analytics/cost?period=day")
assert resp.status_code == 200
assert resp.json()["totals"]["cost_usd"] == 1.23
def test_analytics_performance_endpoint() -> None:
client = _build_client()
with patch(
"pocketpaw.api.v1.analytics.get_performance_analytics",
new=AsyncMock(return_value={"period": "week", "response_latency_ms": {"avg": 123.4}}),
):
resp = client.get("/api/v1/analytics/performance?period=week")
assert resp.status_code == 200
assert resp.json()["period"] == "week"
def test_analytics_usage_endpoint() -> None:
client = _build_client()
with patch(
"pocketpaw.api.v1.analytics.get_usage_analytics",
new=AsyncMock(return_value={"period": "month", "totals": {"messages": 10}}),
):
resp = client.get("/api/v1/analytics/usage?period=month")
assert resp.status_code == 200
assert resp.json()["totals"]["messages"] == 10
def test_analytics_health_endpoint() -> None:
client = _build_client()
with patch(
"pocketpaw.api.v1.analytics.get_health_analytics",
new=AsyncMock(return_value={"status": "healthy", "error_rate_24h": 0.0}),
):
resp = client.get("/api/v1/analytics/health")
assert resp.status_code == 200
assert resp.json()["status"] == "healthy"