# apps/api/tests/test_aider_event_models.py | 2026-04-20 @ Asia/Taipei
from datetime import datetime, timezone, timedelta
import pytest
from pydantic import ValidationError
from src.models.aider import AiderEventIn, AiderBatchIn

TAIPEI = timezone(timedelta(hours=8))


def _base(t="session_start", payload=None):
    return {
        "ts": datetime(2026, 4, 20, 10, 0, tzinfo=TAIPEI).isoformat(),
        "session_id": "01J7XYZABC",
        "host": "ogt-mac",
        "type": t,
        "payload": payload or {},
    }


def test_accepts_all_7_types():
    for t in ("session_start", "file_edit", "error", "commit",
              "silent_timeout", "session_end", "raw"):
        ev = AiderEventIn(**_base(t=t))
        assert ev.type == t


def test_rejects_unknown_type():
    with pytest.raises(ValidationError):
        AiderEventIn(**_base(t="bogus"))


def test_rejects_missing_session_id():
    d = _base(); del d["session_id"]
    with pytest.raises(ValidationError):
        AiderEventIn(**d)


def test_rejects_missing_ts():
    d = _base(); del d["ts"]
    with pytest.raises(ValidationError):
        AiderEventIn(**d)


def test_batch_max_50():
    with pytest.raises(ValidationError):
        AiderBatchIn(events=[_base() for _ in range(51)])


def test_batch_accepts_1():
    b = AiderBatchIn(events=[_base()])
    assert len(b.events) == 1


def test_host_default():
    d = _base(); del d["host"]
    ev = AiderEventIn(**d)
    assert ev.host == "ogt-mac"
