import importlib.util
import tempfile
import unittest
from pathlib import Path


def load_card_checker():
    script_path = Path(__file__).resolve().parents[1] / "scripts" / "check_card_contract.py"
    spec = importlib.util.spec_from_file_location("check_card_contract", script_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


class CardContractTests(unittest.TestCase):
    def test_missing_required_heading_is_reported(self):
        checker = load_card_checker()
        with tempfile.TemporaryDirectory() as tmp:
            root = Path(tmp)
            (root / "docs").mkdir()
            (root / "cards").mkdir()
            (root / "docs" / "MODEL_CARD_CONTRACT.md").write_text(
                "# Model Card Contract\n\n## Required Sections\n\n## 模型名称\n\n## 调用关键词\n",
                encoding="utf-8"
            )
            (root / "cards" / "qpi.md").write_text(
                "# QPI\n\n## 模型名称\n\nQPI\n",
                encoding="utf-8"
            )

            errors = checker.check_cards(root)

            self.assertEqual(errors, ["cards/qpi.md missing required heading ## 调用关键词"])


if __name__ == "__main__":
    unittest.main()
