feat(shipping-notice): 落地 XFA 统一视觉拆分确认门禁与处理投影收敛
闭合拆分预览令牌单次消费、样本 checksum 绑定与确认审计;正式提取改为 bindings-only。 同步统一标注工作台交互,并补齐进度投影/任务中心与迁移文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d2b3d74def
commit
84a881b554
195 files changed
+20269
-3463
No files matched your search
@@ -0,0 +1,172 @@
|
||||
"""
|
||||
tests/test_shipping_xfa_visual_binding.py
|
||||
|
||||
用途:验收 XFA 视觉绑定候选评分、歧义诊断与审计 before/after。
|
||||
依赖:xfa_visual_binding、xfa_acroform、order_document_profile_visual_binding_service。
|
||||
逻辑:纯函数 + mock Widget/datasets;不接真实 PDFium/google-re2。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.shipping_notice.document_layout.xfa_acroform import XfaAcroformWidget
|
||||
from app.services.shipping_notice.document_layout.xfa_dataset import XfaDatasetAnalysis
|
||||
from app.services.shipping_notice.document_layout.xfa_visual_binding import (
|
||||
suggest_visual_binding_candidates,
|
||||
visual_binding_audit_snapshot,
|
||||
)
|
||||
from app.services.shipping_notice.order_document_profile_audit_service import (
|
||||
XFA_VISUAL_BINDING_AUDIT_EVENTS,
|
||||
)
|
||||
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
|
||||
|
||||
|
||||
def _dataset_analysis() -> XfaDatasetAnalysis:
|
||||
"""构造最小 datasets 分析(双 MATNR 歧义路径)。"""
|
||||
return XfaDatasetAnalysis(
|
||||
nodes=[],
|
||||
dataset_root_path='/data',
|
||||
leaf_values_by_path={
|
||||
'/data/HEADER/EBELN': '4510039227',
|
||||
'/data/ITEMS/DATA/MATNR': 'TEMP-S000218922',
|
||||
'/data/ITEMS1/DATA/MATNR': 'TEMP-S000218922',
|
||||
},
|
||||
header_candidates=['/data/HEADER'],
|
||||
collection_candidates=[],
|
||||
known_path_ids=set(),
|
||||
stats={},
|
||||
)
|
||||
|
||||
|
||||
def _widget(
|
||||
*,
|
||||
short_name: str,
|
||||
value: str,
|
||||
page_no: int = 1,
|
||||
bbox: dict[str, float] | None = None,
|
||||
) -> XfaAcroformWidget:
|
||||
"""合成单个 Widget。"""
|
||||
region = bbox or {'x0': 0.1, 'y0': 0.4, 'x1': 0.5, 'y1': 0.55}
|
||||
qname = f'form[0].{short_name}[0]'
|
||||
return XfaAcroformWidget(
|
||||
widget_id=f'wgt-{short_name}',
|
||||
qualified_name=qname,
|
||||
short_name=short_name,
|
||||
page_no=page_no,
|
||||
normalized_bbox=region,
|
||||
value=value,
|
||||
value_masked=value[:4] + '***' if value else '',
|
||||
)
|
||||
|
||||
|
||||
def test_visual_binding_candidates_never_auto_confirm():
|
||||
"""所有候选 status 必须为 suggested,diagnostics 阻断自动确认。"""
|
||||
widgets = [
|
||||
_widget(short_name='EBELN', value='4510039227', bbox={'x0': 0.1, 'y0': 0.1, 'x1': 0.4, 'y1': 0.2}),
|
||||
_widget(short_name='MATNR', value='TEMP-S000218922', bbox={'x0': 0.1, 'y0': 0.4, 'x1': 0.5, 'y1': 0.55}),
|
||||
]
|
||||
result = suggest_visual_binding_candidates(
|
||||
page_no=1,
|
||||
normalized_bbox={'x0': 0.1, 'y0': 0.38, 'x1': 0.52, 'y1': 0.58},
|
||||
dataset_analysis=_dataset_analysis(),
|
||||
widgets=widgets,
|
||||
)
|
||||
assert result['candidates']
|
||||
assert all(item['status'] == 'suggested' for item in result['candidates'])
|
||||
assert result['diagnostics']['auto_confirm_blocked'] is True
|
||||
assert 'confirmed' not in str(result).lower() or all(
|
||||
item['status'] != 'confirmed' for item in result['candidates']
|
||||
)
|
||||
|
||||
|
||||
def test_ambiguous_path_candidates_flagged():
|
||||
"""同短名多 datasets 路径时必须标记 ambiguous。"""
|
||||
widgets = [_widget(short_name='MATNR', value='TEMP-S000218922')]
|
||||
result = suggest_visual_binding_candidates(
|
||||
page_no=1,
|
||||
normalized_bbox={'x0': 0.1, 'y0': 0.4, 'x1': 0.5, 'y1': 0.55},
|
||||
dataset_analysis=_dataset_analysis(),
|
||||
widgets=widgets,
|
||||
)
|
||||
top = result['candidates'][0]
|
||||
assert len(top['path_candidates']) >= 2
|
||||
assert result['diagnostics']['ambiguous'] is True
|
||||
|
||||
|
||||
def test_visual_binding_audit_events_registered():
|
||||
"""视觉绑定审计事件须注册于契约白名单。"""
|
||||
for event in XFA_VISUAL_BINDING_AUDIT_EVENTS:
|
||||
assert event in XFA_AUDIT_EVENT_TYPES, event
|
||||
assert event in XFA_PROGRESS_EVENT_CODES, event
|
||||
|
||||
|
||||
def test_confirm_visual_binding_writes_before_after_audit(app):
|
||||
"""确认视觉绑定必须写入 before/after 审计摘要。"""
|
||||
from app.services.shipping_notice import order_document_profile_visual_binding_service as svc
|
||||
from app.services.shipping_notice import order_document_profile_lease_service as lease_service
|
||||
|
||||
definition = {
|
||||
'template_mode': 'xfa_dataset',
|
||||
'source_contract': {
|
||||
'template_packet_checksum': 'sha256:' + ('a' * 64),
|
||||
'dataset_structure_fingerprint': 'sha256:' + ('b' * 64),
|
||||
'dataset_root_path': '/data',
|
||||
'authoritative_line_collection_path': '/data/ITEMS/DATA',
|
||||
'mirror_collection_paths': [],
|
||||
},
|
||||
'header_mappings': [],
|
||||
'line_mapping': {'row_path': '/data/ITEMS/DATA', 'fields': []},
|
||||
'bindings': [{
|
||||
'binding_id': 'bind-test-001',
|
||||
'field_id': 'f-po',
|
||||
'field_code': 'customer_po_no',
|
||||
'scope': 'header',
|
||||
'value_binding': {
|
||||
'type': 'xfa_dataset',
|
||||
'path_id': 'path-ebeln',
|
||||
'data_path': '/data/HEADER/EBELN',
|
||||
},
|
||||
'visual_binding': {
|
||||
'page_no': 1,
|
||||
'normalized_bbox': {'x0': 0.1, 'y0': 0.1, 'x1': 0.3, 'y1': 0.2},
|
||||
'source': 'suggested',
|
||||
'status': 'suggested',
|
||||
'confidence': 0.9,
|
||||
},
|
||||
}],
|
||||
}
|
||||
before_snap = visual_binding_audit_snapshot(definition['bindings'][0])
|
||||
assert before_snap['status'] == 'suggested'
|
||||
|
||||
with patch.object(lease_service, 'require_active_lease', return_value=object()):
|
||||
with patch.object(svc, '_profile') as profile_mock:
|
||||
profile = type('P', (), {'id': 1, 'status': 'draft', 'template_mode': 'xfa_dataset', 'definition_json': definition})()
|
||||
profile_mock.return_value = profile
|
||||
with patch.object(svc.db.session, 'flush', lambda: None):
|
||||
with patch.object(svc.db.session, 'commit', lambda: None):
|
||||
with patch.object(svc, 'record_visual_binding_audit') as audit_mock:
|
||||
svc.confirm_xfa_visual_binding(
|
||||
1,
|
||||
'bind-test-001',
|
||||
visual_binding={
|
||||
'page_no': 1,
|
||||
'normalized_bbox': {'x0': 0.11, 'y0': 0.11, 'x1': 0.31, 'y1': 0.21},
|
||||
'source': 'manual_draw',
|
||||
'confidence': 1.0,
|
||||
},
|
||||
path_id=None,
|
||||
actor_id=1,
|
||||
browser_session_id='tab',
|
||||
lease_token='lease',
|
||||
)
|
||||
audit_mock.assert_called_once()
|
||||
kwargs = audit_mock.call_args.kwargs
|
||||
assert kwargs['event_type'] == 'xfa_visual_binding_confirmed'
|
||||
assert kwargs['before']['status'] == 'suggested'
|
||||
assert kwargs['after']['status'] == 'confirmed'
|
||||
assert kwargs['before']['normalized_bbox']['x0'] == 0.1
|
||||
assert kwargs['after']['normalized_bbox']['x0'] == 0.11
|
||||
Reference in new issue
Block a user