"""Synthetic pinned-profile installation and rollback-path contracts.""" import hashlib import io from pathlib import Path import tempfile import unittest from unittest.mock import patch from basalt_tuning import install_profile,profile_path,profile_ready class TuningTests(unittest.TestCase): def recipe(self,data=b'synthetic routing metadata'): return dict(tuning=dict(expert_profile=dict(file='fixture.prof',url='https://raw.githubusercontent.com/example/pinned/fixture.prof',size=len(data),sha256=hashlib.sha256(data).hexdigest()))) def test_install_verify_and_idempotent(self): with tempfile.TemporaryDirectory() as tmp: data=b'synthetic routing metadata';r=self.recipe(data) with patch('basalt_tuning.urllib.request.urlopen',return_value=io.BytesIO(data)) as fetch: p=install_profile(tmp,r);self.assertTrue(profile_ready(tmp,r));self.assertEqual(p.read_bytes(),data) self.assertEqual(install_profile(tmp,r),p);fetch.assert_called_once() p.write_bytes(b'corrupt');self.assertFalse(profile_ready(tmp,r)) with self.assertRaises(ValueError):install_profile(tmp,r) def test_wrong_hash_leaves_no_installed_file(self): with tempfile.TemporaryDirectory() as tmp,patch('basalt_tuning.urllib.request.urlopen',return_value=io.BytesIO(b'bad')): with self.assertRaises(ValueError):install_profile(tmp,self.recipe()) self.assertFalse(profile_ready(tmp,self.recipe())) def test_legacy_recipe_retains_upstream_path(self): self.assertEqual(profile_path('/runtime',{}),Path('/runtime/source/data/profiles/qwen4exp-48x512.prof')) self.assertTrue(profile_ready('/runtime',{})) def test_traversal_and_symlink_rejected(self): with tempfile.TemporaryDirectory() as tmp: r=self.recipe();r['tuning']['expert_profile']['file']='../outside.prof' with self.assertRaises(ValueError):install_profile(tmp,r) (Path(tmp)/'elsewhere').mkdir();(Path(tmp)/'profiles').symlink_to(Path(tmp)/'elsewhere') with self.assertRaises(ValueError):install_profile(tmp,self.recipe()) if __name__=='__main__':unittest.main()