"""ORCH-016 / AC-13 + AC-22: fmt_duration formatting contract.

Pure-function tests for the duration formatter used by build_status_comment.
No DB, no I/O — just the table in ADR-001 §8 / AC-13.
"""

import os

os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")

from src.usage import fmt_duration  # noqa: E402


# ---------------------------------------------------------------------------
# TC-21: table-driven happy path (AC-13)
# ---------------------------------------------------------------------------
def test_fmt_duration_boundary_table():
    cases = [
        (0,     "0s"),
        (12,    "12s"),
        (59,    "59s"),
        (60,    "1m 00s"),
        (252,   "4m 12s"),
        (3599,  "59m 59s"),
        (3600,  "1h 00m"),
        (3780,  "1h 03m"),
        (10020, "2h 47m"),
    ]
    for seconds, expected in cases:
        assert fmt_duration(seconds) == expected, (
            f"fmt_duration({seconds}) -> {fmt_duration(seconds)!r}; expected {expected!r}"
        )


# ---------------------------------------------------------------------------
# TC-22: None / negative -> empty string (caller drops the line) (AC-13)
# ---------------------------------------------------------------------------
def test_fmt_duration_none_returns_empty():
    assert fmt_duration(None) == ""


def test_fmt_duration_negative_returns_empty():
    assert fmt_duration(-1) == ""
    assert fmt_duration(-3600) == ""


def test_fmt_duration_garbage_returns_empty():
    # Non-coercible input must not raise (defensive).
    assert fmt_duration("abc") == ""
    assert fmt_duration([1, 2]) == ""


def test_fmt_duration_float_seconds_truncated():
    # int(12.9) == 12 — integer truncation, not rounding.
    assert fmt_duration(12.9) == "12s"
    assert fmt_duration(61.4) == "1m 01s"


# ---------------------------------------------------------------------------
# Caller contract: empty string => the 'Длительность:' line is NOT printed.
# build_status_comment is unit-tested in test_status_comment_format; here we
# just sanity-check the helper used to gate that decision.
# ---------------------------------------------------------------------------
def test_empty_string_is_falsy():
    assert not fmt_duration(None)
    assert not fmt_duration(-5)
    assert fmt_duration(0)  # "0s" IS truthy: AC-13 wants the line printed
