"""Tests for graphs/stats — TDD: tests before implementation.""" import pytest from i2p_apps.console.app import create_app from i2p_apps.console.services.stats_collector import StatsCollector @pytest.fixture def client(): app = create_app({"TESTING": True}) return app.test_client() class TestGraphsPage: def test_graphs_renders(self, client): resp = client.get("/graphs") assert resp.status_code == 200 assert b"html" in resp.data.lower() def test_graphs_has_chart_container(self, client): resp = client.get("/graphs") assert b"plotly" in resp.data.lower() or b"chart" in resp.data.lower() class TestStatsCollector: def test_creation(self): sc = StatsCollector() assert sc is not None def test_record_and_get(self): sc = StatsCollector() sc.record("bandwidth.in", 1024) sc.record("bandwidth.in", 2048) values = sc.get("bandwidth.in") assert len(values) == 2 def test_get_nonexistent(self): sc = StatsCollector() assert sc.get("nonexistent") == [] def test_average(self): sc = StatsCollector() sc.record("test.stat", 10) sc.record("test.stat", 20) sc.record("test.stat", 30) assert sc.average("test.stat") == 20.0 def test_average_empty(self): sc = StatsCollector() assert sc.average("empty") == 0.0 def test_summary(self): sc = StatsCollector() sc.record("stat1", 100) sc.record("stat2", 200) summary = sc.summary() assert "stat1" in summary assert "stat2" in summary def test_max_samples(self): sc = StatsCollector(max_samples=3) for i in range(10): sc.record("test", float(i)) assert len(sc.get("test")) == 3