import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) from nao_product_identification import identify_products, resolve_clarification_choice, authorized_extra_fields class ProductIdentificationTests(unittest.TestCase): def test_explicit_negative_dimension_does_not_return_as_positive_clarification_option(self): options={'products':['18X15X4+T','18X15X4+Z','18X15X4.1+Z']} result=identify_products('18X15X4合格率,不要包含18X15X4.1',options,respect_exclusions=True) self.assertEqual(result['productMatch']['status'],'ambiguous') self.assertEqual([x['name'] for x in result['productMatch']['candidates']],['18X15X4+T','18X15X4+Z']) self.assertEqual(result['explicitlyExcludedProducts'],['18X15X4.1+Z']) cancelled=identify_products('18X15X4合格率,取消排除18X15X4.1',options,respect_exclusions=True) self.assertEqual(cancelled['explicitlyExcludedProducts'],[]) negative_only=identify_products('看公司整体合格率,排除18X15X4.1+Z',options,respect_exclusions=True) self.assertEqual(negative_only['productMatch']['status'],'none') self.assertEqual(negative_only['productMatch']['candidates'],[]) self.assertEqual(negative_only['explicitlyExcludedProducts'],['18X15X4.1+Z']) named=identify_products('查看CUSTOM_SPEC',{'products':['CUSTOM_SPEC']},respect_exclusions=True) self.assertEqual(named['productMatch']['status'],'exact') def test_round_two_axis_geometry_preserves_diameter_and_exact_suffix(self): options={'products':['D37.2X6.5+C','D37.2X6.51+C','37.2X6.5X2+C','18X15X4+T']} for text in ('D37.2x6.5','Φ37.20×6.50','Ø37.2*6.5'): match=identify_products(text,options)['productMatch'] self.assertEqual(match['status'],'unique');self.assertEqual([item['name'] for item in match['candidates']],['D37.2X6.5+C']) self.assertEqual(identify_products('D37.2x6.5+Q',options)['productMatch']['status'],'none') self.assertEqual(identify_products('D18x15x4',options)['productMatch']['status'],'none') def setUp(self): self.options = { 'products': ['18X15X4+T', '18X15X4+Z', '18X15X4.1+Z', '28X20X3'], 'organizationPaths': [ {'product': '18X15X4+T', 'department': '汽车', 'group': '国内'}, {'product': '18X15X4+Z', 'department': '汽车', 'group': '海外'}, {'product': '18X15X4.1+Z', 'department': '工业', 'group': '美洲'}, ], } def test_dimension_shorthand_produces_candidates_without_wrong_decimal(self): result = identify_products('今年18x15x4合格率变化趋势', self.options) self.assertEqual(result['mentionedProducts'], []) match = result['productMatch'] self.assertEqual(match['status'], 'ambiguous') self.assertEqual([item['name'] for item in match['candidates']], ['18X15X4+T', '18X15X4+Z']) self.assertEqual(match['candidateCount'], 2) self.assertNotIn('18X15X4.1+Z', [item['name'] for item in match['candidates']]) def test_full_name_wins_over_other_suffix_and_normalizes_multiplication(self): result = identify_products('今年18×15×4+t的趋势', self.options) self.assertEqual(result['mentionedProducts'], ['18X15X4+T']) self.assertEqual(result['productMatch']['status'], 'exact') self.assertEqual([item['name'] for item in result['productMatch']['candidates']], ['18X15X4+T']) def test_unique_and_decimal_equivalent_dimensions(self): match = identify_products('18.0*15*4.10的变化', self.options)['productMatch'] self.assertEqual(match['status'], 'unique') self.assertEqual(match['candidates'][0]['name'], '18X15X4.1+Z') self.assertEqual(match['candidates'][0]['departments'], ['工业']) def test_unknown_dimension_does_not_guess(self): match = identify_products('20x11x7的趋势', self.options)['productMatch'] self.assertEqual(match['status'], 'none') self.assertEqual(match['candidates'], []) self.assertEqual(match['mentions'][0]['query'], '20x11x7') def test_only_authorized_options_can_be_candidates(self): scoped = {'products': ['18X15X4+T'], 'organizationPaths': self.options['organizationPaths']} match = identify_products('18x15x4', scoped)['productMatch'] self.assertEqual(match['status'], 'unique') self.assertEqual([item['name'] for item in match['candidates']], ['18X15X4+T']) self.assertEqual(match['candidates'][0]['groups'], ['国内']) def test_multiple_specifications_stay_separate(self): match = identify_products('比较18×15×4+T和28x20x3', self.options)['productMatch'] self.assertEqual(match['status'], 'multiple') self.assertEqual([m['status'] for m in match['mentions']], ['exact', 'exact']) self.assertEqual([[c['name'] for c in m['candidates']] for m in match['mentions']], [['18X15X4+T'], ['28X20X3']]) def test_candidate_cap_does_not_misrepresent_ambiguity(self): options = {'products': [f'18X15X4+S{i}' for i in range(30)]} match = identify_products('18x15x4', options)['productMatch'] self.assertEqual(match['status'], 'ambiguous') self.assertEqual(match['candidateCount'], 30) self.assertEqual(len(match['candidates']), 20) self.assertTrue(match['truncated']) current=identify_products('18x15x4',options,respect_exclusions=True)['productMatch'] self.assertEqual(current['candidateCount'],30) self.assertEqual(len(current['candidates']),20) self.assertTrue(current['truncated']) def test_non_product_question_and_empty_catalog_are_safe(self): self.assertEqual(identify_products('你好', self.options)['productMatch']['status'], 'none') self.assertEqual(identify_products('18x15x4', {'products': None})['productMatch']['status'], 'none') def pending(self): return {'needsClarification': True, 'productResolution': { 'originalQuestion': '今年18x15x4合格率变化趋势', 'productMatch': identify_products('今年18x15x4合格率变化趋势', self.options)['productMatch'], }} def test_followup_suffix_is_resolved_only_from_pending_candidates(self): for reply, expected in (('选 +T', '18X15X4+T'), ('选择+z', '18X15X4+Z')): with self.subTest(reply=reply): result = resolve_clarification_choice(reply, self.options, self.pending()) self.assertEqual(result['mentionedProducts'], [expected]) self.assertEqual(result['productMatch']['status'], 'exact') self.assertEqual(result['productMatch']['previousQuestion'], '今年18x15x4合格率变化趋势') self.assertTrue(result['productMatch']['selectedFromClarification']) def test_selection_never_searches_suffix_globally_or_uses_revoked_access(self): options = {'products': ['18X15X4+Z', '28X20X3+T']} self.assertIsNone(resolve_clarification_choice('选+T', options, self.pending())) def test_failed_selected_question_retry_recovers_only_same_authorized_choice(self): previous = self.pending() previous['needsClarification'] = False previous['productResolution'].update(selectedName='18X15X4+T', selectionQuestion='选 +T') result = resolve_clarification_choice('选+T', self.options, previous) self.assertEqual(result['mentionedProducts'], ['18X15X4+T']) self.assertEqual(result['productMatch']['previousQuestion'], '今年18x15x4合格率变化趋势') self.assertIsNone(resolve_clarification_choice('选+Z', self.options, previous)) self.assertIsNone(resolve_clarification_choice('选+T', {'products':['18X15X4+Z']}, previous)) previous['productResolution']['selectedName'] = '28X20X3' self.assertIsNone(resolve_clarification_choice('选+T', self.options, previous)) def test_unrelated_or_multi_variant_followup_does_not_choose(self): for reply in ('再看整体合格率', '我想比较+T和+Z', '只查18x15x4+T九月趋势'): with self.subTest(reply=reply): self.assertIsNone(resolve_clarification_choice(reply, self.options, self.pending())) prior = self.pending(); prior['needsClarification'] = False self.assertIsNone(resolve_clarification_choice('选+T', self.options, prior)) def test_complete_candidate_choice_from_ui_uses_the_same_pending_frame(self): for reply in ('选择18x15x4+T','18X15X4+T','选择18X15X4+T。','继续上一轮分析,保留未修改的原目标和条件。\n我的选择:18X15X4+T'): result=resolve_clarification_choice(reply,self.options,self.pending()) self.assertEqual(result['mentionedProducts'],['18X15X4+T']) self.assertTrue(result['productMatch']['selectedFromClarification']) self.assertEqual(result['productMatch']['previousQuestion'],'今年18x15x4合格率变化趋势') def test_truncated_or_duplicate_suffix_candidates_cannot_select(self): prior = self.pending(); prior['productResolution']['productMatch']['truncated'] = True self.assertIsNone(resolve_clarification_choice('选+T', self.options, prior)) options = {'products': ['18X15X4+A+T', '18X15X4+B+T']} prior = {'needsClarification': True, 'productResolution': { 'originalQuestion':'今年18x15x4合格率变化趋势', 'productMatch': identify_products('18x15x4', options)['productMatch']}} self.assertIsNone(resolve_clarification_choice('选+T', options, prior)) def test_case_colliding_catalog_names_remain_ambiguous(self): match = identify_products('18x15x4+t', {'products':['18X15X4+T', '18x15x4+t']})['productMatch'] self.assertEqual(match['status'], 'ambiguous') self.assertEqual(match['candidateCount'], 2) def test_customer_code_that_looks_like_specification_is_not_a_product(self): for question in ('今年客户代码18x15x4的合格率变化趋势', '查客户编码为18X15X4+T的趋势', '客户代码“18x15x4”今年合格率趋势', '查客户代码为「18X15X4+T」的趋势', '客户代码 "18x15x4" 的合格率走势'): with self.subTest(question=question): result = identify_products(question, self.options) self.assertEqual(result['mentionedProducts'], []) self.assertEqual(result['productMatch']['status'], 'none') def test_customer_code_and_explicit_product_are_kept_separate(self): result = identify_products('客户代码18x15x4的18X15X4+T产品合格率趋势', self.options) self.assertEqual(result['mentionedProducts'], ['18X15X4+T']) self.assertEqual(len(result['productMatch']['mentions']), 1) self.assertEqual(result['productMatch']['status'], 'exact') quoted = identify_products('客户代码“18x15x4”的18X15X4+T产品合格率趋势', self.options) self.assertEqual(quoted['mentionedProducts'], ['18X15X4+T']) self.assertEqual(quoted['productMatch']['status'], 'exact') def test_authorized_extra_fields_are_not_inferred_or_expanded(self): catalog = {'kind':'analysis_catalog', 'extraFields':[ {'key':'customerCode', 'label':'客户代码', 'blackColumn':'客户代码', 'brightColumn':'客户代码', 'enabled':True}, {'key':'disabled', 'label':'禁用字段', 'enabled':False}, ]} self.assertEqual(authorized_extra_fields(catalog), {'extraFieldsStatus':'ready', 'extraFields':[{'key':'customerCode','label':'客户代码'}]}) self.assertEqual(authorized_extra_fields({'kind':'analysis_catalog','extraFields':[]})['extraFields'], []) self.assertEqual(authorized_extra_fields({'kind':'missing'})['extraFieldsStatus'], 'unavailable') if __name__ == '__main__': unittest.main()