Files

176 lines
6.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""回归测试:AI 识别纠错(v1.0.30)。
针对 11-04 晚现场日志复盘出的三类问题:
① 超差(应 70 斤实 3 斤)以前弹了人工页还照旧提交 —— 现在必须停下等人;
② 自动记账的超收闸门(应 13 收 21.28 斤 → 转人工),正常 ±30% 内不受影响;
③ 同类商品闸门收紧:95% 高把握豁免、数量匹配用 ±30% 严格口径;
④ 净重 0.090 kg 空秤杂波不起识别流程;
⑤ AI 原文名 ≠ 名单名时按名单标准名记帐并留痕。
不需要秤/摄像头/网络。
"""
import sys
import io
import os
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
CFG = {
'flow': {'min_scale_kg': 0.05, 'stable_ms': 400, 'settle_ms': 650,
'auto_submit': True, 'tolerance_policy': 'block_auto'},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 60, 'same_kind_guard': True,
'same_kind_max_conf': 88, 'same_kind_tol': 30,
'auto_over_pct': 30, 'min_recog_kg': 0.15},
'server': {'mock': True},
}
logs = []
events = []
eng = FlowEngine(CFG, lambda ev, p=None: (logs.append((ev, p)),
events.append((ev, p))))
eng.refresh_pending = lambda initial=False: None
def reset(station, rows, server_tol=80):
logs.clear()
events.clear()
eng.station = station
eng.state = 'IDLE'
eng.rows = [dict(r) for r in rows]
eng.server_config = {'weigh_match_tolerance_mode': 'percent',
'weigh_match_tolerance_value': server_tol}
eng._running = True
eng._aborted = False
eng.logged_in = True
eng._count_mode = False
eng._frozen_kg = 0.0
eng._frozen_row = None
eng._cycle_recog_path = 'shot.jpg'
eng._cycle_conf = None
eng._stocked = {}
eng._last_ok = None
eng._hold_kg = 0.0
eng._plan = []
eng.api = type('A', (), {'user_id': 'u1',
'base_url': 'http://test'})()
eng.recog = type('R', (), {
'identify_ex': lambda self, *a, **k: {'name': None, 'conf': None,
'alt': '', 'alt_conf': None},
'synonym_match': lambda self, a, b: ('', '无')})()
eng.frames = type('F', (), {'poke_focus': lambda self: None})()
eng._record = lambda *a, **k: None
eng._print_async = lambda *a, **k: None
submitted = []
eng._start_submit = (lambda row, business, source:
submitted.append((str(row.get('id')),
round(float(business), 2),
source)))
return submitted
def logs_text():
return ' | '.join(str(p) for e, p in logs if e == 'log')
ROW = {'id': 'r1', 'item_name': '白萝卜', 'unit': '斤', 'required_qty': 70}
print('=== 1. 超差(应70 实3):弹人工页并停下,绝不自动提交 ===')
sub = reset('picking', [ROW])
eng._frozen_kg = 1.5
eng._decide_auto(ROW, 3.0, 'AI')
print(' state =', eng.state, ' 提交 =', sub)
assert eng.state == 'CONFIRM', '超差必须停在人工确认(v1.0.31 起是主界面确认条)'
assert sub == [], '超差后不允许继续自动提交'
assert '重量超差' in logs_text()
print('=== 2. 超收 30% 闸门:应13 收21.28 → 转人工(入库/分拣都拦)===')
row13 = dict(ROW, required_qty=13)
for st in ('inspecting', 'picking'):
sub = reset(st, [row13])
eng._frozen_kg = 10.64
eng._decide_auto(row13, 21.28, 'AI')
print(' [%s] state = %s 提交 = %s' % (st, eng.state, sub))
assert eng.state == 'CONFIRM' and sub == []
assert '超出应' in logs_text()
print('=== 3. 正常范围(±30% 内):不再自动提交,等人工确认 ===')
sub = reset('inspecting', [row13])
eng._frozen_kg = 6.75
eng._decide_auto(row13, 13.5, 'AI')
print(' state =', eng.state, ' 提交 =', sub)
assert eng.state == 'CONFIRM' and sub == []
ok, msg = eng.confirm_focus()
print(' 点确认 →', ok, msg, ' 提交 =', sub)
assert ok and sub == [('r1', 13.5, 'AI识别+人工确认')]
print('=== 4. 同类商品闸门收紧 ===')
rows = [{'id': 'a', 'item_name': '黄瓜', 'unit': '斤', 'required_qty': 3.4},
{'id': 'b', 'item_name': '冬瓜', 'unit': '斤', 'required_qty': 8}]
# 4a. AI 把握 95 → 豁免,不再打扰
sub = reset('picking', rows)
eng._cycle_conf = 95
warn, extra = eng._same_kind_guard('黄瓜', [rows[0]], 3.28)
print(' conf=95 warn =', warn)
assert warn is None
# 4b. conf 82 + 同类行数量 ±30% 内且更贴 → 仍要拦(真歧义)
sub = reset('picking', rows)
eng._cycle_conf = 82
eng.rows = [dict(rows[0], required_qty=4.0), dict(rows[1],
required_qty=3.0)]
warn, extra = eng._same_kind_guard('黄瓜', [eng.rows[0]], 3.28)
print(' conf=82 warn =', warn)
assert warn is not None
# 4c. conf 82 + 同类行数量差 58%(3.28 vs 8)→ 不拦
sub = reset('picking', rows)
eng._cycle_conf = 82
warn, extra = eng._same_kind_guard('黄瓜', [rows[0]], 3.28)
print(' conf=82 (冬瓜差58%) warn =', warn)
assert warn is None
print('=== 5. 空秤杂波(0.09 kg)不起识别流程 ===')
sub = reset('picking', [ROW])
eng.frames = type('F', (), {'poke_focus': lambda self: None})()
eng._capture_worker = lambda w: None # 抓拍线程替身
eng.state = 'IDLE'
eng._begin_cycle(0.09)
print(' state =', eng.state)
assert eng.state == 'IDLE', '轻货不该进入 CAPTURE'
assert '低于识别下限' in logs_text()
eng._begin_cycle(2.0)
print(' 2.0 kg → state =', eng.state)
assert eng.state == 'CAPTURE'
print('=== 6. AI 原文名 ≠ 名单名 → 按名单标准名记帐并留痕 ===')
sub = reset('inspecting', [{'id': 'w', 'item_name': '莴笋(斤)',
'unit': '斤', 'required_qty': 13}])
eng._cycle_conf = 85
eng._aborted = False
eng._running = True
decided = []
eng._decide_auto = (lambda row, business, source:
decided.append((str(row.get('id')),
round(float(business), 2), source)))
eng._frozen_kg = 10.64
eng._run_recognition = eng._run_recognition
eng.recog = type('R', (), {
'identify_ex': lambda self, *a, **k: {'name': '莴笋', 'conf': 85,
'alt': '', 'alt_conf': None},
'synonym_match': lambda self, a, b: ('', '无')})()
eng._stocked = {}
eng._last_ok = None
eng._run_recognition()
print(' 日志:', [str(p) for e, p in logs if e == 'log'][:3])
assert any('按名单标准名「莴笋(斤)」' in str(p)
for e, p in logs if e == 'log')
assert decided == [('w', 21.28, 'AI')]
print('\n全部通过 ✔')