"""_claim_running is the single-run guard that both the scheduled fire and the manual trigger go
through (at the top of _job): holding _activity_lock across the check-and-set means exactly one
caller can hold a job's "running" claim at a time, and it's claimable again once released."""
from app import scheduler

_JOB = "unit_test_claim"  # not a real job id — exercises the primitive without touching live jobs


def _release():
    scheduler._record(_JOB, running=False)


def test_claim_is_exclusive_until_released():
    _release()
    # First caller claims it.
    assert scheduler._claim_running(_JOB) is True
    # A second caller is refused while the first still holds it (the TOCTOU the fix closes).
    assert scheduler._claim_running(_JOB) is False
    assert scheduler._claim_running(_JOB) is False
    # _job's finally releases the claim...
    _release()
    # ...and now it can be claimed again.
    assert scheduler._claim_running(_JOB) is True
    _release()
