"""Tests for coverage_planner clamp behavior — Phase 2 of coverage-pass-planner-retry-fix."""

import sys
from pathlib import Path

# Path setup so these tests can import workspace + pipeline modules
_RECOIL_ROOT = Path(__file__).resolve().parent.parent
_PIPELINE_ROOT = _RECOIL_ROOT / "pipeline"
for _p in [str(_RECOIL_ROOT), str(_PIPELINE_ROOT)]:
    if _p not in sys.path:
        sys.path.insert(0, _p)

from orchestrator.coverage_planner import _build_segment, _build_wildcard_segment


def _make_shot(duration_s):
    """Minimal shot dict understood by _build_segment."""
    return {
        "shot_id": "SH001",
        "source_text": "test",
        "routing_data": {"target_editorial_duration_s": duration_s},
        "prompt_data": {
            "shot_type": "MS",
            "duration_s": duration_s,
            "prompt_skeleton": {},
        },
        "asset_data": {},
        "spatial_data": {},
    }


def test_planner_clamps_sub_minimum_duration_up():
    """A 2.5s shot with seeddance-2.0 (min=4) should clamp up to 4."""
    shot = _make_shot(2.5)
    seg = _build_segment(shot, 0, model="seeddance-2.0")
    assert seg.duration_s == 4


def test_planner_preserves_in_range_duration():
    """A 6s shot with seeddance-2.0 (min=4, max=15) should stay at 6."""
    shot = _make_shot(6)
    seg = _build_segment(shot, 0, model="seeddance-2.0")
    assert seg.duration_s == 6


def test_planner_clamps_above_max():
    """A 20s shot with seeddance-2.0 (max=15) should clamp down to 15."""
    shot = _make_shot(20)
    seg = _build_segment(shot, 0, model="seeddance-2.0")
    assert seg.duration_s == 15


def test_wildcard_clamps_to_model_min():
    """Default wildcard is 3s; seeddance-2.0 min is 4s — must clamp up to 4 so
    the validator doesn't BLOCK at --lock time."""
    wc = _build_wildcard_segment("WREN", model="seeddance-2.0")
    assert wc.duration_s >= 4
    assert wc.is_wildcard is True
