feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-06 10:18:37 +08:00
1 parent 3a9fbac2fc
commit d2b3d74def
571 files changed
+78064 -24681

No files matched your search

@@ -1,9 +1,9 @@
"""
backend/tests/test_shipping_document_layout_runtime.py
用途:验证 Intake 阶段 MinerU 紧凑快照、PDFium 页面复用、降级行为和容量门禁。
用途:验证 MinerU 优先的紧凑快照、PDFium 页面复用、受控降级行为和容量门禁。
依赖:document_layout.runtime/mineru 构造候选、Flask 测试配置。
逻辑:Mock 客户端避免真实网络;只允许受预算文本块、表格 HTML 和能力摘要进入快照,原响应不得持久化。
逻辑:Mock 客户端避免真实网络;MinerU 有表格时不得被 PDFium 替换,只有空表或失败才允许严格回退。
"""
from __future__ import annotations
@@ -152,8 +152,56 @@ def test_runtime_reuses_native_pdf_pages_across_fallback_layout_and_evidence(app
assert 'shipping_document_business_table_not_detected' in snapshot['issues']
def test_runtime_native_pdf_fast_path_skips_mineru_when_strict_table_evidence_exists(app):
"""文字型 PDF 可证明订单表格时应立即完成,不得继续等待 MinerU。"""
def test_runtime_empty_mineru_tables_use_pdfium_provider_fallback(app):
"""MinerU 真正返回空表时,严格原生表须成为显式 PDFium 降级快照。"""
app.config.update(
SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED=True,
SHIPPING_NOTICE_LAYOUT_MINERU_BASE_URL='http://mineru.internal:8100',
)
empty_candidate = _candidate().model_copy(update={'pages': [
_candidate().pages[0].model_copy(update={'tables': []}),
]})
page = type('Page', (), {'page_no': 1, 'width': 100, 'height': 200})()
table = {
'table_id': 'native-1', 'page_no': 1,
'bbox': {'x0': 0, 'y0': 20, 'x1': 100, 'y1': 100},
'normalized_bbox': {'x0': 0, 'y0': .1, 'x1': 1, 'y1': .5},
'html': '<table><tr><th>Item</th><th>Qty</th></tr><tr><td>10</td><td>1</td></tr></table>',
'row_count': 2, 'cell_count': 4, 'has_rowspan': False, 'has_colspan': False,
}
with app.app_context(), patch(
'app.services.shipping_notice.document_layout.native_pdf.extract_native_pdf_pages',
return_value=[page],
), patch(
'app.services.shipping_notice.document_layout.native_pdf.build_native_pdf_text_table_fallbacks',
return_value=[table],
), patch(
'app.services.shipping_notice.document_layout.native_pdf.build_native_pdf_layout_candidate',
) as build_layout, patch(
'app.services.shipping_notice.document_layout.runtime._table_coordinate_evidence',
return_value={'version': 'test'},
), patch(
'app.services.shipping_notice.document_layout.runtime.MinerUClient.parse_file_async',
return_value={'status': 'completed'},
), patch(
'app.services.shipping_notice.document_layout.runtime.parse_mineru_candidate',
return_value=empty_candidate,
):
build_layout.return_value.model_dump.return_value = {'schema_version': 'shipping_document_layout.v1'}
snapshot = build_mineru_table_snapshot(
filename='order.pdf', content=b'%PDF', document_id=1, content_hash='d' * 64,
)
assert snapshot['provider'] == 'pdfium'
assert snapshot['backend'] == 'native_pdf'
assert snapshot['tables'][0]['table_id'] == 'native-1'
assert snapshot['layout_candidate']['schema_version'] == 'shipping_document_layout.v1'
assert 'mineru_provider_fallback_used' in snapshot['issues']
assert 'mineru_table_structure_missing' in snapshot['issues']
def test_runtime_mineru_table_remains_primary_when_native_table_evidence_exists(app):
"""本地文字可恢复表格时仍须先调用 MinerU,且不得替换 Provider 已返回的表格。"""
app.config.update(
SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED=True,
SHIPPING_NOTICE_LAYOUT_MINERU_BASE_URL='http://mineru.internal:8100',
@@ -172,14 +220,18 @@ def test_runtime_native_pdf_fast_path_skips_mineru_when_strict_table_evidence_ex
), patch(
'app.services.shipping_notice.document_layout.native_pdf.build_native_pdf_text_table_fallbacks',
return_value=[table],
), patch(
) as fallback, patch(
'app.services.shipping_notice.document_layout.native_pdf.build_native_pdf_layout_candidate',
) as build_layout, patch(
'app.services.shipping_notice.document_layout.runtime._table_coordinate_evidence',
return_value={'version': 'shipping_order_coordinate_evidence.v1'},
), patch(
'app.services.shipping_notice.document_layout.runtime.MinerUClient.parse_file_async',
) as mineru:
return_value={'status': 'completed'},
) as mineru, patch(
'app.services.shipping_notice.document_layout.runtime.parse_mineru_candidate',
return_value=_candidate(),
):
build_layout.return_value.model_dump.return_value = {'schema_version': 'shipping_document_layout.v1'}
timings: dict[str, int] = {}
snapshot = build_mineru_table_snapshot(
@@ -188,14 +240,15 @@ def test_runtime_native_pdf_fast_path_skips_mineru_when_strict_table_evidence_ex
)
assert snapshot['status'] == 'ready'
assert snapshot['provider'] == 'pdfium'
assert snapshot['tables'][0]['table_id'] == 'native-1'
assert timings['layout_fast_path'] == 1
mineru.assert_not_called()
assert snapshot['provider'] == 'mineru'
assert snapshot['tables'][0]['table_id'] == 'table-1'
assert 'layout_fast_path' not in timings
mineru.assert_called_once()
fallback.assert_not_called()
def test_runtime_native_fast_path_defers_page_rendering(app):
"""本地文字证据命中时只提取字符,不能预先渲染高分辨率页面图。"""
def test_runtime_provider_failure_uses_strict_native_fallback_without_page_rendering(app):
"""MinerU 失败后才允许原生文字降级,且复用前置字符预读而不渲染页面图。"""
app.config.update(
SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED=True,
SHIPPING_NOTICE_LAYOUT_MINERU_BASE_URL='http://mineru.internal:8100',
@@ -219,13 +272,21 @@ def test_runtime_native_fast_path_defers_page_rendering(app):
) as build_layout, patch(
'app.services.shipping_notice.document_layout.runtime._table_coordinate_evidence',
return_value={'version': 'test'},
):
), patch(
'app.services.shipping_notice.document_layout.runtime.MinerUClient.parse_file_async',
side_effect=ServiceUnavailableError(
'failed', code='SHIPPING_NOTICE.LAYOUT_PROVIDER_UNAVAILABLE',
),
) as mineru:
build_layout.return_value.model_dump.return_value = {'schema_version': 'shipping_document_layout.v1'}
snapshot = build_mineru_table_snapshot(
filename='order.pdf', content=b'%PDF', document_id=1, content_hash='b' * 64,
)
assert snapshot['provider'] == 'pdfium'
assert 'mineru_provider_fallback_used' in snapshot['issues']
assert 'SHIPPING_NOTICE.LAYOUT_PROVIDER_UNAVAILABLE' in snapshot['issues']
mineru.assert_called_once()
extract_pages.assert_called_once_with(b'%PDF', render_images=False)
@@ -248,6 +309,36 @@ def test_runtime_provider_failure_is_safe_fallback(app):
}
def test_runtime_native_fallback_failure_preserves_mineru_error(app):
"""严格 PDFium 降级自身异常时不得覆盖 MinerU 原始失败结果。"""
app.config.update(
SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED=True,
SHIPPING_NOTICE_LAYOUT_MINERU_BASE_URL='http://mineru.internal:8100',
)
page = type('Page', (), {'page_no': 1, 'width': 100, 'height': 200})()
with app.app_context(), patch(
'app.services.shipping_notice.document_layout.native_pdf.extract_native_pdf_pages',
return_value=[page],
), patch(
'app.services.shipping_notice.document_layout.native_pdf.build_native_pdf_text_table_fallbacks',
side_effect=ValueError('invalid native table evidence'),
), patch(
'app.services.shipping_notice.document_layout.runtime.MinerUClient.parse_file_async',
side_effect=ServiceUnavailableError(
'failed', code='SHIPPING_NOTICE.LAYOUT_PROVIDER_UNAVAILABLE',
),
):
snapshot = build_mineru_table_snapshot(
filename='order.pdf', content=b'%PDF', document_id=1, content_hash='c' * 64,
)
assert snapshot == {
'snapshot_version': 'shipping_document_table_snapshot.v1',
'provider': 'mineru', 'backend': 'pipeline', 'status': 'failed',
'tables': [], 'issues': ['SHIPPING_NOTICE.LAYOUT_PROVIDER_UNAVAILABLE'],
}
def test_runtime_attachment_budget_preserves_order_and_marks_overflow(app):
"""EML 多附件只并发处理预算内文档,超量附件显式降级且原顺序不变。"""
app.config.update(