"""Check the exercise's functional ground truth, not an agent's review.

Run: python3 grade_fixture.py review_target.py
Only run on these trusted example modules: loading a Python module executes it.
Keep this file outside the reviewer's accessible workspace during an evaluation.
"""
import importlib.util
import json
import sys
from pathlib import Path

CASES = [
    ("partial_refund", (1000, 200, 300), 500),
    ("zero_refund", (1000, 600, 0), 400),
    ("entire_remainder", (1000, 600, 400), 0),
    ("negative_request", (1000, 0, -100), ValueError),
    ("exceeds_remainder", (1000, 600, 500), ValueError),
]


def main():
    source = Path(sys.argv[1]).resolve()
    spec = importlib.util.spec_from_file_location("review_fixture", source)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    results = []
    for case_id, args, expected in CASES:
        try:
            actual = module.remaining_after_refund(*args)
            passed = expected is not ValueError and actual == expected
            observed = actual
        except Exception as error:
            passed = expected is ValueError and type(error) is ValueError
            observed = type(error).__name__
        results.append({"case": case_id, "passed": passed, "observed": observed})
    print(json.dumps({"passed": sum(r["passed"] for r in results),
                      "total": len(results), "cases": results}, indent=2))
    raise SystemExit(0 if all(result["passed"] for result in results) else 1)


if __name__ == "__main__":
    main()
