"""Single-period and mixed historical report composition tests.""" import io import json import tempfile import sys import threading import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) from nao_agent_report import build_agent_report, build_noncomparison_report import nao_evidence_finalizer as finalizer RELEASE = 'release-verified' def task(question, *, kind='metrics', period=None, filters=None, stages=None, fields=None, history_mode='none'): return {'version': 1, 'question': question, 'kind': kind, 'historyMode': history_mode, 'filters': filters or {}, 'period': period, 'comparisonPeriod': None, 'dimensions': [], 'stages': stages or [], 'fields': fields or [], 'verifySource': False, 'fullDetails': False} def metric_doc(*, month=9, product='产品甲', rate=.81, black_rate=.84, bright_rate=.78, black_rows=2, bright_rows=2, evidence='A'): period = {'year': 2026, 'month': month} filters = {'product': product} if product else {} coverage = {'sourceMonths': [{'year': 2026, 'month': month, 'blackRows': black_rows, 'brightRows': bright_rows}]} summary = {'compositeRate': rate, 'black': {'rate': black_rate, 'totalWeight': 123.25, 'scrapWeight': 3.5}, 'bright': {'rate': bright_rate, 'totalWeight': 88.75, 'scrapWeight': 2.25}} return {'evidenceRef': 'Q' + evidence * 8, 'mode': 'query', 'result': {'kind': 'quality_metrics', 'releaseId': RELEASE, 'period': period, 'filters': filters, 'view': 'overall', 'summary': summary, 'coverage': coverage}} def comparison_doc(*, month=9, product='产品甲', evidence='B'): def snapshot(selected_month, rate): return {'period': {'year': 2026, 'month': selected_month}, 'compositeRate': rate, 'black': {'rate': .9, 'totalWeight': 100, 'scrapWeight': 2}, 'bright': {'rate': .9, 'totalWeight': 100, 'scrapWeight': 2}, 'coverage': {'sourceMonths': [{'year': 2026, 'month': selected_month, 'blackRows': 2, 'brightRows': 2}]}} return {'evidenceRef': 'Q' + evidence * 8, 'mode': 'compare', 'result': {'kind': 'quality_comparison', 'releaseId': RELEASE, 'filterBinding': {'product': product}, 'current': snapshot(month, .92), 'comparison': snapshot(month - 1, .9), 'delta': {'available': True, 'percentagePoints': 2}, 'stageRateChangesPP': {'black': 0, 'bright': 0}}} class NoncomparisonReportTest(unittest.TestCase): def test_leaf_only_report_has_a_real_core_conclusion(self): facts=[{'id':'F1','kind':'defect','text':'缺陷甲来源重量12.50 kg。','evidenceRef':'QAAAAAAAA'}, {'id':'F2','kind':'defect','text':'缺陷乙来源重量8.00 kg。','evidenceRef':'QAAAAAAAA'}] answer,selected=finalizer._focused_report_answer(facts,['F1','F2']) self.assertIn('## 核心结论\n\n- 缺陷甲',answer) self.assertIn('## 优先关注\n\n- 缺陷乙',answer) self.assertEqual(selected,['F1','F2']) def context(self, question, **updates): value = {'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {'product': '产品甲'}, '_agentTask': task(question, period={'year': 2026, 'month': 9}, filters={'product': '产品甲'}, fields=['rate'])} value['_agentTask'].update(updates) return value def test_single_query_uses_bound_quality_metrics_not_a_stale_generic_query(self): question = '查询产品甲本月合格率' fresh = metric_doc(rate=.81) stale = {'evidenceRef': 'Q' + 'C' * 8, 'mode': 'query', 'result': {'kind': 'quality_query', 'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {'product': '产品甲'}, 'summary': {'compositeRate': .99}}} report = build_noncomparison_report([stale, fresh], self.context(question), question) rendered = report['headline'] + '\n' + '\n'.join(report['sections']) self.assertIn('81.00%', rendered) self.assertNotIn('99.00%', rendered) self.assertEqual(report['facts'][0]['evidenceRef'], fresh['evidenceRef']) self.assertEqual(report['coverageSufficient'], True) def test_explicit_stage_and_fields_do_not_add_unrequested_numeric_facts(self): question = '只看黑片投入重量' context = self.context(question) context['_agentTask'].update({'stages': ['black'], 'fields': ['totalWeight']}) report = build_noncomparison_report([metric_doc()], context, question) texts = [fact['text'] for fact in report['facts']] self.assertEqual(len(texts), 1) self.assertIn('黑片投入重量:123.25 kg', texts[0]) self.assertNotIn('亮片', texts[0]) self.assertNotIn('综合', texts[0]) def test_fieldless_report_includes_composite_and_stage_input_scrap(self): question = '生成质量指标报告' context = {'releaseId': RELEASE, 'filters': {'product': '产品甲'}, '_agentTask': task(question, kind='report', history_mode='all')} report = build_noncomparison_report([metric_doc()], context, question) labels = [fact['label'] for fact in report['facts']] self.assertEqual(labels, ['综合合格率', '黑片合格率', '黑片投入重量', '黑片报废重量', '亮片合格率', '亮片投入重量', '亮片报废重量']) self.assertIn('81.00%', report['sections'][0]) self.assertIn('123.25 kg', report['sections'][0]) def test_quarter_frame_uses_all_three_server_coverage_months(self): question = '查询产品甲第三季度合格率' doc = metric_doc(month=9, rate=.81) doc['result']['period'] = {'year': 2026, 'quarter': 3} doc['result']['coverage']['sourceMonths'] = [ {'year': 2026, 'month': month, 'blackRows': 2, 'brightRows': 2} for month in (7, 8, 9)] context = {'releaseId': RELEASE, 'period': {'year': 2026, 'quarter': 3}, 'filters': {'product': '产品甲'}, '_agentTask': task(question, period={'year': 2026, 'quarter': 3}, filters={'product': '产品甲'}, fields=['rate'])} report = build_noncomparison_report([doc], context, question) self.assertIn('2026年第3季度', report['sections'][0]) self.assertIn('81.00%', report['facts'][0]['text']) self.assertTrue(report['coverageSufficient']) def test_multiple_noncomparison_frames_remain_separately_scoped(self): question = '整合历史统计生成报告' context = {'releaseId': RELEASE, '_agentTask': task(question, kind='report', history_mode='all')} first = metric_doc(month=9, product='产品甲', rate=.81, evidence='1') second = metric_doc(month=7, product='产品乙', rate=.76, evidence='2') report = build_noncomparison_report([first, second], context, question) self.assertEqual(len(report['sections']), 2) text = '\n'.join(report['sections']) self.assertIn('产品甲', text) self.assertIn('产品乙', text) self.assertIn('81.00%', text) self.assertIn('76.00%', text) def test_missing_stage_coverage_remains_partial_and_does_not_show_rate(self): question = '查询黑片合格率' context = self.context(question) context['_agentTask'].update({'stages': ['black'], 'fields': ['rate']}) doc = metric_doc(black_rows=0) report = build_noncomparison_report([doc], context, question) self.assertFalse(report['coverageSufficient']) self.assertTrue(report['missingEvidence']) self.assertIn('黑片合格率:未提供', report['facts'][0]['text']) self.assertNotIn('84.00%', report['facts'][0]['text']) def test_period_and_scope_mismatch_is_not_replaced_with_another_frame(self): question = '查询产品甲本月合格率' context = self.context(question) with self.assertRaises(ValueError): build_noncomparison_report([metric_doc(month=8)], context, question) class MixedHistoryReportTest(unittest.TestCase): def test_comparison_history_appends_a_distinct_single_period_scope(self): question = '整合前述分析生成报告' context = {'releaseId': RELEASE, '_agentTask': task(question, kind='report', history_mode='all')} comparison = comparison_doc(product='产品甲') historic_metric = metric_doc(month=7, product='产品乙', rate=.81, evidence='D') report = build_agent_report([comparison, historic_metric], context, question) text = report['headline'] + '\n' + '\n'.join(report['sections']) self.assertIn('产品甲', text) self.assertIn('产品乙', text) self.assertIn('81.00%', text) self.assertTrue(any(fact['evidenceRef'] == historic_metric['evidenceRef'] for fact in report['facts'])) def test_metrics_already_inside_comparison_are_not_duplicated(self): question = '整合前述分析生成报告' context = {'releaseId': RELEASE, '_agentTask': task(question, kind='report', history_mode='all')} comparison = comparison_doc(product='产品甲') duplicate = metric_doc(month=9, product='产品甲', rate=.92, evidence='E') report = build_agent_report([comparison, duplicate], context, question) refs = [fact['evidenceRef'] for fact in report['facts']] self.assertNotIn(duplicate['evidenceRef'], refs) self.assertEqual(report['coverageSufficient'], True) def test_verified_source_and_rows_summaries_are_appended_without_raw_paths(self): question = '整合前述分析生成报告' context = {'releaseId': RELEASE, '_agentTask': task(question, kind='report', history_mode='all')} comparison = comparison_doc() investigations = [ {'evidenceRef': 'Q' + 'F' * 8, 'mode': 'investigate', 'result': {'kind': 'quality_investigation', 'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'stage': 'black', 'filters': {}, 'action': 'source', 'current': {'context': {'releaseId': RELEASE, 'year': 2026, 'month': 9, 'stage': 'black'}, 'totalRows': 205, 'aggregateComplete': True, 'sourceVerification': {'complete': True, 'allSelectedValuesMatchStored': True, 'expectedRows': 205, 'verifiedRows': 205, 'missingRows': 0, 'mismatchRows': 0, 'files': [{'sourceFile': '/private/raw.xlsx'}]}}}}, {'evidenceRef': 'Q' + 'A' * 8, 'mode': 'investigate', 'result': {'kind': 'quality_investigation', 'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'stage': 'bright', 'filters': {}, 'action': 'rows', 'current': {'context': {'releaseId': RELEASE, 'year': 2026, 'month': 9, 'stage': 'bright'}, 'matchingRows': 12, 'offset': 0, 'returnedRows': 10, 'hasMore': True, 'recordFilter': 'differences', 'rows': [{'sensitive': 'must not render'}]}}} ] report = build_agent_report([comparison, *investigations], context, question) rendered = '\n'.join(report['sections']) self.assertIn('所选原表值与已存储原始值全部匹配且核验完整', rendered) self.assertIn('已核验205行/应核验205行', rendered) self.assertIn('共12条', rendered) self.assertIn('差异记录', rendered) self.assertNotIn('/private/raw.xlsx', rendered) self.assertNotIn('must not render', rendered) self.assertIn('QFFFFFFFF', rendered) self.assertIn('QAAAAAAAA', rendered) table = rendered.split('### 原表核验与明细读取', 1)[1].split('\n### ', 1)[0] table_rows = [line for line in table.splitlines() if line.startswith('|')] self.assertTrue(table_rows) self.assertEqual(len(table_rows[0].split('|')), 7) self.assertTrue(all(len(line.split('|')) == 7 for line in table_rows[1:])) def test_full_details_preserve_all_four_rows_and_blank_values_without_paths(self): question = '读取9月黑片全部明细' detail_task = task(question, kind='details', period={'year': 2026, 'month': 9}, filters={}, stages=['black'], fields=[]) detail_task['fullDetails'] = True context = {'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {}, '_agentTask': detail_task} records = [ {'rowNumber': 11, 'product': '规格甲', 'sourceFile': '/private/source.xlsx', 'values': {'数值字段': 0}}, {'rowNumber': 12, 'product': '规格乙', 'sourceFile': '/private/source.xlsx', 'values': {'空值字段': None}}, {'rowNumber': 13, 'product': '规格丙', 'sourceFile': '/private/source.xlsx', 'values': {'文本字段': '已保留'}}, {'rowNumber': 14, 'product': '规格丁', 'sourceFile': '/private/source.xlsx', 'values': {'重量字段': 2.5}}, ] document = {'evidenceRef': 'Q' + '6' * 8, 'mode': 'investigate', 'result': {'kind': 'quality_investigation', 'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {}, 'stage': 'black', 'category': None, 'action': 'rows', 'current': {'context': {'releaseId': RELEASE, 'year': 2026, 'month': 9, 'stage': 'black', 'category': None, 'includeUnassigned': False}, 'matchingRows': 4, 'offset': 0, 'returnedRows': 4, 'hasMore': False, 'recordFilter': 'all', 'rows': records}}} with tempfile.TemporaryDirectory() as temp_dir: evidence_path = Path(temp_dir) / ('nao-quality-' + '6' * 32 + '.json') evidence_path.write_text(json.dumps({'tool': 'quality_data', 'mode': 'investigate', 'releaseId': RELEASE, 'evidenceRef': document['evidenceRef'], 'apiSource': {'base': '/private/source.xlsx'}, 'result': document['result']}, ensure_ascii=False)) evidence_path.chmod(0o600) summaries = finalizer.evidence_summaries(Path(temp_dir), RELEASE) page = summaries[0]['result']['current'] self.assertEqual(len(page['differenceRecords']), 4) self.assertEqual(page['differenceRecords'][0]['values'], {'数值字段': 0}) self.assertEqual(page['differenceRecords'][1]['values'], {'空值字段': None}) self.assertNotIn('sourceFile', json.dumps(summaries, ensure_ascii=False)) self.assertNotIn('/private/source.xlsx', json.dumps(summaries, ensure_ascii=False)) summarized_doc = {**document, 'result': summaries[0]['result']} report = build_agent_report([summarized_doc], context, question) rendered = '\n'.join(report['sections']) detail_table = rendered.split('### 2026年9月 · 黑片原表行明细', 1)[1] table_lines = [line for line in detail_table.splitlines() if line.startswith('|')] self.assertEqual(len(table_lines[0].split('|')), 6) self.assertEqual(len(table_lines), 6) for row_number in ('11', '12', '13', '14'): self.assertIn(row_number + ' [Q66666666]', detail_table) self.assertIn('| 0 |', detail_table) self.assertIn('| 未提供 |', detail_table) self.assertIn('已保留', detail_table) self.assertIn('2.5', detail_table) self.assertNotIn('/private/source.xlsx', rendered) self.assertTrue(report['coverageSufficient']) def test_source_only_request_can_render_exact_verified_proof_without_kpi(self): question = '核对本月黑片原表' source_task = task(question, kind='source_verify', period={'year': 2026, 'month': 9}, filters={}, stages=['black'], fields=[], history_mode='none') source_task['verifySource'] = True context = {'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {}, '_agentTask': source_task} proof = {'complete': True, 'allSelectedValuesMatchStored': True, 'expectedRows': 12, 'verifiedRows': 12, 'missingRows': 0, 'mismatchRows': 0} evidence = [{'evidenceRef': 'Q' + '9' * 8, 'mode': 'investigate', 'result': {'kind': 'quality_investigation', 'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {}, 'stage': 'black', 'action': 'source', 'current': {'context': {'releaseId': RELEASE, 'year': 2026, 'month': 9, 'stage': 'black'}, 'totalRows': 12, 'aggregateComplete': True, 'sourceVerification': proof}}}] report = build_agent_report(evidence, context, question) self.assertTrue(report['coverageSufficient']) self.assertIn('已核验12行/应核验12行', report['sections'][0]) self.assertFalse(report['rawMetrics']) def test_source_only_history_with_mismatched_proof_stays_partial(self): question = '整合前述分析生成报告' context = {'releaseId': RELEASE, '_agentTask': task(question, kind='report', history_mode='all')} evidence = [{'evidenceRef': 'Q' + '8' * 8, 'mode': 'investigate', 'result': {'kind': 'quality_investigation', 'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {}, 'stage': 'black', 'action': 'source', 'current': {'context': {'releaseId': RELEASE, 'year': 2026, 'month': 9, 'stage': 'black'}, 'totalRows': 12, 'aggregateComplete': True, 'sourceVerification': {'complete': True, 'allSelectedValuesMatchStored': True, 'expectedRows': 12, 'verifiedRows': 11, 'missingRows': 1, 'mismatchRows': 0}}}}] report = build_agent_report(evidence, context, question) self.assertFalse(report['coverageSufficient']) self.assertIn('原表核验未完整通过', report['sections'][0]) self.assertTrue(report['missingEvidence']) class ReportTitleTest(unittest.TestCase): question = '整合前述分析生成报告' def run_synthesis(self, parsed=None, *, timed_out=False, capture=None): context = {'releaseId': RELEASE, '_agentTask': task(self.question, kind='report', history_mode='all')} def opener(request, timeout): if timed_out: raise TimeoutError('synthetic timeout') if capture is not None: capture['body'] = json.loads(request.data) body = {'choices': [{'finish_reason': 'stop', 'message': {'content': json.dumps(parsed or {}, ensure_ascii=False)}}]} return io.BytesIO(json.dumps(body, ensure_ascii=False).encode()) return finalizer.synthesize(base_url='https://offline.invalid/v1', api_key='synthetic', model='compatible/test', question=self.question, evidence=[comparison_doc()], context=context, cancel_event=threading.Event(), opener=opener) @staticmethod def composed(title): return {'reportTitle': title, 'answerBlocks': [{'type': 'paragraph', 'text': '{{F1}}'}], 'evidenceSufficient': True, 'missingEvidence': []} def test_model_title_is_accepted_when_bound_to_scope_period_and_topic(self): title = '2026年8月至9月 产品甲 合格率变化' captured = {} result = self.run_synthesis(self.composed(title), capture=captured) self.assertEqual(result['reportTitle'], title) self.assertTrue(result['titleGeneratedByModel']) selector = json.loads(captured['body']['messages'][1]['content']) self.assertIn('2026年8月', selector['reportTitleGuide']['actualPeriods']) self.assertIn('规格=产品甲', selector['reportTitleGuide']['actualScopes']) self.assertIn('合格率', selector['reportTitleGuide']['topics']) self.assertIn('reportTitle', captured['body']['messages'][0]['content']) def test_question_as_title_is_replaced_with_neutral_evidence_title(self): result = self.run_synthesis(self.composed(self.question)) self.assertNotEqual(result['reportTitle'], self.question) self.assertFalse(result['titleGeneratedByModel']) self.assertIn('2026年', result['reportTitle']) def test_natural_title_paraphrase_does_not_require_fixed_topic_words(self): title = '2026年8—9月质量变化与重点规格报告' result = self.run_synthesis(self.composed(title)) self.assertEqual(result['reportTitle'], title) self.assertTrue(result['titleGeneratedByModel']) def test_malformed_unsafe_or_unbound_numeric_titles_fall_back(self): for title in ('', 'https://example.test 2026年产品甲合格率', 'sk-abcdef0123456789 2026年产品甲合格率', '2026年8月\n产品甲合格率', '2027年8月产品甲合格率'): with self.subTest(title=title): result = self.run_synthesis(self.composed(title)) self.assertFalse(result['titleGeneratedByModel']) self.assertNotIn('<', result['reportTitle']) self.assertNotIn('https://', result['reportTitle']) self.assertNotIn('2027', result['reportTitle']) def test_valid_model_title_survives_answer_block_fallback(self): title = '2026年8月至9月 产品甲 合格率变化' result = self.run_synthesis({ **self.composed(title), 'answerBlocks': [{'type': 'paragraph', 'text': '未绑定的99%结论{{F1}}'}], }) self.assertTrue(result['selectionFallback']) self.assertEqual(result['reportTitle'], title) self.assertTrue(result['titleGeneratedByModel']) def test_brief_metric_fallback_renders_only_requested_bound_fact(self): question = '查询产品甲本月综合合格率' context = {'releaseId': RELEASE, 'period': {'year': 2026, 'month': 9}, 'filters': {'product': '产品甲'}, '_agentTask': task(question, period={'year': 2026, 'month': 9}, filters={'product': '产品甲'}, fields=['rate'])} context['_agentTask'].update({'kind': 'metrics', 'responseStyle': 'brief'}) evidence = [metric_doc(rate=.81, evidence='7')] parsed = {'reportTitle': '2026年9月 产品甲 合格率', 'answerBlocks': [{'type': 'paragraph', 'text': '未绑定结论99%:{{F1}}'}], 'evidenceSufficient': True, 'missingEvidence': []} def opener(request, timeout): body = {'choices': [{'finish_reason': 'stop', 'message': {'content': json.dumps(parsed, ensure_ascii=False)}}]} return io.BytesIO(json.dumps(body, ensure_ascii=False).encode()) result = finalizer.synthesize(base_url='https://offline.invalid/v1', api_key='synthetic', model='compatible/test', question=question, evidence=evidence, context=context, cancel_event=threading.Event(), opener=opener) self.assertTrue(result['selectionFallback']) self.assertEqual(result['reportTitle'], parsed['reportTitle']) self.assertTrue(result['titleGeneratedByModel']) self.assertIn('81.00%', result['answer']) self.assertIn('[Q77777777]', result['answer']) self.assertNotIn('| 指标 |', result['answer']) self.assertNotIn('重点关注', result['answer']) self.assertNotIn('重量单位', result['answer']) def test_timeout_and_legacy_response_have_neutral_nonquestion_titles(self): timeout_result = self.run_synthesis(timed_out=True) self.assertFalse(timeout_result['titleGeneratedByModel']) self.assertNotIn(self.question, timeout_result['reportTitle']) self.assertIn('产品甲', timeout_result['reportTitle']) legacy = self.run_synthesis({'otherFactIds': ['F1'], 'groupFactIds': [], 'productFactIds': [], 'defectFactIds': [], 'evidenceSufficient': True, 'missingEvidence': []}) self.assertFalse(legacy['titleGeneratedByModel']) self.assertIn('合格率', legacy['reportTitle']) self.assertNotIn(self.question, legacy['reportTitle']) if __name__ == '__main__': unittest.main()