from tools.pipeline.ffmpeg_ops import frame_args, crossfade_loop_args, transcode_args


def test_frame_args_trims_scales_pads_and_strips_audio():
    args = frame_args(
        "src.mp4", "out.mp4",
        start=2.0, duration=8.0, width=1920, height=1080, ff="ffmpeg",
    )
    assert args[0] == "ffmpeg"
    assert "-ss" in args and args[args.index("-ss") + 1] == "2.0"
    assert "-t" in args and args[args.index("-t") + 1] == "8.0"
    assert "-an" in args                      # audio stripped (bases are video-only)
    vf = args[args.index("-vf") + 1]
    assert "scale=1920:1080:force_original_aspect_ratio=decrease" in vf
    assert "pad=1920:1080" in vf
    assert "format=yuv420p" in vf
    assert args[-1] == "out.mp4"


def test_crossfade_loop_builds_head_mid_tail_xfade_concat():
    args = crossfade_loop_args("in.mp4", "loop.mp4", duration=8.0, overlap=1.0)
    fc = args[args.index("-filter_complex") + 1]
    assert "trim=0:1.0" in fc                       # head = first O secs
    assert "trim=1.0:7.0" in fc                     # mid  = [O, D-O]
    assert "trim=7.0:8.0" in fc                     # tail = last O secs
    assert "xfade=transition=fade:duration=1.0:offset=0" in fc
    assert "concat=n=2:v=1" in fc
    assert "-an" in args
    assert args[-1] == "loop.mp4"


def test_crossfade_loop_rejects_overlap_too_large():
    import pytest
    with pytest.raises(ValueError):
        crossfade_loop_args("in.mp4", "loop.mp4", duration=4.0, overlap=2.0)


def test_crossfade_loop_rejects_nonpositive_overlap():
    import pytest
    with pytest.raises(ValueError):
        crossfade_loop_args("in.mp4", "loop.mp4", duration=8.0, overlap=0.0)


def test_transcode_args_high_profile_even_dims_faststart():
    args = transcode_args("loop.mp4", "proxy.mp4", width=1920, height=1080,
                          crf=20, fps=30)
    assert "-profile:v" in args and args[args.index("-profile:v") + 1] == "high"
    assert "-crf" in args and args[args.index("-crf") + 1] == "20"
    vf = args[args.index("-vf") + 1]
    assert "scale=1920:1080:force_original_aspect_ratio=decrease" in vf
    assert "fps=30" in vf
    assert "+faststart" in args
    assert "-an" in args
    assert args[-1] == "proxy.mp4"
