"""Typing contract test helpers."""

from __future__ import annotations

import sys
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path

import pytest

ViolationTuple = tuple[str, str, int, int, str]


@pytest.fixture(scope="session")
def typing_contract_violations() -> list[ViolationTuple]:
    repo_root = Path(__file__).resolve().parents[2]
    checker_path = repo_root / "scripts" / "check_no_wildcard_types.py"
    spec = spec_from_file_location("check_no_wildcard_types", checker_path)
    if spec is None or spec.loader is None:
        msg = f"Could not load checker module at {checker_path}"
        raise RuntimeError(msg)
    module = module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)

    violations = module.scan_sources(
        repo_root=repo_root,
        src_root=repo_root / "src" / "auen",
        allowlist_path=repo_root / "scripts" / "typing_contract_allowlist.txt",
    )
    return [(v.kind, v.path, v.line, v.column, v.message) for v in violations]
