338 lines
13 KiB
Python
338 lines
13 KiB
Python
"""
|
||
tests/test_shipping_xfa_ocr_automark.py
|
||
|
||
用途:验收 XFA OCR 自动标记入队、accept 分层、mismatch 阻断、发布门禁与 renderer bump 降级。
|
||
依赖:order_document_profile_xfa_automark_service、assert_xfa_synthetic_preview_gate、
|
||
publish_check _blocking_xfa_visual_bindings;mock queue / lease / confirm / native pages。
|
||
逻辑:纯单元为主;enqueue mock 原生页指纹;不依赖真实 RapidOCR / DB 全栈。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
import pytest
|
||
|
||
from app.core.errors import ValidationError
|
||
from app.services.shipping_notice.order_document_profile_publish_check_service import (
|
||
_blocking_xfa_visual_bindings,
|
||
)
|
||
from app.services.shipping_notice.order_document_profile_validation_service import (
|
||
assert_xfa_synthetic_preview_gate,
|
||
)
|
||
from app.services.shipping_notice.order_document_profile_xfa_automark_service import (
|
||
AUTOMARK_MISMATCH_BLOCKED,
|
||
accept_xfa_automark_suggestion,
|
||
demote_stale_confirmed_visual_bindings_on_renderer_bump,
|
||
enqueue_xfa_ocr_automark,
|
||
)
|
||
|
||
pytestmark = pytest.mark.pure_unit
|
||
|
||
|
||
def _source_contract() -> dict:
|
||
"""最小 XFA source_contract。"""
|
||
return {
|
||
'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': [],
|
||
}
|
||
|
||
|
||
def _definition_with_binding(*, visual_status: str = 'suggested', region_kind: str = 'field') -> dict:
|
||
"""构造含统一 binding 的草稿 definition。"""
|
||
checksum = 'sha256:' + ('c' * 64)
|
||
visual = {
|
||
'page_no': 1,
|
||
'normalized_bbox': {'x0': 0.1, 'y0': 0.4, 'x1': 0.4, 'y1': 0.5},
|
||
'source': 'suggested',
|
||
'status': visual_status,
|
||
'confidence': 0.95,
|
||
'region_kind': region_kind,
|
||
}
|
||
if visual_status == 'confirmed':
|
||
visual.update({
|
||
'preview_render_checksum': checksum,
|
||
'render_mode': 'xfa_dataset_preview',
|
||
'confirmed_on': 'xfa_dataset_preview',
|
||
'renderer_version': 'hxops.xfa_static_preview.v1',
|
||
})
|
||
return {
|
||
'template_mode': 'xfa_dataset',
|
||
'source_contract': _source_contract(),
|
||
'header_mappings': [],
|
||
'line_mapping': {'row_path': '/data/ITEMS/DATA', 'fields': []},
|
||
'bindings': [{
|
||
'binding_id': 'bind-po-001',
|
||
'field_id': 'f-h-po',
|
||
'field_code': 'customer_po_no',
|
||
'scope': 'header',
|
||
'value_binding': {
|
||
'type': 'xfa_dataset',
|
||
'path_id': 'path-po',
|
||
'data_path': '/data/HEADER/EBELN',
|
||
},
|
||
'visual_binding': visual,
|
||
}],
|
||
'xfa_preview': {
|
||
'status': 'ready',
|
||
'preview_render_checksum': checksum,
|
||
'render_mode': 'xfa_dataset_preview',
|
||
'renderer_version': 'hxops.xfa_static_preview.v1',
|
||
'page_count': 1,
|
||
'unresolved_ocr_mismatch_count': 0,
|
||
},
|
||
'xfa_ocr_automark': {
|
||
'status': 'completed',
|
||
'preview_render_checksum': checksum,
|
||
'unresolved_ocr_mismatch_count': 0,
|
||
'suggestions': [],
|
||
},
|
||
}
|
||
|
||
|
||
def test_enqueue_xfa_ocr_automark_returns_job_id():
|
||
"""关键断言:enqueue 以原生 PDF 指纹入队,不再依赖 synthetic preview。"""
|
||
definition = _definition_with_binding()
|
||
profile = MagicMock(
|
||
id=12,
|
||
status='draft',
|
||
template_mode='xfa_dataset',
|
||
definition_json=definition,
|
||
)
|
||
native = {
|
||
'pages': [{'page_no': 1, 'png_bytes': b'png'}],
|
||
'page_count': 1,
|
||
'preview_render_checksum': 'sha256:' + ('c' * 64),
|
||
'renderer_version': 'hxops.native_pdf.v1',
|
||
'render_mode': 'native_pdf',
|
||
}
|
||
with patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._profile',
|
||
return_value=profile,
|
||
), patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._load_native_pdf_pages_for_ocr',
|
||
return_value=native,
|
||
), patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service.enqueue',
|
||
return_value=4242,
|
||
) as enqueue_mock, patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service.db',
|
||
) as db_mock:
|
||
job = MagicMock(status='queued')
|
||
db_mock.session.get.return_value = job
|
||
result = enqueue_xfa_ocr_automark(
|
||
12,
|
||
document_id=77,
|
||
actor_id=1,
|
||
force=False,
|
||
)
|
||
assert result['job_id'] == 4242
|
||
assert result['status'] == 'queued'
|
||
assert result['preview_render_checksum'].startswith('sha256:')
|
||
assert result['render_mode'] == 'native_pdf'
|
||
enqueue_mock.assert_called_once()
|
||
call_kwargs = enqueue_mock.call_args
|
||
assert call_kwargs.args[0].value == 'shipping_xfa_ocr_automark' or str(call_kwargs.args[0]) == 'shipping_xfa_ocr_automark'
|
||
|
||
|
||
def test_execute_blank_native_pdf_returns_empty_suggestions():
|
||
"""关键断言:空白壳原生页返回空建议成功,不报 XFA_PREVIEW_UNAVAILABLE。"""
|
||
from app.services.shipping_notice.order_document_profile_xfa_automark_service import (
|
||
execute_xfa_ocr_automark_job,
|
||
)
|
||
|
||
profile = MagicMock(
|
||
id=12,
|
||
status='draft',
|
||
template_mode='xfa_dataset',
|
||
definition_json=_definition_with_binding(),
|
||
)
|
||
native = {
|
||
'pages': [],
|
||
'page_count': 0,
|
||
'preview_render_checksum': 'sha256:' + ('d' * 64),
|
||
'renderer_version': 'hxops.native_pdf.v1',
|
||
'render_mode': 'native_pdf',
|
||
}
|
||
with patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._profile',
|
||
return_value=profile,
|
||
), patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._load_native_pdf_pages_for_ocr',
|
||
return_value=native,
|
||
), patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._audit_automark',
|
||
), patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service.db',
|
||
):
|
||
result = execute_xfa_ocr_automark_job({
|
||
'profile_id': 12,
|
||
'document_id': 77,
|
||
'actor_id': 1,
|
||
'job_id': 9,
|
||
})
|
||
assert result['status'] == 'success'
|
||
assert result['suggestions'] == []
|
||
assert result['error_code'] is None
|
||
assert result['page_count'] == 0
|
||
assert profile.definition_json['xfa_ocr_automark']['suggestion_count'] == 0
|
||
|
||
|
||
def test_accept_high_requires_checksum_and_confirms(app):
|
||
"""关键断言:high 接受走 confirm,且须携带 preview_render_checksum。"""
|
||
from app.services.shipping_notice import order_document_profile_lease_service as lease_service
|
||
from app.services.shipping_notice import order_document_profile_visual_binding_service as visual_svc
|
||
|
||
checksum = 'sha256:' + ('c' * 64)
|
||
definition = _definition_with_binding()
|
||
definition['xfa_ocr_automark']['suggestions'] = [{
|
||
'suggestion_id': 'sugg-high-1',
|
||
'field_key': 'path-po',
|
||
'page_no': 1,
|
||
'normalized_bbox': {'x0': 0.1, 'y0': 0.4, 'x1': 0.4, 'y1': 0.5},
|
||
'match_class': 'high',
|
||
'needs_review': False,
|
||
'confidence': 0.96,
|
||
'region_kind': 'header',
|
||
'preview_render_checksum': checksum,
|
||
'status': 'suggested',
|
||
}]
|
||
profile = MagicMock(
|
||
id=12,
|
||
status='draft',
|
||
template_mode='xfa_dataset',
|
||
definition_json=definition,
|
||
)
|
||
|
||
with patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._profile',
|
||
return_value=profile,
|
||
), patch.object(lease_service, 'require_active_lease', return_value=object()), patch.object(
|
||
visual_svc,
|
||
'confirm_xfa_visual_binding',
|
||
return_value={
|
||
'binding_id': 'bind-po-001',
|
||
'visual_binding': {
|
||
'status': 'confirmed',
|
||
'preview_render_checksum': checksum,
|
||
'region_kind': 'header',
|
||
},
|
||
},
|
||
) as confirm_mock, patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._mark_suggestion_resolved',
|
||
), patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service.db',
|
||
):
|
||
result = accept_xfa_automark_suggestion(
|
||
12,
|
||
'sugg-high-1',
|
||
actor_id=1,
|
||
browser_session_id='tab',
|
||
lease_token='lease',
|
||
)
|
||
assert result['action'] == 'confirmed'
|
||
confirm_mock.assert_called_once()
|
||
kwargs = confirm_mock.call_args.kwargs
|
||
assert kwargs['visual_binding']['region_kind'] == 'header'
|
||
|
||
|
||
def test_accept_mismatch_blocked(app):
|
||
"""关键断言:needs_review / mismatch 接受被阻断。"""
|
||
from app.services.shipping_notice import order_document_profile_lease_service as lease_service
|
||
|
||
definition = _definition_with_binding()
|
||
definition['xfa_ocr_automark']['suggestions'] = [{
|
||
'suggestion_id': 'sugg-bad-1',
|
||
'field_key': 'path-po',
|
||
'page_no': 1,
|
||
'normalized_bbox': {'x0': 0.1, 'y0': 0.4, 'x1': 0.4, 'y1': 0.5},
|
||
'match_class': 'needs_review',
|
||
'needs_review': True,
|
||
'status': 'suggested',
|
||
}]
|
||
profile = MagicMock(
|
||
id=12,
|
||
status='draft',
|
||
template_mode='xfa_dataset',
|
||
definition_json=definition,
|
||
)
|
||
with patch(
|
||
'app.services.shipping_notice.order_document_profile_xfa_automark_service._profile',
|
||
return_value=profile,
|
||
), patch.object(lease_service, 'require_active_lease', return_value=object()):
|
||
with pytest.raises(ValidationError) as caught:
|
||
accept_xfa_automark_suggestion(
|
||
12,
|
||
'sugg-bad-1',
|
||
actor_id=1,
|
||
browser_session_id='tab',
|
||
lease_token='lease',
|
||
)
|
||
assert caught.value.code == AUTOMARK_MISMATCH_BLOCKED
|
||
|
||
|
||
def test_publish_gate_blocks_unresolved_ocr_mismatch():
|
||
"""关键断言:unresolved_ocr_mismatch_count>0 时发布门禁阻断。"""
|
||
with pytest.raises(ValidationError) as caught:
|
||
assert_xfa_synthetic_preview_gate(
|
||
template_mode='xfa_dataset',
|
||
preview_status='ready',
|
||
preview_render_checksum='sha256:' + ('c' * 64),
|
||
bindings=[{
|
||
'field_id': 'f-h-po',
|
||
'visual_binding': {'status': 'confirmed'},
|
||
}],
|
||
schema_field_ids={'f-h-po'},
|
||
preview_meta={
|
||
'status': 'ready',
|
||
'page_count': 1,
|
||
'pages_ready': [1],
|
||
'unresolved_ocr_mismatch_count': 2,
|
||
'renderer_version': 'hxops.xfa_static_preview.v1',
|
||
},
|
||
current_renderer_version='hxops.xfa_static_preview.v1',
|
||
)
|
||
keys = {item['key'] for item in caught.value.details.get('blocking') or []}
|
||
assert 'xfa_ocr_unresolved_mismatch' in keys
|
||
|
||
|
||
def test_publish_check_blocks_unresolved_mismatch_via_definition():
|
||
"""关键断言:_blocking_xfa_visual_bindings 透传 unresolved mismatch。"""
|
||
definition = _definition_with_binding(visual_status='confirmed')
|
||
definition['xfa_preview']['unresolved_ocr_mismatch_count'] = 1
|
||
definition['xfa_ocr_automark']['unresolved_ocr_mismatch_count'] = 1
|
||
blocking = _blocking_xfa_visual_bindings(
|
||
definition=definition,
|
||
schema_field_ids={'f-h-po'},
|
||
)
|
||
assert any(item.get('key') == 'xfa_ocr_unresolved_mismatch' for item in blocking)
|
||
|
||
|
||
def test_renderer_bump_demotes_confirmed_missing_current_checksum():
|
||
"""关键断言:renderer bump / checksum 漂移时 confirmed → suggested。"""
|
||
definition = _definition_with_binding(visual_status='confirmed')
|
||
result = demote_stale_confirmed_visual_bindings_on_renderer_bump(
|
||
definition,
|
||
current_renderer_version='hxops.xfa_static_preview.v2',
|
||
current_preview_render_checksum='sha256:' + ('d' * 64),
|
||
)
|
||
assert result['demoted_count'] == 1
|
||
visual = definition['bindings'][0]['visual_binding']
|
||
assert visual['status'] == 'suggested'
|
||
assert 'confirmed_on' not in visual
|
||
|
||
|
||
def test_renderer_bump_keeps_matching_confirmed():
|
||
"""关键断言:版本与 checksum 均匹配时不降级。"""
|
||
definition = _definition_with_binding(visual_status='confirmed')
|
||
result = demote_stale_confirmed_visual_bindings_on_renderer_bump(
|
||
definition,
|
||
current_renderer_version='hxops.xfa_static_preview.v1',
|
||
current_preview_render_checksum='sha256:' + ('c' * 64),
|
||
)
|
||
assert result['demoted_count'] == 0
|
||
assert definition['bindings'][0]['visual_binding']['status'] == 'confirmed'
|