feat(shipping-notice): 完成货代发货通知全门禁恢复MVP收口

完成全门禁恢复MVP版本交付:调整自动上传与重试配置,重构正式执行链路;修复多处代码bug,新增测试与审计工具;更新全流程文档与契约规范。
This commit is contained in:
hugo committed 2026-08-11 14:04:39 +08:00
1 parent 8e0bbf7a81
commit c94d173f92
181 files changed
+12947 -1724

No files matched your search

@@ -10,6 +10,7 @@ backend/tests/test_shipping_processing_projection_invariants.py
from __future__ import annotations
from app.services.shipping_notice.processing_projection_invariants import (
apply_user_projection_fields,
build_stage_nodes,
derivation_status_for_extraction_run,
normalize_action_code,
@@ -66,6 +67,22 @@ def test_completed_clears_write_actions_and_gate():
assert payload['available_actions'] == ['view_result']
def test_projection_fact_read_failure_is_fail_closed():
"""关键事实读取失败时必须进入人工诊断态,不得静默返回旧动作。"""
payload = reconcile_processing_projection_facts({
'status': 'running',
'stage': 'inventory_preparation',
'source_type': 'eml',
'projection_data_inconsistent': True,
'available_actions': ['retry_inventory_preparation'],
})
assert payload['status'] == 'waiting_human'
assert payload['retryable'] is False
assert payload['primary_action'] is None
assert payload['available_actions'] == []
assert payload['blocking_reason']['code'] == 'projection_data_inconsistent'
def test_normalize_unknown_action_codes():
"""未知 / 别名动作码映射到契约或丢弃。"""
assert normalize_action_code('retry_failed_documents') == 'retry'
@@ -133,6 +150,47 @@ def test_download_excel_requires_excel_ready_fact():
assert 'regenerate_excel' not in with_ready
def test_artifact_gate_overrides_stale_inventory_stage_and_action():
"""Gate 18 阻断时不得继续展示旧库存匹配阶段或 match_inventory。"""
payload = reconcile_processing_projection_facts({
'status': 'automating',
'stage': 'inventory_matching',
'source_type': 'order_task',
'shipping_notice_no': 'SN-1',
'current_gate': {
'gate_code': 'artifact_generation',
'status': 'blocked_retryable',
'next_action_code': 'regenerate_excel_artifact',
'blocking_reason_code': 'artifact_generation_failed',
},
'available_actions': ['match_inventory'],
})
assert payload['status'] == 'blocked'
assert payload['stage'] == 'excel_generating'
assert payload['stage_label_key'] == 'excel_generating'
assert payload['current_gate']['next_action_code'] == 'regenerate_excel_artifact'
assert payload['available_actions'] == ['regenerate_excel']
def test_artifact_gate_primary_action_uses_gate_not_legacy_stage():
"""用户态主动作必须投影为重新生成 Excel。"""
payload = apply_user_projection_fields({
'status': 'blocked',
'stage': 'excel_generating',
'source_type': 'order_task',
'shipping_notice_no': 'SN-1',
'current_gate': {
'gate_code': 'artifact_generation',
'status': 'blocked_retryable',
'next_action_code': 'regenerate_excel_artifact',
'blocking_reason_code': 'artifact_generation_failed',
},
'available_actions': ['regenerate_excel'],
})
assert payload['user_status'] == 'retryable'
assert payload['primary_action']['code'] == 'regenerate_excel'
def test_map_notice_to_processing_status_stage_domain_mapping():
"""BE-05:Notice 履约态映射属领域层,写/读共用。"""
from types import SimpleNamespace
@@ -207,3 +265,73 @@ def test_derive_available_actions_failed_adds_retry():
excel_ready=False,
)
assert 'retry' in actions
def test_waiting_human_reconcile_action_uses_projected_available_actions():
"""人工阻断存在统一对账动作时,主动作必须从当前投影动作列表中安全选取。"""
payload = apply_user_projection_fields({
'status': 'waiting_review',
'stage': 'inventory_matching',
'current_gate': {
'gate_code': 'inventory_candidates_calculated',
'status': 'blocked',
'blocking_reason_code': 'inventory_candidates_required',
},
'available_actions': ['reconcile_and_progress', 'view_details'],
})
# 关键断言:该分支不得因未定义局部变量返回 500,且必须选中统一恢复入口。
assert payload['user_status'] == 'waiting_human'
assert payload['primary_action']['code'] == 'reconcile_and_progress'
def test_waiting_human_without_reconcile_action_has_no_false_primary_action():
"""未知人工阻断未提供统一对账动作时,不得凭空生成可执行主动作。"""
payload = apply_user_projection_fields({
'status': 'waiting_review',
'stage': 'inventory_matching',
'current_gate': {
'gate_code': 'inventory_candidates_calculated',
'status': 'blocked',
'blocking_reason_code': 'inventory_candidates_required',
},
'available_actions': ['view_details'],
})
# 关键断言:没有正式动作事实时保持 None,避免前端展示无效恢复按钮。
assert payload['user_status'] == 'waiting_human'
assert payload['primary_action'] is None
def test_apply_user_projection_fields_fail_closes_inconsistent_projection():
"""事实读取异常即使发生在用户态计算后,也必须清空动作并进入诊断态。"""
payload = apply_user_projection_fields({
'status': 'running',
'stage': 'inventory_preparation',
'current_gate': {'gate_code': 'inventory_preparation', 'status': 'running'},
'available_actions': ['retry_inventory_preparation'],
'projection_data_inconsistent': True,
})
assert payload['user_status'] == 'waiting_human'
assert payload['retryable'] is False
assert payload['primary_action'] is None
assert payload['available_actions'] == []
assert payload['blocking_reason']['code'] == 'projection_data_inconsistent'
def test_inventory_milestones_are_projection_facts_not_runtime_gate_codes():
"""四个 MVP 库存里程碑必须由正式 Gate 和结构化事实派生,不能创建 APX Gate。"""
payload = apply_user_projection_fields({
'status': 'automating',
'stage': 'inventory_matching',
'current_gate': {'gate_code': 'inventory_matching', 'status': 'running'},
'available_actions': [],
})
assert payload['facts']['inventory_milestones'] == {
'candidates_calculated': 'completed',
'candidates_frozen': 'completed',
'snapshots_ready': 'completed',
'matched': 'pending',
}