Files
syops/backend/tests/test_shipping_status_contract_hygiene.py

66 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
tests/test_shipping_status_contract_hygiene.py
用途:状态契约卫生门禁——禁止在 projector 白名单外扩散 synonym frozenset。
依赖:pathlib、re、pytest。
逻辑:扫描 services/shipping_notice/*.py;白名单内允许持有跨层翻译袋
(upload_attempt_service、order_review_workbench_projection);
另断言货代前端视图不再双读 pending_human_reason。
"""
from __future__ import annotations
import re
from pathlib import Path
import pytest
BACKEND_ROOT = Path(__file__).resolve().parents[1]
SERVICES_DIR = BACKEND_ROOT / 'app' / 'services' / 'shipping_notice'
FRONTEND_FREIGHT_VIEW = (
BACKEND_ROOT.parent
/ 'frontend'
/ 'src'
/ 'features'
/ 'chat'
/ 'views'
/ 'ShippingOrderReviewView.vue'
)
# 允许持有跨层 synonym 袋的唯一投影模块。
_WHITELIST = frozenset({
'upload_attempt_service.py',
'order_review_workbench_projection.py',
})
# 读侧禁止外溢的典型 synonym 袋(字面量形态)。
_FORBIDDEN_PATTERNS = (
re.compile(r"\{['\"]cancelled['\"]\s*,\s*['\"]canceled['\"]"),
re.compile(r"\{['\"]canceled['\"]\s*,\s*['\"]cancelled['\"]"),
re.compile(r"\{['\"]succeeded['\"]\s*,\s*['\"]success['\"]\s*,\s*['\"]completed['\"]"),
re.compile(r"\{['\"]failed['\"]\s*,\s*['\"]dead['\"]"),
re.compile(r"\{['\"]uploaded['\"]\s*,\s*['\"]succeeded['\"]\s*,\s*['\"]success['\"]"),
re.compile(r"\{['\"]waiting_human['\"]\s*,\s*['\"]idle['\"]"),
)
@pytest.mark.pure_unit
def test_synonym_frozenset_only_in_whitelist_projectors():
"""白名单外不得出现状态同义词 frozenset/set 字面量。"""
offenders: list[str] = []
for path in sorted(SERVICES_DIR.glob('*.py')):
if path.name in _WHITELIST or path.name.startswith('_'):
continue
text = path.read_text(encoding='utf-8')
for pattern in _FORBIDDEN_PATTERNS:
if pattern.search(text):
offenders.append(f'{path.name}: {pattern.pattern}')
assert not offenders, 'synonym bags leaked outside projectors:\n' + '\n'.join(offenders)
@pytest.mark.pure_unit
def test_freight_view_does_not_read_pending_human_reason():
"""货代上传 FE 禁止再双读 pending_human_reason(D1)。"""
text = FRONTEND_FREIGHT_VIEW.read_text(encoding='utf-8')
assert 'pending_human_reason' not in text