import json import sys import tempfile import unittest from pathlib import Path from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "src" if str(SRC) not in sys.path: sys.path.insert(0, str(SRC)) import aman_maint import aman_model_sync class AmanMaintTests(unittest.TestCase): def test_parse_args_sync_default_model_command(self): args = aman_maint.parse_args( [ "sync-default-model", "--report", "benchmarks/results/latest.json", "--artifacts", "benchmarks/model_artifacts.json", "--constants", "src/constants.py", "--check", ] ) self.assertEqual(args.command, "sync-default-model") self.assertEqual(args.report, "benchmarks/results/latest.json") self.assertEqual(args.artifacts, "benchmarks/model_artifacts.json") self.assertEqual(args.constants, "src/constants.py") self.assertTrue(args.check) def test_main_dispatches_sync_default_model_command(self): with patch("aman_model_sync.sync_default_model_command", return_value=7) as handler: exit_code = aman_maint.main(["sync-default-model"]) self.assertEqual(exit_code, 7) handler.assert_called_once() def test_sync_default_model_command_updates_constants(self): with tempfile.TemporaryDirectory() as td: report_path = Path(td) / "latest.json" artifacts_path = Path(td) / "artifacts.json" constants_path = Path(td) / "constants.py" report_path.write_text( json.dumps({"winner_recommendation": {"name": "test-model"}}), encoding="utf-8", ) artifacts_path.write_text( json.dumps( { "models": [ { "name": "test-model", "filename": "winner.gguf", "url": "https://example.invalid/winner.gguf", "sha256": "a" * 64, } ] } ), encoding="utf-8", ) constants_path.write_text( ( 'MODEL_NAME = "old.gguf"\n' 'MODEL_URL = "https://example.invalid/old.gguf"\n' 'MODEL_SHA256 = "' + ("b" * 64) + '"\n' ), encoding="utf-8", ) args = aman_maint.parse_args( [ "sync-default-model", "--report", str(report_path), "--artifacts", str(artifacts_path), "--constants", str(constants_path), ] ) exit_code = aman_model_sync.sync_default_model_command(args) self.assertEqual(exit_code, 0) updated = constants_path.read_text(encoding="utf-8") self.assertIn('MODEL_NAME = "winner.gguf"', updated) self.assertIn('MODEL_URL = "https://example.invalid/winner.gguf"', updated) self.assertIn('MODEL_SHA256 = "' + ("a" * 64) + '"', updated) def test_sync_default_model_command_check_mode_returns_2_on_drift(self): with tempfile.TemporaryDirectory() as td: report_path = Path(td) / "latest.json" artifacts_path = Path(td) / "artifacts.json" constants_path = Path(td) / "constants.py" report_path.write_text( json.dumps({"winner_recommendation": {"name": "test-model"}}), encoding="utf-8", ) artifacts_path.write_text( json.dumps( { "models": [ { "name": "test-model", "filename": "winner.gguf", "url": "https://example.invalid/winner.gguf", "sha256": "a" * 64, } ] } ), encoding="utf-8", ) constants_path.write_text( ( 'MODEL_NAME = "old.gguf"\n' 'MODEL_URL = "https://example.invalid/old.gguf"\n' 'MODEL_SHA256 = "' + ("b" * 64) + '"\n' ), encoding="utf-8", ) args = aman_maint.parse_args( [ "sync-default-model", "--report", str(report_path), "--artifacts", str(artifacts_path), "--constants", str(constants_path), "--check", ] ) exit_code = aman_model_sync.sync_default_model_command(args) self.assertEqual(exit_code, 2) updated = constants_path.read_text(encoding="utf-8") self.assertIn('MODEL_NAME = "old.gguf"', updated) if __name__ == "__main__": unittest.main()