chore(shipping-notice): 统一货代发货生命周期阶段命名并优化全链路业务逻辑

This commit is contained in:
hugo committed 2026-08-17 19:56:51 +08:00
1 parent 5ef8329bd8
commit 4ec4e57005
183 files changed
+14653 -3936

No files matched your search

@@ -80,6 +80,83 @@ def test_rpa_attempt_status_updates_its_bound_preparation_source(app):
assert preparation.status == 'succeeded'
def test_rpa_callback_completes_waiting_preparation_recovery_plan(app):
"""RPA 成功回调完成准备轮次后,关联恢复计划不得继续占用 waiting_external。"""
from app import db
from app.models import (
ShippingNotice,
ShippingProcessExecution,
ShippingTaskInventoryRpaAttempt,
)
from app.services.shipping_notice.inventory_recovery_service import (
create_inventory_preparation_round,
sync_inventory_preparation_round,
sync_preparation_source_from_rpa_attempt,
)
task = ShippingNotice(task_no='SN-PREP-PLAN-COMPLETE')
db.session.add(task)
db.session.flush()
execution = ShippingProcessExecution(
execution_no='SPE-PREP-PLAN-COMPLETE', task_id=int(task.id), status='active',
current_gate_code='inventory_preparation',
)
db.session.add(execution)
db.session.flush()
plan = ShippingProcessRecoveryPlan(
plan_no='RCP-PREP-PLAN-COMPLETE', execution_id=int(execution.id),
current_gate='inventory_matching', action_code='retry_inventory_from_preparation',
target_gate='inventory_preparation', resume_after_gate='inventory_matching',
status='waiting_external', invalidation_plan_json={},
idempotency_key='prep-plan-complete', actor_type='user',
)
db.session.add(plan)
db.session.flush()
preparation = create_inventory_preparation_round(
task=task, execution_id=int(execution.id), recovery_plan_id=int(plan.id),
action_code='retry_inventory_from_preparation', source_codes=['DBS'], gate_attempt_id=None,
)
rpa = ShippingTaskInventoryRpaAttempt(
task_no=task.task_no, source_code='DBS', attempt_no=1, source_run_no='PSR-PREP-PLAN-COMPLETE',
status='queued', lease_token='lease-prep-plan', extra_json={}, callback_payload_json={},
)
db.session.add(rpa)
db.session.flush()
sync_inventory_preparation_round(
preparation_attempt=preparation,
result={'pending_source_codes': ['DBS'], 'source_attempts': {'DBS': {'attempt_id': rpa.id}}},
)
sync_preparation_source_from_rpa_attempt(rpa_attempt_id=int(rpa.id), rpa_status='succeeded')
db.session.refresh(plan)
assert preparation.status == 'succeeded'
assert plan.status == 'succeeded'
assert plan.completed_at is not None
def test_per_source_summary_exposes_attempt_id_for_preparation_lineage(app):
"""新建 RPA Attempt 的汇总必须返回主键,准备轮次才能被回调准确收口。"""
from app import db
from app.models import ShippingNotice, ShippingTaskInventoryRpaAttempt
from app.services.shipping_notice.per_source_rpa_service import _attempts_summary
task = ShippingNotice(task_no='SN-PREP-SUMMARY-ID')
db.session.add(task)
db.session.flush()
attempt = ShippingTaskInventoryRpaAttempt(
task_no=task.task_no, source_code='DBS', attempt_no=1,
source_run_no='PSR-PREP-SUMMARY-ID', status='queued',
lease_token='lease-prep-summary', extra_json={}, callback_payload_json={},
)
db.session.add(attempt)
db.session.flush()
summary = _attempts_summary(task.task_no, source_codes=['DBS'])
assert summary['DBS']['attempt_id'] == int(attempt.id)
def test_preparation_trigger_identity_includes_gate_and_callback(monkeypatch, app):
"""库存准备 Trigger 必须把 GateAttempt 身份原样带到 RPA。"""
from app import db
@@ -186,7 +263,7 @@ def test_formal_gate_inventory_retry_uses_operator_id_for_snapshot_freeze(monkey
payload={'action_code': 'retry_inventory_preparation', 'operator_id': 77},
)
assert seen['actor_id'] == 77
assert result['outcome'] in {'blocked_retryable', 'blocked_manual'}
assert result['outcome'] == 'waiting_external'
def test_per_source_callback_rejects_candidate_set_identity_mismatch(app):
@@ -340,7 +417,7 @@ def test_rpa_report_inventory_parses_per_source_run_with_hyphenated_task_no(app,
def test_rpa_report_inventory_batch_fails_closed_on_per_source_identity_mismatch(app, monkeypatch):
"""批量成功项也必须先通过逐源身份校验,不能先入库再拒绝。"""
"""异步批量处理体必须先通过逐源身份校验,不能先入库再拒绝。"""
from app import db
from app.models import ShippingNotice, ShippingTaskEvent
from app.services.shipping_notice import inventory_rpa_workflow
@@ -360,7 +437,8 @@ def test_rpa_report_inventory_batch_fails_closed_on_per_source_identity_mismatch
lambda task: legacy_called.__setitem__('value', True) or task,
)
response = inventory_rpa_workflow.rpa_report_inventory_batch({
# HTTP 入口只负责受控收件和入队;身份围栏属于 Worker 正式处理体。
response = inventory_rpa_workflow.process_rpa_report_inventory_batch({
'schema_version': 'shipping_inventory_rpa.v2',
'callback_id': 'batch-callback-identity',
'batch_run_no': 'BATCH-CALLBACK-IDENTITY',
@@ -458,6 +536,80 @@ def test_inventory_rpa_failure_is_not_projected_back_to_waiting_external(app):
assert execution.status == 'active'
def test_ready_inventory_snapshot_overrides_stale_rpa_exhausted_gate(app):
"""迟到成功回调已落库存行时,历史 RPA 耗尽不得把准备 Gate 回退为人工上传。"""
from app import db
from app.models import ShippingNotice, ShippingProcessExecution, ShippingProcessGate
from app.services.shipping_notice.process_gate_bridge import sync_fulfillment_gates_from_task
task = ShippingNotice(
task_no='SN-RPA-LATE-SUCCESS',
pending_human_reason='rpa_exhausted',
inventory_lines_json=[{'part_number': 'PN-1', 'available_qty': 1}],
)
db.session.add(task)
db.session.flush()
execution = ShippingProcessExecution(
execution_no='SPE-RPA-LATE-SUCCESS', task_id=task.id, status='active',
current_gate_code='inventory_preparation',
)
db.session.add(execution)
db.session.flush()
sync_fulfillment_gates_from_task(execution=execution)
preparation = ShippingProcessGate.query.filter_by(
execution_id=execution.id, gate_code='inventory_preparation',
).one()
matching = ShippingProcessGate.query.filter_by(
execution_id=execution.id, gate_code='inventory_matching',
).one()
assert preparation.status == 'passed'
assert preparation.blocking_reason_code is None
# 断言自动推进确实进入后续匹配,而不是伪造“整条链路成功”。
assert matching.status == 'blocked_retryable'
assert matching.blocking_reason_code == 'inventory_match_failed'
def test_external_wait_skips_stale_preparation_poll_after_gate_switch(app):
"""跨 Gate 恢复后,旧准备轮询不得在库存匹配 Gate 产生技术失败。"""
from datetime import datetime
from app import db
from app.models import ShippingNotice, ShippingProcessExecution, ShippingProcessGate
from app.services.shipping_notice.process_gate_bridge import sync_fulfillment_gates_from_task
from app.services.shipping_notice.process_gate_job_service import schedule_waiting_external_poll
from app.services.shipping_notice.process_gate_service import queue_gate_attempt
task = ShippingNotice(task_no='SN-STALE-PREP-POLL')
db.session.add(task)
db.session.flush()
execution = ShippingProcessExecution(
execution_no='SPE-STALE-PREP-POLL', task_id=task.id, status='active',
current_gate_code='inventory_matching',
)
db.session.add(execution)
db.session.flush()
sync_fulfillment_gates_from_task(execution=execution)
gate = ShippingProcessGate.query.filter_by(
execution_id=execution.id, gate_code='inventory_matching',
).one()
gate.status = 'waiting_external'
gate.next_action_code = 'poll_inventory_preparation'
gate.effective_result_json = {'waiting_since': datetime.utcnow().isoformat()}
attempt = queue_gate_attempt(
execution_no=execution.execution_no,
gate_code=gate.gate_code,
trigger_type='user_retry', actor_type='system', actor_id='test',
idempotency_key='stale-preparation-poll',
input_snapshot={'action_code': 'retry_inventory_from_preparation', 'payload': {}},
)
db.session.flush()
assert schedule_waiting_external_poll(attempt_id=int(attempt.id)) is None
assert attempt.job_id is None
def test_formal_gate_attempt_creates_preparation_lineage_and_passes_gate(app, monkeypatch):
"""正式库存准备 Gate 执行必须创建 Preparation/Source lineage 并据此通过 Gate。"""
from app import db
@@ -956,7 +1108,7 @@ def test_inventory_preparation_retry_freezes_legacy_task_before_rpa(monkeypatch)
'freeze_mode': 'manual',
}
assert task.order_snapshot_id == 901
assert result['outcome'] == 'blocked_retryable'
assert result['outcome'] == 'waiting_external'
def test_manual_business_block_is_a_successful_attempt(app):