"""Tests for A1 replayer instrumentation: new timestamp + join fields."""

from __future__ import annotations

import json
from dataclasses import asdict

from replayer.metrics import RequestMetrics


def _make_metric(**overrides) -> RequestMetrics:
    base = dict(
        request_id="sess1:0:42:0",
        session_id="sess1",
        turn_id=0,
        trace_timestamp_s=1.0,
        input_length=1000,
        output_length=50,
        request_type="user",
        effective_input_length=1000,
        cached_tokens=512,
        latency_s=2.5,
        ttft_s=0.5,
        tpot_s=0.04,
    )
    base.update(overrides)
    return RequestMetrics(**base)


def test_request_metrics_has_new_join_fields():
    m = _make_metric()
    fields = asdict(m)
    for key in (
        "t_dispatch_unix",
        "t_first_token_unix",
        "t_finish_unix",
        "proxy_request_id",
        "endpoint_url",
        "trace_hash_ids",
    ):
        assert key in fields, f"missing {key}"


def test_request_metrics_defaults_when_unset():
    m = _make_metric()
    assert m.t_dispatch_unix is None
    assert m.t_first_token_unix is None
    assert m.t_finish_unix is None
    assert m.proxy_request_id is None
    assert m.endpoint_url is None
    assert m.trace_hash_ids == ()


def test_request_metrics_round_trips_through_json():
    m = _make_metric(
        t_dispatch_unix=1700000000.123,
        t_first_token_unix=1700000000.5,
        t_finish_unix=1700000002.0,
        proxy_request_id="sess1:0:42:0",
        endpoint_url="http://node:8000",
        trace_hash_ids=(11, 22, 33),
    )
    payload = json.dumps(asdict(m), sort_keys=True)
    parsed = json.loads(payload)
    assert parsed["t_dispatch_unix"] == 1700000000.123
    assert parsed["t_first_token_unix"] == 1700000000.5
    assert parsed["t_finish_unix"] == 1700000002.0
    assert parsed["proxy_request_id"] == "sess1:0:42:0"
    assert parsed["endpoint_url"] == "http://node:8000"
    assert parsed["trace_hash_ids"] == [11, 22, 33]


def test_proxy_request_id_matches_replayer_request_id():
    m = _make_metric(proxy_request_id="sess1:0:42:0")
    assert m.proxy_request_id == m.request_id
