""" tests/test_conversation_retry_auto_progress.py 用途:锁定对话级重试自动推进的 eligible 判定、跳过原因与汇总结构。 依赖:conversation_retry_auto_progress_service。 逻辑:纯单元测跳过映射与 summary;不依赖完整 DB 工厂。 """ from __future__ import annotations from app.services.shipping_notice.conversation_retry_auto_progress_service import ( ACTION_RETRY_AUTO_PROGRESS, _skip_reason_from_automation, _summarize_items, project_conversation_allowed_actions, ) def test_formal_trigger_failure_overrides_stale_automation(monkeypatch): """最新 Attempt 的 Trigger 缺失事实优先于历史 Job 的 needs_human 投影。""" from app.services.shipping_notice import conversation_retry_auto_progress_service as service task = type('Task', (), {'task_no': 'OT-TRIGGER', 'id': 1})() class _Query: def filter_by(self, **_kwargs): return self def first(self): return task monkeypatch.setattr(service.ShippingNotice, 'query', _Query(), raising=False) monkeypatch.setattr( 'app.services.shipping_notice.task_retry_decision_service.summarize_inventory_rpa_latest_attempts', lambda *_args, **_kwargs: { 'latest_attempts': { 'DSV360_AWSSL': { 'status': 'failed', 'error_code': 'trigger_not_claimed_missing', }, }, }, ) assert service._formal_task_has_retryable_inventory_trigger_failure('OT-TRIGGER') is True def test_failed_gate_job_does_not_count_as_active_attempt(monkeypatch): """Attempt 残留 queued 但正式 Job 已失败时,不得继续投影 already_running。""" from app.services.shipping_notice import conversation_retry_auto_progress_service as service task = type('Task', (), {'task_no': 'OT-STALE', 'id': 1})() class _Query: def filter_by(self, **_kwargs): return self def first(self): return task monkeypatch.setattr(service.ShippingNotice, 'query', _Query(), raising=False) monkeypatch.setattr( 'app.services.shipping_notice.task_retry_decision_service.summarize_inventory_rpa_latest_attempts', lambda *_args, **_kwargs: {'active_attempt_count': 0}, ) assert service._formal_task_has_active_attempt('OT-STALE') is False def test_skip_reason_maps_queued_and_not_retryable(): """queued → already_running;retryable=false → not_retryable。""" assert _skip_reason_from_automation( {'status': 'queued', 'retryable': False}, task_missing=False, ) == 'already_running' assert _skip_reason_from_automation( {'status': 'waiting_to_start', 'retryable': False}, task_missing=False, ) == 'not_retryable' assert _skip_reason_from_automation( {'status': 'failed', 'retryable': True}, task_missing=False, ) is None def test_manual_fallback_human_owner_still_retryable(): """manual_fallback 虽 owner=human,但 retryable 时不得跳过。""" assert _skip_reason_from_automation( {'status': 'manual_fallback', 'retryable': True, 'owner_type': 'human'}, task_missing=False, ) is None def test_inventory_needs_human_remains_retryable(): """库存快照/RPA 暂态 needs_human 必须继续进入库存重试决策器。""" assert _skip_reason_from_automation( { 'status': 'needs_human', 'retryable': True, 'reason_code': 'inventory_snapshot_prepare_failed', }, task_missing=False, ) is None def test_missing_inventory_trigger_needs_human_remains_retryable(): """Trigger 未被领取的技术失败必须继续触发正式 OT 库存重试。""" assert _skip_reason_from_automation( { 'status': 'needs_human', 'retryable': True, 'error_code': 'trigger_not_claimed_missing', }, task_missing=False, ) is None def test_business_needs_human_stays_skipped(): """重复/短缺等业务裁决的 needs_human 不得触发库存 RPA。""" assert _skip_reason_from_automation( { 'status': 'needs_human', 'retryable': True, 'reason_code': 'inventory_shortage', }, task_missing=False, ) == 'human_blocking' def test_task_missing_always_skipped(): assert _skip_reason_from_automation( {'status': 'failed', 'retryable': True}, task_missing=True, ) == 'task_missing' def test_summarize_items_counts_outcomes(): summary = _summarize_items([ {'outcome': 'queued'}, {'outcome': 'queued'}, {'outcome': 'skipped', 'reason_code': 'already_running'}, {'outcome': 'failed'}, ]) assert summary == {'total': 4, 'retried': 2, 'skipped': 1, 'failed': 1, 'blocked': 0} def test_project_conversation_allowed_actions_empty_without_conversation(): """无 conversation_id 时不返回禁用空按钮。""" assert project_conversation_allowed_actions(user_id=1, conversation_id='') == [] assert ACTION_RETRY_AUTO_PROGRESS == 'retry_auto_progress'