# -*- coding: utf-8 -*-
"""task-2644 fixture loader (conftest 외부) — pytest 가 상위 conftest.py 와 충돌하지 않도록 분리."""
from __future__ import annotations

import json
from pathlib import Path
from typing import Dict, Optional


FIXTURE_ROOT = Path(__file__).resolve().parents[2] / "fixtures" / "callback_control_plane"

FIXTURE_NAMES = [
    "merge_ready",
    "critical7",
    "auto_remediation",
    "sibling_incomplete",
    "sibling_final",
    "log_only_fail",
    "callback_received_misuse",
    "fallback_safety_net_log_recovery_without_control_plane_adjudication",
]


def _read_json(path: Path) -> Optional[Dict]:
    if not path.is_file():
        return None
    return json.loads(path.read_text(encoding="utf-8"))


def _read_text(path: Path) -> str:
    if not path.is_file():
        return ""
    return path.read_text(encoding="utf-8")


def load_fixture(name: str) -> Dict:
    base = FIXTURE_ROOT / name
    assert base.is_dir(), f"fixture missing: {name}"
    return {
        "name": name,
        "envelope": _read_json(base / "envelope.json"),
        "expected": _read_json(base / "expected.json"),
        "ledger_entry": _read_json(base / "ledger_entry.json"),
        "last_output_text": _read_text(base / "last_output_text.txt"),
        "provenance": _read_text(base / "PROVENANCE.md"),
    }
