feat(shipping-notice): 增强发货通知与订单抽取功能,支持多进程处理与紧急停止
- 移除不再使用的 SHIPPING_NOTICE_LAYOUT_PARSER_REVISION 配置 - 在环境配置中新增 SHIPPING_NOTICE_WORKER_CONCURRENCY 和 SHIPPING_NOTICE_JOB_RUNNING_TIMEOUT_SECONDS - 实现发货通知 worker 进程,支持并发处理订单抽取任务 - 引入紧急停止功能,允许管理员在运行中控制订单解析状态 - 更新相关 API,支持读取与设置紧急停止状态 - 增强文档与代码注释,反映新的业务逻辑与功能要求
This commit is contained in:
1 parent
d0fe084999
commit
b352c281d5
149 files changed
+12550
-1874
No files matched your search
@@ -1,14 +1,14 @@
|
||||
"""
|
||||
backend/tests/test_shipping_document_layout_runtime.py
|
||||
|
||||
用途:验证 Intake 阶段 MinerU 紧凑文本/表格快照、降级行为和容量门禁。
|
||||
用途:验证 Intake 阶段 MinerU 紧凑快照、PDFium 页面复用、降级行为和容量门禁。
|
||||
依赖:document_layout.runtime/mineru 构造候选、Flask 测试配置。
|
||||
逻辑:Mock 客户端避免真实网络;只允许受预算文本块、表格 HTML 和能力摘要进入快照,原响应不得持久化。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import call, patch
|
||||
|
||||
from app.core.errors import ServiceUnavailableError
|
||||
from app.services.shipping_notice.document_layout.contracts import LayoutBBox
|
||||
@@ -102,6 +102,133 @@ def test_runtime_limits_persisted_mineru_text(app):
|
||||
assert 'mineru_text_budget_exceeded' in snapshot['issues']
|
||||
|
||||
|
||||
def test_runtime_reuses_native_pdf_pages_across_fallback_layout_and_evidence(app):
|
||||
"""单次 PDF 快照只允许提取/渲染一次页面,后续三个本地阶段必须复用同一对象。"""
|
||||
app.config.update(
|
||||
SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED=True,
|
||||
SHIPPING_NOTICE_LAYOUT_MINERU_BASE_URL='http://mineru.internal:8100',
|
||||
)
|
||||
native_pages = [object()]
|
||||
timings: dict[str, int] = {}
|
||||
with app.app_context(), 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=_candidate().model_copy(update={'pages': [
|
||||
_candidate().pages[0].model_copy(update={'tables': []}),
|
||||
]}),
|
||||
), patch(
|
||||
'app.services.shipping_notice.document_layout.native_pdf.extract_native_pdf_pages',
|
||||
return_value=native_pages,
|
||||
) as extract_pages, patch(
|
||||
'app.services.shipping_notice.document_layout.native_pdf.build_native_pdf_text_table_fallbacks',
|
||||
return_value=[],
|
||||
) 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': 'test'},
|
||||
) as build_evidence:
|
||||
build_layout.return_value.model_dump.return_value = {'version': 'layout.v1'}
|
||||
snapshot = build_mineru_table_snapshot(
|
||||
filename='order.pdf',
|
||||
content=b'%PDF',
|
||||
document_id=1,
|
||||
content_hash='hash',
|
||||
timings=timings,
|
||||
)
|
||||
|
||||
assert snapshot['status'] == 'ready'
|
||||
assert extract_pages.call_args_list == [
|
||||
call(b'%PDF', render_images=False),
|
||||
call(b'%PDF'),
|
||||
]
|
||||
assert fallback.call_args.kwargs['native_pages'] is native_pages
|
||||
assert build_layout.call_args.kwargs['native_pages'] is native_pages
|
||||
assert build_evidence.call_args.kwargs['pages'] is native_pages
|
||||
assert 'mineru_wait_ms' in timings
|
||||
assert 'native_pdf_extract_ms' in timings
|
||||
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。"""
|
||||
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})()
|
||||
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>Material</th><th>Qty</th></tr><tr><td>10</td><td>A</td><td>1</td></tr></table>',
|
||||
'row_count': 2, 'cell_count': 6, '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': 'shipping_order_coordinate_evidence.v1'},
|
||||
), patch(
|
||||
'app.services.shipping_notice.document_layout.runtime.MinerUClient.parse_file_async',
|
||||
) as mineru:
|
||||
build_layout.return_value.model_dump.return_value = {'schema_version': 'shipping_document_layout.v1'}
|
||||
timings: dict[str, int] = {}
|
||||
snapshot = build_mineru_table_snapshot(
|
||||
filename='order.pdf', content=b'%PDF', document_id=1,
|
||||
content_hash='a' * 64, timings=timings,
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
|
||||
def test_runtime_native_fast_path_defers_page_rendering(app):
|
||||
"""本地文字证据命中时只提取字符,不能预先渲染高分辨率页面图。"""
|
||||
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})()
|
||||
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],
|
||||
) as extract_pages, 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'},
|
||||
):
|
||||
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'
|
||||
extract_pages.assert_called_once_with(b'%PDF', render_images=False)
|
||||
|
||||
|
||||
def test_runtime_provider_failure_is_safe_fallback(app):
|
||||
"""MinerU 失败只记录错误码并返回空表,不能抛出导致 Intake 文本解析失败。"""
|
||||
app.config.update(
|
||||
|
||||
Reference in new issue
Block a user