from __future__ import annotations

from datetime import datetime, timezone

from supply_infra.db.models.pipeline_run import PipelineRun
from supply_infra.pipeline.run_service import serialize_run


def test_manual_run_serializes_null_deadline_and_china_timestamps() -> None:
    run = PipelineRun(
        run_id="manual-run",
        dedupe_key="supply_pipeline:20260727:full",
        pipeline_key="supply_pipeline",
        biz_dt="20260727",
        trigger_type="api",
        run_mode="full",
        dry_run=True,
        status="queued",
        deadline_at=None,
        config_snapshot_json={},
        date_snapshot_json={},
        created_at=datetime(2026, 7, 27, 6, 30),
        updated_at=datetime(2026, 7, 27, 6, 31),
    )

    payload = serialize_run(run)

    assert payload["deadline_at"] is None
    assert payload["created_at"] == "2026-07-27T06:30:00+08:00"
    assert payload["updated_at"] == "2026-07-27T06:31:00+08:00"


def test_aware_timestamp_is_converted_to_china_time() -> None:
    run = PipelineRun(
        run_id="aware-run",
        dedupe_key="supply_pipeline:20260727:aware",
        pipeline_key="supply_pipeline",
        biz_dt="20260727",
        trigger_type="api",
        run_mode="full",
        dry_run=True,
        status="queued",
        deadline_at=None,
        config_snapshot_json={},
        date_snapshot_json={},
        created_at=datetime(2026, 7, 27, 6, 30, tzinfo=timezone.utc),
        updated_at=datetime(2026, 7, 27, 6, 31, tzinfo=timezone.utc),
    )

    payload = serialize_run(run)

    assert payload["created_at"] == "2026-07-27T14:30:00+08:00"
    assert payload["updated_at"] == "2026-07-27T14:31:00+08:00"
