Merge branch 'hugo-shipping-notice' into dev

This commit is contained in:
hugo committed 2026-08-20 20:27:21 +08:00
commit 3985b34dda
606 files changed
+52167 -36839

No files matched your search

@@ -26,7 +26,6 @@ from app.services.shipping_notice.stp_line_inventory_match import prepare_candid
def _task(task_no: str) -> ShippingNotice:
task = ShippingNotice(
task_no=task_no,
process_status='created',
source_type='manual',
)
db.session.add(task)
@@ -19,7 +19,7 @@ tests/services/shipping_notice/test_reconciliation_phase34_contract.py
b) legacy_job_id=999 + is_admin=False → 422
[P4 T7] ownership_anomaly BRANCH F:
断言触发路径最终 outcome=ownership_anomaly_rejected 且不会脏写
(这里只测 ValidationError 降级情形 hasattr(case,process_status)=False)。
(这里只测 ValidationError 降级情形,Review Case 不含旧流程字段)。
全部为 pure_unit 或 DB 无关单测,不启动 Flask/Worker,不写 DB。
"""
@@ -132,6 +132,20 @@ def test_p3_t4_primary_action_matrix(bcode, actions_in, retryable, expected_pa_c
assert got['variant'] == variant, label
@pytest.mark.pure_unit
def test_resolved_field_conflict_has_no_current_task_cta():
"""字段冲突清零后,保存校验留在底栏,当前任务不得再制造 CTA。"""
from app.services.shipping_notice.reconciliation_projector import project_case_primary_action
action = project_case_primary_action(
'waiting_human',
{},
['view', 'validate'],
)
assert action is None
@pytest.mark.pure_unit
def test_p3_t2_allowed_actions_inject_reconcile_and_remove_retry():
"""[P3 T2] 业务阻断 → REMOVE_SET 删除 retry_stp_auto_create 且注入 reconcile_and_progress。"""
@@ -183,6 +197,95 @@ def test_p3_t5_bundle_six_fields_shapes():
assert bundle['primary_action'] is None or isinstance(bundle['primary_action'], dict)
@pytest.mark.pure_unit
def test_case_bundle_prefers_persisted_formal_gate_over_case_draft(monkeypatch):
"""正式 field_completeness 阻断必须覆盖 Case draft 的 idle/order_review_build 推导。"""
from app.services.shipping_notice import reconciliation_projector
case = _fake_case(status='draft', candidate_id=None)
monkeypatch.setattr(
reconciliation_projector,
'_project_case_formal_gate_summary',
lambda _case: {
'current_gate': {
'gate_code': 'field_completeness',
'status': 'blocked_manual',
'blocking_reason_code': 'required_field_missing',
'next_action_code': 'edit_order_fields',
},
'current_blocking_gate': {
'gate_code': 'field_completeness',
'status': 'blocked_manual',
'blocking_reason_code': 'required_field_missing',
'next_action_code': 'edit_order_fields',
},
'blocking_reason_code': 'required_field_missing',
'next_action_code': 'edit_order_fields',
},
)
bundle = reconciliation_projector.project_review_case_projection_bundle(
case,
validation={'can_build': False, 'can_auto_create': False},
post_confirm=None,
is_admin=False,
)
assert bundle['current_gate']['gate_code'] == 'field_completeness'
assert bundle['current_gate']['blocking_reason_code'] == 'required_field_missing'
assert bundle['blocking_reason']['code'] == 'required_field_missing'
assert bundle['user_status'] == 'waiting_human'
@pytest.mark.pure_unit
def test_pending_successor_gate_clears_a_resolved_field_conflict_from_current_task():
"""字段冲突 Gate 已通过后,下游 pending 只能表示系统推进,不能回退旧 STP 冲突。"""
from app.services.shipping_notice.reconciliation_projector import (
_project_case_formal_gate_facts,
)
facts = _project_case_formal_gate_facts({
'current_gate': {
'gate_code': 'inventory_source_routing',
'status': 'pending',
'blocking_reason_code': None,
'next_action_code': None,
},
'current_blocking_gate': None,
'blocking_reason_code': None,
'next_action_code': None,
})
assert facts is not None
current_gate, blocking_reason, user_status = facts
assert current_gate['gate_code'] == 'inventory_source_routing'
assert blocking_reason['code'] is None
assert user_status == 'running'
@pytest.mark.pure_unit
def test_pending_order_freeze_readiness_waits_for_the_user_to_submit_validation():
"""建单前校验未提交时必须提示人工保存,不能伪装为自动处理中。"""
from app.services.shipping_notice.reconciliation_projector import (
_project_case_formal_gate_facts,
)
facts = _project_case_formal_gate_facts({
'current_gate': {
'gate_code': 'order_freeze_readiness',
'status': 'pending',
'blocking_reason_code': None,
'next_action_code': None,
},
})
assert facts is not None
current_gate, blocking_reason, user_status = facts
assert current_gate['gate_code'] == 'order_freeze_readiness'
assert blocking_reason['code'] is None
assert user_status == 'waiting_human'
# ============================================================
# [P4 T6] 管理员权限拦截(纯函数 PRE-CHECK,通过 monkeypatch 替换锁 DB 查询)
# ============================================================
@@ -336,7 +439,7 @@ def test_p4_t6c_legacy_job_requires_failed_or_dead(monkeypatch):
# ============================================================
# [P4 T7] BRANCH F ownership_anomaly 422 降级(process_status 不存在)
# [P4 T7] BRANCH F ownership_anomaly 422 降级(Review Case 不含旧流程字段)
# ============================================================
@pytest.mark.pure_unit
@@ -363,8 +466,7 @@ def test_p4_t7_branch_f_ownership_anomaly_throws_validation(monkeypatch):
{'step': 'L3_AMBIGUOUS', 'matched': True},
],
}
# 关键:模拟 ReviewCase 没有 process_status 列 → BRANCH F 必须抛 422(而非软返回 outcome)
assert not hasattr(case, 'process_status')
# 关键:Review Case 不含旧流程字段 → BRANCH F 必须抛 422(而非软返回 outcome)
with pytest.raises(ValidationError) as exc:
reconcile_and_progress(
@@ -1,11 +1,7 @@
"""
tests/services/shipping_notice/test_recovery_contract_phase1.py
用途:Phase 1 合同测试 — 锁定错误分类白名单与阻断→动作映射的纯函数矩阵。
依赖:recovery_contract(is_tech_retryable_error、build_stp_job_blocked_manual_result、
should_general_retry_be_removed、derive_blocking_next_actions)。
逻辑:纯单元测,不依赖 DB/Worker,确保 T1 错误分类矩阵与 T3 动作注入判定
与 Phase 1 冻结契约一致;一旦白名单/阻断规则被误修改将立即红。
用途:恢复契约测试——失败必须停止,用户通过明确动作恢复。
"""
from __future__ import annotations
@@ -17,56 +13,11 @@ from app.services.shipping_notice.recovery_contract import (
BLOCKING_REASON_TO_ACTION_CODES,
REMOVE_ACTIONS_WHEN_BLOCKED,
build_stp_job_blocked_manual_result,
build_stp_job_failed_transient_result,
derive_blocking_next_actions,
is_tech_retryable_error,
should_general_retry_be_removed,
)
# ============================================================
# T1:is_tech_retryable_error 白名单矩阵(fail-closed)
# ============================================================
T1_MATRIX = [
# (error_code, category_hint, expected_retryable, case_label)
('SHIPPING.TRANSIENT_NETWORK_TIMEOUT', None, True, 'TRANSIENT_ 前缀 → 技术临时网络'),
('SHIPPING.TRANSIENT_STORAGE_CONNECTION', None, True, 'TRANSIENT_ 前缀 → 技术存储'),
('SHIPPING.WORKER_TIMEOUT_LEASE_EXPIRED', None, True, 'WORKER_TIMEOUT_ 前缀'),
('worker_timeout', 'worker_timeout', True, 'category_hint=worker_timeout'),
('queue_unavailable', 'queue_unavailable', True, 'category_hint=queue_unavailable'),
('temporary_network_error', 'temporary_network_error', True, 'category_hint 网络临时'),
('temporary_storage_error', 'temporary_storage_error', True, 'category_hint 存储临时'),
('external_service_5xx', 'external_service_5xx', True, 'category_hint 外部 5xx'),
('lease_expired', 'lease_expired', True, 'category_hint 租约过期'),
# 业务阻断码:默认不可重试(Q5 白名单制,不在即拒绝)
('SHIPPING.NOTICE_EXACT_DUPLICATE', None, False, '精确重复 — 业务阻断,不可重试'),
('SHIPPING.STP_NOT_ELIGIBLE', None, False, 'STP 资格不足 — 业务阻断'),
('SHIPPING.REVIEW_DRAFT_MISSING', None, False, '草稿缺失 — 业务阻断'),
('SHIPPING.REVIEW_RUN_MISSING', None, False, 'Run 缺失 — 业务阻断'),
('SHIPPING.REVIEW_DOCUMENT_INVALID', None, False, '文档无效 — 业务阻断'),
('SHIPPING.REVIEW_DRAFT_VERSION_CONFLICT', None, False, '版本冲突 — 业务阻断,应 409 让前端刷新'),
('profile_schema_mismatch', None, False, 'Profile 模板不匹配 — 业务阻断'),
('final_required_field_missing', None, False, '关键字段缺失 — 业务阻断'),
('duplicate_decision_required', None, False, '等待重复裁决 — 业务阻断'),
# 空码/未知码:fail-closed(默认不可重试,不能因空码回退到 retryable=true)
('', None, False, '空码 → fail-closed 不可重试'),
('RANDOM.UNKNOWN_ERROR_XYZ', None, False, '未知码 → fail-closed 不可重试'),
(None, None, False, 'None 码 → fail-closed 不可重试'),
# category_hint 与前缀冲突:前缀优先(空码除外)
('SHIPPING.TRANSIENT_NETWORK', 'worker_timeout', True, '前缀命中即使 hint 不同也可重试'),
('trigger_not_claimed_missing', None, True, '库存 Trigger 缺失 → 技术重试'),
('trigger_not_claimed_timeout', None, True, '库存 Trigger 超时 → 技术重试'),
]
@pytest.mark.parametrize('error_code, category_hint, expected, label', T1_MATRIX, ids=[m[3] for m in T1_MATRIX])
def test_t1_is_tech_retryable_error_matrix(error_code, category_hint, expected, label):
"""T1:错误码 → 是否允许技术自动重试(白名单制,fail-closed)。"""
_ = label
assert is_tech_retryable_error(error_code, category_hint=category_hint) is expected
# ============================================================
# T1-ext:build_stp_job_*_result 的结构契约(Q4 Job.result_json schema)
# ============================================================
@@ -92,20 +43,6 @@ def test_t1_blocked_manual_result_shape():
assert 'decision_trace' in result
def test_t1_transient_result_shape():
"""技术临时错误 → retryable=true,blocking_reason/next_action 置空。"""
result = build_stp_job_failed_transient_result(
error_code='SHIPPING.TRANSIENT_NETWORK_TIMEOUT',
message='Dify API 503',
category_hint='external_service_5xx',
)
assert result['status'] == 'failed'
assert result['retryable'] is True
assert result['blocking_reason'] is None
assert result['next_action'] is None
assert result['contract_version'] == RECOVERY_CONTRACT_VERSION
# ============================================================
# T3:should_general_retry_be_removed + derive 动作矩阵(Q9 后端注入/删除动作)
# ============================================================