import sys import unittest from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'scripts')) from nao_evidence_report import build_report,render_report,product_priority_summary from nao_evidence_finalizer import evidence_summaries def comparison(ref='Q00000001', filters=None, rate=.95, month=7): def coverage(): return {'sourceMonths': [{'year': 2026, 'month': selected, 'blackRows': 2, 'brightRows': 2} for selected in range(1, 13)]} return {'evidenceRef':ref,'mode':'compare','result':{'kind':'quality_comparison','filterBinding':filters or {}, 'current':{'period':{'year':2026,'month':month},'compositeRate':rate,'coverage':coverage()}, 'comparison':{'period':{'year':2026,'month':month-1},'compositeRate':.96,'coverage':coverage()}, 'delta':{'available':True,'percentagePoints':(rate-.96)*100}}} def coverage_comparison(): from nao_quality_tools import QualityTools def metrics(month): missing=month==8 return {'summary':{'compositeRate':.9 if missing else .95, 'black':{'rate':1 if missing else .97,'qualifiedWeight':None if missing else 97,'totalWeight':None if missing else 100,'scrapWeight':None if missing else 3}, 'bright':{'rate':.9 if missing else .98,'qualifiedWeight':90 if missing else 98,'totalWeight':100,'scrapWeight':10 if missing else 2}}, 'coverage':{'rawRows':50 if missing else 100,'includedRows':50 if missing else 100, 'sourceMonths':[{'year':2026,'month':month,'blackRows':0 if missing else 50,'brightRows':50}]}} tools=QualityTools.__new__(QualityTools) tools._metrics=lambda release,period,filters,view:metrics(period['month']) return {'mode':'compare','evidenceRef':'Q00000004','result':tools._compare({'id':'synthetic'}, {'year':2026,'month':8},{'year':2026,'month':7},{},None,None)} class BoundReportTest(unittest.TestCase): def test_scope_label_distinguishes_full_value_from_substring_filters(self): from nao_evidence_report import _scope_label label = _scope_label({'columnFilters': [ {'field': 'orderNo', 'stages': ['black'], 'operator': 'in', 'values': ['WO-7']}, {'field': 'orderNo', 'stages': ['bright'], 'operator': 'contains', 'values': ['07']}, ]}) self.assertIn('黑片来源字段为WO-7', label) self.assertIn('亮片来源字段包含片段07', label) def test_priority_summary_keeps_three_signals_and_rate_denominators_separate(self): signals={'scrapIncrease':[{'label':'放量规格','scrapDelta':120}], 'currentScrap':[{'label':'存量规格','current':{'scrapWeight':500}}], 'rateDrop':[{'label':'小量规格','rateDrop':0.8, 'comparison':{'blackInputWeight':400,'brightInputWeight':300}, 'current':{'blackInputWeight':4,'brightInputWeight':3}}]} text=product_priority_summary(signals,None) self.assertIn('放量规格(+120.00 kg)',text) self.assertIn('存量规格(500.00 kg)',text) self.assertIn('小量规格(下降80.00个百分点',text) self.assertIn('黑片投入400.00→4.00 kg',text) self.assertIn('不能相加为总体贡献',text) def test_frozen_group_period_anchor_wins_over_global_regardless_of_order(self): global_doc=comparison('Q00000001',{},.97) local=comparison('Q00000002',{'group':'组A'},.80) another_month=comparison('Q00000003',{'group':'组A'},.70,6) for docs in ([global_doc,local,another_month],[another_month,local,global_doc]): report=build_report(docs,{'year':2026,'month':7,'group':'组A'}) self.assertIn('80.00%',report['headline']);self.assertNotIn('97.00%',report['headline']) self.assertIn('业务分组=组A',report['policy']) self.assertTrue(report['coverageSufficient']) def test_ambiguous_frames_stay_separate_and_cannot_claim_completion(self): report=build_report([comparison('Q00000001',{},.97),comparison('Q00000002',{'group':'组A'},.80)]) self.assertFalse(report['coverageSufficient']) text=render_report(report,[report['facts'][0]['id']],report['missingEvidence']) self.assertIn('97.00%',text);self.assertIn('80.00%',text);self.assertIn('业务分组=组A',text) self.assertIn('多个期间和范围',text) mismatch=build_report([comparison()],{'year':2026,'month':7,'group':'未查组'}) self.assertFalse(mismatch['coverageSufficient']) self.assertIn('未匹配',mismatch['missingEvidence'][0]) def test_annual_and_quarter_anchors_do_not_match_months(self): monthly=comparison('Q00000001',{},.95) annual=comparison('Q00000002',{},.85) annual['result']['current']['period']={'year':2026} annual['result']['comparison']['period']={'year':2025} quarterly=comparison('Q00000003',{},.75) quarterly['result']['current']['period']={'year':2026,'quarter':3} quarterly['result']['comparison']['period']={'year':2026,'quarter':2} docs=[monthly,annual,quarterly] self.assertIn('85.00%',build_report(docs,{'year':2026})['headline']) self.assertIn('75.00%',build_report(docs,{'year':2026,'quarter':3})['headline']) def test_other_period_defects_cannot_borrow_root_column_labels(self): docs=[comparison(),{'evidenceRef':'Q00000003','mode':'defects','result':{ 'kind':'quality_fine_defects','stage':'black','dimension':'defect','filters':{}, 'period':{'year':2026,'month':6},'comparisonPeriod':{'year':2026,'month':5}, 'categoryReconciliation':[], 'defectColumns':['field','category','currentWeight','comparisonWeight','deltaWeight','deltaAvailable'], 'defectValues':[['合成磕边重量','料废',30,10,20,True]]}}] report=build_report(docs) text=render_report(report,[report['facts'][0]['id']],[]) self.assertNotIn('合成磕边重量',text) self.assertIn('其他期间证据未合并',text) self.assertIn('[Q00000003]',text) def test_real_tool_missing_stage_remains_missing_and_raw_value_is_traceable(self): doc=coverage_comparison() self.assertTrue(doc['result']['delta']['available']) report=build_report([doc]) text=render_report(report,[report['facts'][0]['id']],report['missingEvidence']) self.assertTrue(report['coverageSufficient']) self.assertIn('缺失的黑片来源月份:2026-08',text) self.assertNotIn('100.00%',text) self.assertIn('| 黑片合格率 | 97.00% | — | 不可比 |',text) self.assertEqual(report['rawMetrics']['current']['black']['rate'],1) self.assertEqual(doc['result']['current']['black']['rate'],1) def test_unknown_comparison_coverage_stays_incomplete(self): doc=comparison() for side in ('current','comparison'): doc['result'][side].pop('coverage') report=build_report([doc]) self.assertFalse(report['coverageSufficient']) self.assertIn('没有可引用的正式综合合格率', ';'.join(report['missingEvidence'])) self.assertIn('—→', report['headline']) def test_frozen_stage_uses_its_formal_delta_and_other_stage_does_not_block_it(self): doc=coverage_comparison() # The synthetic tool result has bright coverage in both periods, even # though no composite comparison is possible without the black source. report=build_report([doc],{'year':2026,'month':8,'stage':'bright'}) self.assertTrue(report['coverageSufficient']) self.assertIn('亮片合格率',report['headline']) self.assertIn('98.00%→2026年8月90.00%',report['headline']) self.assertIn('下降8.00个百分点',report['headline']) self.assertIn('缺失的黑片来源月份',report['policy']) black=build_report([doc],{'year':2026,'month':8,'stage':'black'}) self.assertFalse(black['coverageSufficient']) self.assertNotIn('100.00%',black['headline']) def test_chapter_leads_are_bound_facts_before_their_data_tables(self): docs=[{'evidenceRef':'Q00000001','result':{'kind':'quality_comparison','filterBinding':{}, 'current':{'period':{'year':2026,'month':9},'compositeRate':0.95, 'black':{'rate':0.97,'totalWeight':100,'qualifiedWeight':97,'defects':[{'name':'责废','weight':12},{'name':'料废','weight':5}]}, 'bright':{'rate':0.98,'totalWeight':100,'qualifiedWeight':98}, 'coverage':{'sourceMonths':[{'year':2026,'month':9,'blackRows':2,'brightRows':2}]}}, 'comparison':{'period':{'year':2026,'month':8},'compositeRate':0.96, 'black':{'rate':0.98,'totalWeight':100,'qualifiedWeight':98,'defects':[{'name':'责废','weight':8},{'name':'料废','weight':4}]}, 'bright':{'rate':0.98,'totalWeight':100,'qualifiedWeight':98}, 'coverage':{'sourceMonths':[{'year':2026,'month':8,'blackRows':2,'brightRows':2}]}}, 'delta':{'percentagePoints':-1},'stageRateChangesPP':{'black':-1,'bright':0}}}, {'evidenceRef':'Q00000002','result':{'kind':'quality_changes','dimension':'group','filters':{}, 'currentPeriod':{'year':2026,'month':9},'comparisonPeriod':{'year':2026,'month':8}, 'rows':[{'label':'改善组','current':{'rate':0.93},'comparison':{'rate':0.90},'rateDrop':-0.03,'scrapDelta':-10}, {'label':'下降组','current':{'rate':0.93},'comparison':{'rate':0.95},'rateDrop':0.02,'scrapDelta':12}]}}] report=build_report(docs) stage=next(s for s in report['sections'] if s.startswith('### 总体与阶段')) self.assertLess(stage.index('黑片合格率:98.00%→97.00%,下降1.00个百分点。[Q00000001]'),stage.index('| 指标')) category=next(s for s in report['sections'] if s.startswith('### 正式报废类别')) self.assertIn('黑片责废:8.00→12.00 kg,增减+4.00 kg。[Q00000001]',category.split('| 类别')[0]) group=next(s for s in report['sections'] if s.startswith('### 业务分组变化')) self.assertIn('本次返回的组织中,下降组',group.split('| 范围')[0]) self.assertNotIn('生产主因', '\n'.join(report['sections'])) def test_actual_wrong_group_directions_and_product_citations_cannot_be_generated(self): root=Path(__file__).resolve().parents[1]/'output/20261023-autonomous-investigation/runtime' paths=list(root.glob('.runtime/nao-users/*/sessions/4c3c5e05-3c81-4c43-acf4-3e70af3ab7c7/evidence')) if not paths:self.skipTest('Private real acceptance evidence not present') docs=evidence_summaries(paths[0],'194b0287-9044-4d80-9138-ecd3c481015c') report=build_report(docs) groups={row['label']:row for row in report['facts'] if row['kind']=='group'} self.assertIn('下降0.38个百分点',groups['工业电机-中国区']['text']) self.assertIn('下降0.05个百分点',groups['工业电机-VCM']['text']) self.assertIn('上升3.12个百分点',groups['工业电机-亚太']['text']) product=next(row for row in report['facts'] if row['kind']=='product') self.assertEqual(product['evidenceRef'],'QE8DBF76C') text=render_report(report,[groups['工业电机-中国区']['id'],groups['工业电机-VCM']['id'],product['id']],[]) self.assertIn('本部报废',text);self.assertIn('VCM亮片仅录合计',text) self.assertNotIn('中国区、VCM、亚太合格率小幅上行',text) with self.assertRaises(ValueError):render_report(report,['F999999'],[]) def test_missing_rates_are_not_imputed_to_zero(self): docs=[{'evidenceRef':'Q00000001','result':{'kind':'quality_comparison','filterBinding':{}, 'current':{'period':{'year':2026,'month':9},'compositeRate':0.95}, 'comparison':{'period':{'year':2026,'month':8},'compositeRate':0.96},'delta':{'percentagePoints':-1}}}, {'evidenceRef':'Q00000002','result':{'kind':'quality_changes','dimension':'group','filters':{}, 'currentPeriod':{'year':2026,'month':9},'comparisonPeriod':{'year':2026,'month':8}, 'rows':[{'label':'新进组','current':{'rate':0.93},'comparison':{'rate':None},'rateDrop':None,'scrapDelta':12}]}}] report=build_report(docs);group=next(row for row in report['facts'] if row['kind']=='group') self.assertIn('不可比',group['text']);self.assertIn('—→93.00%',group['text']);self.assertNotIn('0.00%→',group['text']) def test_rate_drop_product_remains_a_volume_qualified_signal_with_exact_pp(self): root=comparison(month=9) product={'evidenceRef':'Q00000002','result':{'kind':'quality_changes','dimension':'product','stage':None, 'filters':{},'currentPeriod':{'year':2026,'month':9},'comparisonPeriod':{'year':2026,'month':8}, 'sort':'rate_drop','totalRows':3566,'rows':[{'label':'小量规格','rateDrop':0.9943484950890867, 'scrapDelta':6.737,'current':{'rate':0,'scrapWeight':9.095,'blackInputWeight':7.1,'brightInputWeight':39.656}, 'comparison':{'rate':0.9943484950890867,'scrapWeight':2.358,'blackInputWeight':481.48,'brightInputWeight':417.234}}]}} report=build_report([root,product]) section=next(s for s in report['sections'] if s.startswith('### 产品率变化线索')) self.assertNotIn('### 重点产品',section) self.assertIn('下降99.43个百分点',section) self.assertIn('黑片投入481.48→7.10 kg',section) self.assertIn('亮片投入417.23→39.66 kg',section) self.assertIn('不能只凭率降幅确定重点',section) self.assertIn('仅是率变化线索',next(f for f in report['facts'] if f['kind']=='product_rate_signal')['text']) self.assertFalse(any(f['kind']=='product' for f in report['facts'])) def test_stage_rates_and_totals_only_vcm_detail_keep_their_actual_meaning(self): root=Path(__file__).resolve().parents[1]/'output/20261023-autonomous-investigation/runtime' paths=list(root.glob('.runtime/nao-users/*/sessions/24e16a62-0527-4bff-ba9f-535005acbfaf/evidence')) if not paths:self.skipTest('Private real acceptance evidence not present') report=build_report(evidence_summaries(paths[0],'194b0287-9044-4d80-9138-ecd3c481015c')) text=render_report(report,[report['facts'][0]['id']],[]) self.assertIn('黑片业务分组变化',text) self.assertIn('亮片业务分组变化',text) self.assertIn('黑片合格率:比较期→本期',text) self.assertFalse(any(row['kind']=='cross' and 'VCM607+Z' in row['label'] for row in report['facts'])) self.assertIn('VCM亮片仅录合计',text) if __name__=='__main__':unittest.main()