# SPDX-License-Identifier: AGPL-3.0-or-later
"""The full-B trigger arithmetic (performance plan, instrumentation)."""

from __future__ import annotations

import pytest

from orchestrator.projection import (
    FULL_B_CLONE_REPLAY_SECONDS,
    FULL_B_WALL_CLOCK_FRACTION,
    RunProjection,
    full_b_triggered,
)


def _projection(clone_replay: float) -> RunProjection:
    return RunProjection(
        run_id="RUN",
        events=1,
        state_size_bytes=1,
        clone_seconds=clone_replay,
        replay_seconds=0.0,
    )


def test_full_b_trigger_absolute_bound():
    assert not full_b_triggered(_projection(FULL_B_CLONE_REPLAY_SECONDS - 1))
    assert full_b_triggered(_projection(FULL_B_CLONE_REPLAY_SECONDS))


def test_full_b_trigger_takes_the_lower_threshold():
    # A short wall-clock lowers the threshold: 25% of 40 s is 10 s, well under
    # the 60 s absolute bound.
    assert not full_b_triggered(_projection(9.0), wall_clock_seconds=40.0)
    assert full_b_triggered(_projection(10.0), wall_clock_seconds=40.0)
    # A long wall-clock keeps the absolute bound as the lower threshold:
    # 25% of 600 s is 150 s, so 60 s still governs.
    assert not full_b_triggered(_projection(FULL_B_CLONE_REPLAY_SECONDS - 1.0), wall_clock_seconds=600.0)
    assert full_b_triggered(_projection(FULL_B_CLONE_REPLAY_SECONDS), wall_clock_seconds=600.0)


def test_full_b_trigger_without_wall_clock_uses_the_absolute_bound():
    assert not full_b_triggered(_projection(1.0), wall_clock_seconds=None)
    assert full_b_triggered(_projection(FULL_B_CLONE_REPLAY_SECONDS), wall_clock_seconds=None)
    # A non-positive wall-clock is ignored rather than dividing by it.
    assert full_b_triggered(_projection(FULL_B_CLONE_REPLAY_SECONDS), wall_clock_seconds=0.0)
