"""Production pacing for the ahead-of-air neural commentary cache."""
from __future__ import annotations

import inspect
import sys
import threading
from pathlib import Path

import pytest

_LIVE = Path(__file__).resolve().parents[2] / "live"
if str(_LIVE) not in sys.path:
    sys.path.insert(0, str(_LIVE))

from tts import live_cache as LC  # noqa: E402


def test_a_new_revision_uses_one_paced_worker_by_default():
    params = inspect.signature(LC.LiveNeuralTtsCache).parameters
    assert params["workers"].default == 1
    assert params["min_request_interval_s"].default >= 0.20


def test_request_slots_are_shared_and_wait_between_cloud_calls(monkeypatch):
    cache = object.__new__(LC.LiveNeuralTtsCache)
    cache._lock = threading.Lock()
    cache._min_request_interval_s = 0.12
    cache._next_request_at = 0.0
    ticks = iter((10.0, 10.05))
    sleeps = []
    monkeypatch.setattr(LC.time, "monotonic", lambda: next(ticks))
    monkeypatch.setattr(LC.time, "sleep", sleeps.append)

    cache._wait_for_request_slot()
    cache._wait_for_request_slot()

    assert sleeps == [pytest.approx(0.07)]
    assert cache._next_request_at == pytest.approx(10.24)
