112 lines
5.1 KiB
Python
112 lines
5.1 KiB
Python
"""
|
|
用途:验证上传未知状态保护,防止未知回执下重复创建或回写旧 Artifact。
|
|
依赖:upload_attempt_service、ShippingUploadAttempt 模型。
|
|
逻辑:覆盖 unknown 状态创建保护和 Artifact superseded 回调拒绝。
|
|
"""
|
|
|
|
from app.core.errors import ConflictError
|
|
|
|
|
|
def test_unknown_upload_allows_fresh_retry_key_and_superseded_callback(app):
|
|
"""unknown 结果不阻断全新重试键(业务决定:允许继续尝试 RPA,避免卡死);Artifact superseded 后任何回调仍返回冲突。"""
|
|
from app import db
|
|
from app.models import ShippingExportArtifact
|
|
from app.services.shipping_notice.upload_attempt_service import (
|
|
mark_attempt_result_unknown,
|
|
start_rpa_attempt,
|
|
)
|
|
from tests.test_shipping_upload_attempt import _ready_artifact
|
|
|
|
with app.app_context():
|
|
task, artifact, _reservation = _ready_artifact()
|
|
attempt = start_rpa_attempt(task, artifact_id=artifact.id)
|
|
mark_attempt_result_unknown(attempt)
|
|
# 业务决定:unknown(无法判断上传是否成功/失败)不再阻断重新下发 RPA,允许再次尝试。
|
|
retry = start_rpa_attempt(task, artifact_id=artifact.id, idempotency_key='fresh-retry-key')
|
|
assert retry.id != attempt.id
|
|
assert retry.status == 'running'
|
|
|
|
artifact.status = 'superseded'
|
|
db.session.flush()
|
|
from app.services.shipping_notice.upload_attempt_service import complete_attempt
|
|
try:
|
|
complete_attempt(attempt, success=True, callback_id='superseded-callback')
|
|
except ConflictError as exc:
|
|
assert exc.code == 'SHIPPING.ARTIFACT_ALREADY_SUPERSEDED'
|
|
else:
|
|
raise AssertionError('superseded Artifact must reject callbacks')
|
|
|
|
|
|
def test_upload_unknown_error_codes_are_stable():
|
|
"""稳定错误码必须区分未知状态与已失效 Artifact。"""
|
|
assert ConflictError('x', code='SHIPPING.UPLOAD_ATTEMPT_STATUS_UNKNOWN').code == 'SHIPPING.UPLOAD_ATTEMPT_STATUS_UNKNOWN'
|
|
assert ConflictError('x', code='SHIPPING.ARTIFACT_ALREADY_SUPERSEDED').code == 'SHIPPING.ARTIFACT_ALREADY_SUPERSEDED'
|
|
|
|
|
|
def test_inventory_callback_id_is_checked_before_attempt_identity(app):
|
|
"""库存回调先按 callback_id 去重,跨 Attempt 重放不得写入另一条 Attempt。"""
|
|
from app import db
|
|
from app.models import ShippingNotice, ShippingTaskInventoryRpaAttempt
|
|
from app.services.shipping_notice.per_source_rpa_service import per_source_callback_handler
|
|
|
|
with app.app_context():
|
|
task = ShippingNotice(task_no='SN-CB-ID-FIRST', source_type='manual')
|
|
db.session.add(task)
|
|
db.session.flush()
|
|
# 冻结的逐源 Attempt 须携带完整 v2 身份(lease_token / candidate_set / execution / gate),
|
|
# 否则回调会先在 _validate_callback_attempt_identity 被 identity_mismatch 拒绝,
|
|
# 无法走到失败分支去写 callback_id。
|
|
first = ShippingTaskInventoryRpaAttempt(
|
|
task_no=task.task_no, source_code='SRC-A', attempt_no=1,
|
|
source_run_no='PSR-FIRST', status='failed', callback_id=None,
|
|
lease_token='lease-first',
|
|
extra_json={'candidate_set_id': 1, 'execution_id': 1, 'gate_id': 1},
|
|
)
|
|
second = ShippingTaskInventoryRpaAttempt(
|
|
task_no='SN-CB-ID-OTHER', source_code='SRC-B', attempt_no=1,
|
|
source_run_no='PSR-SECOND', status='failed', callback_id=None,
|
|
)
|
|
db.session.add_all([first, second])
|
|
db.session.flush()
|
|
|
|
def _payload(run_no: str, source_code: str, attempt_no: int) -> dict:
|
|
"""构造完整 v2 身份围栏的回调 payload。
|
|
|
|
与 _validate_callback_attempt_identity 的 required 字段一一对应,
|
|
使失败回调(error_code)能通过身份校验并回写 callback_id。
|
|
"""
|
|
return {
|
|
'schema_version': 'shipping_inventory_rpa.v2',
|
|
'callback_id': 'cb-unique',
|
|
'task_no': task.task_no,
|
|
'source_code': source_code,
|
|
'source_run_no': run_no,
|
|
'attempt_no': attempt_no,
|
|
'lease_token': 'lease-first',
|
|
'candidate_set_id': 1,
|
|
'execution_id': 1,
|
|
'gate_id': 1,
|
|
'error_code': 'RPA_TIMEOUT',
|
|
}
|
|
|
|
result, handled = per_source_callback_handler(
|
|
task, source_code='SRC-A', run_no='PSR-FIRST',
|
|
callback_payload=_payload('PSR-FIRST', 'SRC-A', 1),
|
|
)
|
|
assert result == 'failed'
|
|
assert handled.id == first.id
|
|
|
|
result, handled = per_source_callback_handler(
|
|
task, source_code='SRC-A', run_no='PSR-FIRST',
|
|
callback_payload=_payload('PSR-FIRST', 'SRC-A', 1),
|
|
)
|
|
assert result == 'idempotent_ok'
|
|
assert handled.id == first.id
|
|
|
|
result, handled = per_source_callback_handler(
|
|
task, source_code='SRC-B', run_no='PSR-SECOND',
|
|
callback_payload=_payload('PSR-SECOND', 'SRC-B', 1),
|
|
)
|
|
assert result == 'callback_conflict'
|
|
assert handled.id == first.id
|