feat(shipping-notice): 收敛订单处理状态与履约门禁

This commit is contained in:
hugo committed 2026-09-07 09:42:12 +08:00
1 parent bffcd55b40
commit 70dd0051b8
153 files changed
+10916 -4296

No files matched your search

@@ -1,45 +1,45 @@
"""
tests/test_shipping_notice_state_machine.py
用途:校验发货通知 checkpoint 内部流程映射与 STP checkpoint_kind。
依赖:pytest、state_machine、constants、ShippingNotice。
逻辑:validate_registry 无错误;关键检查点 kind 正确;resolve_task_ui_state 对简单任务属性可解析。
用途:校验发货通知位置→用户模式查找表与 Gate/review 薄回退。
依赖:pytest、state_machine、constants、ShippingOrderTask。
逻辑:validate_registry 无错误;关键位置 user_mode 正确;resolve_task_ui_state
无 Gate 时仅凭 review_status 回退。
"""
from types import SimpleNamespace
from app.models import ShippingOrderTask
from app.services.shipping_notice.constants import (
CP_EXPORT_READY,
CP_PARSE_REVIEW,
CP_READY_EXPORT,
CP_UPLOADED,
POSITION_EXPORT_READY,
POSITION_PARSE_REVIEW,
POSITION_UPLOADED,
PH_LOW_CONF,
RS_PARSE_REVIEW,
RS_REJECTED,
)
from app.services.shipping_notice.state_machine import (
CheckpointKind,
checkpoint_kind_for,
UserMode,
resolve_task_ui_state,
resolve_task_ui_state_from_fields,
user_mode_for_position,
validate_registry,
)
def test_validate_registry_has_no_errors() -> None:
"""checkpoint 仅维护内部流程分类。"""
"""位置→用户模式查找表非空且自洽。"""
assert validate_registry() == []
def test_checkpoint_kinds_for_stp_stop_points() -> None:
"""STP 关键检查点 kind:导出 Excel 后待人工上传登记 / 解析复核 / 已上传终态。"""
assert checkpoint_kind_for(CP_EXPORT_READY) == CheckpointKind.WAIT_MANUAL_UPLOAD
assert checkpoint_kind_for(CP_PARSE_REVIEW) == CheckpointKind.MUST_HUMAN
assert checkpoint_kind_for(CP_UPLOADED) == CheckpointKind.COMPLETE
def test_user_modes_for_stp_stop_points() -> None:
"""STP 关键位置 user_mode:导出就绪待人工 / 解析复核 / 已上传终态。"""
assert user_mode_for_position(POSITION_EXPORT_READY) == UserMode.WAIT_MANUAL_UPLOAD
assert user_mode_for_position(POSITION_PARSE_REVIEW) == UserMode.MUST_HUMAN
assert user_mode_for_position(POSITION_UPLOADED) == UserMode.COMPLETE
def test_resolve_task_ui_state_parse_review(app) -> None:
"""pending_review + parse_review 应映射为 must_human / parse_review。"""
"""无 Gate 时 review_status=parse_review 应映射为 must_human / parse_review。"""
with app.app_context():
task = ShippingOrderTask(
task_no="OT-SM-PARSE",
@@ -47,94 +47,41 @@ def test_resolve_task_ui_state_parse_review(app) -> None:
review_status=RS_PARSE_REVIEW,
pending_human_reason=PH_LOW_CONF,
)
ui = resolve_task_ui_state_from_fields(
review_status=task.review_status,
pending_human_reason=task.pending_human_reason,
customer_resolution_status=None,
order_snapshot_id=None,
match_status=None,
export_file_path=None,
has_dynamic_order_lines=False,
)
assert ui.checkpoint == CP_PARSE_REVIEW
assert ui.checkpoint_kind == CheckpointKind.MUST_HUMAN
ui = resolve_task_ui_state(task)
assert ui.position == POSITION_PARSE_REVIEW
assert ui.user_mode == UserMode.MUST_HUMAN
def test_resolve_task_ui_state_export_ready(app) -> None:
"""matched 且已有 export_file_path 应为 wait_manual_upload(待人工上传登记)。"""
def test_resolve_task_ui_state_rejected(app) -> None:
"""无 Gate 时 review_status=rejected → complete / rejected。"""
with app.app_context():
ui = resolve_task_ui_state_from_fields(
review_status=None,
pending_human_reason=None,
customer_resolution_status=None,
order_snapshot_id=1,
match_status=None,
export_file_path="/tmp/shipping-export.xlsx",
has_dynamic_order_lines=False,
task = ShippingOrderTask(
task_no="OT-SM-REJECT",
user_id=1,
review_status=RS_REJECTED,
)
assert ui.checkpoint == CP_EXPORT_READY
assert ui.checkpoint_kind == CheckpointKind.WAIT_MANUAL_UPLOAD
assert not hasattr(ui, 'human_options')
ui = resolve_task_ui_state(task)
assert ui.position == 'rejected'
assert ui.user_mode == UserMode.COMPLETE
def test_resolve_task_ui_state_uploaded_complete(app, monkeypatch) -> None:
"""upload_status=uploaded 应为 complete。"""
def test_resolve_without_review_status_is_thin(app) -> None:
"""无 Gate 且无 review_status 时不从领域标量投影位置(薄回退)。"""
with app.app_context():
task = SimpleNamespace(upload_status="uploaded", lifecycle_status="active")
# [FIX] 上传完成由可审计事实 has_verified_upload_success 裁决,不再读取 upload_status 字段。
monkeypatch.setattr(
'app.services.shipping_notice.upload_attempt_service.has_verified_upload_success',
lambda _task: True,
)
ui = resolve_task_ui_state_from_fields(
review_status=None,
pending_human_reason=None,
customer_resolution_status=None,
order_snapshot_id=1,
match_status=None,
export_file_path=None,
has_dynamic_order_lines=False,
upload_req_task=task,
)
assert ui.checkpoint == CP_UPLOADED
assert ui.checkpoint_kind == CheckpointKind.COMPLETE
task = ShippingOrderTask(task_no="OT-SM-THIN", user_id=1)
ui = resolve_task_ui_state(task)
assert ui.position is None
assert ui.user_mode == UserMode.AUTO_CONTINUE
def test_resolve_with_mocked_simple_namespace_attributes(monkeypatch) -> None:
"""无 DB 会话时可用简易命名空间验证 uploaded 终态分支。"""
def test_resolve_with_mocked_simple_namespace_attributes() -> None:
"""无 DB 会话时简易命名空间:无 review_status → 薄回退 None/auto。"""
task = SimpleNamespace(
upload_status="uploaded",
review_status=None,
pending_human_reason=None,
customer_resolution_status=None,
order_snapshot_id=None,
inventory_lines_json=None,
match_status=None,
export_file_path=None,
current_order_extraction_id=None,
step_payload_json=None,
)
# [FIX] 上传完成由可审计事实 has_verified_upload_success 裁决,不再读取 upload_status 字段。
monkeypatch.setattr(
'app.services.shipping_notice.upload_attempt_service.has_verified_upload_success',
lambda _task: True,
order_task_id=None,
id=None,
)
ui = resolve_task_ui_state(task) # type: ignore[arg-type]
assert ui.checkpoint_kind == CheckpointKind.COMPLETE
assert ui.checkpoint == CP_UPLOADED
def test_order_task_inventory_checkpoint_uses_structured_facts(monkeypatch) -> None:
"""OT 库存 JSON 为空时,状态机仍须按 Link/File/Match 事实进入导出准备。"""
from app.models import ShippingOrderTask
from app.services.shipping_notice import state_machine
task = ShippingOrderTask(task_no='OT-SM-STRUCTURED', user_id=1, stage='inventory_matching')
monkeypatch.setattr(state_machine, '_order_task_inventory_facts', lambda _task: {
'inventory_ready': True,
'match_complete': True,
'match_sufficient': True,
'has_shortage': False,
})
assert state_machine.resolve_task_ui_state(task).checkpoint == CP_READY_EXPORT
assert ui.user_mode == UserMode.AUTO_CONTINUE
assert ui.position is None