166 lines
10 KiB
Python
166 lines
10 KiB
Python
import copy
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
from test_workflow import brief, REGISTRY
|
|
from engine.reference_contract import (BindingDecision, ContractAudit, validate_audit,
|
|
apply_audit, audit_reference_contract, prioritize_functional_geometry, unfinished_geometry,
|
|
RadiusScope, RadiusObservation)
|
|
from engine.quality_pipeline import QualityWorkflow
|
|
from engine.quality_models import Requirement
|
|
from engine.config import QualityConfig
|
|
from engine.job import QualityJob
|
|
|
|
|
|
def setup_contract():
|
|
b = brief()
|
|
b.requirements += [Requirement(id='outline', description='外形贴合机身,底部必须有 R3.20 圆角', stage='base',
|
|
source='explicit', evidence='source_01 图纸', critical=True, envelope_dimension=None),
|
|
Requirement(id='opening', description='摄像头整体开口不遮挡镜头', stage='features',
|
|
source='explicit', evidence='不能挡住摄像头', critical=True, envelope_dimension=None)]
|
|
decisions = [BindingDecision(requirement_id=r.id, action='keep', source_id='', page=None,
|
|
region='', quote='', evidence='保留用户原始功能', incorrect_fragment='', replacement='') for r in b.requirements]
|
|
decisions[1] = BindingDecision(requirement_id='outline', action='correct_binding', source_id='source_01', page=5,
|
|
region='底部材料限制区域;实际外形见 Detail A', quote='R3.20 DO NOT OBSTRUCT WITH METAL',
|
|
evidence='引出线指向材料限制阴影的圆角,不是机身外边界', incorrect_fragment='底部必须有 R3.20 圆角',
|
|
replacement='按 Detail A 实际外轮廓构造底部过渡')
|
|
return b, ContractAudit(bindings=decisions)
|
|
|
|
|
|
class ReferenceContractTests(unittest.TestCase):
|
|
sources = [{'id': 'source_01', 'pages': [2, 5], 'images': ['p2.png', 'p5.png']}]
|
|
|
|
def test_minimal_correction_keeps_function_and_dimensions(self):
|
|
b, a = setup_contract()
|
|
self.assertEqual(validate_audit(a, b, self.sources, '不能挡住摄像头'), [])
|
|
corrected = apply_audit(b, a)
|
|
self.assertEqual(corrected.requirements[0], b.requirements[0])
|
|
self.assertEqual(corrected.requirements[2], b.requirements[2])
|
|
self.assertTrue(corrected.requirements[1].critical)
|
|
self.assertTrue(corrected.requirements[1].description.startswith('外形贴合机身'))
|
|
self.assertNotIn('R3.20', corrected.requirements[1].description)
|
|
|
|
def test_cannot_drop_ids_invent_sources_or_rewrite_user(self):
|
|
for mutate in (lambda a:a.bindings.pop(), lambda a:setattr(a.bindings[1], 'page', 8),
|
|
lambda a:setattr(a.bindings[1], 'source_id', 'invented'),
|
|
lambda a:setattr(a.bindings[1], 'replacement', '圆角 99 mm')):
|
|
b,a=setup_contract();mutate(a)
|
|
self.assertTrue(validate_audit(a,b,self.sources,''))
|
|
b,a=setup_contract()
|
|
self.assertTrue(validate_audit(a,b,self.sources,'底部必须有 R3.20 圆角'))
|
|
|
|
def test_document_page_is_not_a_geometric_dimension(self):
|
|
b,a=setup_contract()
|
|
for reference in ('第2页 Detail A', 'page 2 Detail A'):
|
|
a.bindings[1].replacement=f'依据{reference}构造实际外轮廓'
|
|
self.assertEqual(validate_audit(a,b,self.sources,''),[])
|
|
a.bindings[1].replacement='依据第8页 Detail A 构造实际外轮廓'
|
|
self.assertTrue(validate_audit(a,b,self.sources,''))
|
|
a.bindings[1].replacement='依据第2页 Detail A 构造 2 mm 圆角'
|
|
self.assertTrue(validate_audit(a,b,self.sources,''))
|
|
|
|
def test_material_classifier_cannot_remove_camera_or_explicit_envelope(self):
|
|
b,a=setup_contract()
|
|
a.bindings[2]=a.bindings[1].model_copy(update={'requirement_id':'opening','action':'material_only'})
|
|
self.assertTrue(validate_audit(a,b,self.sources,''))
|
|
|
|
def test_blind_material_evidence_prevents_unsubstantiated_keep(self):
|
|
b,a=setup_contract()
|
|
a.bindings[1].action='keep'
|
|
scope=RadiusScope(observations=[RadiusObservation(source_id='source_01',page=5,radius='3.20',
|
|
target='material_zone',object_description='导电材料限制区域',evidence='引出线落在阴影内凹台阶')])
|
|
self.assertTrue(validate_audit(a,b,self.sources,'不能挡住摄像头',scope))
|
|
a.bindings[1].action='correct_binding'
|
|
self.assertEqual(validate_audit(a,b,self.sources,'不能挡住摄像头',scope),[])
|
|
a.bindings[0]=a.bindings[1].model_copy(update={'requirement_id':'body','action':'material_only'})
|
|
self.assertTrue(validate_audit(a,b,self.sources,''))
|
|
|
|
def test_final_contract_audit_receives_original_page_images(self):
|
|
b,a=setup_contract()
|
|
content=[{'type':'input_image','image_url':'data:image/png;base64,test'}]
|
|
from types import SimpleNamespace
|
|
with tempfile.TemporaryDirectory() as d:
|
|
job=QualityJob(d,'不能挡住摄像头',[],QualityConfig())
|
|
def call(*args, **kw):
|
|
self.assertIn(content[0],args[3]);self.assertEqual(kw['validate'](a),[]);return a
|
|
result=audit_reference_contract(None,job,b,SimpleNamespace(sources=self.sources,content=lambda:content),job.text if hasattr(job,'text') else '不能挡住摄像头',call)
|
|
self.assertTrue((job.path/'reference_binding_audit.json').exists())
|
|
self.assertNotEqual(result,b)
|
|
|
|
def test_resume_reuses_contract_only_for_same_request_sources_and_requirements(self):
|
|
from types import SimpleNamespace
|
|
import hashlib
|
|
b,a=setup_contract();text='不能挡住摄像头'
|
|
corrected=prioritize_functional_geometry(apply_audit(b,a))
|
|
with tempfile.TemporaryDirectory() as d:
|
|
job=QualityJob(d,text,[],QualityConfig())
|
|
store=SimpleNamespace(sources=self.sources,content=lambda:[])
|
|
job.write('restored_reference_contract.json',{'before':b.model_dump(),'audit':a.model_dump(),
|
|
'text_sha256':hashlib.sha256(text.encode()).hexdigest(),'source_hashes':{'source_01':None}})
|
|
with patch('engine.reference_contract.add_material_details'):
|
|
call=__import__('unittest.mock',fromlist=['Mock']).Mock(return_value=a)
|
|
self.assertEqual(audit_reference_contract(None,job,corrected,store,text,call),corrected)
|
|
call.assert_not_called()
|
|
audit_reference_contract(None,job,b,store,text+'修改尺寸',call)
|
|
self.assertTrue(call.called)
|
|
|
|
def test_critical_opening_due_at_first_gate_and_partial_report_names_it(self):
|
|
b,_=setup_contract(); b=prioritize_functional_geometry(b)
|
|
self.assertIn('opening',[r.id for r in b.due('base')])
|
|
self.assertEqual(len(unfinished_geometry(b,[])),3)
|
|
reviewed=[{'stage':'base','review':{'items':[{'requirement_id':'body','status':'pass'}]}}]
|
|
self.assertEqual({r['id'] for r in unfinished_geometry(b,reviewed)},{'outline','opening'})
|
|
self.assertEqual(unfinished_geometry(b,reviewed,True),[])
|
|
|
|
def test_radius_changes_without_success_stop_same_failure(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
w=QualityWorkflow(None,QualityJob(d,'part',[],QualityConfig()),REGISTRY,brief(),'part',[])
|
|
for radius in (3.2, 3.0):
|
|
w.track_failure('execute_batch',{'operations':[{'op':'fillet_edge','params':{'radius':radius}}]},
|
|
{'ok':False,'failed_index':0,'error':'EXECUTION_ERROR: Failed fillet'})
|
|
with self.assertRaisesRegex(RuntimeError,'RepairLimit'):
|
|
w.track_failure('execute_batch',{'operations':[{'op':'fillet_edge','params':{'radius':2.8}}]},
|
|
{'ok':False,'failed_index':0,'error':'EXECUTION_ERROR: Failed fillet'})
|
|
|
|
def test_source_recheck_is_bounded_and_invalidates_old_acceptance(self):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
b,a=setup_contract()
|
|
w=QualityWorkflow(None,QualityJob(d,'part',[],QualityConfig()),REGISTRY,b,'part',[])
|
|
w.reference_store=object();w.stage_index=1;w.reviews=[{'accepted':True}]
|
|
corrected=apply_audit(b,a)
|
|
with patch('engine.quality_pipeline.audit_reference_contract',return_value=corrected) as call:
|
|
for radius in (3.2,3.0):
|
|
result={'ok':False,'failed_index':0,'error':'EXECUTION_ERROR: fillet'}
|
|
w.track_failure('execute_batch',{'operations':[{'op':'fillet_edge','params':{'radius':radius}}]},result)
|
|
call.assert_called_once()
|
|
self.assertTrue(result['source_contract_corrected'])
|
|
self.assertEqual(w.stage_index,0)
|
|
self.assertEqual(w.reviews,[])
|
|
self.assertTrue(w.binding_rechecked)
|
|
|
|
def test_material_detail_crop_preserves_retrieval_metadata(self):
|
|
import hashlib,json
|
|
from engine.reference_contract import add_material_details
|
|
with tempfile.TemporaryDirectory() as d:
|
|
job=QualityJob(d,'part',[],QualityConfig())
|
|
pdf=job.path/'source_01.pdf'
|
|
stream=b'BT /F1 12 Tf 40 700 Td (DO NOT OBSTRUCT WITH CONDUCTIVE MATERIAL R3.20) Tj ET'
|
|
objects=[b'<< /Type /Catalog /Pages 2 0 R >>',b'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
|
|
b'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 800] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>',
|
|
b'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>',
|
|
b'<< /Length '+str(len(stream)).encode()+b' >>\nstream\n'+stream+b'\nendstream']
|
|
raw=b'%PDF-1.4\n';offsets=[0]
|
|
for i,obj in enumerate(objects,1):
|
|
offsets.append(len(raw));raw+=str(i).encode()+b' 0 obj\n'+obj+b'\nendobj\n'
|
|
xref=len(raw);raw+=b'xref\n0 6\n0000000000 65535 f \n'
|
|
raw+=b''.join(f'{v:010d} 00000 n \n'.encode() for v in offsets[1:])
|
|
raw+=f'trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF'.encode();pdf.write_bytes(raw)
|
|
source={'id':'source_01','pages':[1],'sha256':hashlib.sha256(pdf.read_bytes()).hexdigest()}
|
|
job.write('reference_sources.json',{'sources':[source],'queries':['device'],'attempts':2})
|
|
add_material_details(job,[source])
|
|
self.assertEqual(len(source['details']),4)
|
|
add_material_details(job,[source])
|
|
self.assertEqual(len(source['details']),4)
|
|
saved=json.loads((job.path/'reference_sources.json').read_text())
|
|
self.assertEqual(saved['queries'],['device']);self.assertEqual(saved['attempts'],2)
|