Files
syops/backend/tests/test_shipping_notice_ui_state.py
T
hugo 5ef8329bd8 refactor(shipping-notice): 重构货代发货通知状态管理,移除废弃的process_status字段
统一货代发货通知的业务阶段为四个标准阶段:接收解析、订单复核、发货准备、上传完成
设置 ui_state 为前端唯一状态数据源,不再依赖 process_status 驱动业务流程
删除 shipping_notices 表的废弃 process_status 列及对应复合索引,新增数据库迁移脚本
替换全栈代码中所有对 process_status 的引用为生命周期、上传状态等领域字段
更新相关文档、测试用例与前端展示逻辑适配新的状态管理规则
2026-08-15 10:19:10 +08:00

241 lines
9.4 KiB
Python

"""
tests/test_shipping_notice_ui_state.py
用途:校验发货通知 UI State 摘要字段与 entry_surface 映射。
依赖:pytest、ShippingNotice、ui_state、state_machine constants。
逻辑:build_task_ui_state.summary 含 STP 可解释字段;复核 vs 待上传入口面正确。
"""
from app.models import ShippingNotice
from app.services.shipping_notice.constants import (
CP_EXPORT_READY,
CP_PARSE_REVIEW,
PH_LOW_CONF,
RS_PARSE_REVIEW,
)
from app.services.shipping_notice.ui_state import build_task_ui_state
from app.services.shipping_notice.ui_state import _unified_actions
_REQUIRED_SUMMARY_KEYS = {
"title",
"message",
"issues",
"stage",
"checkpoint_kind",
"current_todo",
"pause_reason",
"next_auto_step",
"event_at",
"entry_surface",
"allowed_actions",
}
def test_unified_actions_deduplicates_gate_primary_action():
"""Gate 主动作同时出现在展示列表时,只能渲染一次。"""
state = _unified_actions(
process_gate={
"primary_action": {"code": "open_order_review", "label": "进入订单复核"},
"action_presentation": [
{"code": "open_order_review", "label": "进入订单复核"},
{"code": "view_audit", "label": "查看审计", "kind": "view"},
],
},
action_buttons=[],
)
assert state["primary"]["code"] == "open_order_review"
assert [item["code"] for item in state["secondary"]] == ["view_audit"]
def test_unified_action_shape_contains_gate_version_fields():
"""统一动作 DTO 必须包含前端提交所需的版本与禁用字段。"""
state = _unified_actions(
process_gate={
"lock_version": 7,
"current_gate": {"evaluation_version": 3},
"primary_action": {"code": "retry_inventory_matching", "label": "重新匹配"},
"action_presentation": [],
},
action_buttons=[],
)
assert state["primary"]["expected_gate_version"] == 3
assert "disabled_reason" in state["primary"]
def test_build_task_ui_state_summary_has_required_keys(app) -> None:
"""summary 必须包含 STP 可解释字段全集。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-SUMMARY",
review_status=RS_PARSE_REVIEW,
pending_human_reason=PH_LOW_CONF,
source_type="manual",
)
state = build_task_ui_state(task)
assert set(_REQUIRED_SUMMARY_KEYS).issubset(state["summary"].keys())
assert state["checkpoint"]["code"] == CP_PARSE_REVIEW
assert state["checkpoint_kind"] == "must_human"
assert "stp_facts" not in state["summary"]
def test_build_task_ui_state_exposes_four_business_stages_and_unified_actions(app) -> None:
"""业务 UI 只消费四阶段与后端统一 actions,不再要求前端解释旧七步。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-STAGE-ACTIONS",
review_status=RS_PARSE_REVIEW,
pending_human_reason=PH_LOW_CONF,
source_type="manual",
)
state = build_task_ui_state(task)
assert state["stage"]["code"] == "order_review"
assert state["stage"]["index"] == 2
assert state["stage"]["total"] == 4
assert state["actions"]["primary"] is not None
assert state["actions"]["primary"]["code"] == "confirm"
assert all("code" in item and "kind" in item for item in state["actions"]["secondary"])
def test_build_task_ui_state_maps_domain_facts_to_fulfillment_and_upload_stages(app) -> None:
"""库存匹配/上传结果只映射四个业务阶段。"""
with app.app_context():
fulfillment = ShippingNotice(
task_no="SN-UI-FULFILLMENT",
source_type="manual",
match_status="partial",
inventory_lines_json=[{"part_number": "P-1"}],
)
upload = ShippingNotice(
task_no="SN-UI-UPLOAD",
source_type="manual",
upload_status="uploaded",
)
assert build_task_ui_state(fulfillment)["stage"]["code"] == "fulfillment_prep"
assert build_task_ui_state(upload)["stage"]["code"] == "upload_complete"
def test_entry_surface_parse_review_is_review_workbench(app) -> None:
"""解析复核应进入人工复核工作台。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-REVIEW",
review_status=RS_PARSE_REVIEW,
pending_human_reason=PH_LOW_CONF,
source_type="manual",
)
state = build_task_ui_state(task)
assert state["summary"]["entry_surface"] == "review_workbench"
def test_entry_surface_export_ready_is_task_workbench(app) -> None:
"""Excel 待人工上传登记应进入任务处理工作台(下载 + 登记)。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-EXPORT",
source_type="manual",
export_file_path="/tmp/shipping-export.xlsx",
export_file_name="shipping-export.xlsx",
)
state = build_task_ui_state(task)
assert state["checkpoint"]["code"] == CP_EXPORT_READY
assert state["checkpoint_kind"] == "wait_manual_upload"
assert state["summary"]["entry_surface"] == "task_workbench"
assert state["summary"].get("pause_reason_code") == "wait_manual_upload"
assert "登记凭据" in str(state["summary"].get("pause_reason") or "")
button_ids = [
item.get("code")
for item in [state["actions"].get("primary"), *(state["actions"].get("secondary") or [])]
if item
]
assert "download_excel" in button_ids
assert "mark_manual_uploaded" in button_ids
def test_stp_facts_candidates_frozen_projected(app) -> None:
"""多候选冻结任务须投影 stp_facts.frozen_candidate_codes。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-STP-FROZEN",
source_type="manual",
inventory_source_status="candidates_frozen",
inventory_source_method="stp_build_freeze",
step_payload_json={
"line_inventory_candidates": {
"source_codes": ["DBS_DUB", "DBS_RTM"],
"frozen_at": "build",
},
},
)
state = build_task_ui_state(task)
facts = state["summary"]["stp_facts"]
assert facts["inventory_source_status"] == "candidates_frozen"
assert facts["frozen_candidate_codes"] == ["DBS_DUB", "DBS_RTM"]
assert facts["multi_source_excel_block"] is False
def test_stp_facts_projects_line_resolutions(app) -> None:
"""stp_facts 须包含逐行 Reservation/料号/实际库存源/文件证据。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-STP-LINES",
source_type="manual",
match_status="fulfillable",
inventory_source_status="resolved",
inventory_source_code="DBS_DUB",
inventory_source_method="stp_line_match",
step_payload_json={
"line_inventory_resolutions": [
{
"line_index": 0,
"disposition": "ok",
"effective_reservation_id": "3000092917",
"part_number": "PN-1",
"resolved_inventory_source_code": "DBS_DUB",
"inventory_file_no": "INV-1",
"inventory_row_ref": {
"reservation_id": "3000092917",
"part_number": "PN-1",
"source_code": "DBS_DUB",
},
},
],
},
)
state = build_task_ui_state(task)
rows = state["summary"]["stp_facts"]["line_resolutions"]
assert rows[0]["effective_reservation_id"] == "3000092917"
assert rows[0]["part_number"] == "PN-1"
assert rows[0]["resolved_inventory_source_code"] == "DBS_DUB"
assert rows[0]["inventory_file_no"] == "INV-1"
def test_stp_facts_multi_source_excel_block_legacy_payload(app) -> None:
"""历史 multi_source_excel_block 载荷仍覆盖 pause_reason(兼容旧任务)。"""
from app.services.shipping_notice.constants import RS_SHORTAGE_REVIEW
with app.app_context():
task = ShippingNotice(
task_no="SN-UI-STP-MULTI",
review_status=RS_SHORTAGE_REVIEW,
pending_human_reason="shortage",
source_type="manual",
match_status="fulfillable",
inventory_source_status="candidates_frozen",
inventory_source_method="stp_line_match",
step_payload_json={
"multi_source_excel_block": True,
"resolved_ok_source_codes": ["DBS_DUB", "DBS_RTM"],
"line_inventory_resolutions": [
{"line_index": 0, "disposition": "ok", "resolved_inventory_source_code": "DBS_DUB"},
{"line_index": 1, "disposition": "ok", "resolved_inventory_source_code": "DBS_RTM"},
],
},
)
state = build_task_ui_state(task)
summary = state["summary"]
assert summary["pause_reason_code"] == "multi_source_excel_block"
assert summary["stp_facts"]["multi_source_excel_block"] is True
assert summary["entry_surface"] == "task_workbench"