"""Explore reach sampler (#1476) — pure unit tests for `_reach_sample`, which picks
a distance-rank spread so the neighbour pool handed to MMR spans near→mid-far and
the walk can escape a dense cluster. No DB. `_reach_sample` only indexes rows, so
plain ints stand in for the (ImageRecord, ...) tuples."""
from backend.app.services.gallery_service import _reach_sample


def test_reach_zero_or_negative_passes_through():
    rows = list(range(1000))
    assert _reach_sample(rows, 40, 0.0) is rows
    assert _reach_sample(rows, 40, -1.0) is rows


def test_small_pool_passes_through():
    # n <= want (limit*8 = 320) → nothing to reach into.
    rows = list(range(50))
    assert _reach_sample(rows, 40, 1.0) is rows


def test_higher_reach_reaches_deeper_ranks():
    rows = list(range(1000))
    near = _reach_sample(rows, 40, 0.2)
    far = _reach_sample(rows, 40, 1.0)
    # Both keep the nearest rank (stride starts at 0) so you can still tag the cluster.
    assert near[0] == 0
    assert far[0] == 0
    # But higher reach samples genuinely farther ranks.
    assert max(far) > max(near)
    assert max(far) >= 900          # reach=1 spans (almost) the whole pool
    assert max(near) <= 550         # reach=0.2 stays in the near half
    # Never runs past the pool.
    assert max(far) <= len(rows) - 1
