Files

196 lines
6.5 KiB
Python

# -*- coding: utf-8 -*-
"""回归测试 v1.0.27:现场 11-04 晚 21:31~21:39 的重复入库事故。
事故(data/_remote/app.log):
21:31:42 藕 7.30kg → 14.60 斤 入库
21:33:10 藕 7.30kg → 14.60 斤 再入一次 ← 同物异名分支把已入库行捞回来
21:34:18 大白菜 11.90kg → 23.80 斤
21:35:04 大白菜 11.90kg → 23.80 斤 再入 ← 独立入库路径完全不查重
21:35:56 「已作废上一件(DONE)」 ← 已入库,作废是假的
21:36:09 复核失败 19.690 → -0.010 ← 负重量被当有效读数
"""
import io
import os
import sys
import time
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, 'same_name_auto': True,
'merge_same_name': True, 'dup_window_s': 180},
'scale': {'simulate': True},
'camera': {'simulate': True},
'vision': {'min_confidence': 60},
'server': {'mock': True},
}
logs = []
stock_calls = []
def ev(name, p=None):
logs.append((name, p))
def row(rid, name, qty, unit='斤', order='', indep=False, sku=''):
r = {'id': rid, 'item_name': name, 'unit': unit,
'prepare_qty': qty, 'remaining_qty': qty, 'order_code': order,
'sku_code': sku}
if indep:
r['processing_details'] = {'independent_inbound': True}
r['order_code'] = None
return r
class FakeJournal:
def begin(self, *a, **k):
return {'ok': True}
def update(self, *a, **k):
pass
def conclude(self, *a, **k):
pass
def has_unresolved(self):
return False
class FakeApi:
user_id = 'u1'
base_url = 'http://x'
def stock_in(self, sku, qty, unit, cost=0, production_date=None,
remark='', image_b64=None):
stock_calls.append((sku, qty, unit))
return {'stock_in': True}
class FakeScale:
def __init__(self, kg):
self.kg = kg
self.connected = True
def state(self, flow=None):
w = max(0.0, float(self.kg))
return {'weight': w, 'gross': w, 'tare': 0.0, 'zero': 0.0,
'present': w >= 0.05, 'fresh': True, 'connected': True,
'overload': False, 'paused': False, 'error': None}
class FakeRecog:
def __init__(self, name):
self.name = name
def identify_ex(self, path, names, timeout=8):
return {'name': self.name, 'conf': 85, 'alt': '', 'alt_conf': 0}
def synonym_match(self, detected, names):
return detected, '同一商品'
ok = True
def check(label, cond, detail=''):
global ok
if not cond:
ok = False
print((' ✔ ' if cond else ' ✘ ') + label + ((' ' + detail) if detail else ''))
eng = FlowEngine(CFG, ev)
eng.refresh_pending = lambda initial=False: None
eng.journal = FakeJournal()
eng.api = FakeApi()
eng._running = True
eng._print_async = lambda r, q: None
eng._record = lambda *a, **k: None
print('=== 1. 独立入库查重:同 SKU + 同重量 → 拒绝 ===')
eng._frozen_kg = 7.30
eng._last_ok = {'row_id': 'r6', 'name': '藕', 'kg': 7.30,
'at': time.monotonic(), 'unit': '斤', 'qty': 14.60,
'sku': 'SKU-OU'}
msg = eng._dup_stock_in(row('r6', '藕', 13, indep=True, sku='SKU-OU'))
check('同一个 SKU 同一堆货被判重复', bool(msg), str(msg))
check('提示里给出下一步', '这是一件新货' in (msg or ''))
msg2 = eng._dup_stock_in(row('r7', '大白菜', 20, indep=True, sku='SKU-BC'))
check('不同 SKU 不硬拦', msg2 is None, str(msg2))
eng._frozen_kg = 9.41
msg3 = eng._dup_stock_in(row('r6', '藕', 13, indep=True, sku='SKU-OU'))
check('重量不同(9.41 vs 7.30)不拦', msg3 is None, str(msg3))
print('=== 2. _start_stock_in 命中查重 → 一个请求都不发 ===')
eng._frozen_kg = 7.30
eng._last_ok = {'row_id': 'r6', 'name': '藕', 'kg': 7.30,
'at': time.monotonic(), 'unit': '斤', 'qty': 14.60,
'sku': 'SKU-OU'}
eng._stocked = {'r6': time.monotonic()}
before = len(stock_calls)
eng._start_stock_in(row('r6', '藕', 13, indep=True, sku='SKU-OU'), 14.60, 'AI')
time.sleep(0.15)
check('没有发 stock-in', len(stock_calls) == before, str(stock_calls[before:]))
check('停到 DONE 而不是继续提交', eng.state == 'DONE', eng.state)
print('=== 3. 同物异名分支不能把已入库的行捞回来 ===')
eng2 = FlowEngine(CFG, ev)
eng2.refresh_pending = lambda initial=False: None
eng2.journal = FakeJournal()
eng2.api = FakeApi()
eng2._running = True
eng2._print_async = lambda r, q: None
eng2._record = lambda *a, **k: None
eng2.rows = [row('r6', '藕', 13, indep=True, sku='SKU-OU'),
row('r8', '大白菜', 30, indep=True, sku='SKU-BC')]
eng2.recog = FakeRecog('藕')
eng2._stocked = {'r6': time.monotonic()} # 藕 刚入过
eng2._frozen_kg = 7.30
eng2._cycle_recog_path = 'x.jpg'
eng2.state = 'RECOGNIZE'
before = len(stock_calls)
eng2._run_recognition()
time.sleep(0.15)
check('识别到已入库的藕 → 不再入一次', len(stock_calls) == before,
str(stock_calls[before:]))
check('给出重复收货提示', any(e == 'result' and p and not p[0]
for e, p in logs[-6:]), str(logs[-3:]))
print('=== 4. 复核时秤上没货(-0.010 / 回零)→ 不要再空转 ===')
eng3 = FlowEngine(CFG, ev)
eng3.scale = FakeScale(-0.010)
eng3._frozen_kg = 19.69
eng3.rows = [row('r9', '莴笋(斤)', 7)]
eng3.state = 'SUBMIT'
okr = eng3._snapshot_ok(eng3.rows[0])
check('复核不通过', okr is False)
check('记下的是实际读数 0(不是 -0.010)',
abs(eng3._snap_now_kg) < 1e-6, str(eng3._snap_now_kg))
check('日志说明秤上已无货',
any(e == 'log' and '已无货' in str(p) for e, p in logs), str(logs[-2:]))
print('=== 5. 已完成(DONE)后刷新待办:不许再说「已作废」 ===')
eng3.scale = FakeScale(19.69)
eng3.state = 'DONE'
eng3._frozen_kg = 19.69
okr, msg = eng3.hard_refresh('刷新待办')
check('返回成功', okr is True, str(okr))
check('文案说明不能作废', '不能作废' in msg, msg)
check('不再出现「已作废上一件(DONE)」', '已作废上一件' not in msg, msg)
print('=== 6. _is_stocked 有时效:窗口外允许同名 SKU 再来一件 ===')
eng4 = FlowEngine(CFG, ev)
eng4._stocked = {'r10': time.monotonic() - 999}
check('窗口外不再屏蔽', eng4._is_stocked('r10') is False,
str(eng4._stocked))
eng4._stocked = {'r11': time.monotonic()}
check('窗口内屏蔽', eng4._is_stocked('r11') is True)
print()
print('全部通过 ✔' if ok else '有失败项 ✘')
sys.exit(0 if ok else 1)