feat(shipping-notice): 新增发货通知自动上传功能与候选资格评估

- 在环境配置中新增 SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED 选项,默认关闭,需显式开启以支持自动上传。
- 引入新的 pytest.ini 文件,增加测试标记以支持自动化测试。
- 删除不再使用的 seed_shipping_notice_agents.py 文件,简化代码库。
- 更新相关 API,增强发货通知与候选资格评估功能,支持多种筛选条件。
- 增强文档与代码注释,反映新的业务逻辑与功能要求。
This commit is contained in:
hugo committed 2026-08-02 19:26:35 +08:00
1 parent cef3d5b26a
commit 3a9fbac2fc
226 files changed
+25010 -2098

No files matched your search

@@ -0,0 +1,198 @@
"""
tests/test_shipping_field_highlight_contract.py
用途:锁定字段精确高亮契约——质量状态、issue code、投影规则与四态 API 形状。
依赖:field_highlight_contract、order_review_evidence_service(集成四态用例)。
逻辑:纯契约断言默认 pure_unit;禁止 quote=machine_value 自证 matched;
非法 bbox 不得进入 drawable 字段。
"""
from __future__ import annotations
import pytest
from app.services.shipping_notice.field_highlight_contract import (
COORDINATE_ISSUE_CODES,
DRAWABLE_HIGHLIGHT_QUALITIES,
FIELD_HIGHLIGHT_CONTRACT_VERSION,
HIGHLIGHT_QUALITY_VALUES,
assert_field_highlight_contract_integrity,
build_field_highlight_projection,
classify_value_match_status,
should_draw_highlight,
)
pytestmark = pytest.mark.pure_unit
def test_field_highlight_contract_integrity():
"""冻结集合与绘制规则必须自洽。"""
assert_field_highlight_contract_integrity()
assert FIELD_HIGHLIGHT_CONTRACT_VERSION == 'shipping-field-highlight-v1'
assert HIGHLIGHT_QUALITY_VALUES == {'verified', 'candidate', 'unavailable', 'invalid'}
assert DRAWABLE_HIGHLIGHT_QUALITIES == {'verified', 'candidate', 'unavailable', 'invalid'}
assert 'duplicate_text_coordinate_ambiguous' in COORDINATE_ISSUE_CODES
assert 'quote_not_independent' in COORDINATE_ISSUE_CODES
def test_self_reported_quote_cannot_match():
"""quote 等于 machine_value 但无独立来源时必须 unchecked,不得自证 matched。"""
# 关键断言:禁止用识别值回声绕过 bbox 文本校验
assert classify_value_match_status(
machine_value='20',
quote='20',
) == 'unchecked'
assert classify_value_match_status(
machine_value='20',
quote='20',
quote_source='machine_value',
) == 'unchecked'
assert classify_value_match_status(
machine_value='20',
quote='20',
bbox_text='20',
quote_source='bbox_tokens',
) == 'matched'
assert classify_value_match_status(
machine_value='20',
quote='20',
quote_source='layout_cell_text',
) == 'matched'
def test_projection_four_states_and_no_forged_bbox():
"""
API 契约覆盖四种 highlight_quality;非法/不可用时 highlight_bbox 必为 null。
"""
verified = build_field_highlight_projection(
normalized_bbox={'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.4},
page_no=1,
source='pdf_coordinate',
machine_value='20',
quote='20',
bbox_text='20',
quote_source='bbox_tokens',
evidence_ref='layout:1:p1:table-1:cell-r2-c6',
token_ids=['p1-glyph-81'],
observation={'highlight_quality': 'verified'},
)
# 关键断言:上游 verified + 合法 bbox + 独立文本 → 可绘制 verified
assert verified['highlight_quality'] == 'verified'
assert verified['coordinate_status'] == 'verified'
assert verified['highlight_bbox'] == {'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.4}
assert should_draw_highlight(verified['highlight_quality'])
assert verified['value_match_status'] == 'matched'
assert verified['evidence_ref'] == 'layout:1:p1:table-1:cell-r2-c6'
assert verified['token_ids'] == ['p1-glyph-81']
assert verified['drawable'] is True
assert verified['reliable'] is True
assert verified['requires_review'] is False
candidate = build_field_highlight_projection(
normalized_bbox={'x0': 0.12, 'y0': 0.36, 'x1': 0.19, 'y1': 0.39},
page_no=1,
source='pdf_coordinate',
machine_value='PO-1',
quote='PO-1',
quote_source='layout_cell_text',
)
# 有合法坐标但无完整门禁 → candidate,不得静默升 verified
assert candidate['highlight_quality'] == 'candidate'
assert candidate['highlight_bbox'] is not None
assert should_draw_highlight(candidate['highlight_quality'])
assert candidate['drawable'] is True
assert candidate['reliable'] is False
assert candidate['requires_review'] is True
# 自报 quote:即使上游误标 verified,也只能降为 candidate
self_reported = build_field_highlight_projection(
normalized_bbox={'x0': 0.12, 'y0': 0.36, 'x1': 0.19, 'y1': 0.39},
page_no=1,
source='pdf_coordinate',
machine_value='PO-1',
quote='PO-1',
observation={'highlight_quality': 'verified'},
)
assert self_reported['highlight_quality'] == 'candidate'
assert self_reported['value_match_status'] == 'unchecked'
assert 'quote_not_independent' in self_reported['coordinate_issue_codes']
unavailable = build_field_highlight_projection(
normalized_bbox=None,
page_no=2,
source='machine_extraction',
machine_value='EA',
quote='EA',
quote_source='layout_cell_text',
)
assert unavailable['highlight_quality'] == 'unavailable'
assert unavailable['highlight_bbox'] is None
assert 'coordinate_missing' in unavailable['coordinate_issue_codes']
# 无 bbox 时仍不会绘制;质量函数只表达该状态允许被颜色化展示。
assert should_draw_highlight(unavailable['highlight_quality'])
invalid = build_field_highlight_projection(
normalized_bbox=None,
raw_bbox_present=True,
raw_bbox_invalid=True,
page_no=1,
source='pdf_coordinate',
machine_value='20',
quote='20',
quote_source='bbox_tokens',
)
assert invalid['highlight_quality'] == 'invalid'
assert invalid['highlight_bbox'] is None
assert 'coordinate_bbox_invalid' in invalid['coordinate_issue_codes']
assert should_draw_highlight(invalid['highlight_quality'])
def test_mismatched_value_blocks_drawing():
"""值与独立引文不一致时即使有 bbox 也必须降为 invalid 且清空绘制坐标。"""
payload = build_field_highlight_projection(
normalized_bbox={'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.4},
page_no=1,
machine_value='20',
quote='999',
bbox_text='999',
quote_source='bbox_tokens',
observation={'highlight_quality': 'verified'},
)
assert payload['highlight_quality'] == 'invalid'
# 值不一致降为红色诊断,但保留合法 bbox 供用户核对原文位置。
assert payload['highlight_bbox'] is not None
assert payload['value_match_status'] == 'mismatched'
assert 'value_bbox_text_mismatch' in payload['coordinate_issue_codes']
def test_highlight_projection_does_not_emit_confirmation_gates():
"""高亮投影不得夹带自动确认/建单门禁字段。"""
payload = build_field_highlight_projection(
normalized_bbox={'x0': 0.1, 'y0': 0.1, 'x1': 0.2, 'y1': 0.2},
page_no=1,
machine_value='A',
quote='A',
quote_source='layout_cell_text',
)
forbidden = {
'auto_confirm', 'auto_confirmed', 'build_eligible', 'confirmation_status',
'gate_status', 'blocking_reason_code',
}
assert forbidden.isdisjoint(payload.keys())
def test_ocr_source_cannot_auto_elevate_to_verified():
"""阶段 2:OCR 即使上游误标 verified 也必须降为 candidate。"""
payload = build_field_highlight_projection(
normalized_bbox={'x0': 0.1, 'y0': 0.2, 'x1': 0.3, 'y1': 0.4},
page_no=1,
source='ocr',
machine_value='20',
quote='20',
bbox_text='20',
quote_source='bbox_tokens',
observation={'highlight_quality': 'verified'},
)
assert payload['highlight_quality'] == 'candidate'
assert payload['highlight_source'] == 'ocr'
assert payload['highlight_bbox'] is not None