Spaces:
Running on T4
Running on T4
Download AI-coach-agent/tests/test_report.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.39 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_report.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/tests/test_report.py
-
curl -L -o test_report.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_report.py
3.39 kB
| import json | |
| import pytest | |
| from ai_coach.config import Settings | |
| from ai_coach.cost import UsageLedger | |
| from ai_coach.report import main, render_report | |
| from ai_coach.schemas import Usage | |
| def seed(path): | |
| ledger = UsageLedger(path, daily_budget_usd=1.0) | |
| ledger.record_call( | |
| request_id="a", | |
| model="gemini-3.1-flash-lite", | |
| attempt=1, | |
| escalated=False, | |
| outcome="ok", | |
| usage=Usage(input_tokens=2000, output_tokens=300, cached_tokens=0, reasoning_tokens=20, cost_usd=0.0009), | |
| latency_ms=800, | |
| ) | |
| ledger.record_note( | |
| request_id="a", | |
| discipline="hitting", | |
| source="llm", | |
| model="gemini-3.1-flash-lite", | |
| cache_hit=False, | |
| fallback_reason=None, | |
| attempts=1, | |
| cost_usd=0.0009, | |
| latency_ms=900, | |
| prompt_version="p", | |
| ) | |
| ledger.record_note( | |
| request_id="b", | |
| discipline="hitting", | |
| source="llm", | |
| model="gemini-3.1-flash-lite", | |
| cache_hit=True, | |
| fallback_reason=None, | |
| attempts=0, | |
| cost_usd=0.0, | |
| latency_ms=3, | |
| prompt_version="p", | |
| ) | |
| ledger.record_note( | |
| request_id="c", | |
| discipline="pitching", | |
| source="rules", | |
| model=None, | |
| cache_hit=False, | |
| fallback_reason="nothing_to_judge", | |
| attempts=0, | |
| cost_usd=0.0, | |
| latency_ms=2, | |
| prompt_version="p", | |
| ) | |
| return ledger | |
| def test_render_report_answers_how_many_tokens_and_how_much(tmp_path): | |
| text = render_report(seed(tmp_path / "s.sqlite3").summary(7)) | |
| assert "Requests 3" in text and "served from cache 1" in text and "(33%)" in text | |
| assert "$0.00090" in text and "gemini-3.1-flash-lite" in text and "2,000" in text | |
| assert "nothing_to_judge=1" in text and "Spent today" in text and "$1.00000 budget" in text | |
| def test_report_cli_text_and_json(tmp_path, monkeypatch, capsys): | |
| cache = tmp_path / "c.sqlite3" | |
| seed(cache.with_name("spend.sqlite3")) | |
| monkeypatch.setenv("COACH_CACHE_PATH", str(cache)) | |
| assert main(["--days", "1"]) == 0 | |
| assert "AI coach usage, last 1 day(s)" in capsys.readouterr().out | |
| assert main(["--json"]) == 0 | |
| data = json.loads(capsys.readouterr().out) | |
| assert data["requests"] == 3 and data["model_calls"] == 1 and data["cost_usd"] == 0.0009 | |
| def test_report_on_a_fresh_install_does_not_crash(tmp_path, monkeypatch, capsys): | |
| monkeypatch.setenv("COACH_CACHE_PATH", str(tmp_path / "new" / "c.sqlite3")) | |
| assert main([]) == 0 | |
| assert "Requests 0" in capsys.readouterr().out | |
| assert Settings(_env_file=None).resolved_ledger_path.name == "spend.sqlite3" | |
| def test_report_shows_how_often_the_model_agrees_with_the_baseline(tmp_path): | |
| ledger = seed(tmp_path / "s.sqlite3") | |
| for agrees in (True, True, False): | |
| ledger.record_note( | |
| request_id="x", | |
| discipline="hitting", | |
| source="llm", | |
| model="m", | |
| cache_hit=False, | |
| fallback_reason=None, | |
| attempts=1, | |
| cost_usd=0.001, | |
| latency_ms=10, | |
| prompt_version="p", | |
| agrees_with_baseline=agrees, | |
| ) | |
| summary = ledger.summary(1) | |
| assert summary["agreement_with_baseline"] == pytest.approx(2 / 3) | |
| assert "Priority fix = baseline 67%" in render_report(summary) | |