import json,sys,unittest from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts')) import nao_quality_runtime as runtime class TimelineTests(unittest.TestCase): def test_real_text_tools_results_are_interleaved_and_reasoning_is_private(self): a=runtime.ConversationAccumulator() a.feed({'type':'reasoning-delta','delta':'hidden chain'}) a.feed({'type':'text-delta','id':'intro','delta':'先核查期间与覆盖。'}) call={'type':'tool-input-available','toolName':'mcp_call','toolCallId':'q1','input':{'tool':'quality_data','arguments':{'mode':'defects','stage':'black','apiKey':'secret'}}} a.feed(call); a.feed(call) a.feed({'type':'tool-output-available','toolCallId':'q1','output':{'evidenceRef':'Q123','rows':[{'weight':12}], 'categoryReconciliation':[{'reconciliationStatus':'MISMATCH'}]}}) a.feed({'type':'text-delta','id':'final','delta':'来源缺陷只作为变化线索。'}) self.assertEqual([e['kind'] for e in a.timeline],['assistant','tool_start','tool_result','assistant']) self.assertEqual([e['sequence'] for e in a.timeline],[1,2,3,4]) encoded=json.dumps(a.timeline,ensure_ascii=False) self.assertNotIn('hidden chain',encoded); self.assertNotIn('secret',encoded) self.assertIn('Q123',encoded); self.assertIn('未对账',encoded) def test_unmatched_result_does_not_invent_a_tool_start(self): a=runtime.ConversationAccumulator();a.feed({'type':'tool-output-available','toolCallId':'missing','output':{'rows':[]}}) self.assertEqual(a.timeline,[]) def test_resumed_provider_text_block_stays_after_the_tool(self): a=runtime.ConversationAccumulator() a.feed({'type':'text-delta','id':'same','delta':'先查询。'}) a.feed({'type':'tool-input-available','toolName':'read','toolCallId':'r1'}) a.feed({'type':'tool-output-available','toolCallId':'r1','output':{}}) a.feed({'type':'text-delta','id':'same','delta':'已查询。'}) self.assertEqual([e['kind'] for e in a.timeline],['assistant','tool_start','tool_result','assistant']) self.assertEqual(a.timeline[0]['text'],'先查询。') self.assertEqual(a.timeline[-1]['text'],'已查询。') def test_comparison_side_reconciliation_is_also_disclosed_and_null_is_safe(self): text=runtime.public_tool_result({'categoryReconciliation':None,'comparisonCategoryReconciliation':[{'reconciliationStatus':'DIFF'}],'truncated':True}) self.assertIn('未对账',text);self.assertIn('仅覆盖返回集合',text) text=runtime.public_tool_result({'evidenceRef':'QABC12345','summary':{'compositeRate':0.95},'token':'private'}) self.assertIn('QABC12345',text);self.assertNotIn('private',text);self.assertIn('95.00%',text) if __name__=='__main__': unittest.main() class InvestigationEvidenceStatusTests(unittest.TestCase): def test_expected_vcm_is_not_an_unresolved_warning_but_non_vcm_remains(self): from nao_quality_runtime import public_evidence_checks base={'kind':'quality_investigation','releaseId':'r','stage':'bright','period':{'year':2026,'month':9},'filters':{'group':'工业电机-VCM'},'current':{'categories':[{'category':'镀废','expectedTotalsOnly':{'rows':419},'unexpectedDifferenceRows':0,'requiresFurtherInvestigation':False}],'sourceVerification':{'complete':True,'allSelectedValuesMatchStored':True}}} self.assertEqual(list(public_evidence_checks(base).values()),['expected']) base['current']['categories'][0].update(unexpectedDifferenceRows=12,requiresFurtherInvestigation=True) self.assertEqual(list(public_evidence_checks(base).values()),['unresolved']) def test_source_verification_failure_cannot_be_hidden_by_vcm_policy(self): from nao_quality_runtime import public_evidence_checks r={'kind':'quality_investigation','current':{'categories':[{'category':'镀废','expectedTotalsOnly':{'rows':419},'unexpectedDifferenceRows':0,'requiresFurtherInvestigation':False}],'sourceVerification':{'complete':False}}} self.assertEqual(list(public_evidence_checks(r).values()),['unresolved'])