415 lines
14 KiB
Python
415 lines
14 KiB
Python
"""
|
|
backend/tests/test_gate_derivation.py
|
|
|
|
用途:验收 Gate 推导的粗分类 / 位置 / 待办分类,替代旧 checkpoint_kind。
|
|
依赖:process_gate_contract.derive_*、query_service._todo_category_from_gate、
|
|
ui_state._derived_*、user_facing_status、pytest。
|
|
逻辑:Task1–6 契约纯函数与消费点迁移断言;SQL CASE 双读过渡不在此覆盖。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app.services.shipping_notice.process_gate_contract import (
|
|
derive_position,
|
|
derive_todo_category,
|
|
derive_user_mode,
|
|
)
|
|
|
|
|
|
def test_user_mode_complete_when_rejected():
|
|
assert derive_user_mode(
|
|
user_status='completed',
|
|
gate_code=None,
|
|
review_status='rejected',
|
|
lifecycle_stage=None,
|
|
) == 'complete'
|
|
|
|
|
|
def test_user_mode_wait_upload_handoff():
|
|
assert derive_user_mode(
|
|
user_status='waiting_human',
|
|
gate_code='manual_upload_handoff',
|
|
review_status=None,
|
|
lifecycle_stage='freight_upload',
|
|
) == 'wait_manual_upload'
|
|
|
|
|
|
def test_user_mode_must_human():
|
|
assert derive_user_mode(
|
|
user_status='waiting_human',
|
|
gate_code='inventory_matching',
|
|
review_status=None,
|
|
lifecycle_stage='fulfillment_prep',
|
|
) == 'must_human'
|
|
|
|
|
|
def test_user_mode_auto():
|
|
assert derive_user_mode(
|
|
user_status='running',
|
|
gate_code='inventory_preparation',
|
|
review_status=None,
|
|
lifecycle_stage='fulfillment_prep',
|
|
) == 'auto_continue'
|
|
|
|
|
|
def test_position_review_dispositions():
|
|
assert derive_position(gate_code=None, review_status='rejected') == 'rejected'
|
|
assert derive_position(gate_code=None, review_status='quality_issue') == 'quality_issue'
|
|
assert derive_position(gate_code=None, review_status='rsv_req') == 'rsv_req'
|
|
|
|
|
|
def test_position_fulfillment():
|
|
assert derive_position(gate_code='inventory_preparation', review_status=None) == 'inventory_pending'
|
|
assert derive_position(gate_code='artifact_generation', review_status=None) == 'export_ready'
|
|
|
|
|
|
def test_todo_category():
|
|
assert derive_todo_category(user_mode='must_human', position='parse_review') == 'review'
|
|
assert derive_todo_category(user_mode='must_human', position='quality_issue') == 'exception'
|
|
assert derive_todo_category(user_mode='wait_manual_upload') == 'manual_upload'
|
|
assert derive_todo_category(user_mode='complete') == 'done'
|
|
assert derive_todo_category(user_mode='auto_continue') == 'in_progress'
|
|
|
|
|
|
def test_fetch_gate_positions_empty():
|
|
from app.services.shipping_notice.query_service import fetch_gate_positions
|
|
|
|
assert fetch_gate_positions([]) == {}
|
|
|
|
|
|
def test_fetch_gate_positions_batched(app):
|
|
"""关键断言:一次查询按 task_id 返回 current_gate_code / status。"""
|
|
with app.app_context():
|
|
from app import db
|
|
from app.models import ShippingOrderTask, ShippingProcessExecution
|
|
from app.services.shipping_notice.query_service import fetch_gate_positions
|
|
from tests.test_shipping_order_extraction_runs import _seed_run_input
|
|
|
|
user, _agent, session, _intake, _document = _seed_run_input(suffix='GATE-POS')
|
|
ot_a = ShippingOrderTask(
|
|
task_no='OT-GATE-POS-A',
|
|
user_id=user.id,
|
|
conversation_id=session.session_id,
|
|
status='waiting_review',
|
|
stage='step5_match_inventory',
|
|
progress_version=1,
|
|
)
|
|
ot_b = ShippingOrderTask(
|
|
task_no='OT-GATE-POS-B',
|
|
user_id=user.id,
|
|
conversation_id=session.session_id,
|
|
status='processing',
|
|
stage='inventory_fetching',
|
|
progress_version=1,
|
|
)
|
|
db.session.add_all([ot_a, ot_b])
|
|
db.session.flush()
|
|
db.session.add_all([
|
|
ShippingProcessExecution(
|
|
execution_no='SPE-GATE-POS-A',
|
|
task_id=int(ot_a.id),
|
|
status='blocked',
|
|
current_gate_code='inventory_sufficiency',
|
|
),
|
|
ShippingProcessExecution(
|
|
execution_no='SPE-GATE-POS-B',
|
|
task_id=int(ot_b.id),
|
|
status='active',
|
|
current_gate_code='inventory_preparation',
|
|
),
|
|
])
|
|
db.session.commit()
|
|
|
|
result = fetch_gate_positions([int(ot_a.id), int(ot_b.id), 999999])
|
|
assert result[int(ot_a.id)]['current_gate_code'] == 'inventory_sufficiency'
|
|
assert result[int(ot_a.id)]['status'] == 'blocked'
|
|
assert result[int(ot_b.id)]['current_gate_code'] == 'inventory_preparation'
|
|
assert 999999 not in result
|
|
|
|
|
|
def test_early_stop_uses_derive_user_mode(monkeypatch):
|
|
from app.services.shipping_notice import progress_workflow as pw
|
|
|
|
class FakeTask:
|
|
task_no = 'SN-1'
|
|
review_status = None
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.ui_state._process_gate_user_summary_for_task',
|
|
lambda t: {
|
|
'current_gate': {
|
|
'status': 'blocked_manual',
|
|
'blocking_reason_code': 'inventory_shortage_review_required',
|
|
'gate_code': 'inventory_sufficiency',
|
|
},
|
|
'responsible_party': 'owner',
|
|
'lifecycle_stage': 'fulfillment_prep',
|
|
'current_gate_code': 'inventory_sufficiency',
|
|
'status': 'blocked',
|
|
},
|
|
)
|
|
assert pw._entry_user_mode(FakeTask()) == 'must_human'
|
|
|
|
|
|
def test_entry_user_mode_inventory_prep_retryable_stays_auto(monkeypatch):
|
|
"""
|
|
回归:候选已冻结但尚未取库存时,sync 可能把 inventory_preparation 投影为
|
|
blocked_retryable + inventory_file_empty;自动推进入口不得判 must_human 早停,
|
|
否则永远进不了 ensure_inventory(复用快照 / 触发 RPA)。
|
|
"""
|
|
from app.services.shipping_notice import progress_workflow as pw
|
|
|
|
class FakeTask:
|
|
task_no = 'OT-INV-EMPTY'
|
|
review_status = None
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.ui_state._process_gate_user_summary_for_task',
|
|
lambda t: {
|
|
'current_gate': {
|
|
'status': 'blocked_retryable',
|
|
'blocking_reason_code': 'inventory_file_empty',
|
|
'gate_code': 'inventory_preparation',
|
|
},
|
|
'current_blocking_gate': {
|
|
'status': 'blocked_retryable',
|
|
'blocking_reason_code': 'inventory_file_empty',
|
|
'gate_code': 'inventory_preparation',
|
|
},
|
|
'responsible_party': 'worker',
|
|
'lifecycle_stage': 'fulfillment_prep',
|
|
'current_gate_code': 'inventory_preparation',
|
|
'status': 'blocked',
|
|
'blocking_reason_code': 'inventory_file_empty',
|
|
},
|
|
)
|
|
assert pw._entry_user_mode(FakeTask()) == 'auto_continue'
|
|
|
|
|
|
def test_entry_user_mode_inventory_prep_exhausted_still_must_human(monkeypatch):
|
|
"""RPA 耗尽属 needs_human,入口仍须早停,不能自动空转重试。"""
|
|
from app.services.shipping_notice import progress_workflow as pw
|
|
|
|
class FakeTask:
|
|
task_no = 'OT-INV-EXHAUSTED'
|
|
review_status = None
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.ui_state._process_gate_user_summary_for_task',
|
|
lambda t: {
|
|
'current_blocking_gate': {
|
|
'status': 'blocked_retryable',
|
|
'blocking_reason_code': 'inventory_rpa_exhausted',
|
|
'gate_code': 'inventory_preparation',
|
|
},
|
|
'lifecycle_stage': 'fulfillment_prep',
|
|
'current_gate_code': 'inventory_preparation',
|
|
'status': 'blocked',
|
|
'blocking_reason_code': 'inventory_rpa_exhausted',
|
|
},
|
|
)
|
|
assert pw._entry_user_mode(FakeTask()) == 'must_human'
|
|
|
|
|
|
def test_position_inventory_sufficiency():
|
|
assert derive_position(gate_code='inventory_sufficiency', review_status=None) == 'shortage_review'
|
|
|
|
|
|
def test_todo_category_from_gate_positions():
|
|
from app.services.shipping_notice.query_service import _todo_category_from_gate
|
|
|
|
assert (
|
|
_todo_category_from_gate(
|
|
{'current_gate_code': 'order_field_extraction', 'status': 'blocked_manual'},
|
|
None,
|
|
)
|
|
== 'review'
|
|
)
|
|
assert (
|
|
_todo_category_from_gate(
|
|
{'current_gate_code': 'inventory_preparation', 'status': 'active'},
|
|
None,
|
|
)
|
|
== 'in_progress'
|
|
)
|
|
assert (
|
|
_todo_category_from_gate(
|
|
{'current_gate_code': 'inventory_sufficiency', 'status': 'blocked'},
|
|
None,
|
|
)
|
|
== 'exception'
|
|
)
|
|
|
|
|
|
def test_user_facing_stage_uses_position(monkeypatch):
|
|
from app.services.shipping_notice import user_facing_status as ufs
|
|
|
|
class FakeTask:
|
|
step_payload_json = {}
|
|
pending_human_reason = None
|
|
lifecycle_status = 'active'
|
|
review_status = None
|
|
order_task_id = None
|
|
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.process_gate_projection.task_blocking_reason',
|
|
lambda t: None,
|
|
)
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.ui_state._process_gate_user_summary_for_task',
|
|
lambda t: {'current_gate_code': 'inventory_preparation'},
|
|
)
|
|
assert ufs.user_facing_task_stage(FakeTask()) == '待上传库存'
|
|
|
|
|
|
def test_derived_entry_surface_review():
|
|
from app.services.shipping_notice.ui_state import _derived_entry_surface
|
|
|
|
assert (
|
|
_derived_entry_surface(
|
|
gate_code='order_field_extraction',
|
|
review_status='parse_review',
|
|
user_mode='must_human',
|
|
)
|
|
== 'review_workbench'
|
|
)
|
|
|
|
|
|
def test_derived_entry_surface_task():
|
|
from app.services.shipping_notice.ui_state import _derived_entry_surface
|
|
|
|
assert (
|
|
_derived_entry_surface(
|
|
gate_code='artifact_generation',
|
|
review_status=None,
|
|
user_mode='wait_manual_upload',
|
|
)
|
|
== 'task_workbench'
|
|
)
|
|
|
|
|
|
def test_summary_prefers_gate_shortage_over_freeze(monkeypatch):
|
|
"""关键断言:Gate 短缺时摘要不再沿用 OT 默认 order_freeze 文案。"""
|
|
from app.services.shipping_notice import ui_state as us
|
|
|
|
class FakeTask:
|
|
task_no = 'OT-SHORT'
|
|
review_status = None
|
|
order_task_id = None
|
|
updated_at = None
|
|
error_message = None
|
|
step_payload_json = {}
|
|
|
|
monkeypatch.setattr(us, '_build_stp_facts', lambda t: None)
|
|
monkeypatch.setattr(us, '_collect_parse_stage_issues', lambda *a, **k: [])
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.process_gate_projection.task_blocking_reason',
|
|
lambda t: 'inventory_shortage_review_required',
|
|
)
|
|
monkeypatch.setattr(us, 'user_facing_task_stage', lambda t: '库存不足,无法发货')
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.stp_progress_contract.project_task_progress_nodes',
|
|
lambda t: [],
|
|
)
|
|
summary = us._build_task_summary(
|
|
FakeTask(),
|
|
'shortage_review',
|
|
user_mode='must_human',
|
|
gate_code='inventory_sufficiency',
|
|
)
|
|
assert '缺口' in summary['message'] or '库存' in summary['message']
|
|
assert summary['user_mode'] == 'must_human'
|
|
assert summary['entry_surface'] == 'task_workbench'
|
|
assert summary['next_auto_step'] is None
|
|
|
|
|
|
def test_rejected_maps_to_review_disposition():
|
|
assert derive_position(gate_code=None, review_status='rejected') == 'rejected'
|
|
|
|
|
|
def test_notice_text_failure_maps_to_quality_issue():
|
|
assert derive_position(gate_code=None, review_status='quality_issue') == 'quality_issue'
|
|
|
|
|
|
def test_sql_todo_category_prefers_gate_inventory_sufficiency(app):
|
|
"""关键断言:SQL CASE 在 Execution=inventory_sufficiency/blocked 时为 exception。"""
|
|
with app.app_context():
|
|
from app import db
|
|
from app.models import ShippingOrderTask, ShippingProcessExecution
|
|
from app.services.shipping_notice.query_service import _order_task_todo_category_sql_expr
|
|
from tests.test_shipping_order_extraction_runs import _seed_run_input
|
|
|
|
user, _agent, session, _intake, _document = _seed_run_input(suffix='SQL-GATE')
|
|
ot = ShippingOrderTask(
|
|
task_no='OT-SQL-GATE-SHORT',
|
|
user_id=user.id,
|
|
conversation_id=session.session_id,
|
|
status='waiting_review',
|
|
stage='step5_match_inventory',
|
|
progress_version=1,
|
|
)
|
|
db.session.add(ot)
|
|
db.session.flush()
|
|
db.session.add(
|
|
ShippingProcessExecution(
|
|
execution_no='SPE-SQL-GATE-SHORT',
|
|
task_id=int(ot.id),
|
|
status='blocked',
|
|
current_gate_code='inventory_sufficiency',
|
|
)
|
|
)
|
|
db.session.commit()
|
|
cat = (
|
|
db.session.query(_order_task_todo_category_sql_expr())
|
|
.filter(ShippingOrderTask.id == ot.id)
|
|
.scalar()
|
|
)
|
|
assert cat == 'exception'
|
|
|
|
|
|
def test_sql_todo_category_prefers_gate_manual_upload(app):
|
|
"""关键断言:manual_upload_handoff → manual_upload。"""
|
|
with app.app_context():
|
|
from app import db
|
|
from app.models import ShippingOrderTask, ShippingProcessExecution
|
|
from app.services.shipping_notice.query_service import _order_task_todo_category_sql_expr
|
|
from tests.test_shipping_order_extraction_runs import _seed_run_input
|
|
|
|
user, _agent, session, _intake, _document = _seed_run_input(suffix='SQL-GATE-MU')
|
|
ot = ShippingOrderTask(
|
|
task_no='OT-SQL-GATE-MU',
|
|
user_id=user.id,
|
|
conversation_id=session.session_id,
|
|
status='waiting_upload',
|
|
stage='freight_upload',
|
|
progress_version=1,
|
|
)
|
|
db.session.add(ot)
|
|
db.session.flush()
|
|
db.session.add(
|
|
ShippingProcessExecution(
|
|
execution_no='SPE-SQL-GATE-MU',
|
|
task_id=int(ot.id),
|
|
status='blocked',
|
|
current_gate_code='manual_upload_handoff',
|
|
)
|
|
)
|
|
db.session.commit()
|
|
cat = (
|
|
db.session.query(_order_task_todo_category_sql_expr())
|
|
.filter(ShippingOrderTask.id == ot.id)
|
|
.scalar()
|
|
)
|
|
assert cat == 'manual_upload'
|
|
|
|
|
|
def test_user_mode_review_status_must_human():
|
|
"""复核处置 review_status 在默认 auto 之前映射为 must_human。"""
|
|
for rs in ('parse_review', 'rsv_req', 'quality_issue', 'customer_review', 'shortage_review'):
|
|
assert derive_user_mode(
|
|
user_status='running',
|
|
gate_code=None,
|
|
review_status=rs,
|
|
lifecycle_stage=None,
|
|
) == 'must_human'
|