"""Tests for ai_trade heartbeat payload builder."""
import json

import pytest


def test_trader_payload_market_open():
    from ai_trade.heartbeat import build_trader_payload

    p = json.loads(build_trader_payload("market_open", signals=2))
    assert p["name"] == "ai_trade"
    assert p["kind"] == "trader"
    assert p["state"] == "market_open"
    assert p["ts"].endswith("Z")
    assert p["jobs_done"] == 2


def test_trader_payload_market_closed():
    from ai_trade.heartbeat import build_trader_payload

    p = json.loads(build_trader_payload("market_closed"))
    assert p["name"] == "ai_trade"
    assert p["kind"] == "trader"
    assert p["state"] == "market_closed"
    assert p["jobs_done"] == 0
    assert p["jobs_failed"] == 0
    assert p["last_job_at"] is None


def test_trader_payload_ts_format():
    """ts 필드가 ISO 8601 UTC 형식 (YYYY-MM-DDTHH:MM:SSZ)인지 확인."""
    from ai_trade.heartbeat import build_trader_payload
    import re

    p = json.loads(build_trader_payload("market_open"))
    assert re.match(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z", p["ts"]), (
        f"ts={p['ts']!r} does not match expected UTC format"
    )
