feat(in-transit-transfer): 外部调用切换集成连接器并新增调拨类型与在途仓编码

- 退役 erp_adapter/gateway_adapter:外部查询/写经 capability_client 走集成连接器,端点凭据与并发超时由客户端实例管理
- 域能力代码注册 capabilities:入参出参 Schema、幂等语义与读写标志随域代码版本化,组合根 create_app 幂等注册
- 新增调拨业务类型 transfer_type(在途仓至HUB/HUB维修出/HUB间)冻结枚举,迁移落 CheckConstraint 数据库兜底
- 外部仓库映射新增 in_transit_warehouse_code 拨出库位来源,管理端库存映射页同步维护
- 单据命名外部化:erp_* 字段与索引改称 external_*,单据号格式交由连接器映射,测试数据同步换牌
- 收料归档文件可用性判定收敛 receipt_file_gate 单一事实来源(当天抓取/保留期/清理态)
- 同步 chat 调拨卡片、useChatTransfer、e2e、全套服务/API/Job 测试与域文档
This commit is contained in:
hugo committed 2026-09-23 00:49:38 +08:00
1 parent 31591e663e
commit 9959b06acd
97 files changed
+3525 -1489

No files matched your search

@@ -1,13 +1,12 @@
"""在途调拨测试的可控外部系统替身:FakeERP / FakeRPA / FakeWeCom / FakeClock。
"""在途调拨测试的可控外部系统替身:FakeCapabilityClient / FakeRPA / FakeWeCom / FakeClock。
用途:让 L0/L1 测试在不接触真实 RPA、ERP、企业微信的前提下覆盖全部业务分支,
用途:让 L0/L1 测试在不接触真实 RPA、外部系统、企业微信的前提下覆盖全部业务分支,
并把每一次外部调用完整记录到 call log,供断言副作用与调用次数。
依赖:标准库(dataclasses / decimal / zoneinfo)。
逻辑:FakeERP 以 invoice_no 为业务键维护账面库存与已生成单据;
create_transfer 按注入的 scenario 返回 success / duplicate_success /
business_failed / timeout_before_commit / timeout_after_commit;
所有调用均追加一条 ErpCallRecord(operation_id、invoice_no、erp_org、
来源仓、目标仓、数量、调用序号、方法、返回状态、ERP 单号)。
依赖:标准库(dataclasses / decimal / zoneinfo)、capability_client(域类型)。
逻辑:FakeCapabilityClient 以 (料号, 拨出库位, 拨出批号) 三元组维护账面库存;
query_stock 一次返回组内全部命中行;create_transfer 按注入的 scenario
返回已分类的 TransferWriteResult(success / duplicate_success /
business_failed / technical_failed / result_unknown)。
"""
from __future__ import annotations
@@ -18,7 +17,12 @@ from decimal import Decimal
from typing import Any
from zoneinfo import ZoneInfo
from app.services.in_transit_transfer.erp_adapter import ErpWriteTimeoutError
from app.services.in_transit_transfer.capability_client import (
ExternalQueryTimeoutError,
StockQueryResult,
StockRow,
TransferWriteResult,
)
class FakeClock:
@@ -41,154 +45,182 @@ class FakeClock:
@dataclass(frozen=True)
class ErpCallRecord:
"""FakeERP 单次调用的事实记录;测试断言优先读取该记录。"""
class ExternalCallRecord:
"""FakeCapabilityClient 单次调用的事实记录;测试断言优先读取该记录。"""
operation_id: str
invoice_no: str
erp_org: str
from_warehouse: str
to_warehouse: str
quantity: Decimal
external_org: str
call_seq: int
returned_status: str
method: str
# 查询调用:记录目标元组;写调用:invoice_no + 数量。
targets: tuple[tuple[str, str, str], ...] = ()
invoice_no: str = ""
document_no: str | None = None
class FakeERP:
"""可注入账面库存与响应场景的 ERP 替身;每次调用写入 call log。"""
class FakeCapabilityClient:
"""可注入账面库存与响应场景的外部系统客户端替身;每次调用写入 call log。"""
def __init__(self) -> None:
self.inventory: dict[str, dict[str, Any]] = {}
self.inventory: dict[tuple[str, str, str], Decimal] = {}
self.documents: dict[str, str] = {}
self.verified_posted: dict[str, bool] = {}
self.query_scenario: str = "normal"
self.write_scenario: str = "success"
# 按 Invoice 覆盖写场景(Key=invoice_no);未配置时回落到 write_scenario。
self.write_scenarios: dict[str, str] = {}
self.call_log: list[ErpCallRecord] = []
self.call_log: list[ExternalCallRecord] = []
self._seq = 0
# -- 控制注入 ----------------------------------------------------------
def seed_inventory(self, invoice_no: str, quantity: str | int | Decimal) -> None:
"""设置指定 Invoice 的账面库存。"""
self.inventory[str(invoice_no).strip()] = {"quantity": Decimal(str(quantity))}
def seed_stock(self, material_no: str, from_warehouse: str, lot_no: str,
quantity: str | int | Decimal) -> None:
"""设置指定 (料号, 拨出库位, 拨出批号) 的账面库存。"""
key = (str(material_no).strip(), str(from_warehouse).strip(), str(lot_no).strip())
self.inventory[key] = Decimal(str(quantity))
def clear_stock(self) -> None:
"""清空全部账面库存(模拟 外部系统 库存被移走/未上账)。"""
self.inventory.clear()
def seed_document(self, invoice_no: str, document_no: str, *, posted: bool = True) -> None:
"""预置某 Invoice 已存在的 ERP 单据(幂等重复场景)。"""
"""预置某 Invoice 已存在的单据(幂等重复场景)。"""
self.documents[str(invoice_no).strip()] = document_no
self.verified_posted[str(invoice_no).strip()] = posted
def set_query_scenario(self, scenario: str) -> None:
"""normal | inventory_not_found | timeout。"""
"""normal | inventory_not_found | timeout | error。"""
self.query_scenario = scenario
def set_write_scenario(self, scenario: str) -> None:
"""success | duplicate_success | business_failed | timeout_before_commit | timeout_after_commit。"""
self.write_scenario = scenario
# -- ERP 能力 ----------------------------------------------------------
def query_inventory(self, *, operation_id: str, run_no: str, attempt_no: int,
invoice_no: str, erp_org: str, from_warehouse: str,
to_warehouse: str, quantity: Decimal) -> dict[str, Any]:
"""按 Invoice 返回账面库存;timeout 场景抛超时异常。"""
# -- 外部系域能力 ----------------------------------------------------------
def query_stock(self, *, operation_id: str, external_org: str,
targets: list) -> StockQueryResult:
"""按组织批量返回库存行;timeout/error 场景按客户端语义抛错。"""
self._seq += 1
key = str(invoice_no).strip()
target_keys = [
(str(t.material_no).strip(), str(t.from_warehouse).strip(), str(t.lot_no).strip())
for t in targets
]
if self.query_scenario == "timeout":
raise TimeoutError(f"query timeout operation_id={operation_id}")
raise ExternalQueryTimeoutError(f"query timeout operation_id={operation_id}")
if self.query_scenario == "error":
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "error", "query_inventory")
self.call_log.append(record)
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, "error", "query_stock",
targets=tuple(target_keys),
))
raise RuntimeError("binding not found for capability query_inventory")
if self.query_scenario == "inventory_not_found" or key not in self.inventory:
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "inventory_not_found", "query_inventory")
self.call_log.append(record)
return {"found": False, "quantity": Decimal("0")}
qty = self.inventory[key]["quantity"]
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "success", "query_inventory")
self.call_log.append(record)
return {"found": True, "quantity": qty}
rows: list[StockRow] = []
for key in target_keys:
if self.query_scenario == "inventory_not_found":
continue
if key in self.inventory:
rows.append(StockRow(
material_no=key[0], from_warehouse=key[1], lot_no=key[2],
quantity=self.inventory[key],
))
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq,
"success" if rows else "inventory_not_found", "query_stock",
targets=tuple(target_keys),
))
return StockQueryResult(rows=tuple(rows))
def create_transfer(self, *, operation_id: str, run_no: str, attempt_no: int,
invoice_no: str, erp_org: str, from_warehouse: str,
to_warehouse: str, quantity: Decimal) -> dict[str, Any]:
"""原子建单/审核/过账;按注入场景返回结果并记录调用。"""
invoice_no: str, external_org: str, from_warehouse: str,
to_warehouse: str, quantity: Decimal) -> TransferWriteResult:
"""原子建单/审核/过账;按注入场景返回已分类结果并记录调用。"""
self._seq += 1
key = str(invoice_no).strip()
scenario = self.write_scenarios.get(key, self.write_scenario)
if scenario == "timeout_before_commit":
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "timeout_before_commit", "create_transfer")
self.call_log.append(record)
raise ErpWriteTimeoutError(
"write timeout before commit", committed=False, operation_id=operation_id,
)
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, "timeout_before_commit",
"create_transfer", invoice_no=key,
))
# 可证明未落单 → 明确技术失败(不产生单据)。
return TransferWriteResult(status="technical_failed", error_code="EXTERNAL_TIMEOUT_BEFORE_COMMIT")
if self.write_scenario == "timeout_after_commit" or scenario == "timeout_after_commit":
# 已落单但无法确认:调用方必须投 result_unknown,禁止自动重试。
self.documents.setdefault(key, f"TRF-{self._seq:05d}")
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "timeout_after_commit", "create_transfer",
self.documents[key])
self.call_log.append(record)
raise ErpWriteTimeoutError(
"write timeout after commit", committed=True,
document_no=self.documents[key], operation_id=operation_id,
document_no = self.documents[key]
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, "timeout_after_commit",
"create_transfer", invoice_no=key, document_no=document_no,
))
return TransferWriteResult(
status="result_unknown", error_code="EXTERNAL_TIMEOUT_AFTER_COMMIT",
document_no=document_no,
)
if scenario == "business_failed" or self.write_scenario == "business_failed":
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "business_failed", "create_transfer")
self.call_log.append(record)
return {"status": "business_failed", "code": "ERP_BUSINESS_REJECT", "rolled_back": True}
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, "business_failed",
"create_transfer", invoice_no=key,
))
return TransferWriteResult(
status="business_failed", error_code="EXTERNAL_BUSINESS_REJECT",
error_message="rejected by external system",
)
if key in self.documents:
document_no = self.documents[key]
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "duplicate_success", "create_transfer",
document_no)
self.call_log.append(record)
return {
"status": "duplicate_success",
"document_no": document_no,
"verified_posted": self.verified_posted.get(key, False),
}
posted = self.verified_posted.get(key, False)
status = "duplicate_success" if posted else "duplicate_unverified"
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, status,
"create_transfer", invoice_no=key, document_no=document_no,
))
return TransferWriteResult(
status=status, document_no=document_no, verified_posted=posted,
error_code=None if posted else "TRANSFER_DUPLICATE_NOT_VERIFIED",
)
document_no = f"TRF-{self._seq:05d}"
self.documents[key] = document_no
self.verified_posted[key] = True
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "success", "create_transfer", document_no)
self.call_log.append(record)
return {"status": "success", "document_no": document_no, "posted": True}
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, "success",
"create_transfer", invoice_no=key, document_no=document_no,
))
return TransferWriteResult(status="success", document_no=document_no, verified_posted=True)
def verify_duplicate(self, *, operation_id: str, run_no: str, attempt_no: int,
invoice_no: str, erp_org: str, from_warehouse: str,
to_warehouse: str, quantity: Decimal) -> dict[str, Any]:
invoice_no: str, external_org: str, from_warehouse: str,
to_warehouse: str, quantity: Decimal) -> TransferWriteResult:
"""核实重复 Invoice 原单状态(已审核过账/未确认)。"""
self._seq += 1
key = str(invoice_no).strip()
if key not in self.documents:
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, "duplicate_unverified", "verify_duplicate")
self.call_log.append(record)
return {"status": "duplicate_unverified", "document_no": None, "verified_posted": False}
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, "duplicate_unverified", "verify_duplicate",
invoice_no=key,
))
return TransferWriteResult(
status="duplicate_unverified", error_code="TRANSFER_DUPLICATE_NOT_VERIFIED",
)
document_no = self.documents[key]
posted = self.verified_posted.get(key, False)
status = "duplicate_success" if posted else "duplicate_unverified"
record = ErpCallRecord(operation_id, key, erp_org, from_warehouse, to_warehouse,
quantity, self._seq, status, "verify_duplicate", document_no)
self.call_log.append(record)
return {"status": status, "document_no": document_no, "verified_posted": posted}
self.call_log.append(ExternalCallRecord(
operation_id, external_org, self._seq, status, "verify_duplicate",
invoice_no=key, document_no=document_no,
))
return TransferWriteResult(
status=status, document_no=document_no,
verified_posted=posted if posted else None,
error_code=None if posted else "TRANSFER_DUPLICATE_NOT_VERIFIED",
)
# -- 断言辅助 ----------------------------------------------------------
def calls(self, *, invoice_no: str | None = None) -> list[ErpCallRecord]:
"""按 Invoice 过滤调用记录(默认返回全部)。"""
def calls(self, *, invoice_no: str | None = None) -> list[ExternalCallRecord]:
"""按 Invoice 过滤调用记录(默认返回全部;查询调用 invoice_no 为空串)。"""
if invoice_no is None:
return list(self.call_log)
return [record for record in self.call_log if record.invoice_no == str(invoice_no).strip()]
def write_calls(self) -> list[ErpCallRecord]:
def write_calls(self) -> list[ExternalCallRecord]:
"""仅返回 create_transfer 调用记录(写副作用断言)。"""
return [record for record in self.call_log if record.method == "create_transfer"]