72 lines
2.6 KiB
Python
72 lines
2.6 KiB
Python
# ============================================================
|
|
# MD-05 SAP 集成桩黄金测试
|
|
# ============================================================
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from server.agent_core.intent import parse_fast
|
|
from server.aps_domain.flex import run_flex_schedule
|
|
from server.aps_domain.sap_sync import apply_inbound, apply_outbound, preview_inbound, preview_outbound
|
|
from server.integrations.sap_stub import reset_sap_client
|
|
from server.state.seed import seed_world
|
|
|
|
|
|
class _MemStore:
|
|
def __init__(self, data):
|
|
self.data = data
|
|
|
|
def next_id(self, kind: str) -> int:
|
|
key = f"_c_{kind}"
|
|
self.data[key] = self.data.get(key, 3000) + 1
|
|
return self.data[key]
|
|
|
|
def save(self):
|
|
pass
|
|
|
|
|
|
def test_inbound_creates_flex_orders(tmp_path: Path):
|
|
mirror = tmp_path / "sap_mirror.json"
|
|
reset_sap_client(mirror)
|
|
store = _MemStore(seed_world())
|
|
before = len(store.data.get("flexOrders") or [])
|
|
prev = preview_inbound(store.data)
|
|
assert len(prev["newOrders"]) >= 1
|
|
wire = next(m for m in store.data["flexMaterials"] if m["code"] == "WIRE-HV")
|
|
old_stock = wire["stock"]
|
|
r = apply_inbound(store, actor="test")
|
|
assert r["created"]
|
|
assert len(store.data["flexOrders"]) == before + len(r["created"])
|
|
assert all(o.get("source") == "SAP" for o in r["created"])
|
|
assert all(o.get("externalAufnr") for o in r["created"])
|
|
assert wire["stock"] != old_stock or "WIRE-HV" in r["stockUpdated"]
|
|
# 二次入站应跳过已映射订单
|
|
prev2 = preview_inbound(store.data)
|
|
assert all(s["reason"] == "已同步" for s in prev2["skipOrders"][:3])
|
|
assert len(prev2["newOrders"]) == 0
|
|
|
|
|
|
def test_outbound_idempotent(tmp_path: Path):
|
|
mirror = tmp_path / "sap_mirror.json"
|
|
client = reset_sap_client(mirror)
|
|
store = _MemStore(seed_world())
|
|
run_flex_schedule(store, sort_mode="BOTTLENECK", actor="test")
|
|
prev = preview_outbound(store.data)
|
|
assert prev["items"]
|
|
r1 = apply_outbound(store, actor="test")
|
|
assert r1["pushed"]
|
|
assert len(r1["duplicates"]) == 0
|
|
n1 = client.status()["receiptCount"]
|
|
r2 = apply_outbound(store, actor="test")
|
|
assert len(r2["pushed"]) == 0
|
|
assert len(r2["duplicates"]) == len(r1["pushed"])
|
|
assert client.status()["receiptCount"] == n1
|
|
|
|
|
|
def test_intent_sap_phrases():
|
|
world = seed_world()
|
|
assert parse_fast("SAP连接状态", world).intent == "sap.status"
|
|
assert parse_fast("从SAP拉单", world).intent == "sap.sync.inbound"
|
|
assert parse_fast("同步SAP", world).intent == "sap.sync.inbound"
|
|
assert parse_fast("回写SAP", world).intent == "sap.sync.outbound"
|