Files
syops/backend/tests/test_shipping_xfa_split_audit.py
T
hugoandCursor 84a881b554 feat(shipping-notice): 落地 XFA 统一视觉拆分确认门禁与处理投影收敛
闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。
同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-06 10:21:25 +08:00

89 lines
3.5 KiB
Python

"""
tests/test_shipping_xfa_split_audit.py
用途:锁定 XFA 字段拆分相关审计事件码已注册,且 segment 级 diff 审计会实际写入。
依赖:xfa_contract、progress_audit_contract、order_document_profile_service。
逻辑:契约级断言 + 对 `_audit_xfa_split_segment_changes` 的 mock 断言;不接真实 PDF。
"""
from __future__ import annotations
from unittest.mock import patch
from app.services.shipping_notice.document_layout.xfa_contract import XFA_AUDIT_EVENT_TYPES
from app.services.shipping_notice.progress_audit_contract import XFA_PROGRESS_EVENT_CODES
from app.services.shipping_notice import order_document_profile_service as profiles
REQUIRED_SPLIT_AUDIT_EVENTS = (
'xfa_split_rule_suggested',
'xfa_split_rule_previewed',
'xfa_split_rule_validated',
'xfa_split_rule_confirmed',
'xfa_split_segment_created',
'xfa_split_segment_updated',
'xfa_split_segment_deleted',
'xfa_segment_mapping_updated',
'xfa_profile_draft_saved',
'xfa_profile_validation_executed',
'xfa_profile_published',
'xfa_profile_formal_extracted',
'xfa_profile_needs_review',
)
def test_split_audit_events_registered_in_contracts():
"""拆分映射审计事件必须同时出现在 XFA 契约与进度事件白名单。"""
for event in REQUIRED_SPLIT_AUDIT_EVENTS:
assert event in XFA_AUDIT_EVENT_TYPES, event
assert event in XFA_PROGRESS_EVENT_CODES, event
def _rule_set(*, segment_id: str, handling: str = 'mapped', target: str | None = 'customer_part_number') -> dict:
"""构造最小可 diff 的拆分规则集(matcher 模型)。"""
return {
'rule_set_id': 'rs-1',
'rule_version': 1,
'source_path_id': 'path-matnr',
'source_scope': 'line',
'source_field_code': 'MATNR',
'rules': [{
'rule_id': 'r-1',
'matcher': {'type': 'position', 'line_index': 1},
'segment': {
'segment_id': segment_id,
'segment_code': 'cpn' if segment_id == 'seg-a' else segment_id.replace('-', '_'),
'display_name': 'CPN',
'handling': handling,
'target_field_id': 'f-x' if handling == 'mapped' else None,
'target_field_code': target,
},
}],
}
def test_audit_xfa_split_segment_changes_emits_create_update_delete_and_mapping():
"""保存前后 definition diff 必须产出 segment 级审计事件(非仅登记事件码)。"""
before = {'split_rules': [_rule_set(segment_id='seg-a'), _rule_set(segment_id='seg-b', handling='evidence_only', target=None)]}
after = {
'split_rules': [
_rule_set(segment_id='seg-a', target='part_number'), # mapping 变更
_rule_set(segment_id='seg-c', handling='evidence_only', target=None), # 新增
# seg-b 删除
],
}
with patch.object(profiles, '_audit') as audit_mock:
profiles._audit_xfa_split_segment_changes(
profile_id=9,
actor_id=7,
operation_id='op-split-1',
browser_session_id='tab-1',
previous_definition=before,
next_definition=after,
)
events = [call.kwargs['event_type'] for call in audit_mock.call_args_list]
assert 'xfa_split_segment_created' in events
assert 'xfa_split_segment_deleted' in events
assert 'xfa_segment_mapping_updated' in events
assert all(call.kwargs.get('actor_id') == 7 for call in audit_mock.call_args_list)