Files

126 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
"""回归测试:AI 识别后「该自动的自动、该问人的问人」+ 同名多行自动择行。
背景(现场 20:05~20:06):
冰糖橙 70%(次选橘子)、白萝卜 75%(次选圆萝卜)全被转人工,
等于整台机不再自动入库 —— 因为旧判据是「只要模型给了次选就拦」。
"""
import sys
import os
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from core.flow import FlowEngine, similar_item_name
CFG = {
'flow': {'min_scale_kg': 0.05, 'same_name_auto': True},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 60, 'alt_guard': True,
'alt_min_conf': 55, 'alt_margin': 12},
'server': {'mock': True},
}
logs = []
eng = FlowEngine(CFG, lambda ev, p=None: logs.append((ev, p)))
eng.refresh_pending = lambda initial=False: None
def row(rid, name, qty, unit='斤', order=''):
return {'id': rid, 'item_name': name, 'unit': unit,
'prepare_qty': qty, 'remaining_qty': qty, 'order_code': order}
ROWS = [
row('r1', '冰糖橙', 7.5, order='ORD_0001'),
row('r2', '橘子', 15, order='ORD_0002'),
row('r3', '白萝卜', 2.45, order='ORD_0003'),
row('r4', '圆萝卜', 1, order='ORD_0007'),
row('r5', '圆萝卜', 1, order='ORD_0005'),
row('r6', '生菜', 3, order='ORD_0001'),
row('r7', '生菜', 1.5, order='ORD_0007'),
row('r8', '生菜', 10, order='ORD_0008'),
]
eng.rows = ROWS
eng.server_config = {'weigh_match_tolerance_mode': 'percent',
'weigh_match_tolerance_value': 10}
def hits(name):
return [r for r in ROWS if r['item_name'] == name]
ok = True
def check(label, cond, detail=''):
global ok
print((' ✔ ' if cond else ' ✘ ') + label + ((' ' + detail) if detail else ''))
ok = ok and cond
print('=== 1. 次选差距明显 → 自动入库(现场 20:05 冰糖橙)===')
warn, extra = eng._recog_guard('冰糖橙', 70, '橘子', hits('冰糖橙'), 20)
check('冰糖橙 70% / 次选橘子 20% → 不转人工', warn is None, repr(warn))
print('=== 2. 次选也很有把握 → 转人工(真的容易混)===')
warn, extra = eng._recog_guard('白萝卜', 75, '圆萝卜', hits('白萝卜'), 70)
check('白萝卜 75% / 次选圆萝卜 70% → 转人工', warn is not None, str(warn))
print('=== 3. 次选 55% → 转人工(alt_min_conf 门槛)===')
warn, extra = eng._recog_guard('冰糖橙', 60, '橘子', hits('冰糖橙'), 55)
check('次选把握 55% → 转人工', warn is not None, str(warn))
print('=== 4. 首选自己把握不足 → 转人工 ===')
warn, extra = eng._recog_guard('冰糖橙', 50, '橘子', hits('冰糖橙'), 10)
check('把握 50% < 60% → 转人工', warn is not None, str(warn))
print('=== 5. 没给 alt_conf 但名字相近 → 保守转人工 ===')
warn, extra = eng._recog_guard('白萝卜', 80, '圆萝卜', hits('白萝卜'), None)
check('无 alt_conf + 名字相近 → 转人工', warn is not None, str(warn))
warn, extra = eng._recog_guard('冰糖橙', 80, '橘子', hits('冰糖橙'), None)
check('无 alt_conf + 名字不相近 → 自动', warn is None, repr(warn))
print('=== 6. 名字相近判定 ===')
for a, b, exp in [('白萝卜', '圆萝卜', True), ('大白菜', '尖白菜', True),
('莴笋(斤)', '莴笋', True), ('冰糖橙', '橘子', False),
('四季豆', '青条椒', False), ('生菜', '生菜(斤)', True)]:
check('%s vs %s → %s' % (a, b, exp), similar_item_name(a, b) is exp)
print('=== 7. 同名多行按数量自动择行 ===')
pick, note = eng._choose_same_name(hits('生菜'), 3.0)
check('生菜 3/1.5/10,实收 3.0 → 选 3 那单',
pick is not None and pick['order_code'] == 'ORD_0001',
'%s | %s' % (pick and pick['order_code'], note))
pick, note = eng._choose_same_name(hits('生菜'), 10.0)
check('实收 10.0 → 选 10 那单',
pick is not None and pick['order_code'] == 'ORD_0008', str(note))
pick, note = eng._choose_same_name(hits('圆萝卜'), 1.0)
check('圆萝卜 1/1,实收 1.0 → 自动选最早一单',
pick is not None and pick['order_code'] == 'ORD_0005', str(note))
pick, note = eng._choose_same_name(hits('生菜'), 6.0)
check('实收 6.0 对不上任何一行 → 转人工', pick is None, str(note))
print('=== 8. 同类商品闸门(AI 没给次选、但名单里有同类且数量也对得上)===')
ROWS.extend([row('r9', '橘子', 15, order='ORD_0009'),
row('r10', '冰糖橙', 15, order='ORD_0010'),
row('r11', '冬瓜', 51.72, order='ORD_0011')])
warn, extra = eng._same_kind_guard('橘子', hits('橘子'), 15.0)
check('橘子 15 斤 vs 冰糖橙 15 斤 → 转人工', warn is not None, str(warn))
warn, extra = eng._same_kind_guard('冬瓜', hits('冬瓜'), 8.28)
check('冬瓜 8.28 斤 vs 冬瓜 51.72 斤(同名已排除)→ 自动', warn is None, repr(warn))
warn, extra = eng._same_kind_guard('冰糖橙', hits('冰糖橙'), 15.0)
check('冰糖橙 → 同类橘子也是 15 斤 → 转人工', warn is not None, str(warn))
print('=== 9. 分不清时 same_name_auto=false → 转人工 ===')
eng.cfg['flow']['same_name_auto'] = False
pick, note = eng._choose_same_name(hits('圆萝卜'), 1.0)
check('关掉自动 → 要求人工选单', pick is None, str(note))
eng.cfg['flow']['same_name_auto'] = True
print()
print('全部通过 ✔' if ok else '有失败项 ✘')
sys.exit(0 if ok else 1)