270 lines
9.4 KiB
Python
270 lines
9.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""回归测试:AI 自动免称重(v1.0.29)。
|
|
|
|
现场诉求:不称重的商品(袋/包/桶,分拣站还包括斤类)放上去就该自动
|
|
入库/分拣,不要人去点屏幕上的按钮。这里验证:
|
|
|
|
① 唯一命中 + 把握够 → 直接提交,不弹核对页;
|
|
② 把握不足 / 同名多行 / 剩余量为 0 → 仍旧转核对页(不能瞎记账);
|
|
③ 画面变化 → 静止 → 自动起一轮(先动后静,静止画面只认一次);
|
|
④ 刚提交完的 DONE 观察窗内,取货的动作不算「换货」;
|
|
⑤ 提交过的行本地屏蔽,同一包装不会交两遍;
|
|
⑥ 关掉 noweigh.auto → 退回人工点按钮的老行为。
|
|
|
|
不需要秤/摄像头/网络。
|
|
"""
|
|
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__))))
|
|
|
|
import numpy as np
|
|
|
|
from core.flow import FlowEngine
|
|
|
|
CFG = {
|
|
'flow': {'confirm_before_submit': False, # 这份测试验的是 v1.0.29 自动提交路径
|
|
'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, 'dup_window_s': 180,
|
|
'stocked_block_s': 300},
|
|
'scale': {'simulate': True},
|
|
'camera': {'simulate': True},
|
|
'vision': {'min_confidence': 65},
|
|
'server': {'mock': True},
|
|
'noweigh': {'picking_allow_weight_unit': True, 'auto': 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
|
|
|
|
ROWS = [
|
|
{'id': 'r1', 'item_name': '香菇', 'unit': '袋', 'required_qty': 4,
|
|
'sku_code': 'SK1'},
|
|
{'id': 'r2', 'item_name': '土豆', 'unit': '斤', 'required_qty': 20},
|
|
{'id': 'r3', 'item_name': '圆萝卜', 'unit': '斤', 'required_qty': 2},
|
|
]
|
|
|
|
|
|
class FakeRecog:
|
|
def __init__(self, reply):
|
|
self.reply = reply
|
|
self.calls = []
|
|
|
|
def identify_ex(self, path, names, timeout=8, crop=None):
|
|
# crop 是 v1.0.55 加的(识别前裁剪比例,免称重会传 0.96);
|
|
# 假对象要跟着签名,否则主流程一调用就 TypeError
|
|
self.calls.append((path, list(names)))
|
|
self.crops = getattr(self, 'crops', []) + [crop]
|
|
return dict(self.reply)
|
|
|
|
def synonym_match(self, detected, names):
|
|
return '', '无'
|
|
|
|
|
|
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(station, reply=None):
|
|
logs.clear()
|
|
events.clear()
|
|
eng.cfg['noweigh']['auto'] = True
|
|
eng.station = station
|
|
eng.state = 'IDLE'
|
|
eng.rows = [dict(r) for r in ROWS]
|
|
eng._running = True
|
|
eng._aborted = False
|
|
eng.logged_in = True
|
|
eng.locked_msg = None
|
|
eng._count_mode = False
|
|
eng._auto_round = False
|
|
eng._count_qty = 0.0
|
|
eng._frozen_kg = 0.0
|
|
eng._frozen_row = None
|
|
eng._cycle_recog_path = 'shot.jpg'
|
|
eng._stocked = {}
|
|
eng._last_ok = None
|
|
eng._hold_kg = 0.0
|
|
eng._hold_floor = lambda: 0.05
|
|
eng._plan = []
|
|
eng.api = type('A', (), {'user_id': 'u1',
|
|
'base_url': 'http://test'})()
|
|
eng.journal = FakeJournal()
|
|
eng.recog = FakeRecog(reply or {'name': '香菇', 'conf': 88,
|
|
'alt': '', 'alt_conf': 0})
|
|
eng.frames = type('F', (), {
|
|
'capture_confirmed': lambda self: {'qualified': False,
|
|
'reason': 'sim', 'path': None},
|
|
'poke_focus': lambda self: None})()
|
|
eng.scale = type('S', (), {'state': lambda self, flow=None: {
|
|
'weight': 0.0, 'fresh': True, 'present': False, 'overload': False,
|
|
'zero': 0.0, 'tare': 0.0, 'raw': 0.0, 'at': 0, 'stable': True}})()
|
|
eng._record = lambda *a, **k: None
|
|
eng._print_async = lambda *a, **k: None
|
|
eng._upload_photo_later = lambda *a, **k: None
|
|
eng._mark_ok = lambda *a, **k: None
|
|
started = []
|
|
eng._start_auto_count_submit = (
|
|
lambda row, qty, source='AI自动': started.append(
|
|
(str(row.get('id')), float(qty), source)))
|
|
return started
|
|
|
|
|
|
def confirms():
|
|
return [p for e, p in events if e == 'count_confirm']
|
|
|
|
|
|
print('=== 1. 入库站:AI 认出唯一一行 → 直接提交,不弹核对页 ===')
|
|
started = reset('inspecting')
|
|
eng._auto_round = True
|
|
eng.state = 'COUNT_RECOGNIZE'
|
|
eng._run_count_recognition()
|
|
print(' state =', eng.state, ' 已提交 =', started, ' 弹窗数 =', len(confirms()))
|
|
assert started == [('r1', 4.0, 'AI自动')]
|
|
assert not confirms()
|
|
assert any('AI 自动入库' in str(p) for e, p in logs if e == 'log')
|
|
|
|
print('=== 2. 把握不足 → 转核对页,不自动提交 ===')
|
|
started = reset('inspecting', {'name': '香菇', 'conf': 40,
|
|
'alt': '', 'alt_conf': 0})
|
|
eng._auto_round = True
|
|
eng.state = 'COUNT_RECOGNIZE'
|
|
eng._run_count_recognition()
|
|
print(' state =', eng.state, ' 已提交 =', started, ' 弹窗数 =', len(confirms()))
|
|
assert not started and len(confirms()) == 1
|
|
assert eng.state == 'COUNT_CONFIRM'
|
|
|
|
print('=== 3. 同名多行 → 不猜,转核对页 ===')
|
|
rows = [{'id': 'a', 'item_name': '土豆', 'unit': '袋', 'required_qty': 2},
|
|
{'id': 'b', 'item_name': '土豆', 'unit': '袋', 'required_qty': 3}]
|
|
started = reset('inspecting', {'name': '土豆', 'conf': 90,
|
|
'alt': '', 'alt_conf': 0})
|
|
eng.rows = rows
|
|
eng._auto_round = True
|
|
eng.state = 'COUNT_RECOGNIZE'
|
|
eng._run_count_recognition()
|
|
print(' state =', eng.state, ' 已提交 =', started)
|
|
assert not started and eng.state == 'COUNT_CONFIRM'
|
|
|
|
print('=== 4. 待办剩余为 0 → 不提交(这行刚结过)===')
|
|
started = reset('inspecting')
|
|
eng.rows = [{'id': 'r1', 'item_name': '香菇', 'unit': '袋',
|
|
'required_qty': 0}]
|
|
eng._auto_round = True
|
|
eng.state = 'COUNT_RECOGNIZE'
|
|
eng._run_count_recognition()
|
|
print(' state =', eng.state, ' 已提交 =', started)
|
|
assert not started
|
|
assert any('剩余为 0' in str(p) for e, p in logs if e == 'log')
|
|
|
|
print('=== 5. 画面变化 → 静止 → 自动起一轮 ===')
|
|
started = reset('picking')
|
|
calls = []
|
|
eng.start_count_cycle = (lambda auto=False:
|
|
(calls.append(bool(auto)), (True, ''))[1])
|
|
|
|
|
|
def fake_sig_factory():
|
|
"""前两帧空,然后画面中间出现一块「货」(局部变化,不是整体变色)"""
|
|
seq = []
|
|
|
|
def sig():
|
|
seq.append(1)
|
|
a = np.zeros((24, 32), dtype='float32')
|
|
if len(seq) > 2:
|
|
a[8:16, 10:22] = 60.0
|
|
return a
|
|
return sig
|
|
|
|
|
|
eng._frame_sig = fake_sig_factory()
|
|
st = {'present': False}
|
|
for i in range(6):
|
|
eng._auto_scan(100.0 + i * 1.0, st)
|
|
print(' start_count_cycle 调用 =', calls, ' armed =', eng._auto_armed)
|
|
assert calls == [True]
|
|
assert events and any('画面已静止' in str(p) for e, p in logs if e == 'log')
|
|
|
|
print('=== 6. 静止画面只认一次:不再重复起轮 ===')
|
|
for i in range(6, 14):
|
|
eng._auto_scan(100.0 + i * 1.0, st)
|
|
print(' 再调用后的次数 =', len(calls))
|
|
assert len(calls) == 1
|
|
|
|
print('=== 7. 没有待办行时自动不空转 ===')
|
|
reset('inspecting')
|
|
eng.rows = []
|
|
calls = []
|
|
eng.start_count_cycle = (lambda auto=False:
|
|
(calls.append(bool(auto)), (True, ''))[1])
|
|
eng._frame_sig = fake_sig_factory()
|
|
for i in range(8):
|
|
eng._auto_scan(200.0 + i * 1.0, st)
|
|
print(' 调用次数 =', len(calls))
|
|
assert calls == []
|
|
|
|
print('=== 8. DONE 观察窗内取货不算换货 ===')
|
|
started = reset('inspecting')
|
|
import time as _t
|
|
eng._done_at = _t.monotonic()
|
|
calls = []
|
|
eng.start_count_cycle = (lambda auto=False:
|
|
(calls.append(bool(auto)), (True, ''))[1])
|
|
eng._frame_sig = fake_sig_factory()
|
|
eng.state = 'DONE'
|
|
for i in range(8):
|
|
eng._auto_scan(300.0 + i * 1.0, st)
|
|
print(' DONE 窗内调用次数 =', len(calls))
|
|
assert calls == []
|
|
|
|
print('=== 9. 提交过的行本地屏蔽,同一包装不会交两遍 ===')
|
|
reset('inspecting')
|
|
eng._stocked['r1'] = _t.monotonic()
|
|
names = [r['item_name'] for r in eng.noweigh_rows()]
|
|
print(' noweigh 行 =', names)
|
|
assert '香菇' not in names
|
|
|
|
print('=== 10. 关掉 noweigh.auto → 退回人工点按钮 ===')
|
|
reset('inspecting')
|
|
eng.cfg['noweigh']['auto'] = False
|
|
calls = []
|
|
eng.start_count_cycle = (lambda auto=False:
|
|
(calls.append(bool(auto)), (True, ''))[1])
|
|
for i in range(8):
|
|
eng._auto_scan(400.0 + i * 1.0, st)
|
|
print(' auto 关闭后调用次数 =', len(calls),
|
|
' auto_noweigh_on =', eng.auto_noweigh_on())
|
|
assert calls == [] and not eng.auto_noweigh_on()
|
|
|
|
print('=== 11. 入库站认到「袋」行(正常称重流程的门禁分支)→ 自动记账 ===')
|
|
started = reset('inspecting')
|
|
eng.state = 'RECOGNIZE'
|
|
eng._frozen_kg = 0.8
|
|
eng._run_recognition.__self__ # noqa (仅确认绑定正常)
|
|
# 直接打门禁分支:构造识别结果 → 走 _run_recognition 的完整入口太重,
|
|
# 这里用同样的判定链(_recog_guard + _auto_submit_count)验证
|
|
row = eng.rows[0]
|
|
warn, extra = eng._recog_guard('香菇', 88, '', [row], 0)
|
|
assert warn is None, warn
|
|
ok, why = eng._auto_submit_count(row, 4, '香菇', 88)
|
|
print(' ok =', ok, ' why =', why, ' 已提交 =', started)
|
|
assert ok and started and started[0][1] == 4.0
|
|
|
|
print('\n全部通过 ✔')
|