import copy import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) from analysis_visuals import build_visuals from nao_agent_report import _build_trend_report RELEASE = 'signed-release' FILTERS = {'department': '部门甲'} def trend_document(black_total, bright_total=100.0): rows = [] for month, black_weight in ((1, 120.0), (2, black_total)): rows.append({'period': f'2026-{month:02d}', 'periodLabel': f'2026-{month:02d}', 'black': {'totalWeight': black_weight, 'scrapWeight': 10.0, 'scrapRate': 0.08 if month == 1 else 0.09, 'rate': 0.95, 'qualifiedWeight': 110.0}, 'bright': {'totalWeight': bright_total, 'scrapWeight': 8.0, 'scrapRate': 0.08, 'rate': 0.96, 'qualifiedWeight': 92.0}, 'coverage': {'blackRows': 1, 'brightRows': 1}}) result = {'kind': 'quality_metrics', 'releaseId': RELEASE, 'period': {'year': 2026, 'monthStart': 1, 'monthEnd': 2}, 'filters': copy.deepcopy(FILTERS), 'view': 'overall', 'summary': {'black': {'totalWeight': black_total, 'scrapWeight': 20.0}, 'bright': {'totalWeight': bright_total, 'scrapWeight': 16.0}}, 'trend': rows, 'trendCount': 2, 'trendTruncated': False, 'coverage': {'sourceMonths': [ {'year': 2026, 'month': 1, 'blackRows': 1, 'brightRows': 1}, {'year': 2026, 'month': 2, 'blackRows': 1, 'brightRows': 1}]}} return {'tool': 'quality_data', 'mode': 'query', 'releaseId': RELEASE, 'evidenceRef': 'Q1234ABCD', 'result': result} def child_task(field, *, visual_type='line', period_coverage='available'): return {'version': 1, 'kind': 'metrics', 'question': '黑片投入重量趋势', 'period': {'year': 2026, 'monthStart': 1, 'monthEnd': 2}, 'filters': copy.deepcopy(FILTERS), 'dimensions': [], 'dimensionStages': {}, 'stages': ['black'], 'fields': [field], 'trendRequired': True, 'comparisonRequired': False, 'comparisonPeriod': None, 'periodCoverage': period_coverage, 'visualPreference': 'auto', 'visualType': visual_type} class QueryPlanSignedWeightTrendTests(unittest.TestCase): def test_black_total_weight_keeps_signed_correction_in_two_point_line_and_report(self): document = trend_document(-10.05) task = child_task('totalWeight', visual_type='line') context = {'releaseId': RELEASE, 'period': task['period'], 'filters': FILTERS, '_agentTask': task} visuals = build_visuals([document], task['question'], 'turn-signed', context=context) chart = next(item for item in visuals['visuals'] if item['metric'] == 'black_total_weight') self.assertEqual(chart['type'], 'line') self.assertEqual(chart['series'][0]['values'], [120.0, -10.05]) self.assertIn('负向修正原值保留', chart['subtitle']) report = _build_trend_report([document], context, task['question']) self.assertTrue(report['coverageSufficient'], report['missingEvidence']) self.assertIn('2月-10.05 kg', report['facts'][0]['text']) def test_negative_bright_total_is_rejected_and_area_still_requires_three_points(self): document = trend_document(100.0, bright_total=-1.0) bright_task = {**child_task('totalWeight', visual_type='line'), 'stages': ['bright'], 'question': '亮片投入重量趋势'} bright_context = {'releaseId': RELEASE, 'period': bright_task['period'], 'filters': FILTERS, '_agentTask': bright_task} bright_charts = build_visuals([document], bright_task['question'], 'turn-bright', context=bright_context)['visuals'] self.assertFalse(any(item['metric'] == 'bright_total_weight' for item in bright_charts)) area_task = child_task('totalWeight', visual_type='area') area_context = {'releaseId': RELEASE, 'period': area_task['period'], 'filters': FILTERS, '_agentTask': area_task} area_charts = build_visuals([trend_document(-10.05)], area_task['question'], 'turn-area', context=area_context)['visuals'] self.assertFalse(any(item['metric'] == 'black_total_weight' for item in area_charts)) if __name__ == '__main__': unittest.main()