"""History-only aliases must be attested by their own signed source task.""" import copy,sys,threading,unittest from pathlib import Path from unittest.mock import patch sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts')) import nao_native_answer as n from nao_history_report import _history_title_source_goals,_history_title_is_scope_safe class HistoryTitleSourceAliasTests(unittest.TestCase): def fixture(self,component='3C'): filters={'department':'消费电子产业部','group':'消费电子-'+component} task={'question':'9月'+component+'不良Top5','goal':'2026年9月消费电子-'+component+'产品规格不良排名', 'period':{'year':2026,'month':9},'filters':filters} sections=[{'sourceTurnId':'sep','period':task['period'],'filters':filters}, {'sourceTurnId':'aug','period':{'year':2026,'month':8},'filters':{}}] snapshot={'sections':sections,'historySources':[{'turnId':'sep','available':True,'status':'COMPLETED','taskContext':task}, {'turnId':'aug','available':True,'status':'COMPLETED','taskContext':{'question':'8月全公司Top3','period':sections[1]['period'],'filters':{}}}]} return snapshot def check(self,snapshot,title): report={'facts':[{'id':'F'+str(i),'kind':'product','label':'规格','text':'历史事实', 'scope':s,'validity':{'verified':True,'basis':'protocol_history_snapshot'}} for i,s in enumerate(snapshot['sections'],1)]} context={'_historyTitleExcludedScopeLiterals':['消费电子产业部',snapshot['sections'][0]['filters'].get('group','')], '_historySourceGoals':_history_title_source_goals(snapshot)} return n._verified_content_title(title,report,'生成报告',n._content_title_guide(report,context)) def test_source_task_component_alias_is_period_bound_not_a_specific_word(self): for component in ('3C','AB'): snapshot=self.fixture(component);title='9月'+component+'与8月全公司规格报废Top榜单' self.assertEqual(self.check(snapshot,title),title) self.assertEqual(_history_title_is_scope_safe(title,snapshot,'生成报告'),title) def test_unattested_component_wrong_period_and_partial_report_are_rejected(self): for question in ('9月消费电子-AB不良Top5','9月消费电子AB不良Top5'): snapshot=self.fixture('AB');snapshot['historySources'][0]['taskContext']['question']=question self.assertIsNone(self.check(snapshot,'9月AB与8月全公司报废榜单')) self.assertIsNone(_history_title_is_scope_safe('9月AB与8月全公司报废榜单',snapshot,'生成报告')) snapshot=self.fixture() for title in ('8月3C与9月全公司报废榜单','9月3C报废榜单','8—9月3C报废榜单'): self.assertIsNone(self.check(snapshot,title)) def test_same_period_ambiguous_alias_cannot_choose_an_owner(self): snapshot=self.fixture('AB');other=copy.deepcopy(snapshot['sections'][0]);other.update(sourceTurnId='other',filters={'group':'汽车交通-AB','department':'汽车交通产业部'}) snapshot['sections'].append(other);snapshot['historySources'].append({'turnId':'other','available':True,'status':'COMPLETED', 'taskContext':{'question':'9月AB不良','period':other['period'],'filters':other['filters']}}) self.assertIsNone(self.check(snapshot,'9月AB与8月全公司报废榜单')) def test_no_customer_spec_or_raw_source_results_alias_authority(self): for field in ('product','extraFilters'): snapshot=self.fixture();filters={'product':'规格-3C'} if field=='product' else {'extraFilters':{'客户':['客户-3C']}} snapshot['sections'][0]['filters']=filters;snapshot['historySources'][0]['taskContext']['filters']=filters self.assertIsNone(self.check(snapshot,'9月3C与8月全公司报废榜单')) snapshot=self.fixture();task=snapshot['historySources'][0].pop('taskContext');snapshot['sourceResults']=[{'sourceTurnId':'sep','task':task}] self.assertEqual(len(_history_title_source_goals(snapshot)),1) self.assertIsNone(self.check(snapshot,'9月3C与8月全公司报废榜单')) def test_wrong_source_filters_and_period_cannot_register_the_component(self): for key,value in (('period',{'year':2026,'month':8}),('filters',{'group':'别的-3C'})): snapshot=self.fixture();snapshot['historySources'][0]['taskContext'][key]=value self.assertIsNone(self.check(snapshot,'9月3C与8月全公司报废榜单')) def test_actual_history_adapter_forwards_visible_source_question_to_title_registry(self): from verification.test_nao_history_report import ranking_history_snapshot,report_task,authority,RELEASE,TURN snapshot=ranking_history_snapshot();section=snapshot['sections'][0] snapshot['historySources'][0]['taskContext']={'question':'9月3C不良Top5','period':section['period'],'filters':section['filters']} seen=[] def inspect(**kwargs): guide=n._content_title_guide(kwargs['report'],kwargs['context']);seen.append(guide) return {'titleValid':False} with patch.object(n,'_synthesize_verified_report',side_effect=inspect): n.synthesize_history_snapshot(base_url='https://offline.invalid',api_key='fixture',model='fixture',question='生成报告', snapshot=snapshot,task=report_task(),authority=authority(),release_id=RELEASE,request_turn_id=TURN,cancel_event=threading.Event()) self.assertEqual(seen[0]['scopeContexts'][0]['aliases']['3C'],'消费电子-3C') if __name__=='__main__':unittest.main()