"""Fourth-round scope and read-only acceptance output guards."""
import importlib.util
from pathlib import Path
import sys

import pytest

PROJECT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT/'scripts'))


def load(name):
    spec = importlib.util.spec_from_file_location(name+'_scope_test', PROJECT/'scripts'/(name+'.py'))
    module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module)
    return module


def test_completed_class20_is_outside_fourth_round_scope():
    module = load('build_mask_under50_requests_v4')
    assert module.CLASSES == (11, 12, 16, 17, 19)
    assert 20 not in module.CLASSES
    assert module.OUT.name == 'pallet_mask_under50_requests_v4'
    assert module.SOURCES.name == 'pallet_mask_continuation_sources_v6'


def test_acceptance_rejects_unapproved_output_before_reading_inputs(tmp_path):
    checker = load('check_mask_supplement_runtime')
    with pytest.raises(ValueError, match='Fresh acceptance'):
        checker.check(4, tmp_path/'evidence', tmp_path/'source-audit')


@pytest.mark.parametrize('batch', [True, 0, 1, 2, 3, '4'])
def test_public_checker_requires_explicit_integer_new_batch(batch, tmp_path):
    checker = load('check_mask_supplement_runtime')
    with pytest.raises(ValueError, match='Explicit'):
        checker.check(batch, tmp_path/'evidence', tmp_path/'source-audit')


def test_ui_checker_does_not_overwrite_previous_evidence(tmp_path):
    checker = load('check_mask_supplement_ui')
    with pytest.raises(ValueError, match='fresh'):
        checker.check(4, tmp_path)
