"""Tests for the hand-authored rotating-pool manifest builder
(simulator/build_pool_manifest.py) — the per-clip-pair morph model."""

import simulator.build_pool_manifest as b


def _expected_pairs():
    """Every adjacent (higher H, lower L) edge, every member pair, BOTH directions."""
    order = b.RING_ORDER
    n = len(order)
    expected = set()
    for i in range(n):
        H, L = order[i], order[(i + 1) % n]
        for h in b.POOLS[H]:
            for l in b.POOLS[L]:
                expected.add((h, l, f"transitions/{h}__{l}.mp4"))       # zoom in
                expected.add((l, h, f"transitions/{h}__{l}.rev.mp4"))   # zoom out
    return expected


def test_transitions_are_per_clip_pair_both_directions():
    m = b.build_manifest()
    trans = m["ring"]["transitions"]
    expected = _expected_pairs()
    got = {(t["from"], t["to"], t["file"]) for t in trans}
    assert got == expected
    assert len(trans) == len(expected) == 558
    assert all(t["model"] == "placeholder-zoom" for t in trans)


def test_generate_media_bakes_every_member_pair(monkeypatch, tmp_path):
    calls = []

    def fake_run(args, **kw):
        out = args[-1]
        from pathlib import Path
        Path(out).parent.mkdir(parents=True, exist_ok=True)
        Path(out).write_bytes(b"x")
        calls.append(str(out))

        class R:
            returncode = 0

        return R()

    monkeypatch.setattr(b, "MEDIA", tmp_path)
    monkeypatch.setattr(b.subprocess, "run", fake_run)
    monkeypatch.setattr(b, "_ffmpeg", lambda: "ffmpeg")
    b.generate_media()
    outs = {c.split("transitions/")[-1] for c in calls}
    fwd = {f"{h}__{l}.mp4" for H, L in b._adjacent_edges()
           for h in b.POOLS[H] for l in b.POOLS[L]}
    rev = {f"{h}__{l}.rev.mp4" for H, L in b._adjacent_edges()
           for h in b.POOLS[H] for l in b.POOLS[L]}
    assert fwd <= outs and rev <= outs
    assert len(fwd) + len(rev) == 558


def test_transition_cmd_is_all_intra():
    cmd = b.transition_cmd("ffmpeg", "/a/base.mp4", "/b/base.mp4", "/out/x__y.mp4")
    assert "-g" in cmd and cmd[cmd.index("-g") + 1] == "1"
    assert "-keyint_min" in cmd and cmd[cmd.index("-keyint_min") + 1] == "1"
    assert "-sc_threshold" in cmd and cmd[cmd.index("-sc_threshold") + 1] == "0"
    assert "libx264" in cmd


def test_reverse_cmd_is_all_intra():
    cmd = b.reverse_cmd("ffmpeg", "/out/x__y.mp4", "/out/x__y.rev.mp4")
    assert "-g" in cmd and cmd[cmd.index("-g") + 1] == "1"
    assert "reverse" in " ".join(cmd)
