""" tests/test_task_retry_decision_service.py 用途:锁定统一重试决策树的只读分支(noop / fetch)与行级 retryable。 依赖:task_retry_decision_service、line_inventory_match_projection。 逻辑:不启全量 Flask;验证决策优先级。 """ from __future__ import annotations from types import SimpleNamespace from app.services.shipping_notice.line_inventory_match_projection import project_line_retryable from app.services.shipping_notice.task_retry_decision_service import ( ACTION_FETCH_INVENTORY, ACTION_NOOP, decide_retry_action_only, _is_inventory_rpa_active, ) def _task(**kwargs): defaults = { 'task_no': 'OT-1', 'id': 1, 'process_status': 'parsed', 'pending_human_reason': None, 'inventory_lines_json': None, 'inventory_snapshot_id': None, 'inventory_source_status': 'resolved', 'inventory_source_code': 'DSB_SLC', 'inventory_source_confirmed_by': None, 'order_data_version': 3, } defaults.update(kwargs) return SimpleNamespace(**defaults) def test_project_line_retryable_hides_shortage_and_running(): assert project_line_retryable( inventory_allowed_actions=['retry_inventory_preparation'], user_line_status='shortage', ) is False assert project_line_retryable( inventory_allowed_actions=['retry_inventory_preparation'], user_line_status='inventory_file_missing', inventory_rpa_running=True, ) is False assert project_line_retryable( inventory_allowed_actions=['retry_inventory_preparation'], user_line_status='inventory_file_missing', ) is True def test_decide_completed_is_noop(): task = _task(process_status='completed') decided = decide_retry_action_only(task, scope='task') assert decided['action'] == ACTION_NOOP assert decided['reason_code'] == 'already_completed' def test_decide_missing_snapshot_fetch(monkeypatch): task = _task(process_status='parsed', inventory_snapshot_id=None, inventory_lines_json=None) monkeypatch.setattr( 'app.services.shipping_notice.inventory_source_gate.is_inventory_source_stable', lambda _t: True, ) decided = decide_retry_action_only(task, scope='task') assert decided['action'] == ACTION_FETCH_INVENTORY def test_decide_rpa_pending_already_running(): task = _task(pending_human_reason='rpa_pending', process_status='waiting_inventory') decided = decide_retry_action_only(task, scope='task') assert decided['action'] == ACTION_NOOP assert decided['reason_code'] == 'already_running' def test_structured_failed_attempt_overrides_stale_rpa_pending(monkeypatch): """逐源 Attempt 已失败时,旧 rpa_pending 摘要不得阻止用户手动重试。""" task = _task(task_no='OT-FAILED', pending_human_reason='rpa_pending') class _Query: calls = 0 def filter(self, *_args): return self def order_by(self, *_args): return self def first(self): self.calls += 1 if self.calls == 1: return None return SimpleNamespace(status='failed') monkeypatch.setattr( 'app.models.ShippingTaskInventoryRpaAttempt.query', _Query(), raising=False, ) assert _is_inventory_rpa_active(task) is False def test_structured_running_attempt_blocks_duplicate_retry(monkeypatch): """逐源 Attempt 仍在运行时,必须拦截重复派发。""" task = _task(task_no='OT-RUNNING', pending_human_reason=None) class _Query: def filter(self, *_args): return self def order_by(self, *_args): return self def first(self): return SimpleNamespace(status='running') monkeypatch.setattr( 'app.models.ShippingTaskInventoryRpaAttempt.query', _Query(), raising=False, ) assert _is_inventory_rpa_active(task) is True