""" 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, record_projection_inconsistency_audit, ) 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='onhand_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_onhand_match_review' not in payload['available_actions'] assert 'download_excel' not in payload['available_actions'] assert len(payload['stage_nodes']) == 15 def test_projection_inconsistency_audit_uses_allowed_failure_result(app): """不一致审计必须以 audit_service 接受的 failure 落库,不能在 GET 路径反复报参数非法。""" with app.app_context(): with patch( 'app.services.shipping_notice.processing_ownership.append_shipping_audit_event', ) as append_mock, patch( 'app.services.shipping_notice.processing_ownership.db.session.flush', ): record_projection_inconsistency_audit({ 'projection_inconsistency_code': PROJECTION_INCONSISTENT_ERROR_CODE, 'work_item_no': 'OT-9', 'shipping_notice_no': 'SN-9', 'stage': 'waiting_review', }) assert append_mock.call_args.kwargs['result'] == 'failure' 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