Files

142 lines
4.6 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 -*-
"""回归测试:同一堆货不准重复收货(19:21 事故场景)。
不需要秤/摄像头/网络:把 scale.state、refresh_pending、_begin_cycle
全部替换成假实现,只验证状态机在「重量一模一样、中途没空过」时
会不会又起一轮流程。
"""
import sys
import io
import json
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, 'dup_window_s': 180},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 65},
'server': {'mock': True},
}
logs = []
eng = FlowEngine(CFG, lambda ev, p=None: logs.append((ev, p)))
eng.refresh_pending = lambda initial=False: None # 不联网
class FakeScale:
weight = 0.0
def state(self, flow=None):
w = self.weight
return {'weight': w, 'fresh': True, 'present': w >= 0.05,
'overload': False, 'zero': 0.0, 'tare': 0.0,
'raw': w, 'at': 0, 'stable': True}
fs = FakeScale()
eng.scale = fs
class FakeFrames:
def ready(self):
return True
def poke_focus(self):
pass
eng.frames = FakeFrames()
begins = []
eng._begin_cycle = lambda w: begins.append(w)
def tick(w, secs=0.0):
fs.weight = w
if secs:
import time
eng._last_move_at = time.monotonic() - secs
eng._done_at = time.monotonic() - secs
eng._tick()
return eng.state
print('=== 场景 1:提交成功后在 DONE,货没取走、超时回待机 ===')
eng.state = 'DONE'
eng._frozen_kg = 4.11
eng._after_success = lambda *a, **k: None # 只用下面手工设置
eng._hold_kg = 4.11
eng._last_ok = {'row_id': 'row-土豆', 'name': '去皮小土豆', 'kg': 4.11,
'at': __import__('time').monotonic(), 'unit': '斤',
'qty': 8.22}
eng._done_at = __import__('time').monotonic() - 13
fs.weight = 4.11
eng._tick()
print(' DONE 超时后状态 =', eng.state, '_hold_kg =', eng._hold_kg)
assert eng.state == 'IDLE', eng.state
assert eng._hold_kg == 4.11, eng._hold_kg
print('=== 场景 2:重量 4.110(一模一样,没动过)→ 不许起流程 ===')
n0 = len(begins)
st = tick(4.11, secs=2)
print(' 状态 =', st, '起流程次数 =', len(begins) - n0)
assert st == 'IDLE' and len(begins) == n0, '应该拦住重复收货!'
print(' 日志:', [p for e, p in logs if e == 'log'][-1])
print('=== 场景 3:取走货(秤上只剩 0.2kg 空筐)→ 解锁 ===')
st = tick(0.2, secs=2)
print(' 状态 =', st, '_hold_kg =', eng._hold_kg)
assert eng._hold_kg == 0.0
eng.state = 'IDLE' # 0.2kg 空筐已经进了 WEIGHING,这里只测解锁
print('=== 场景 4:放上另一袋同规格 4.110 → 应正常起流程 ===')
n0 = len(begins)
st = tick(4.11, secs=2) # IDLE → WEIGHING
st = tick(4.11, secs=2) # 稳定后起流程
print(' 状态 =', st, '起流程次数 =', len(begins) - n0)
assert len(begins) == n0 + 1, '取走后再放同重量应放行'
print('=== 场景 5:DONE 里没拿空就加货到 6.4(同批续称)→ 超时后应放行 ===')
eng.state = 'DONE'
eng._frozen_kg = 4.11
eng._hold_kg = 4.11
eng._done_at = __import__('time').monotonic()
n0 = len(begins)
tick(6.40, secs=1)
print(' 1 秒后 _hold_kg =', eng._hold_kg, '状态 =', eng.state,
'(仍停在 DONE,等取货或 12 秒超时)')
tick(6.40, secs=13) # 超时回待机
print(' 超时后状态 =', eng.state)
tick(6.40, secs=2) # 重量比冻结值多 2.3kg → 是新货/续称
tick(6.40, secs=2)
print(' 起流程次数 =', len(begins) - n0)
assert len(begins) >= n0 + 1, '同一批继续加货应该能重新起流程'
print('=== 场景 6:提交前查重(不同行、重量几乎相同)===')
eng._hold_kg = 0.0
eng.state = 'IDLE'
eng._frozen_kg = 4.11
msg = eng._dup_guard({'id': 'row-冰糖橙', 'item_name': '冰糖橙'})
print(' 查重结果:', msg)
assert msg and '疑似重复' in msg
print('=== 场景 7:同一行续收(分批)不算重复 ===')
eng.state = 'IDLE'
msg2 = eng._dup_guard({'id': 'row-土豆', 'item_name': '去皮小土豆'})
print(' 查重结果:', msg2)
assert msg2 is None
print('=== 场景 8:重量明显不同(7.5kg)不算重复 ===')
eng._frozen_kg = 7.50
msg3 = eng._dup_guard({'id': 'row-橘子', 'item_name': '橘子'})
print(' 查重结果:', msg3)
assert msg3 is None
print('\n全部通过 ✔')