from scripts.parse_timing import parse_line, aggregate

SAMPLE = (
    "2026-06-11 14:35:00 - vllm_batcher - _process_jobs - line:119 - INFO - "
    "surya_timing_summary request_id=a,b batch_size=2 events=["
    "{'name': 'openai_chat_completion', 'duration_ms': 100.0, 'metadata': {'token_count': 40}}, "
    "{'name': 'openai_chat_completion', 'duration_ms': 300.0, 'metadata': {'token_count': 60}}, "
    "{'name': 'recognition_manager_generate', 'duration_ms': 450.0}]"
)


def test_parse_line_extracts_events_and_batch_size():
    rec = parse_line(SAMPLE)
    assert rec["batch_size"] == 2
    assert len(rec["events"]) == 3
    assert rec["events"][0]["metadata"]["token_count"] == 40


def test_parse_line_returns_none_for_unrelated_line():
    assert parse_line("2026-06-11 - foo - bar - line:1 - INFO - request_start id=x") is None


def test_aggregate_counts_and_sums_by_span_name():
    rec = parse_line(SAMPLE)
    agg = aggregate([rec])
    chat = agg["openai_chat_completion"]
    assert chat["count"] == 2
    assert chat["total_ms"] == 400.0
    assert chat["mean_ms"] == 200.0
    assert chat["total_tokens"] == 100
