闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
210 lines
7.8 KiB
Python
210 lines
7.8 KiB
Python
"""
|
||
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 (
|
||
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_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_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(
|
||
process_status='uploaded', current_step='', upload_status='', export_file_path=None,
|
||
)
|
||
assert map_notice_to_processing_status_stage(uploaded) == ('completed', 'completed')
|
||
|
||
waiting = SimpleNamespace(
|
||
process_status='waiting_upload', current_step='', upload_status='', export_file_path='/x.xlsx',
|
||
)
|
||
assert map_notice_to_processing_status_stage(waiting) == ('automating', 'freight_uploading')
|
||
|
||
matching = SimpleNamespace(
|
||
process_status='matching', current_step='step5_match', upload_status='', 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(
|
||
process_status='totally_unknown_xyz',
|
||
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
|