"""Pure authority construction tests; synthetic files, no graph or GPU claims.""" import copy import json from pathlib import Path import tempfile import unittest from unittest.mock import patch import execution_authority as A class AuthorityTests(unittest.TestCase): def setUp(self): self.tmp=tempfile.TemporaryDirectory();self.root=Path(self.tmp.name).resolve() self.case=self.root/'case';self.parent=self.root/'parent';self.update=self.root/'update' for p in (self.case,self.parent,self.update):p.mkdir() self.write(self.parent/'checkpoint-manifest.json',{'fixture':True}) self.write(self.parent/'summary.json',{'steps':1200,'effective_weights_raw_sha256':'a'*64,'diagnostic_mode':'reference'}) self.write(self.case/'spec.json',{'steps':1000,'global_tick_offset':1200, 'parent_checkpoint_manifest_sha256':A.digest(self.parent/'checkpoint-manifest.json')}) (self.case/'requests.npz').write_bytes(b'synthetic-file-not-a-real-npz') (self.parent/'state.npz').write_bytes(b'synthetic-state-not-a-real-npz') self.before=dict(overlay_file=str(self.root/'before.npz'),overlay_manifest=str(self.root/'before.json')) self.after=dict(overlay_file=str(self.root/'after.npz'),overlay_manifest=str(self.root/'after.json')) for p in (*self.before.values(),*self.after.values()):Path(p).write_bytes(b'fixture') self.epoch=self.root/'epoch.json';self.cpu=self.root/'cpu.json' self.write(self.epoch,{});self.write(self.cpu,{}) for n in ('chain-receipt.json','update-receipt.json','delta-after.npz','overlay-manifest.json'): (self.update/n).write_bytes(b'fixture') def tearDown(self):self.tmp.cleanup() def write(self,path,value):path.write_text(json.dumps(value),encoding='utf-8') def make(self,**changes): kw=dict(boundary_id='fixture',registration_sha256='c'*64,case=self.case,parent=self.parent, transition='epoch_update',input_policy='empty_idle',mode='light',before_overlay=self.before, after_overlay=self.after,source_files={'fixture':A.pin(self.cpu)},frozen_inputs={str(self.epoch):A.digest(self.epoch)}, epoch=self.epoch,update_dir=self.update,cpu_receipt=self.cpu) kw.update(changes) # Hash computation is stubbed only here: no effective-weight verification claim. with patch.object(A,'effective_hash',side_effect=['a'*64,'b'*64] if kw['transition']=='epoch_update' else ['a'*64]*2): return A.boundary_authority(**kw) def test_exact_boundary_fields_and_live_payload_pins(self): value=self.make() self.assertEqual(value['global_tick_offset'],1200) self.assertEqual(value['parent']['checkpoint_manifest_sha256'],A.digest(self.parent/'checkpoint-manifest.json')) self.assertEqual(value['update']['files']['delta-after.npz'],A.pin(self.update/'delta-after.npz')) self.assertEqual(value['expected_effective_weight_after_sha256'],'b'*64) def test_changed_clock_or_parent_rejected(self): original=A.read(self.case/'spec.json') for key,value in (('global_tick_offset',1199),('parent_checkpoint_manifest_sha256','0'*64)): spec={**original,key:value};self.write(self.case/'spec.json',spec) with self.subTest(key=key),self.assertRaises(ValueError):self.make() def test_later_parent_uses_absolute_next_tick(self): summary=A.read(self.parent/'summary.json');summary['global_next_tick']=11200 self.write(self.parent/'summary.json',summary) spec=A.read(self.case/'spec.json');spec['global_tick_offset']=11200;self.write(self.case/'spec.json',spec) value=self.make(transition='idle',epoch=None,update_dir=None,cpu_receipt=None) self.assertEqual(value['global_tick_offset'],11200);self.assertIsNone(value['update']) def test_epoch_update_requires_all_evidence(self): for name in ('epoch','update_dir','cpu_receipt'): with self.subTest(name=name),self.assertRaises(ValueError):self.make(**{name:None}) def test_idle_cannot_carry_new_update(self): with self.assertRaisesRegex(ValueError,'Idle cannot'):self.make(transition='idle') def test_boundary_copies_descriptors_not_aliases(self): sources={'role':A.pin(self.cpu)};frozen={str(self.epoch):A.digest(self.epoch)} value=self.make(source_files=sources,frozen_inputs=frozen) sources['role']['sha256']='0'*64;frozen.clear() self.assertNotEqual(value['source_files'],sources);self.assertTrue(value['frozen_inputs']) def test_complete_direct_closure_and_conflicting_pin(self): value=self.make();pins=A.files_from_authority(value) for p in (self.epoch,self.cpu,self.case/'requests.npz',self.parent/'checkpoint-manifest.json',self.update/'delta-after.npz'): self.assertEqual(pins[str(p)],A.digest(p)) value['frozen_inputs'][str(self.cpu)]='0'*64 with self.assertRaisesRegex(ValueError,'Conflicting'):A.files_from_authority(value) def test_epoch_template_is_not_modified(self): spec=A.read(self.case/'spec.json');spec['steps']=1200;self.write(self.case/'spec.json',spec) template=dict(schema='phase4an-epoch-postprocess-authority-v1',files={'old':{'path':'literal','sha256':'1'*64}}, frozen_inputs={'literal':'1'*64}) original=copy.deepcopy(template) result=A.epoch_authority(template,case=self.case,checkpoint=self.parent,overlay=self.before, epoch_id='mechanical',us_tick=1199) self.assertEqual(template,original);self.assertEqual(result['command']['us_tick'],1199) self.assertEqual(result['files']['state'],A.pin(self.parent/'state.npz')) with self.assertRaises(ValueError):A.epoch_authority(template,case=self.case,checkpoint=self.parent, overlay=self.before,epoch_id='bad',us_tick=1200) def test_duplicate_or_nonfinite_JSON_rejected(self): for data in ('{"a":1,"a":2}','{"a":NaN}'): self.cpu.write_text(data) with self.assertRaises(ValueError):A.read(self.cpu) if __name__=='__main__':unittest.main()