"""Continued review stays outside frozen models and old publication bindings."""
import importlib.util
from pathlib import Path

import pytest

from pallet.mask_review_continuation import audit_snapshot

PROJECT = Path(__file__).resolve().parents[1]


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


def test_fresh_source_audit_does_not_overwrite_old_results(tmp_path):
    with pytest.raises(ValueError, match='New private'):
        audit_snapshot(tmp_path)


def test_second_batch_separate_task_and_source_directories():
    module = load('build_mask_under50_requests_v2')
    assert module.OUT != module.SOURCES
    assert module.OUT.name == 'pallet_mask_under50_requests_v2'
    assert module.SOURCES.name == 'pallet_mask_continuation_sources_v2'


def test_second_service_uses_new_port_and_runtime_config():
    module = load('serve_mask_under50_review_v2')
    prior = (module.service.__file__, module.service.TASKS, module.service.STATE,
        module.service.CONFIG, module.service.PORT, module.service.LOCAL)
    try:
        module.configure_module()
        assert module.service.PORT == 3396
        assert module.service.TASKS.name == 'pallet_mask_under50_requests_v2'
        assert module.service.CONFIG.parent.name == 'pallet_mask_under50_managed_v2'
        assert not module.service.CONFIG.is_relative_to(PROJECT/'configs')
    finally:
        (module.service.__file__, module.service.TASKS, module.service.STATE,
            module.service.CONFIG, module.service.PORT, module.service.LOCAL) = prior
