"""Block B B2 — `step_running_timeout_seconds` config field.

Plan v2.1.3 / spec V5 §AC-C12: B4 (`_evaluate_running_state`) 의 timeout 임계값.
default 1h (3600s). env override 가능.
"""
from __future__ import annotations


def test_default_is_3600_seconds():
    """default 1h — Block C atomic claim steal 의 일반적 운영 기준."""
    from app.core.config import settings

    assert settings.step_running_timeout_seconds == 3600


def test_can_override_via_env(monkeypatch):
    """ENV STEP_RUNNING_TIMEOUT_SECONDS 으로 override 가능 (운영 튜닝)."""
    monkeypatch.setenv("STEP_RUNNING_TIMEOUT_SECONDS", "7200")
    from app.core.config import Settings

    s = Settings()
    assert s.step_running_timeout_seconds == 7200


# B4 follow-up — startup validator: v <= 0 fail-fast.
# Block C atomic claim 후 v=0/음수 가 모든 running row 를 즉시 stale 분류 →
# multi-worker race / lock 사고. 잘못된 ENV 를 startup 에서 차단.


def test_zero_timeout_raises_validation_error(monkeypatch):
    import pytest
    from pydantic import ValidationError

    monkeypatch.setenv("STEP_RUNNING_TIMEOUT_SECONDS", "0")
    from app.core.config import Settings

    with pytest.raises(ValidationError) as exc_info:
        Settings()
    assert "STEP_RUNNING_TIMEOUT_SECONDS" in str(exc_info.value)


def test_negative_timeout_raises_validation_error(monkeypatch):
    import pytest
    from pydantic import ValidationError

    monkeypatch.setenv("STEP_RUNNING_TIMEOUT_SECONDS", "-1")
    from app.core.config import Settings

    with pytest.raises(ValidationError) as exc_info:
        Settings()
    assert "STEP_RUNNING_TIMEOUT_SECONDS" in str(exc_info.value)
