# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Verify offline examples and that fixture tests reject common mistakes."""

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(ROOT / "reference"), str(ROOT / "checks")]
from bdf2 import start, step
from check_port import fixed_cases, read_cases, run_checks
from make_cases import build


def explicit_start(f, J, t, y, h):
    return tuple(a+h*b for a, b in zip(y, f(t, y), strict=True))


def endpoint_euler(f, J, t, previous, current, h):
    return start(f, J, t, current, h)


def old_time(f, J, t, previous, current, h):
    return step(f, J, t-h, previous, current, h)


def swapped_history(f, J, t, previous, current, h):
    return step(f, J, t, current, previous, h)


if __name__ == "__main__":
    actual, stored = json.loads(json.dumps(build())), read_cases()
    # Metadata must regenerate exactly. run_checks compares endpoints at their fixture tolerances.
    for record in (actual, stored):
        for case in record["cases"]:
            del case["expected"]
    assert actual == stored
    assert run_checks(start, step) == 0
    for label, starter, advance in [
        ("explicit startup", explicit_start, step), ("BDF1 repeated", start, endpoint_euler),
        ("old RHS time", start, old_time), ("swapped history", start, swapped_history),
    ]:
        failures = sum(not ok for ok, _ in fixed_cases(starter, advance))
        assert failures, label
        print(f"PASS {label}: rejected by {failures} fixture(s)")
