Files
quality_analyse/verification/test_ai_combined_ranking.py

247 lines
16 KiB
Python

import copy
import sys
import unittest
from unittest.mock import patch
from pathlib import Path
sys.path[:0] = [str(Path(__file__).resolve().parents[1] / 'scripts')]
from nao_weight_ranking import rank_response, ranking_report, ranking_chart
from nao_native_answer import _validate_blocks, _binding_inventory, _bind_literal_measures
def fixture():
rows = []
for i, (black, bright) in enumerate([(5, 60), (40, 1), (30, 30), (1, 40), (20, 10)], 1):
rows.append({'key': f'product-{i}', 'label': f'规格{i}',
'black': {'scrapWeight': black, 'totalWeight': 100},
'bright': {'scrapWeight': bright, 'totalWeight': 100},
'compositeRate': .8 + i / 100,
'coverage': {'blackRows': 1, 'brightRows': 1}})
return {'rows': rows, 'summary': {'black': {'scrapWeight': 96}, 'bright': {'scrapWeight': 141}},
'context': {'rowCount': 5}, 'coverage': {'sourceMonths': [
{'year': 2026, 'month': 9, 'blackRows': 5, 'brightRows': 5}]}}
class CombinedRankingTests(unittest.TestCase):
def test_combined_contract_survives_task_normalization_and_execution_arguments(self):
from nao_agent_task import normalize_task
from nao_protocol_execution import _scoped_arguments
value={'goal':'产品不良TOP5,报废合计与综合率','kind':'analysis','fields':['rate','scrapWeight'],
'dimensions':['product'],'dimensionStages':{'product':[]},'stages':[],
'period':{'year':2026,'month':9},'comparisonPeriod':None,'comparisonRequired':False,
'trendRequired':False,'freshQuery':True,'verifySource':False,'fullDetails':False,
'needsClarification':False,'filterUpdates':{},'queryPlan':[{
'nodeId':'rank','purpose':'ranking','fields':['rate','scrapWeight'],'stages':[],
'dimension':'product','limit':5,'selector':{'kind':'base'},'visualType':'bar',
'visualPreference':'auto','comparisonRequired':False}]}
task=normalize_task(value,'产品不良TOP5',{'_protocol':{'version':1}})
self.assertEqual(task['stages'],[])
args=_scoped_arguments({'releaseId':'release-a'},{'task':task},task['queryPlan'][0])
self.assertEqual(len(args),1)
self.assertIsNone(args[0]['stage'])
self.assertEqual(args[0]['metric'],'scrapWeight')
from nao_plan_recovery import repair_summary
redundant=copy.deepcopy(value);redundant['stages']=['black','bright']
repaired,changes=repair_summary(redundant,{'_protocol':{'version':1}})
self.assertEqual(repaired['stages'],[])
self.assertEqual(repaired['queryPlan'],value['queryPlan'])
from nao_query_plan import normalize_query_plan
follow={'nodeId':'selected','purpose':'metrics','fields':['rate'],'stages':[],
'selector':{'kind':'ranking_top','sourceNodeId':'rank','stage':'overall','metric':'scrapWeight','count':2},
'comparisonRequired':False,'visualType':'bar','visualPreference':'auto'}
compound=copy.deepcopy(value);compound['queryPlan'].append(follow)
self.assertEqual(len(normalize_task(compound,'产品不良TOP5及前两项综合率',{'_protocol':{'version':1}})['queryPlan']),2)
def result(self):
return rank_response(fixture(), {'id': 'release-a'}, {'year': 2026, 'month': 9}, {},
'product', None, 'scrapWeight', 5)
def test_sum_both_stages_before_top_cut_and_keep_formal_rate(self):
result = self.result()
self.assertEqual([row['key'] for row in result['rows']],
['product-1', 'product-3', 'product-2', 'product-4', 'product-5'])
self.assertEqual(result['totalWeight'], 237)
self.assertEqual(result['rows'][0]['weight'], 65)
self.assertEqual(result['rows'][0]['compositeRate'], .81)
self.assertIsNone(result['stage'])
def test_formal_composite_survives_either_single_source_direction(self):
for absent in ('black', 'bright'):
with self.subTest(absent=absent):
source = fixture()
row = source['rows'][0]
source['summary'][absent]['scrapWeight'] -= row[absent]['scrapWeight']
row[absent] = {'scrapWeight': 0, 'totalWeight': 0, 'rate': 1}
row['coverage'][absent + 'Rows'] = 0
result = rank_response(source, {'id': 'release-a'}, {'year': 2026, 'month': 9},
{}, 'product', None, 'scrapWeight', 5)
selected = next(item for item in result['rows'] if item['key'] == row['key'])
self.assertEqual(selected['compositeRate'], row['compositeRate'])
def test_month_coverage_accepts_either_formal_source_without_faking_missing_months(self):
from nao_weight_ranking import ranking_gaps
for absent in ('black', 'bright'):
with self.subTest(absent=absent):
source = fixture()
source['summary'][absent]['scrapWeight'] = 0
source['coverage']['sourceMonths'][0][absent + 'Rows'] = 0
for row in source['rows']:
row[absent] = {'scrapWeight': 0, 'totalWeight': 0, 'rate': 1}
row['coverage'][absent + 'Rows'] = 0
result = rank_response(source, {'id': 'release-a'}, {'year': 2026, 'month': 9},
{}, 'product', None, 'scrapWeight', 5)
task = {'period': result['period'], 'filters': {}, 'dimensions': ['product'],
'dimensionStages': {'product': []}, 'stages': [],
'fields': ['rate', 'scrapWeight'], 'periodCoverage': 'complete'}
self.assertTrue(result['periodCoverage']['complete'])
self.assertEqual(ranking_gaps([{'result': result, 'evidenceRef': 'Q00000001'}],
task, {'releaseId': 'release-a'}), [])
def test_missing_or_unknown_row_source_cannot_validate_default_composite(self):
from nao_quality_tools import QualityTools
for coverage in ({'blackRows': 0, 'brightRows': 0}, {'blackRows': 1}, None):
with self.subTest(coverage=coverage):
row = {**fixture()['rows'][0], 'coverage': coverage, 'compositeRate': 1}
self.assertIsNone(QualityTools._row_rate(row, None))
def test_top5_all_reach_narration_and_combined_chart(self):
result = self.result()
task = {'period': result['period'], 'filters': {}, 'dimensions': ['product'],
'dimensionStages': {'product': []}, 'stages': [], 'fields': ['rate', 'scrapWeight'],
'visualType': 'bar', 'periodCoverage': 'available'}
doc = {'result': result, 'evidenceRef': 'Q00000001'}
report = ranking_report([doc], task, {'releaseId': 'release-a'})
self.assertEqual(len([f for f in report['facts'] if f.get('rank')]), 5)
self.assertIn('综合合格率', report['sections'][0])
chart = ranking_chart(doc, 'turn-a', task)
self.assertEqual(chart['series'][0]['values'], [65, 60, 41, 41, 30])
self.assertEqual(chart['metric'], 'current_scrap_weight')
def test_missing_auto_combined_ranking_chart_is_reported(self):
from analysis_visual_evidence import protocol_visual_result
result = self.result()
task = {'kind': 'analysis', 'period': result['period'], 'filters': {},
'dimensions': ['product'], 'dimensionStages': {'product': []},
'stages': [], 'fields': ['rate', 'scrapWeight'],
'visualType': 'bar', 'visualPreference': 'auto'}
scope = {'nodeId': 'rank', 'scopeId': 'scope', 'purpose': 'ranking', 'task': task}
with patch('analysis_visuals.build_visuals', return_value={
'visualState': 'unavailable', 'visuals': []}):
visuals = protocol_visual_result([], [scope], '产品不良Top5', 'turn',
release_id='release-a')
self.assertTrue(visuals['missingEvidence'])
def test_observed_zero_scrap_completes_without_inventing_top_shares_or_a_chart(self):
from nao_weight_ranking import ranking_gaps, missing_ranking_visuals
source = fixture()
for stage in ('black', 'bright'):
source['summary'][stage]['scrapWeight'] = 0
for row in source['rows']:
row[stage]['scrapWeight'] = 0
for stage in (None, 'black', 'bright'):
with self.subTest(stage=stage):
result = rank_response(source, {'id': 'release-a'}, {'year': 2026, 'month': 9},
{}, 'product', stage, 'scrapWeight', 5)
task = {'kind': 'analysis', 'period': result['period'], 'filters': {},
'dimensions': ['product'], 'dimensionStages': {'product': [] if stage is None else [stage]},
'stages': [] if stage is None else [stage],
'fields': ['rate', 'scrapWeight'] if stage is None else ['scrapWeight'],
'visualType': 'bar', 'visualPreference': 'auto'}
doc = {'result': result, 'evidenceRef': 'Q00000001'}
self.assertEqual(result['rows'], [])
self.assertEqual(result['totalWeight'], 0)
self.assertEqual(result['emptyReason'], 'no_positive_scrap')
self.assertEqual(ranking_gaps([doc], task, {'releaseId': 'release-a'}), [])
self.assertEqual(missing_ranking_visuals(task, [], [doc]), [])
report = ranking_report([doc], task, {'releaseId': 'release-a'})
self.assertTrue(report['coverageSufficient'])
self.assertIn('0.00', report['facts'][0]['text'])
self.assertIsNone(ranking_chart(doc, 'turn', task))
def test_missing_source_is_not_an_observed_zero_and_no_fake_rate(self):
source = fixture()
source['rows'][0]['bright'] = {'scrapWeight': None, 'totalWeight': None}
with self.assertRaises(ValueError):
rank_response(source, {'id': 'release-a'}, {'year': 2026, 'month': 9}, {}, 'product', None, 'scrapWeight', 5)
def test_table_ordinals_are_not_business_measurements(self):
facts = [{'id': f'F{i}', 'kind': 'product', 'rank': i, 'label': f'规格甲{chr(64+i)}',
'text': f'报废重量{10*i}.00 kg。', 'evidenceRef': 'Q00000001'} for i in range(1, 6)]
parsed = {'blocks': [{'role': 'core', 'text': '已列出重点规格。', 'factIds': ['F1']},
{'role': 'key_evidence', 'text': '### 报废排名\n\n| 排名 | 报废总重 |\n| --- | --- |\n' +
'\n'.join(f'| {i} | {10*i}.00 kg |' for i in range(1, 6)),
'factIds': [f'F{i}' for i in range(1, 6)]}]}
tokens = _binding_inventory(facts)
parsed = _bind_literal_measures(copy.deepcopy(parsed), tokens, {})
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertIn('| 5 | 50.00 kg |', blocks[1]['text'])
def test_verified_alphanumeric_organization_names_are_not_kpi_literals(self):
from nao_native_answer import _bind_exact_identifiers
facts = [{'id': 'F1', 'kind': 'product', 'label': 'P1+A', 'rank': 1,
'text': 'P1+A报废24.50 kg;综合合格率95.00%。', 'evidenceRef': 'Q00000001',
'scope': {'period': {'year': 2026, 'month': 9},
'filters': {'department': '器件事业部', 'group': '器件-X7'}}}]
tokens = _binding_inventory(facts)
for name in ('器件事业部-器件-X7', 'X7业务分组'):
with self.subTest(name=name):
parsed = {'blocks': [{'role': 'core', 'text': name+'中P1+A报废24.50 kg,为TOP1。',
'factIds': ['F1']}]}
parsed = _bind_exact_identifiers(parsed, tokens, {})
parsed = _bind_literal_measures(parsed, tokens, {})
blocks, _ = _validate_blocks(parsed, facts, tokens)
self.assertIn(name, blocks[0]['text'])
unsupported = {'blocks': [{'role': 'core', 'text': 'X9业务分组报废24.50 kg。', 'factIds': ['F1']}]}
unsupported = _bind_exact_identifiers(unsupported, tokens, {})
unsupported = _bind_literal_measures(unsupported, tokens, {})
with self.assertRaises(ValueError):
_validate_blocks(unsupported, facts, tokens)
def test_inline_rank_number_must_belong_to_the_named_verified_product(self):
from nao_native_answer import _bind_exact_identifiers
facts = [{'id': 'F'+str(i), 'kind': 'product', 'label': '规格'+label, 'rank': i,
'text': '规格'+label+'报废30.00 kg。', 'evidenceRef': 'Q00000001'}
for i, label in [(3, '丙'), (4, '丁')]]
tokens = _binding_inventory(facts)
good = {'blocks': [{'role': 'core', 'text': '3号规格丙与4号规格丁值得关注。', 'factIds': ['F3', 'F4']}]}
bound = _bind_exact_identifiers(good, tokens, {})
self.assertEqual(_validate_blocks(bound, facts, tokens)[0][0]['text'], good['blocks'][0]['text'])
bad = {'blocks': [{'role': 'core', 'text': '4号规格丙值得关注。', 'factIds': ['F3']}]}
with self.assertRaises(ValueError):
_validate_blocks(_bind_exact_identifiers(bad, tokens, {}), facts, tokens)
def test_reviewed_clause_removal_does_not_leave_an_orphan_colon(self):
from nao_native_answer import _review_supported_remainder
facts = [{'id': 'F1', 'kind': 'product', 'label': '规格甲',
'text': '规格甲报废30.00 kg。', 'evidenceRef': 'Q00000001'}]
result = _review_supported_remainder(
{'text': '错误的统计概括:规格甲报废30.00 kg。', 'factIds': ['F1']},
{'unsupportedClauses': ['错误的统计概括'], 'remainingClaimsSupported': True,
'remainingStatisticalClaimsSupported': True}, facts)
self.assertIsNotNone(result)
self.assertEqual(result['text'], '规格甲报废30.00 kg。')
def test_complete_ranking_survives_unrequested_uncited_optional_detail(self):
from test_native_answer import invoke, response, request_phase
facts=[{'id':f'F{i}','kind':'product','rank':i,'label':f'规格{chr(64+i)}',
'text':f'规格{chr(64+i)}报废重量{10*i}.00 kg。','evidenceRef':'Q00000001'} for i in range(1,6)]
report={'facts':facts,'requiredFactIds':[f['id'] for f in facts],'priorityFactIds':[],
'scopes':[],'missingEvidence':[],'rejectedEvidenceRefs':[]}
def provider(request,timeout):
if request_phase(request)=='evidence_review':
return response({'reviews':[{'index':i,'supported':True,'statisticalClaimsSupported':True} for i in range(3)],'missingGoalDescriptions':[]})
return response({'blocks':[
{'role':'core','text':'本轮TOP5已列出。','factIds':[f['id'] for f in facts]},
{'role':'key_evidence','text':'### 排名\n\n| 排名 | 报废重量 |\n| --- | --- |\n'+
'\n'.join(f'| {i} | {10*i}.00 kg |' for i in range(1,6)),'factIds':[f['id'] for f in facts]},
{'role':'key_evidence','text':'本轮TOP5中,规格A报废重量为10.00 kg。','factIds':[f['id'] for f in facts]},
{'role':'detail','text':'未提供本轮没有要求的阶段拆分。','factIds':[]}]})
with patch('nao_native_answer.build_native_report',return_value=report):
final=invoke(provider)
self.assertTrue(final['evidenceSufficient'],final['missingEvidence'])
self.assertNotIn('未提供本轮没有要求的阶段拆分',final['answer'])
self.assertEqual(final['compositionDiagnostics']['omittedUncitedOptionalDetails'],[3])
if __name__ == '__main__':
unittest.main()