feat(token-tracking): surface token_usage in ReportData
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
"""Tests for token_usage passthrough in the webapp report builder."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from webapp.services.report_builder import build_report
|
||||
|
||||
|
||||
def _write_minimal_run(run_dir: Path, token_usage: dict | None) -> None:
|
||||
run_dir.mkdir(parents=True, exist_ok=True)
|
||||
(run_dir / "scores.csv").write_text(
|
||||
"sample_id,faithfulness\ns1,0.9\n", encoding="utf-8"
|
||||
)
|
||||
(run_dir / "summary.md").write_text("summary", encoding="utf-8")
|
||||
metadata = {"run_id": run_dir.name}
|
||||
if token_usage is not None:
|
||||
metadata["token_usage"] = token_usage
|
||||
(run_dir / "metadata.json").write_text(json.dumps(metadata), encoding="utf-8")
|
||||
|
||||
|
||||
def test_build_report_passes_through_token_usage(tmp_path: Path) -> None:
|
||||
run_dir = tmp_path / "run"
|
||||
_write_minimal_run(
|
||||
run_dir,
|
||||
token_usage={"gpt-5": {"input_tokens": 100, "output_tokens": 50, "calls": 2}},
|
||||
)
|
||||
|
||||
report = build_report(run_dir, ["faithfulness"])
|
||||
|
||||
assert report.token_usage == {
|
||||
"gpt-5": {"input_tokens": 100, "output_tokens": 50, "calls": 2}
|
||||
}
|
||||
|
||||
|
||||
def test_build_report_defaults_token_usage_to_empty_dict(tmp_path: Path) -> None:
|
||||
run_dir = tmp_path / "run"
|
||||
_write_minimal_run(run_dir, token_usage=None)
|
||||
|
||||
report = build_report(run_dir, ["faithfulness"])
|
||||
|
||||
assert report.token_usage == {}
|
||||
|
||||
|
||||
def test_build_report_early_return_branch_still_surfaces_token_usage(tmp_path: Path) -> None:
|
||||
"""metrics=[] forces the early-return branch; token_usage must still surface."""
|
||||
run_dir = tmp_path / "run"
|
||||
_write_minimal_run(
|
||||
run_dir,
|
||||
token_usage={"gpt-5": {"input_tokens": 5, "output_tokens": 5, "calls": 1}},
|
||||
)
|
||||
|
||||
report = build_report(run_dir, [])
|
||||
|
||||
assert report.token_usage == {"gpt-5": {"input_tokens": 5, "output_tokens": 5, "calls": 1}}
|
||||
Reference in New Issue
Block a user