fix(shipping-notice): 修复订单阶段投影与复核冻结门禁

This commit is contained in:
hugo committed 2026-09-15 10:29:59 +08:00
1 parent 9745bb78a7
commit 44c3bd0e80
8 files changed
+259 -17

No files matched your search

+63 -1
View File
@@ -167,6 +167,67 @@ def test_task_ui_state_stage_context_is_opt_in(app) -> None:
assert "stage_context" in detailed_state
def test_build_task_ui_state_notice_uses_ot_stage_not_freeze_gate_lifecycle(app) -> None:
"""
Notice 路径必须读 OT.stage 做五阶段,禁止仅靠 order_freeze→order_review 兜底。
断言意图:OT.stage=order_creating 时,任务中心/详情与批次同为 fulfillment_prep,
不得因 Notice 无 stage 列 + freeze Gate 显示「订单复核」。
"""
with app.app_context():
from app.services.shipping_notice.process_gate_service import (
ensure_execution_for_workflow_version,
)
from app.services.shipping_notice.ui_state import (
_process_gate_user_summary_for_task,
_task_lifecycle,
)
user = User(
username='ui-notice-ot-stage',
email='ui-notice-ot-stage@example.com',
password_hash='x',
)
db.session.add(user)
db.session.flush()
order_task = ShippingOrderTask(
task_no='OT-UI-NOTICE-OT-STAGE',
user_id=user.id,
source_type='direct_pdf',
status='waiting_review',
stage='order_creating',
progress_version=1,
)
db.session.add(order_task)
db.session.flush()
notice = ShippingNotice(
task_no='SN-UI-NOTICE-OT-STAGE',
user_id=user.id,
source_type='manual',
order_task_id=int(order_task.id),
)
db.session.add(notice)
db.session.flush()
execution = ensure_execution_for_workflow_version(
task_id=int(order_task.id), actor_type='system',
)
execution.status = 'blocked'
execution.current_gate_code = 'order_freeze_readiness'
order_task.active_process_execution_id = int(execution.id)
# ensure_execution 在已有 Notice 时会 sync_order_task_stage_for_notice,
# 可能把 stage 写成 waiting_review;本用例要验证「已写入的 order_creating」权威。
order_task.stage = 'order_creating'
order_task.status = 'waiting_review'
db.session.commit()
process_gate = _process_gate_user_summary_for_task(notice)
lifecycle = _task_lifecycle(task=notice, position=None, process_gate=process_gate)
assert lifecycle['lifecycle_stage'] == 'fulfillment_prep'
assert lifecycle['lifecycle_authority'] == 'processing_stage'
assert lifecycle['lifecycle_stage'] != 'order_review'
def test_build_task_ui_state_without_formal_gate_is_unclassified(app) -> None:
"""无正式 Gate 时位置不能再推导五阶段或任何可写 UI 动作。"""
with app.app_context():
@@ -245,7 +306,8 @@ def test_task_ui_state_formal_gate_overrides_stale_task_fields(app) -> None:
state = build_task_ui_state(task)
assert state['lifecycle_stage'] == 'fulfillment_prep'
assert state['lifecycle_authority'] == 'process_gate'
# Notice/OT 无合法 stage 时由 Gate 抬升为 inventory_matching,权威记为 processing_stage。
assert state['lifecycle_authority'] == 'processing_stage'
assert state['stage']['code'] == 'fulfillment_prep'
assert state['current_step']['code'] == 'fulfillment_prep'
assert [entry['state'] for entry in state['lifecycle_entries']] == [
@@ -586,6 +586,60 @@ def test_order_task_work_item_uses_ot_execution_when_notice_missing(app) -> None
assert str(primary.get('code') or '') != 'open_order_review'
def test_order_creating_ot_not_pulled_back_to_order_review_by_stale_open_order_review(app) -> None:
"""
OT.stage 已 order_creating 时,不得因 status=waiting_review 合成 open_order_review
把批次五阶段拉回「订单复核」。
断言意图:工作台按 OT.stage→通知准备;批次须同源,禁止再显示订单复核。
"""
with app.app_context():
from tests.test_shipping_order_extraction_runs import _seed_run_input
from tests.test_shipping_order_review_service import _materialized_run
from app.services.shipping_notice.shipping_processing_projection import (
get_shipping_processing_work_item,
)
user, agent, session, intake, _document = _seed_run_input(suffix='OT-STAGE-NO-REWIND')
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
task = ShippingOrderTask(
task_no='OT-STAGE-NO-REWIND',
user_id=user.id,
conversation_id=session.session_id,
extraction_run_id=run.id,
owner_agent_id=int(agent.id),
candidate_id=None,
status='waiting_review',
stage='order_creating',
progress_version=2,
)
db.session.add(task)
db.session.flush()
execution = ShippingProcessExecution(
execution_no='SPE-OT-STAGE-NO-REWIND',
task_id=int(task.id),
status='blocked',
current_gate_code='order_freeze_readiness',
lock_version=1,
)
db.session.add(execution)
db.session.flush()
task.active_process_execution_id = int(execution.id)
run.user_task_no = task.task_no
db.session.commit()
item = get_shipping_processing_work_item(
user_id=user.id,
work_item_type='order_task',
work_item_no=task.task_no,
)
# 阶段文案治本:不得被 BE-04 / status 启发式拉回 order_review。
assert item['stage'] == 'order_creating'
assert item['ui_state']['lifecycle_stage'] == 'fulfillment_prep'
assert item['ui_state']['lifecycle_stage'] != 'order_review'
def test_order_task_with_superseded_candidate_cannot_borrow_current_candidate(app) -> None:
"""历史 OT 不能借同 Run 的当前 Candidate 显示另一 PDF 的订单生命周期。"""
with app.app_context():
@@ -405,9 +405,9 @@ def test_gate_maps_to_lifecycle_stage_b(app, gate_chain, scenario):
"""五阶段门禁→生命周期 唯一映射必须正确(B 侧,真实 Notice 路径)。
`LIFECYCLE_STAGE_BY_GATE`(lifecycle_projection.py:30)是正式 Gate→五阶段业务码的唯一表。
B(build_task_ui_state on Notice,processing_stage=None)经 project_lifecycle 走 Gate 兜底,
必须按该表落到正确业务码。**freight_upload_rpa / manual_upload_handoff 必须属于 freight_upload**
—— 这是用户确认的契约(方案 A)。
B(build_task_ui_state on Notice)经 OT.stage + Gate 仅向前抬升后调用 project_lifecycle;
履约 Gate(inventory_* / freight_*)须落到正确业务码。**freight_upload_rpa /
manual_upload_handoff 必须属于 freight_upload** —— 这是用户确认的契约(方案 A)。
"""
gate_code, expected_stage = scenario
with app.app_context():
@@ -0,0 +1,57 @@
"""
tests/test_shipping_recheck_freeze_ot_extraction.py
用途:锁定冻结就绪重检从 OT 读 current_order_extraction_id,禁止 AttributeError。
依赖:process_gate_executor._handle_recheck_freeze、unittest.mock。
逻辑:模拟 OT(仅有 current_order_extraction_id)触发 recheck;断言能取到 Extraction 并调用评估。
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import patch
from app.services.shipping_notice import process_gate_executor as executor
def test_handle_recheck_freeze_uses_current_order_extraction_id():
"""
OT 无 order_extraction_id 字段;必须用 current_order_extraction_id,否则 Job 直接 failed。
断言意图:复现测试环境 AttributeError,修复后应进入评估而非 missing 短路。
"""
task = SimpleNamespace(id=7, current_order_extraction_id=42)
assert not hasattr(task, 'order_extraction_id')
extraction = SimpleNamespace(id=42)
eval_calls: list[dict] = []
def _fake_eval(**kwargs):
eval_calls.append(kwargs)
return {
'passed': False,
'blocking_reason_code': 'freeze_not_ready',
'issues': [],
'findings': [],
}
with (
patch.object(executor, '_resolve_order_task', return_value=task),
patch('app.models.ShippingOrderExtraction') as extraction_model,
patch(
'app.services.shipping_notice.freeze_readiness_service.evaluate_freeze_readiness_gate',
side_effect=_fake_eval,
),
):
extraction_model.query.get.return_value = extraction
result = executor._handle_recheck_freeze(
execution=SimpleNamespace(task_id=7),
gate=SimpleNamespace(),
payload={'freeze_mode': 'manual'},
)
assert result['outcome'] == 'blocked_manual'
assert result.get('output_snapshot', {}).get('missing') is not True
extraction_model.query.get.assert_called_once_with(42)
assert len(eval_calls) == 1
assert eval_calls[0]['task'] is task
assert eval_calls[0]['extraction'] is extraction