import copy,json,sys,unittest from pathlib import Path sys.path[:0]=[str(Path(__file__).resolve().parents[1]/'scripts'),str(Path(__file__).parent)] from dsh_model_input import canonical_proposal,planning_input,adjacent_month_period,compact_tool_result from nao_native_exploration import prepare_step from nao_quality_tools import QualityToolError from test_native_exploration import root from test_nao_quality_tools import RELEASE class SevenQuestionRegressions(unittest.TestCase): def test_captured_json_string_periods_are_equivalent_to_object_periods(self): task=root();task.update(period={'year':2026,'month':10},comparisonPeriod={'year':2026,'month':9}) args={'mode':'compare','period':json.dumps(task['period']),'comparisonPeriod':json.dumps(task['comparisonPeriod'])} before=copy.deepcopy(args) actual=prepare_step(args,task,{'releaseId':RELEASE,'scopes':[]}) expected=prepare_step({'mode':'compare'},task,{'releaseId':RELEASE,'scopes':[]}) self.assertEqual(actual,expected);self.assertEqual(args,before) for value in ('{bad','[]','2026-10'): with self.assertRaises(QualityToolError):prepare_step({'mode':'compare','period':value},task,{'releaseId':RELEASE,'scopes':[]}) def test_redundant_query_stage_uses_same_formal_query_without_widening_root(self): task=root();task.update(stages=['bright'],comparisonPeriod=None,visualType='donut') args,proposal=prepare_step({'mode':'query','stage':'bright'},task,{'releaseId':RELEASE,'scopes':[]}) self.assertNotIn('stage',args);self.assertEqual(proposal['node']['stages'],['bright']) self.assertIsNone(proposal['node']['visualType']) with self.assertRaises(QualityToolError):prepare_step({'mode':'query','stage':'black'},task,{'releaseId':RELEASE,'scopes':[]}) def test_company_department_ranking_cannot_invent_a_single_department(self): context={'_businessCatalog':{'departments':['工业电机产业部','消费电子产业部','新能源汽车磁钢']}} value={'kind':'analysis','filterUpdates':{'department':'工业电机产业部'},'dimensions':['group'],'stages':['bright']} fixed=canonical_proposal(value,question='生成2026年1—10月产业部亮片报废重量Top5对比简报,说明报废重量主要集中在哪里。',context=context) self.assertEqual(fixed['filterUpdates']['department'],None);self.assertEqual(fixed['dimensions'],['department']) explicit=canonical_proposal(value,question='生成工业电机产业部亮片报废重量Top5对比简报',context=context) self.assertEqual(explicit['filterUpdates']['department'],'工业电机产业部') def test_neighboring_months_are_part_of_the_same_requested_read_window(self): question='生成2026年5月质量异常诊断简报,说明当月表现、与前后月份的差异和可观察到的报废线索。数据不能证明的原因请直接说明。' value={'kind':'analysis','period':{'year':2026,'month':5},'comparisonRequired':False} fixed=canonical_proposal(value,question=question) self.assertEqual(fixed['period'],{'year':2026,'monthStart':4,'monthEnd':6}) self.assertTrue(fixed['trendRequired']);self.assertFalse(fixed['comparisonRequired']) payload=planning_input({'question':question}) self.assertEqual(payload['adjacentPeriodBinding']['focusPeriod'],{'year':2026,'month':5}) self.assertEqual(value['period'],{'year':2026,'month':5}) self.assertIsNone(adjacent_month_period('2026年5月总体表现')) def test_focus_month_and_complete_ranking_metadata_do_not_change_source_values(self): source={'kind':'quality_metrics','summary':{'black':{'scrapWeight':80}},'trend':[ {'periodLabel':'2026-04','black':{'scrapWeight':20}}, {'periodLabel':'2026-05','black':{'scrapWeight':35}}, {'periodLabel':'2026-06','black':{'scrapWeight':25}}]} before=copy.deepcopy(source) projected=compact_tool_result(source,task={'question':'2026年5月诊断,与前后月份比较'}) self.assertEqual(projected['focusMonth'],source['trend'][1]);self.assertEqual(source,before) rank=compact_tool_result({'kind':'quality_weight_ranking','dimension':'department','returnedRows':3,'truncated':False}) self.assertTrue(rank['rankingDisplay']['complete']);self.assertEqual(rank['rankingDisplay']['returnedRows'],3) def test_fine_defect_top_does_not_inherit_a_report_composition_hint(self): task=root();task.update(stages=['bright'],visualType='pareto',comparisonPeriod=None) args,proposal=prepare_step({'mode':'defects','stage':'bright','limit':5},task,{'releaseId':RELEASE,'scopes':[]}) self.assertEqual(proposal['node']['visualType'],'bar') self.assertEqual(args['limit'],5) if __name__=='__main__':unittest.main()