""" tests/test_shipping_uploaded_gate_closure.py 用途:锁定「已上传任务确认后,Execution 全部门禁必须收口」与「阶段投影不得被陈旧 Execution Gate 回退」的一致性。 背景: - 订单生命周期工作台按 has_verified_upload_success / Notice 阶段裁决 → 展示「通知完成」; - 批次中心 / 对话卡按 OT work-item 的 Execution.current_gate_code 覆盖 stage (shipping_processing_projection),若该 Gate 仍是前置履约 Gate,会把 stage 回退成「通知准备」。 - sync_fulfillment_gates_from_task 的 STEP 1A 只在 _derived_fulfillment_status==uploaded 时收口全部前置 Gate; 因而 upload_status 未落到 uploaded(预约单未全量/迟回调覆盖)时,前置 Gate 保留,阶段投影即回退。 """ from __future__ import annotations from app import db from app.models import ShippingNotice, ShippingOrderTask, ShippingProcessExecution, ShippingProcessGate, User from app.services.shipping_notice.process_gate_bridge import sync_fulfillment_gates_from_task from app.services.shipping_notice.process_gate_service import ensure_execution_for_workflow_version def _mk_execution(task: ShippingOrderTask) -> ShippingProcessExecution: """建正式 20-Gate Execution,并把 inventory_sufficiency 置为人工阻断,让任务停留在「通知准备」。""" execution = ensure_execution_for_workflow_version(task_id=int(task.id)) task.active_process_execution_id = int(execution.id) suff = ShippingProcessGate.query.filter_by( execution_id=execution.id, gate_code='inventory_sufficiency', ).one() suff.status = 'blocked_manual' suff.blocking_reason_code = 'inventory_shortage_review_required' execution.status = 'blocked' execution.current_gate_code = 'inventory_sufficiency' db.session.commit() return execution def test_step1a_closes_all_gates_when_uploaded(app, monkeypatch): """upload_status=uploaded → STEP 1A 收口全部前置 Gate,Execution 落到 completed。""" user = User(username='uploaded-gate-user', email='uploaded-gate@example.com', password_hash='x') db.session.add(user); db.session.flush() task = ShippingOrderTask(task_no='OT-UPLOADED-GATE', user_id=user.id) db.session.add(task) db.session.flush() execution = _mk_execution(task) monkeypatch.setattr( 'app.services.shipping_notice.process_gate_bridge._derived_fulfillment_status', lambda _task: 'uploaded', ) sync_fulfillment_gates_from_task(execution=execution, actor_type='user', actor_id=1) db.session.commit() db.session.refresh(execution) open_cnt = ShippingProcessGate.query.filter( ShippingProcessGate.execution_id == int(execution.id), ShippingProcessGate.status.notin_(('passed', 'skipped')), ).count() assert open_cnt == 0, f'已上传任务仍有 {open_cnt} 个 Gate 未收口' assert execution.current_gate_code is None assert execution.status == 'completed' def test_uploaded_fact_closes_gates_despite_stale_upload_pending_reason(app, monkeypatch): """上传已成功时,历史 upload_rpa_pending 不得将正式 Gate 重新投影为阻断。""" user = User(username='uploaded-stale-user', email='uploaded-stale@example.com', password_hash='x') db.session.add(user); db.session.flush() task = ShippingOrderTask(task_no='OT-UPLOADED-STALE-PENDING', user_id=user.id, pending_human_reason='upload_rpa_pending') db.session.add(task) db.session.flush() execution = _mk_execution(task) monkeypatch.setattr( 'app.services.shipping_notice.process_gate_bridge._derived_fulfillment_status', lambda _task: 'uploaded', ) sync_fulfillment_gates_from_task(execution=execution, actor_type='user', actor_id=1) db.session.commit() db.session.refresh(execution) assert execution.current_gate_code is None assert execution.status == 'completed' def test_missing_upload_status_keeps_gate_stuck(app): """upload_status != uploaded → STEP 1A 不触发,前置 Gate 保留(阶段投影可被回退)。""" user = User(username='upload-not-set-user', email='upload-not-set@example.com', password_hash='x') db.session.add(user); db.session.flush() task = ShippingOrderTask(task_no='OT-UPLOAD-NOT-SET', user_id=user.id) db.session.add(task) db.session.flush() execution = _mk_execution(task) sync_fulfillment_gates_from_task(execution=execution, actor_type='user', actor_id=1) db.session.commit() db.session.refresh(execution) suff = ShippingProcessGate.query.filter_by( execution_id=execution.id, gate_code='inventory_sufficiency', ).one() # 关键断言:非 uploaded 时前置 Gate 不能被收口 —— 这正是批次中心回退「通知准备」的根因。 assert suff.status != 'passed'