feat(shipping-notice): 收敛复核状态隔离、STP 重试幂等与 XFA 合成预览标注

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-07 02:58:28 +08:00
1 parent 84a881b554
commit 74270adc22
149 files changed
+20111 -3871

No files matched your search

@@ -0,0 +1,89 @@
"""
tests/test_shipping_processing_ownership.py
用途:验收投影不一致 fail-closed 与任务归属 ConflictError。
依赖:processing_ownership、processing_projection_invariants、pytest、unittest.mock。
逻辑:不要求完整履约 E2E;ownership 用 mock 链路;不一致用 reconcile 断言动作剥离。
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from app.core.errors import ConflictError
from app.services.shipping_notice.processing_ownership import assert_notice_task_ownership
from app.services.shipping_notice.processing_projection_invariants import (
PROJECTION_INCONSISTENT_ERROR_CODE,
TASK_OWNERSHIP_MISMATCH_ERROR_CODE,
detect_notice_pre_create_inconsistency,
reconcile_processing_projection_facts,
)
def test_detect_notice_pre_create_inconsistency():
"""有正式号 + 建单前 stage → True;履约 stage → False。"""
assert detect_notice_pre_create_inconsistency(
stage='waiting_review',
shipping_notice_no='SN-1',
) is True
assert detect_notice_pre_create_inconsistency(
stage='inventory_matching',
shipping_notice_no='SN-1',
) is False
assert detect_notice_pre_create_inconsistency(
stage='waiting_review',
shipping_notice_no=None,
) is False
def test_reconcile_marks_inconsistency_and_strips_fulfillment():
"""服务级拆链:reconcile 后断言 stage/actions/inconsistency 码。"""
payload = reconcile_processing_projection_facts({
'status': 'blocked',
'stage': 'customer_resolving',
'source_type': 'eml',
'work_item_no': 'OT-9',
'shipping_notice_no': 'SN-LEFT',
'progress_version': 4,
'current_gate': {
'gate_code': 'customer',
'status': 'blocked',
'next_action_code': 'confirm_customer',
'blocking_reason_code': 'customer_unresolved',
'attempt_no': 1,
},
'available_actions': ['confirm_customer', 'match_inventory', 'download_excel'],
}, excel_ready=True)
assert payload['stage'] == 'customer_resolving'
assert payload['projection_inconsistency_code'] == PROJECTION_INCONSISTENT_ERROR_CODE
assert 'confirm_customer' in payload['available_actions']
assert 'open_inventory_match_review' not in payload['available_actions']
assert 'download_excel' not in payload['available_actions']
assert len(payload['stage_nodes']) == 15
def test_assert_ownership_noop_without_expected_context():
"""未传期望 OT/Case/Run 时不拦截(兼容旧客户端)。"""
task = SimpleNamespace(id=1, task_no='SN-1', order_task_id=None)
assert_notice_task_ownership(task)
def test_assert_ownership_mismatch_raises_conflict(app):
"""期望 OT 与 Notice 挂接 OT 不一致时抛出固定错误码。"""
task = SimpleNamespace(id=7, task_no='SN-7', order_task_id=99)
linked_ot = SimpleNamespace(task_no='OT-REAL', extraction_run_id=None, candidate_id=None)
with app.app_context():
with patch('app.services.shipping_notice.processing_ownership.db') as mock_db, \
patch('app.services.shipping_notice.processing_ownership.append_shipping_audit_event'):
mock_db.session.get.return_value = linked_ot
mock_db.session.flush = MagicMock()
with pytest.raises(ConflictError) as exc:
assert_notice_task_ownership(
task,
expected_order_task_no='OT-OTHER',
actor_id=1,
)
assert exc.value.code == TASK_OWNERSHIP_MISMATCH_ERROR_CODE