"""任务集读取与静态校验。"""
from __future__ import annotations

import json
from pathlib import Path

from .models import EvalConfigError, EvalSuite
from .scoring import validate_assertion


def load_suite(path: Path) -> EvalSuite:
    try:
        raw = json.loads(path.read_text(encoding="utf-8"))
    except FileNotFoundError as exc:
        raise EvalConfigError(f"suite 文件不存在: {path}") from exc
    except json.JSONDecodeError as exc:
        raise EvalConfigError(
            f"suite JSON 解析失败: {path}:{exc.lineno}:{exc.colno}: {exc.msg}"
        ) from exc
    suite = EvalSuite.from_dict(raw)
    for case in suite.cases:
        for assertion in case.assertions:
            try:
                validate_assertion(assertion)
            except ValueError as exc:
                raise EvalConfigError(f"case {case.id!r}: {exc}") from exc
    return suite
