"""History display mirrors cannot issue values or replace authorization scopes.""" import copy import io import json import sys import threading import unittest from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts')) from nao_native_answer import (_model_fact_view,_model_history_source_goals,_binding_inventory, _bind_literal_measures,_validate_blocks,_review_blocks,NativeCompositionError) class NativeModelPresentationTests(unittest.TestCase): def fact(self): return {'id':'F1','label':'已确认历史事实','kind':'history','evidenceRef':'Q00000001', 'text':'原始报废重量10.00 kg。','scope':{'period':{'year':2026,'month':9},'filters':{'department':'产业部甲'}, 'stage':'black','dimension':'group','sourceTurnId':'private-owner','metricContextId':'metric-context-1'}, 'validity':{'verified':True,'basis':'protocol_history_snapshot'}, 'modelText':'2026年9月 / 产业部 产业部甲:报废重量10.00 kg。', 'modelScope':{'displayScope':'产业部 产业部甲','stageLabel':'黑片','dimensionLabel':'业务分组'}} def test_verified_mirror_changes_only_model_view(self): fact=self.fact();original=copy.deepcopy(fact) views,scopes=_model_fact_view([fact]) self.assertEqual(views[0]['text'],fact['modelText']);self.assertEqual(scopes[0]['displayScope'],'产业部 产业部甲') self.assertNotIn('filters',scopes[0]);self.assertNotIn('sourceTurnId',scopes[0]);self.assertEqual(fact,original) self.assertEqual(scopes[0]['metricContextId'],'metric-context-1') fact['modelText']='报废重量999.00 kg。' with self.assertRaises(NativeCompositionError): _validate_blocks(_bind_literal_measures({'blocks':[{'role':'core','text':'报废重量999.00 kg。','factIds':['F1']}]}, _binding_inventory([fact]),{}),[fact],_binding_inventory([fact])) def test_unsigned_or_wrong_scope_mirror_cannot_replace_authority(self): for change in ({'validity':{'verified':False,'basis':'protocol_history_snapshot'}}, {'validity':{'verified':True,'basis':'quality_comparison'}}, {'modelScope':{'displayScope':'全公司'}}, {'modelScope':{'displayScope':'产业部甲','filters':{'department':'全公司'}}}): with self.subTest(change=change): fact={**self.fact(),**change};views,scopes=_model_fact_view([fact]) self.assertEqual(views[0]['text'],fact['text']);self.assertEqual(scopes[0]['filters'],{'department':'产业部甲'}) fact=self.fact();fact['modelScope'].update(period={'year':2025},stageLabel='亮片',scopeId='forged') _,scopes=_model_fact_view([fact]) self.assertEqual(scopes[0]['period'],{'year':2026,'month':9});self.assertEqual(scopes[0]['stageLabel'],'黑片') self.assertEqual(scopes[0]['scopeId'],'scope-1') other=copy.deepcopy(fact);other.update(id='F2');other['scope']['sourceTurnId']='other-private-owner' views,scopes=_model_fact_view([fact,other]);self.assertEqual(len(scopes),2) self.assertNotEqual(views[0]['scopeId'],views[1]['scopeId']) def test_native_without_mirror_has_identical_model_scope_and_text(self): fact=self.fact();fact.pop('modelText');fact.pop('modelScope');fact['validity']['basis']='quality_comparison' views,scopes=_model_fact_view([fact]) self.assertEqual(views[0]['text'],fact['text']);self.assertEqual(scopes[0],{'scopeId':'scope-1',**fact['scope']}) def test_review_uses_sanitized_history_goals_without_mutating_internal_goals(self): fact=self.fact();raw=[{'goal':'汇总历史','filters':{'department':'产业部甲'}}] displayed=[{'goal':'汇总历史','scopeLabel':'产业部 产业部甲'}] context={'_autonomousAnswer':True,'_historySourceGoals':copy.deepcopy(raw),'_historyModelSourceGoals':displayed} captured=[] def provider(request,timeout): payload=json.loads(json.loads(request.data)['messages'][1]['content']);captured.append(payload) return io.BytesIO(json.dumps({'choices':[{'finish_reason':'stop','message':{'content':json.dumps({ 'reviews':[{'supported':True,'statisticalClaimsSupported':True}],'missingGoalDescriptions':[]})}}]}).encode()) _review_blocks([{'index':0,'text':'报废重量10.00 kg。','factIds':['F1'],'evidenceRefs':['Q00000001'],'verbatim':False,'claimType':'observation'}], [fact],connection=('https://offline.invalid','fixture','fixture'),cancel_event=threading.Event(),opener=provider, raw_responses=[],usages=[],diagnostics={'issues':[]},context=context) self.assertEqual(captured[0]['historySourceGoals'],displayed);self.assertEqual(context['_historySourceGoals'],raw) unsigned=copy.deepcopy(fact);unsigned['validity']['verified']=False self.assertEqual(_model_history_source_goals(context,[unsigned]),raw) if __name__=='__main__':unittest.main()