"""★야외 최소 closure 의 계측 단위 (2026-09-03 · Codex): blanket single 금지 — loop 를 읽어 적은 단위·호출 수."""
from __future__ import annotations

import pytest

from tools.grounding_audit import canary_run as cr


class TestTheFourOutdoorStepsAreVerified:
    def test_units_come_from_the_loops(self):
        assert cr.metering_unit_of("background_classify") == cr.UNIT_SINGLE
        assert cr.metering_unit_of("outdoor_place_spec") == cr.UNIT_OUTDOOR_GROUP
        assert cr.metering_unit_of("outdoor_lane_plan") == cr.UNIT_OUTDOOR_GROUP
        assert cr.metering_unit_of("outdoor_structure_form_reference") == cr.UNIT_OUTDOOR_GROUP

    def test_calls_per_unit_are_read_from_production(self):
        assert cr.calls_per_unit_of("background_classify") == 3
        assert cr.calls_per_unit_of("outdoor_place_spec") == cr._static_int_constant(
            "app/modules/pipeline/outdoor_place_spec.py", "MAX_ATTEMPTS")
        assert cr.calls_per_unit_of("outdoor_lane_plan") == 3
        assert cr.calls_per_unit_of("outdoor_structure_form_reference") == 5
        assert cr.normal_calls_per_unit_of("outdoor_structure_form_reference") == 3

    def test_the_group_cap_is_declared_by_the_fixture(self):
        dims = cr.dims_of("period_episode") if hasattr(cr, "dims_of") else None
        if dims is None:
            import tests.grounding.fixtures.period_episode as fx
            assert fx.OUTDOOR_GROUP_CAP == 2
            return
        assert dims["outdoor_group_cap"] == 2
        assert cr.logical_cap_of("outdoor_place_spec", dims) == 2
        assert cr.hard_cap_of("outdoor_structure_form_reference", dims) == 10

    def test_an_undeclared_group_cap_stops_before_the_provider(self):
        with pytest.raises(cr.ScopeMismatch):
            cr.logical_cap_of("outdoor_lane_plan", {"scenes": 4, "shots": 10, "fan_out_cap": 10,
                                                    "outdoor_group_cap": None})

    def test_the_render_step_stays_unverified_and_the_seed_is_declared(self):
        """★2026-09-03 08:50 뒤집음: seed 는 한 run 야외 통합의 target 이라 loop 를 읽어 선언했다(`test_one_run_outdoor_integration_units`).
        background_render 는 아직 closure 밖 — unverified 그대로. (background_master_plan 은 05:30 chain_bg 그룹 단위로 선언)"""
        assert cr.metering_unit_of("background_render") == cr.UNIT_UNVERIFIED
        assert cr.metering_unit_of("outdoor_structure_seed") == cr.UNIT_OUTDOOR_GROUP
        assert cr.metering_unit_of("background_master_plan") == cr.UNIT_CHAIN_GROUP
