from __future__ import annotations

import pytest

from core.agent.event_bridge import map_internal_event


def test_map_run_started_event() -> None:
    event = {"kind": "run_started", "session_id": "s1"}

    mapped = map_internal_event(event)

    assert mapped == {"type": "run.started", "run_id": "s1"}


def test_map_message_delta_event() -> None:
    event = {"kind": "message_delta", "message_id": "m1", "delta": "hello"}

    mapped = map_internal_event(event)

    assert mapped == {"type": "message.delta", "message_id": "m1", "delta": "hello"}


def test_map_tool_events() -> None:
    started = {
        "kind": "tool_started",
        "message_id": "m2",
        "tool_name": "asr_fun_asr",
    }
    completed = {
        "kind": "tool_completed",
        "message_id": "m2",
        "tool_name": "asr_fun_asr",
        "result": "ok",
    }

    mapped_started = map_internal_event(started)
    mapped_completed = map_internal_event(completed)

    assert mapped_started["type"] == "tool.started"
    assert mapped_started["tool_name"] == "asr_fun_asr"
    assert mapped_completed["type"] == "tool.completed"
    assert mapped_completed["result"] == "ok"


def test_map_run_completed_event() -> None:
    event = {"kind": "run_completed", "session_id": "s1", "output": "done"}

    mapped = map_internal_event(event)

    assert mapped == {"type": "run.completed", "run_id": "s1", "output": "done"}


def test_map_unknown_event_raises() -> None:
    with pytest.raises(ValueError):
        map_internal_event({"kind": "unknown"})


def test_map_event_missing_required_field_raises_value_error() -> None:
    with pytest.raises(ValueError):
        map_internal_event({"kind": "message_delta", "message_id": "m1"})
