Files
syops/backend/tests/test_shipping_processing_projection_invariants.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

391 lines
15 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
backend/tests/test_shipping_processing_projection_invariants.py
用途:验收统一投影 status/stage/gate/action 不变量与 15 阶节点事实。
依赖:processing_projection_invariants、pytest。
逻辑:不连库;断言 Gate 复核强制 waiting_review、Run completed 非全流程完成、
动作契约映射与 stage_nodes 权威输出。
"""
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,
reconcile_processing_projection_facts,
)
def test_run_completed_is_not_workflow_completed_before_review():
"""识别 Run completed + candidate_creating 不得映射为全流程 completed。"""
assert derivation_status_for_extraction_run('completed', stage='candidate_creating') == 'processing'
assert derivation_status_for_extraction_run('completed', stage='waiting_review') == 'waiting_review'
assert derivation_status_for_extraction_run('completed', stage='completed') == 'completed'
def test_open_order_review_gate_forces_waiting_review_stage():
"""Gate 要求进入复核时,stage/status 必须对齐 waiting_review。"""
payload = reconcile_processing_projection_facts({
'status': 'processing',
'stage': 'candidate_creating',
'source_type': 'direct_pdf',
'current_gate': {
'gate_code': 'order_freeze_readiness',
'status': 'blocked',
'next_action_code': 'open_order_review',
'blocking_reason_code': 'waiting_review',
'attempt_no': 0,
},
'available_actions': ['view_details', 'open_order_review'],
})
assert payload['status'] == 'waiting_review'
assert payload['stage'] == 'waiting_review'
assert payload['available_actions'][0] == 'open_order_review'
current = next(n for n in payload['stage_nodes'] if n['stage'] == 'waiting_review')
assert current['status'] == 'blocked'
assert next(n for n in payload['stage_nodes'] if n['stage'] == 'email_parsing')['status'] == 'skipped'
def test_completed_clears_write_actions_and_gate():
"""status=completed 时 stage 必须 completed,且不得残留写操作。"""
payload = reconcile_processing_projection_facts({
'status': 'completed',
'stage': 'excel_generating',
'source_type': 'eml',
'current_gate': {
'gate_code': 'x',
'status': 'blocked',
'next_action_code': 'open_order_review',
},
'available_actions': ['open_order_review', 'retry'],
})
assert payload['stage'] == 'completed'
assert payload['current_gate'] is None
assert 'open_order_review' not in payload['available_actions']
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'
assert normalize_action_code('open_fulfillment') == 'view_result'
assert normalize_action_code('not_a_real_action') is None
def test_stage_nodes_length_is_fifteen():
"""stage_nodes 恒为 15 阶权威事实。"""
nodes = build_stage_nodes(
stage='inventory_matching',
status='automating',
source_type='eml',
)
assert len(nodes) == 15
assert nodes[11]['stage'] == 'inventory_matching'
assert nodes[11]['status'] == 'running'
assert nodes[10]['status'] == 'passed'
assert nodes[12]['status'] == 'pending'
def test_invalidated_from_stage_marks_nodes_pending():
"""BE-01:改单失效起点及之后(当前游标之前)不得再标 passed。"""
nodes = build_stage_nodes(
stage='inventory_matching',
status='processing',
source_type='eml',
invalidated_from_stage='inventory_source_resolving',
)
# inventory_source_resolving 在当前游标之前且位于失效起点,应标 pending。
inv_node = next(n for n in nodes if n['stage'] == 'inventory_source_resolving')
assert inv_node['status'] == 'pending'
waiting = next(n for n in nodes if n['stage'] == 'waiting_review')
assert waiting['status'] == 'pending'
# 失效起点之前仍可为 passed。
dup_node = next(n for n in nodes if n['stage'] == 'duplicate_checking')
assert dup_node['status'] == 'passed'
current = next(n for n in nodes if n['stage'] == 'inventory_matching')
assert current['status'] == 'running'
def test_download_excel_requires_excel_ready_fact():
"""BE-04:仅 stage=excel_generating 不得推出 download_excel。"""
from app.services.shipping_notice.processing_projection_invariants import (
derive_available_actions,
)
without_ready = derive_available_actions(
status='blocked',
stage='excel_generating',
gate=None,
excel_ready=False,
)
assert 'download_excel' not in without_ready
assert 'regenerate_excel' in without_ready
with_ready = derive_available_actions(
status='blocked',
stage='freight_uploading',
gate={'next_action_code': 'wait_manual_upload'},
excel_ready=True,
)
assert 'download_excel' in with_ready
assert 'open_manual_upload' in with_ready
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
from app.services.shipping_notice.processing_projection_invariants import (
map_notice_to_processing_status_stage,
)
uploaded = SimpleNamespace(
current_step='', upload_status='uploaded', export_file_path=None,
)
assert map_notice_to_processing_status_stage(uploaded) == ('completed', 'completed')
waiting = SimpleNamespace(
current_step='', upload_status='pending', export_file_path='/x.xlsx',
)
assert map_notice_to_processing_status_stage(waiting) == ('automating', 'freight_uploading')
matching = SimpleNamespace(
current_step='step5_match', upload_status='', match_status='matching', export_file_path=None,
)
assert map_notice_to_processing_status_stage(matching) == ('automating', 'inventory_matching')
def test_map_notice_unknown_status_fail_closed():
"""BE-03: unknown Notice status must fail-closed, not order_creating."""
from types import SimpleNamespace
from app.services.shipping_notice.processing_projection_invariants import (
map_notice_to_processing_status_stage,
)
unknown = SimpleNamespace(
current_step='',
upload_status='',
export_file_path=None,
)
assert map_notice_to_processing_status_stage(unknown) == ('blocked', 'waiting_review')
def test_derive_available_actions_blocked_without_retry_gate():
"""BE-04: business blocked without retry Gate must not append retry."""
from app.services.shipping_notice.processing_projection_invariants import (
derive_available_actions,
)
actions = derive_available_actions(
status='blocked',
stage='inventory_source_resolving',
gate={
'gate_code': 'inventory_source',
'status': 'blocked',
'next_action_code': 'confirm_inventory_source',
},
excel_ready=False,
)
assert 'retry' not in actions
assert 'open_inventory_source_review' in actions
def test_derive_available_actions_failed_adds_retry():
"""BE-04: technical failed should allow retry by default."""
from app.services.shipping_notice.processing_projection_invariants import (
derive_available_actions,
)
actions = derive_available_actions(
status='failed',
stage='document_parsing',
gate=None,
excel_ready=False,
)
assert 'retry' in actions
def test_inventory_snapshot_missing_projects_retryable_inventory_action():
"""库存快照缺失属于可恢复技术阻断,用户态必须保留重试动作。"""
payload = apply_user_projection_fields({
'status': 'blocked',
'stage': 'inventory_preparation',
'source_type': 'order_task',
'current_gate': {
'gate_code': 'inventory_preparation',
'status': 'blocked_retryable',
'next_action_code': 'retry_inventory_preparation',
'blocking_reason_code': 'inventory_snapshot_missing',
},
'retry_decision': {
'retryable': True,
'reason_code': 'inventory_snapshot_missing',
},
'available_actions': ['retry_inventory_preparation'],
})
assert payload['user_status'] == 'retryable'
assert payload['retryable'] is True
# 主动作必须保留 Gate 原始码,前端才能提交到正确的库存准备执行器。
assert payload['primary_action'] == {
'code': 'retry_inventory_preparation',
'i18n_key': 'workspace.shippingNotice.processGate.actionCodes.retry_inventory_preparation',
'parameters': {},
}
def test_inventory_file_empty_keeps_gate_retry_as_the_only_primary_action():
"""库存文件缺失必须保留 Gate 重试,不得降级为线外上传或空按钮。"""
payload = apply_user_projection_fields({
'status': 'processing',
'stage': 'inventory_fetching',
'source_type': 'order_task',
'shipping_notice_no': 'SN-1',
'current_gate': {
'gate_code': 'inventory_preparation',
'status': 'blocked_manual',
'next_action_code': 'retry_inventory_preparation',
'blocking_reason_code': 'inventory_file_empty',
},
'available_actions': ['retry_inventory_preparation'],
})
assert payload['user_status'] == 'waiting_human'
assert payload['primary_action'] == {
'code': 'retry_inventory_preparation',
'i18n_key': 'workspace.shippingNotice.processGate.actionCodes.retry_inventory_preparation',
'parameters': {},
}
assert payload['available_actions'] == ['retry_inventory_preparation']
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',
}