Files

203 lines
6.9 KiB
Python

# -*- coding: utf-8 -*-
"""回归测试:识别先定位、人工点头才记账(v1.0.31)
现场诉求:AI 认错一次账就错了,服务端又没有冲正接口。所以把「看」这一步
塞回流程 —— 认出哪一行就把界面跳过去高亮,人点「确认记账」才提交;
点别的行 = 改判;一个都不弹窗。
验证:
① 认出唯一一行 → 不提交,进 CONFIRM 并发出 focus(带行号/数量/单位)
② 点别的行 = 改判,数量按新行重算(件行 → 按待办剩余量)
③ 点「确认记账」才真的提交(提交参数、状态都对)
④ 放弃 → 回 DONE 且把这堆货锁住(不再重复识别)
⑤ 数件的行:数量可改,改完按改的数提交
⑥ 关掉 confirm_before_submit → 回到「认出即提交」的老行为
⑦ 人工兜底(识别不出/歧义)也不再走弹窗,改发 focus
不需要秤/摄像头/网络。
"""
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,
'stable_delta_kg': 0.03, 'done_timeout_s': 12,
'done_lift_off_kg': 0.2, 'confirm_before_submit': True,
'auto_submit': True},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 60, 'auto_over_pct': 30},
'server': {'mock': True},
'noweigh': {'picking_allow_weight_unit': True},
}
EVENTS = []
def emit(ev, p=None):
EVENTS.append((ev, p))
eng = FlowEngine(CFG, emit)
eng.refresh_pending = lambda initial=False: None
eng.logged_in = True
eng._running = True
eng.server_config = {'weigh_match_tolerance_mode': 'percent',
'weigh_match_tolerance_value': 50}
ROWS = [
{'id': 'r1', 'item_name': '圆萝卜', 'unit': '斤', 'required_qty': 20,
'pick_code': 'PK001', 'sku_code': 'S1'},
{'id': 'r2', 'item_name': '土豆', 'unit': '包', 'required_qty': 3,
'pick_code': 'PK001', 'sku_code': 'S2'},
{'id': 'r3', 'item_name': '大白菜', 'unit': '斤', 'required_qty': 10,
'pick_code': 'PK002', 'sku_code': 'S3'},
]
class FakeJournal:
def begin(self, *a, **k):
return {'op': a[0] if a else 'op'}
def update(self, *a, **k):
pass
def conclude(self, *a, **k):
pass
def has_unresolved(self):
return False
def reset():
EVENTS.clear()
eng.rows = [dict(r) for r in ROWS]
eng.state = 'IDLE'
eng._focus = None
eng._count_mode = False
eng._count_qty = 0.0
eng._frozen_kg = 0.0
eng._frozen_row = None
eng._aborted = False
eng._running = True
eng._stocked = {}
eng._last_ok = None
eng._plan = []
eng.journal = FakeJournal()
eng._cycle_conf = 88
eng._cycle_detected = ''
eng.submitted = []
eng.stocked = []
eng._start_submit = lambda row, biz, src: (
eng.submitted.append((row['id'], biz, src)),
eng._set_state('SUBMIT'))
eng._start_stock_in = lambda row, biz, src: (
eng.stocked.append((row['id'], biz, src)),
eng._set_state('SUBMIT'))
def focus_ev():
for e, p in reversed(EVENTS):
if e == 'focus':
return p
return None
print('=== 1. 秤上 10kg 认出圆萝卜 → 不提交,进 CONFIRM ===')
reset()
eng._frozen_kg = 10.0 # 10kg = 20 斤
eng._cycle_recog_path = None
eng._decide_auto(eng.rows[0], 20.0, 'AI')
f = focus_ev()
print(' state =', eng.state, ' / submitted =', eng.submitted)
print(' focus =', {k: f[k] for k in ('mode', 'row_id', 'business', 'unit')})
assert eng.state == 'CONFIRM'
assert not eng.submitted, '识别完不该直接提交'
assert f and f['row_id'] == 'r1' and f['business'] == 20 and f['mode'] == 'weigh'
assert f['qty_editable'] is False
assert len(f['rows']) == 3
print('=== 2. 点「土豆(包)」= 改判:按待办 3 包,数量可改 ===')
ok, msg = eng.pick_focus('r2')
f = focus_ev()
print(' 改判 →', ok, msg, '| mode =', f['mode'], 'biz =', f['business'])
assert ok and f['row_id'] == 'r2' and f['mode'] == 'count'
assert f['business'] == 3 and f['qty_editable'] is True
assert eng.state == 'CONFIRM' and not eng.submitted
print('=== 3. 再改回圆萝卜 → 仍按秤读数 20 斤,确认后提交 ===')
eng.pick_focus('r1')
f = focus_ev()
assert f['mode'] == 'weigh' and f['business'] == 20
ok, msg = eng.confirm_focus()
print(' 确认 →', ok, msg, '| submitted =', eng.submitted, '| state =', eng.state)
assert ok and eng.submitted == [('r1', 20.0, 'AI识别+人工确认')]
assert eng.state == 'SUBMIT'
assert eng._focus is None
assert any(e == 'focus_clear' for e, _ in EVENTS)
print('=== 4. 数件行改数量:3 → 2,按 2 包提交 ===')
reset()
eng._frozen_kg = 0.0
eng._to_focus(eng.rows[1], 3.0, detected='土豆', conf=90, mode='count')
assert eng.state == 'CONFIRM'
ok, msg = eng.confirm_focus(2)
print(' 确认 2 包 →', ok, msg, '| submitted =', eng.submitted)
assert ok and eng.submitted == [('r2', 2.0, '人工确认')]
assert abs(eng._frozen_kg - 0.0) < 1e-9 # 包没有单重 → 重量 0
print('=== 5. 放弃 → 回 DONE,并锁住这堆货(不重复识别)===')
reset()
eng._frozen_kg = 10.0
eng._to_focus(eng.rows[0], 20.0, detected='圆萝卜', conf=90)
ok = eng.cancel_focus('放弃')
print(' cancel →', ok, '| state =', eng.state, '| hold =', eng._hold_kg)
assert ok and eng.state == 'DONE' and abs(eng._hold_kg - 10.0) < 1e-9
assert not eng.submitted
print('=== 6. 关掉 confirm_before_submit → 回到「认出即提交」 ===')
reset()
CFG['flow']['confirm_before_submit'] = False
eng._frozen_kg = 10.0
eng._decide_auto(eng.rows[0], 20.0, 'AI')
print(' state =', eng.state, '| submitted =', eng.submitted,
'| focus =', focus_ev())
assert eng.submitted == [('r1', 20.0, 'AI')]
assert focus_ev() is None
CFG['flow']['confirm_before_submit'] = True
print('=== 7. 识别不出/歧义:不弹窗,改发 focus ===')
reset()
eng._frozen_kg = 6.0
eng._to_manual(None, [])
f = focus_ev()
print(' 无候选 → state =', eng.state, '| row_id =', f['row_id'],
'| mode =', f['mode'])
assert eng.state == 'CONFIRM' and f['row_id'] is None and f['mode'] == 'count'
assert not any(e == 'confirm' for e, _ in EVENTS), '不该再发弹窗事件'
reset()
eng._frozen_kg = 6.0
eng._to_manual('莴笋', [eng.rows[0], eng.rows[2]], warn='同名歧义')
f = focus_ev()
print(' 有候选 → row_id =', f['row_id'], '| candidates =', f['candidates'],
'| warn =', f['warn'])
assert f['row_id'] == 'r1' and f['candidates'] == ['r1', 'r3']
assert '同名歧义' == f['warn']
print('=== 8. 已提交过的行不出现在待办里(防重复)===')
reset()
import time as _t
eng._stocked = {'r1': _t.monotonic()}
names = [r['item_name'] for r in eng._live_rows()]
print(' live rows =', names)
assert '圆萝卜' not in names and len(names) == 2
print('\n全部通过 ✔')