feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -2,14 +2,16 @@
|
||||
backend/tests/test_shipping_document_layout_lifecycle.py
|
||||
|
||||
用途:验证 L1 版面快照 checksum、版本幂等、容量门禁和当前成功版本保护。
|
||||
依赖:ShippingNoticeDocument/LayoutVersion、document_layout.lifecycle、Flask 测试库。
|
||||
依赖:ShippingOrderDocument/LayoutVersion、document_layout.lifecycle、Flask 测试库。
|
||||
逻辑:相同快照复用;新失败/超限版本保留审计但不覆盖旧成功引用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app import db
|
||||
from app.models import Job, ShippingDocumentLayoutVersion, ShippingNoticeDocument, ShippingNoticeIntake, FileAsset, User
|
||||
from app.models import Job, ShippingDocumentLayoutVersion, ShippingOrderDocument, ShippingOrderIntake, FileAsset, User
|
||||
from app.services.shipping_notice.document_layout.lifecycle import (
|
||||
canonical_layout_payload,
|
||||
claim_layout_version,
|
||||
@@ -21,9 +23,12 @@ from app.services.shipping_notice.document_layout.lifecycle import (
|
||||
sync_layout_after_job_recovery,
|
||||
)
|
||||
from app.services.shipping_notice.document_layout import lifecycle as layout_lifecycle
|
||||
from app.services.shipping_notice.document_layout.on_demand_coordinate_evidence import (
|
||||
ensure_on_demand_coordinate_evidence,
|
||||
)
|
||||
|
||||
|
||||
def _document() -> ShippingNoticeDocument:
|
||||
def _document() -> ShippingOrderDocument:
|
||||
"""创建带稳定内容哈希的最小附件 Document。"""
|
||||
user = User(username='layout-owner', email='layout-owner@example.com', role='user', status='active')
|
||||
db.session.add(user)
|
||||
@@ -36,13 +41,13 @@ def _document() -> ShippingNoticeDocument:
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no='SNI-LAYOUT-L1', asset_id=asset.id, user_id=user.id,
|
||||
purpose='shipping_notice', parse_status='parsed', parser_version='v6',
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.flush()
|
||||
document = ShippingNoticeDocument(
|
||||
document = ShippingOrderDocument(
|
||||
intake_id=intake.id, asset_id=asset.id, document_type='attachment',
|
||||
attachment_key='sample.pdf', filename='sample.pdf', content_hash='a' * 64,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
@@ -57,7 +62,9 @@ def _snapshot(*, status: str = 'ready', marker: str = 'v1') -> dict:
|
||||
return {
|
||||
'snapshot_version': 'shipping_document_table_snapshot.v1',
|
||||
'provider': 'mineru', 'provider_version': '3.1.14', 'backend': 'pipeline',
|
||||
'status': status, 'tables': [], 'issues': [], 'marker': marker,
|
||||
'status': status, 'tables': [], 'layout_candidate': {},
|
||||
'coordinate_evidence': {'pages': {}, 'page_glyphs': {}},
|
||||
'issues': [], 'marker': marker,
|
||||
}
|
||||
|
||||
|
||||
@@ -80,13 +87,13 @@ def test_layout_snapshot_checksum_and_idempotent_current_version(app):
|
||||
|
||||
def test_failed_or_oversized_layout_does_not_replace_last_good_version(app):
|
||||
"""失败/超限版本必须可审计,但绝不能覆盖 Document 上一次成功布局。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 200
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 500
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
good = record_inline_layout_snapshot(document, _snapshot())
|
||||
failed = record_inline_layout_snapshot(document, _snapshot(status='failed', marker='failed'))
|
||||
oversized = record_inline_layout_snapshot(document, {
|
||||
**_snapshot(marker='large'), 'tables': [{'html': 'X' * 500}],
|
||||
**_snapshot(marker='large'), 'tables': [{'html': 'X' * 5000}],
|
||||
})
|
||||
db.session.commit()
|
||||
|
||||
@@ -125,14 +132,14 @@ def test_layout_schedule_localizes_same_owner_completed_layout_for_duplicate_doc
|
||||
with app.app_context():
|
||||
original = _document()
|
||||
ready = record_inline_layout_snapshot(original, _snapshot())
|
||||
duplicate_intake = ShippingNoticeIntake(
|
||||
duplicate_intake = ShippingOrderIntake(
|
||||
intake_no='SNI-LAYOUT-DUPLICATE', asset_id=original.asset_id,
|
||||
user_id=db.session.get(ShippingNoticeIntake, original.intake_id).user_id,
|
||||
user_id=db.session.get(ShippingOrderIntake, original.intake_id).user_id,
|
||||
purpose='order_document_profile_sample', parse_status='parsed', parser_version='v6',
|
||||
)
|
||||
db.session.add(duplicate_intake)
|
||||
db.session.flush()
|
||||
duplicate = ShippingNoticeDocument(
|
||||
duplicate = ShippingOrderDocument(
|
||||
intake_id=duplicate_intake.id, asset_id=original.asset_id, document_type='attachment',
|
||||
attachment_key='duplicate.pdf', filename='duplicate.pdf', content_hash=original.content_hash,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
@@ -152,6 +159,37 @@ def test_layout_schedule_localizes_same_owner_completed_layout_for_duplicate_doc
|
||||
assert ShippingDocumentLayoutVersion.query.count() == 2
|
||||
|
||||
|
||||
def test_recoverable_ready_layout_is_reused_for_duplicate_document(app):
|
||||
"""ready 快照缺整页证据但原 PDF 可读时应复用,正式识别再按需恢复坐标。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
with app.app_context():
|
||||
original = _document()
|
||||
incomplete = record_inline_layout_snapshot(original, {
|
||||
**_snapshot(), 'coordinate_evidence': {'tables': {}},
|
||||
})
|
||||
duplicate_intake = ShippingOrderIntake(
|
||||
intake_no='SNI-LAYOUT-INCOMPLETE-DUP', asset_id=original.asset_id,
|
||||
user_id=db.session.get(ShippingOrderIntake, original.intake_id).user_id,
|
||||
purpose='order_document_profile_sample', parse_status='parsed', parser_version='v6',
|
||||
)
|
||||
db.session.add(duplicate_intake)
|
||||
db.session.flush()
|
||||
duplicate = ShippingOrderDocument(
|
||||
intake_id=duplicate_intake.id, asset_id=original.asset_id, document_type='attachment',
|
||||
attachment_key='duplicate-incomplete.pdf', filename='duplicate-incomplete.pdf',
|
||||
content_hash=original.content_hash, classification='order_notice', parse_status='parsed',
|
||||
)
|
||||
db.session.add(duplicate)
|
||||
db.session.flush()
|
||||
|
||||
scheduled = schedule_layout_version(duplicate)
|
||||
db.session.commit()
|
||||
|
||||
assert incomplete.status == 'ready'
|
||||
assert scheduled.status == 'ready'
|
||||
assert scheduled.id != incomplete.id
|
||||
|
||||
|
||||
def test_layout_parser_fingerprint_change_creates_new_version_without_force(app, monkeypatch):
|
||||
"""布局实现指纹变化后自动创建新版本,不依赖人工修改环境变量。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
@@ -209,18 +247,167 @@ def test_layout_recovery_invalidates_old_lease_and_preserves_good_current(app):
|
||||
assert document.current_layout_version_id == ready.id
|
||||
|
||||
|
||||
def test_compacted_pdf_coordinate_evidence_remains_ready_when_asset_is_recoverable(app):
|
||||
"""PDF 原件仍可读时,压缩整页/表内字符不得丢失模板所需表格结构。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 700
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
pending = schedule_layout_version(document, force=True)
|
||||
job = db.session.get(Job, pending.job_id)
|
||||
job.status = 'running'
|
||||
job.attempts = 1
|
||||
db.session.commit()
|
||||
claimed = claim_layout_version(pending.id, job_id=job.id, owned_attempt=1)
|
||||
assert claimed is not None
|
||||
_version, lease = claimed
|
||||
snapshot = {
|
||||
**_snapshot(),
|
||||
'tables': [{
|
||||
'table_id': 'table-1',
|
||||
'page_no': 1,
|
||||
'bbox': {'x0': 1, 'y0': 2, 'x1': 30, 'y1': 40},
|
||||
'html': '<table><tr><td>PO-1</td></tr></table>',
|
||||
}],
|
||||
'coordinate_evidence': {
|
||||
'pages': {'1': {'width': 100, 'height': 200}},
|
||||
'page_glyphs': {'1': [{'text': 'X' * 3000}]},
|
||||
'tables': {'table-1': [{'text': 'Y' * 5000}]},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
},
|
||||
'layout_candidate': {'table_count': 1, 'tables': [{'table_id': 'table-1'}]},
|
||||
}
|
||||
|
||||
assert complete_layout_version(pending.id, lease_token=lease, snapshot=snapshot) is True
|
||||
|
||||
db.session.refresh(pending)
|
||||
db.session.refresh(document)
|
||||
assert pending.status == 'ready'
|
||||
assert document.current_layout_version_id == pending.id
|
||||
assert pending.payload_json['tables'] == snapshot['tables']
|
||||
assert pending.payload_json['layout_candidate'] == snapshot['layout_candidate']
|
||||
assert 'tables' not in pending.payload_json['coordinate_evidence']
|
||||
assert 'page_glyphs' not in pending.payload_json['coordinate_evidence']
|
||||
assert 'shipping_document_layout_page_glyphs_compacted' in pending.issues_json
|
||||
assert 'shipping_document_layout_table_glyphs_compacted' in pending.issues_json
|
||||
assert 'shipping_document_layout_coordinate_evidence_on_demand' in pending.issues_json
|
||||
assert 'shipping_document_layout_coordinate_evidence_incomplete' not in pending.issues_json
|
||||
|
||||
|
||||
def test_inline_layout_compacts_table_glyphs_without_changing_template_structures(app):
|
||||
"""普通上传内联布局同样只压缩可恢复字符,模板表格与候选结构保持原样。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MAX_SNAPSHOT_BYTES'] = 700
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
snapshot = {
|
||||
**_snapshot(marker='inline-compacted'),
|
||||
'tables': [{
|
||||
'table_id': 'table-1', 'page_no': 1,
|
||||
'bbox': {'x0': 1, 'y0': 2, 'x1': 30, 'y1': 40},
|
||||
'html': '<table><tr><td>PO-1</td></tr></table>',
|
||||
}],
|
||||
'coordinate_evidence': {
|
||||
'pages': {'1': {'width': 100, 'height': 200}},
|
||||
'page_glyphs': {'1': [{'text': 'X' * 3000}]},
|
||||
'tables': {'table-1': [{'text': 'Y' * 5000}]},
|
||||
},
|
||||
'layout_candidate': {'table_count': 1, 'tables': [{'table_id': 'table-1'}]},
|
||||
}
|
||||
|
||||
version = record_inline_layout_snapshot(document, snapshot)
|
||||
|
||||
assert version.status == 'ready'
|
||||
assert version.payload_json['tables'] == snapshot['tables']
|
||||
assert version.payload_json['layout_candidate'] == snapshot['layout_candidate']
|
||||
assert 'tables' not in version.payload_json['coordinate_evidence']
|
||||
assert 'shipping_document_layout_table_glyphs_compacted' in version.issues_json
|
||||
assert 'shipping_document_layout_coordinate_evidence_on_demand' in version.issues_json
|
||||
|
||||
|
||||
def test_on_demand_evidence_does_not_treat_business_table_list_as_coordinate_mapping(app):
|
||||
"""压缩后顶层业务 tables 列表不得进入坐标映射并触发 dict 转换异常。"""
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
layout = {
|
||||
**_snapshot(),
|
||||
'tables': [{
|
||||
'table_id': 'table-1', 'page_no': 1,
|
||||
'bbox': {'x0': 1, 'y0': 2, 'x1': 30, 'y1': 40},
|
||||
'html': '<table><tr><td>PO-1</td></tr></table>',
|
||||
'cells': [], 'rows': [], 'columns': [], 'source': 'native', 'status': 'ready',
|
||||
}],
|
||||
'coordinate_evidence': {
|
||||
'pages': {'1': {'width': 100, 'height': 200}},
|
||||
'sources': {'pages': {'1': 'pdf_coordinate'}},
|
||||
},
|
||||
}
|
||||
record_inline_layout_snapshot(document, layout)
|
||||
|
||||
restored = ensure_on_demand_coordinate_evidence(
|
||||
document=document,
|
||||
layout=layout,
|
||||
page_nos=set(),
|
||||
)
|
||||
|
||||
assert restored['tables'] == layout['tables']
|
||||
assert 'tables' not in restored['coordinate_evidence']
|
||||
|
||||
|
||||
def test_on_demand_evidence_restores_every_table_on_the_same_page(app, monkeypatch):
|
||||
"""同一页存在多张物理表时,每个 table_id 都必须获得独立坐标字符证据。"""
|
||||
with app.app_context():
|
||||
document = _document()
|
||||
layout = {
|
||||
**_snapshot(),
|
||||
'tables': [
|
||||
{'table_id': 'table-1', 'page_no': 1, 'bbox': {'x0': 0, 'y0': 10, 'x1': 100, 'y1': 40}},
|
||||
{'table_id': 'table-2', 'page_no': 1, 'bbox': {'x0': 0, 'y0': 50, 'x1': 100, 'y1': 90}},
|
||||
],
|
||||
'coordinate_evidence': {'pages': {'1': {'width': 100, 'height': 200}}},
|
||||
}
|
||||
page = SimpleNamespace(
|
||||
page_no=1, width=100, height=200, image=None,
|
||||
characters=[SimpleNamespace(model_dump=lambda mode='json': {'text': 'X'})],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.file_assets.service.read_asset_bytes', lambda _asset: b'%PDF-test',
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.document_layout.native_pdf.extract_native_pdf_pages',
|
||||
lambda _content: [page],
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.order_document_profile_service._native_characters_in_table',
|
||||
lambda *, characters, table_bbox: [{'text': str(table_bbox['y0'])}],
|
||||
)
|
||||
|
||||
restored = ensure_on_demand_coordinate_evidence(
|
||||
document=document,
|
||||
layout=layout,
|
||||
page_nos={1},
|
||||
# 兼容旧调用方即使只传入同页最后一张表,也不能覆盖 layout 中的第一张表。
|
||||
table_by_page={1: layout['tables'][1]},
|
||||
)
|
||||
|
||||
# 关键断言:同页表格不能被 page_no 字典覆盖,正式抽取可按真实 table_id 逐表重建。
|
||||
assert restored['coordinate_evidence']['tables'] == {
|
||||
'table-1': [{'text': '10'}],
|
||||
'table-2': [{'text': '50'}],
|
||||
}
|
||||
|
||||
|
||||
def test_layout_document_budget_creates_unsupported_audit_versions(app):
|
||||
"""超过单 Intake 配额的支持附件必须有 unsupported 审计,不能静默跳过。"""
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_ENABLED'] = True
|
||||
app.config['SHIPPING_NOTICE_LAYOUT_MINERU_MAX_DOCUMENTS_PER_INTAKE'] = 1
|
||||
with app.app_context():
|
||||
first = _document()
|
||||
second = ShippingNoticeDocument(
|
||||
second = ShippingOrderDocument(
|
||||
intake_id=first.intake_id, asset_id=first.asset_id, document_type='attachment',
|
||||
attachment_key='second.pdf', filename='second.pdf', content_hash='b' * 64,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
)
|
||||
text = ShippingNoticeDocument(
|
||||
text = ShippingOrderDocument(
|
||||
intake_id=first.intake_id, asset_id=first.asset_id, document_type='attachment',
|
||||
attachment_key='notes.txt', filename='notes.txt', content_hash='c' * 64,
|
||||
classification='order_notice', parse_status='parsed',
|
||||
@@ -250,5 +437,5 @@ def test_deleting_document_cascades_layout_versions(app):
|
||||
db.session.delete(document)
|
||||
db.session.commit()
|
||||
|
||||
assert db.session.get(ShippingNoticeDocument, document_id) is None
|
||||
assert db.session.get(ShippingOrderDocument, document_id) is None
|
||||
assert db.session.get(ShippingDocumentLayoutVersion, version_id) is None
|
||||
Reference in new issue
Block a user