"""V14 live-representative preseal semantic regressions."""

from __future__ import annotations

import copy
from pathlib import Path
import sys

import pytest
from pydantic import JsonValue

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from scripts.verify_nutricoach_v150_preseal_v14 import (
    PRESEAL,
    VerificationDenied,
    load_document,
    validate_bound_contract,
)


def _documents() -> tuple[dict[str, JsonValue], ...]:
    target = load_document(PRESEAL / "sealed-target.json")
    package_path = target["permission_package"]
    assert isinstance(package_path, str)
    return (
        target,
        load_document(Path(package_path)),
        load_document(PRESEAL / "package-supersession.json"),
        load_document(PRESEAL / "registry-shape.json"),
    )


def test_v14_contract_denies_synthetic_one_customer_shape() -> None:
    target, package, supersession, registry_shape = _documents()
    altered = copy.deepcopy(registry_shape)
    altered["customer_count"] = 1
    altered["disabled_customer_keys"] = []

    with pytest.raises(VerificationDenied, match="registry_shape"):
        validate_bound_contract(target, package, supersession, altered)


def test_v14_contract_denies_v13_authority_root() -> None:
    target, package, supersession, registry_shape = _documents()
    altered = copy.deepcopy(target)
    altered["global_approval_ledger"] = str(
        altered["global_approval_ledger"]
    ).replace("live-authorization-v14", "live-authorization-v13")

    with pytest.raises(VerificationDenied, match="v14_paths"):
        validate_bound_contract(altered, package, supersession, registry_shape)
