feat(shipping-notice): 收敛复核阶段同源投影与 OT 后履约推进
统一 Case/Progress 阶段 label_key、修复 Job 成功未推进 OT 的可重试失败投影与写路径,并落地 OT 后行级匹配/波次履约与版面诊断开关。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
74270adc22
commit
fd6a23f870
100 files changed
+12138
-1017
No files matched your search
@@ -0,0 +1,87 @@
|
||||
"""
|
||||
backend/tests/test_shipping_document_layout_diagnostics.py
|
||||
|
||||
用途:验证 Layout Job PR-0 脱敏 timings/counts 契约与 OCR 计时拆分。
|
||||
依赖:layout_diagnostics、ocr_pdf、unittest.mock。
|
||||
逻辑:覆盖全键初始化、freeze 规范化、extract_ocr_lines 推理计数,以及诊断字段不进入快照的断言辅助。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import numpy as np
|
||||
|
||||
from app.services.shipping_notice.document_layout.layout_diagnostics import (
|
||||
LAYOUT_DIAGNOSTIC_COUNT_KEYS,
|
||||
LAYOUT_DIAGNOSTIC_TIMING_KEYS,
|
||||
LayoutJobDiagnostics,
|
||||
empty_layout_counts,
|
||||
empty_layout_timings,
|
||||
freeze_layout_diagnostics,
|
||||
)
|
||||
from app.services.shipping_notice.document_layout.ocr_pdf import extract_ocr_lines
|
||||
|
||||
|
||||
def test_empty_diagnostics_contain_full_contract_keys():
|
||||
"""PR-0 初始化必须保证全部 timing/count 键存在,未执行阶段为 0。"""
|
||||
timings = empty_layout_timings()
|
||||
counts = empty_layout_counts()
|
||||
assert set(timings) >= set(LAYOUT_DIAGNOSTIC_TIMING_KEYS)
|
||||
assert set(counts) >= set(LAYOUT_DIAGNOSTIC_COUNT_KEYS)
|
||||
assert all(timings[key] == 0 for key in LAYOUT_DIAGNOSTIC_TIMING_KEYS)
|
||||
assert counts['ocr_engine_warm'] is False
|
||||
assert all(counts[key] == 0 for key in LAYOUT_DIAGNOSTIC_COUNT_KEYS if key != 'ocr_engine_warm')
|
||||
|
||||
|
||||
def test_freeze_layout_diagnostics_normalizes_types_and_keeps_extra_timing():
|
||||
"""freeze 输出 int/bool,并保留额外 timing 键供降级路径兼容。"""
|
||||
timings = empty_layout_timings()
|
||||
counts = empty_layout_counts()
|
||||
timings['native_layout_build_ms'] = 12.7
|
||||
timings['native_table_detect_ms'] = 3
|
||||
counts['ocr_call_count'] = 2
|
||||
counts['ocr_engine_warm'] = 1
|
||||
frozen_timings, frozen_counts = freeze_layout_diagnostics(timings, counts)
|
||||
assert frozen_timings['native_layout_build_ms'] == 12
|
||||
assert frozen_timings['native_table_detect_ms'] == 3
|
||||
assert frozen_counts['ocr_call_count'] == 2
|
||||
assert frozen_counts['ocr_engine_warm'] is True
|
||||
|
||||
|
||||
def test_extract_ocr_lines_records_inference_without_changing_lines():
|
||||
"""传入 diagnostics 时必须增加 ocr_call_count,且行坐标结果与无诊断调用一致。"""
|
||||
image = np.zeros((200, 400, 3), dtype=np.uint8)
|
||||
response = [
|
||||
[[[40, 20], [120, 20], [120, 60], [40, 60]], 'Part', 0.95],
|
||||
]
|
||||
diagnostics = LayoutJobDiagnostics()
|
||||
with patch('app.services.shipping_notice.document_layout.ocr_pdf._ocr_engine') as engine:
|
||||
engine.return_value.return_value = (response, 0.1)
|
||||
# mock 无 cache_info 时不得把调用计为 engine init
|
||||
lines_with = extract_ocr_lines(
|
||||
image, page_width=200, page_height=100,
|
||||
diagnostics=diagnostics, page_no=1,
|
||||
)
|
||||
lines_without = extract_ocr_lines(image, page_width=200, page_height=100)
|
||||
|
||||
assert lines_with[0].text == lines_without[0].text
|
||||
assert lines_with[0].bbox.model_dump() == lines_without[0].bbox.model_dump()
|
||||
assert diagnostics.counts['ocr_call_count'] == 1
|
||||
assert diagnostics.counts['ocr_page_count'] == 1
|
||||
assert diagnostics.timings['ocr_engine_init_ms'] == 0
|
||||
assert diagnostics.timings['ocr_inference_ms'] >= 0
|
||||
|
||||
|
||||
def test_snapshot_must_not_embed_diagnostics_keys():
|
||||
"""回归辅助:正式 snapshot 不得出现 timings/counts 诊断字段。"""
|
||||
snapshot = {
|
||||
'status': 'ready',
|
||||
'tables': [],
|
||||
'layout_candidate': {'pages': []},
|
||||
'coordinate_evidence': {'tables': {}, 'pages': {}},
|
||||
}
|
||||
assert 'timings' not in snapshot
|
||||
assert 'counts' not in snapshot
|
||||
assert 'timings' not in snapshot['layout_candidate']
|
||||
assert 'counts' not in snapshot['coordinate_evidence']
|
||||
Reference in new issue
Block a user