from pathlib import Path import hashlib,json import pytest from tools import build_product_gui as gui @pytest.fixture(params=['6400-front-rear','6400-v2','mrdimm-chamber']) def build_context(tmp_path,monkeypatch,request): product=request.param;root=tmp_path/'source';root.mkdir() (root/'VERSION').write_text('2.1.0\n') monkeypatch.setattr(gui,'ROOT',root) monkeypatch.setattr(gui,'source_identity',lambda _: {'source_kind':'exported','clean':None,'commit':'a'*40,'tree':'b'*40}) monkeypatch.setattr(gui,'generate_named_target',lambda *a,**kw:None) inputs={} for name in ('model.v','project.json','closure.json'): (root/name).write_text(name);inputs[name]=gui.sha(root/name) monkeypatch.setattr(gui.td,'input_manifest',lambda _:inputs) monkeypatch.setattr(gui.td,'HARDWARE_PROFILES',{product:{'project':'project.json','closure':'closure.json'}}) generated=root/'generated'/gui.PRODUCTS[product]['generated'];generated.mkdir(parents=True) (generated/'contract-sha256.txt').write_text('c'*64+'\n') vendor=root/'tools/_vendor';vendor.mkdir(parents=True) for name in ('hth_protocol','prod2u_status'): (vendor/(name+'.py')).write_bytes(b'codec fixture') (vendor/(name+'.sha256')).write_text(gui.sha(vendor/(name+'.py'))) bit=tmp_path/'fixture.bit';bit.write_bytes(b'unit fixture, not a hardware bitstream') receipt={'profile':product,'status':'PASS','production':True,'simulation_only':False,'timing_pass_25mhz':True, 'warning_audit':{'status':'PASS'},'build_parameters':{'host_baud':460800},'commit':'d'*40,'tree':'e'*40,'inputs':inputs, 'source_manifest_sha256':hashlib.sha256(json.dumps(inputs,sort_keys=True,separators=(',',':')).encode()).hexdigest(), 'project_sha256':inputs['project.json'],'source_closure_sha256':inputs['closure.json'],'bitstream_sha256':gui.sha(bit)} path=tmp_path/'receipt.json';path.write_text(json.dumps(receipt)) return product,root,bit,path,receipt def test_product_build_identity_uses_export_and_real_bit_hash(build_context): product,_,bit,path,_=build_context meta=gui.prepare_identity(product,bit,path) assert meta['source_kind']=='exported' and meta['commit']=='a'*40 assert meta['bitstream_source_commit']=='d'*40 and meta['bitstream_source_tree']=='e'*40 assert meta['bitstream_sha256']==gui.sha(bit) assert meta['product']==product assert meta['host_baud']==460800 and meta['release_version']=='2.1.0' @pytest.mark.parametrize('change',['profile','failed','timing','bit','source','missing_input','project_identity']) def test_gui_rejects_unqualified_or_mismatched_bit(build_context,change): product,root,bit,path,receipt=build_context if change=='profile':receipt['profile']='wrong-product' elif change=='failed':receipt['status']='FAIL' elif change=='timing':receipt['timing_pass_25mhz']=False elif change=='bit':bit.write_bytes(b'changed') elif change=='source':(root/'model.v').write_text('changed') elif change=='missing_input':receipt['inputs']=dict(receipt['inputs']);receipt['inputs'].pop('model.v') else:receipt['project_sha256']='0'*64 path.write_text(json.dumps(receipt)) with pytest.raises(ValueError):gui.prepare_identity(product,bit,path)