- 移除 Agent 的 exposure_mode 与 internal_executor 相关逻辑,清理关联的过滤、序列化代码 - 删除废弃的 AgentRuntimeBinding 模型及其依赖 - 新增订单抽取上传批次、请求快照等数据库迁移与业务逻辑 - 修复附件完整性检测、订单抽取状态映射问题 - 更新多语言错误文案与前端单元测试配置 - 清理平台配置中不再使用的暴露模式配置
96 lines
4.9 KiB
Python
96 lines
4.9 KiB
Python
"""验证 EML 正文补充表的确定性解析、关联和数量保护。
|
|
|
|
依赖:正文补充解析器与合并服务。
|
|
逻辑:正文只补 Reservation/来源字段,PDF 数量始终保持权威,歧义稳定进入复核。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app.services.shipping_notice.email_allocation_table_parser import (
|
|
parse_email_allocation_tables,
|
|
parse_email_allocation_text,
|
|
)
|
|
from app.services.shipping_notice.order_supplement_merge import merge_email_supplements
|
|
|
|
|
|
SCHEMA = {'fields': [
|
|
{'field_id': 'po', 'field_code': 'customer_po_number'},
|
|
{'field_id': 'part', 'field_code': 'part_number'},
|
|
{'field_id': 'qty', 'field_code': 'qty'},
|
|
{'field_id': 'reservation', 'field_code': 'reservation_id'},
|
|
{'field_id': 'allocation', 'field_code': 'allocation_id'},
|
|
{'field_id': 'source', 'field_code': 'inventory_source_hint'},
|
|
]}
|
|
|
|
|
|
def _order() -> dict:
|
|
return {
|
|
'_hxops_intake_id': 1,
|
|
'header_fields': [{'field_id': 'po', 'observations': [{'value': '4518738152'}]}],
|
|
'lines': [{'fields': [
|
|
{'field_id': 'part', 'observations': [{'value': 'D4-A08AR12000002'}]},
|
|
{'field_id': 'qty', 'observations': [{'value': 30}]},
|
|
]}],
|
|
'conflicts': [],
|
|
}
|
|
|
|
|
|
def _value(fields: list[dict], field_id: str):
|
|
"""读取测试订单中唯一字段值,断言前由调用方确保该字段存在。"""
|
|
field = next(item for item in fields if item['field_id'] == field_id)
|
|
return field['observations'][0]['value']
|
|
|
|
|
|
def test_email_supplement_html_grid_merges_by_po_and_mpn_without_overwriting_pdf_qty():
|
|
"""正文表可补齐 Reservation/来源,PDF 的数量不被 allocated qty 覆盖。"""
|
|
supplement = parse_email_allocation_tables(
|
|
content_hash='a' * 64,
|
|
html_tables=['''<table><tr><th>PO#</th><th>MPN</th><th>Reservation ID</th><th>Source Type</th><th>Allocated Qty</th></tr><tr><td>4518738152</td><td>D4-A08AR12000002</td><td>2000484577</td><td>DSB CMH</td><td>99</td></tr></table>'''],
|
|
)
|
|
supplement['document_id'] = 44
|
|
assert supplement['rows'][0]['evidence']['reservation_id'] == {
|
|
'table_index': 0, 'row_index': 1, 'column_index': 2,
|
|
'cell_id': f"email-supplement:{'a' * 64}:0:r1:c2", 'raw_value': '2000484577',
|
|
}
|
|
merged = merge_email_supplements(
|
|
orders=[_order()], supplements_by_intake={1: supplement}, schema_definition=SCHEMA,
|
|
)
|
|
fields = merged[0]['lines'][0]['fields']
|
|
assert _value(fields, 'reservation') == '2000484577'
|
|
assert _value(fields, 'source') == 'DSB CMH'
|
|
assert _value(fields, 'qty') == 30
|
|
assert merged[0]['_hxops_email_supplement_relation'] == 'po_mpn'
|
|
|
|
|
|
def test_email_supplement_po_fallback_requires_one_reservation():
|
|
"""缺 MPN 时只有唯一 Reservation 的 PO 可广播;多 Reservation 必须复核。"""
|
|
one = {'document_id': 44, 'rows': [{'po_number': '4518738152', 'reservation_id': '2000484577', 'evidence': {}}]}
|
|
assert merge_email_supplements(orders=[_order()], supplements_by_intake={1: one}, schema_definition=SCHEMA)[0]['_hxops_email_supplement_relation'] == 'po_fallback'
|
|
ambiguous = {'document_id': 44, 'rows': [
|
|
{'po_number': '4518738152', 'reservation_id': '2000484577', 'evidence': {}},
|
|
{'po_number': '4518738152', 'reservation_id': '2000484578', 'evidence': {}},
|
|
]}
|
|
result = merge_email_supplements(orders=[_order()], supplements_by_intake={1: ambiguous}, schema_definition=SCHEMA)[0]
|
|
assert any(item.get('reason') == 'supplement_relation_ambiguous' for item in result['conflicts'])
|
|
|
|
|
|
def test_email_supplement_plain_text_vertical_table_is_parsed_without_guessing_body_text():
|
|
"""Outlook 展平的纵向表头可恢复补充行,尾部邮件正文不会被误当数据。"""
|
|
supplement = parse_email_allocation_text(
|
|
content_hash='b' * 64,
|
|
body_text='''Hello\nallocation_id\nreservation_id\nsource_from_party\nsource_type\nallocated_entity\nmpn\nallocated_qty\nPO#\nA-1\n2000484577\nWelltrust\nDSB CMH\nIEC-CJS\nD4-A08AR12000002\n30\n4518838152\nThank you''',
|
|
)
|
|
assert supplement['rows'] == [{
|
|
'allocation_id': 'A-1', 'reservation_id': '2000484577',
|
|
'source_from_party': 'Welltrust', 'source_type': 'DSB CMH',
|
|
'allocated_entity': 'IEC-CJS', 'mpn': 'D4-A08AR12000002',
|
|
'allocated_qty': '30', 'po_number': '4518838152',
|
|
'evidence': supplement['rows'][0]['evidence'], 'table_index': None, 'row_index': 1,
|
|
}]
|
|
|
|
|
|
def test_email_supplement_plain_text_deduplicates_quoted_email_rows():
|
|
"""转发邮件重复同一正文表时只保留一个事实行,避免制造补充关联冲突。"""
|
|
body = '''allocation_id\nreservation_id\nsource_from_party\nsource_type\nallocated_entity\nmpn\nallocated_qty\nPO#\nA-1\n2000484577\nWelltrust\nDSB CMH\nIEC-CJS\nD4-A08AR12000002\n30\n4518838152\nA-1\n2000484577\nWelltrust\nDSB CMH\nIEC-CJS\nD4-A08AR12000002\n30\n4518838152'''
|
|
assert len(parse_email_allocation_text(body_text=body, content_hash='c' * 64)['rows']) == 1
|