229 lines
8.8 KiB
Python
229 lines
8.8 KiB
Python
"""
|
|
backend/tests/test_shipping_task_parsed_projection.py
|
|
|
|
用途:验证 Task Detail / Candidate brief 对订单解析结果与门禁的持久只读投影。
|
|
依赖:内存数据库、ShippingTask / ShippingNoticeCandidate、query_service、document_candidate_persist。
|
|
逻辑:构造带 step_payload 与 source_candidate 的任务,断言 DETAIL 顶层字段与 Candidate brief 暴露证据。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.models import ShippingNoticeCandidate, ShippingTask
|
|
from app.services.shipping_notice.document_candidate_persist import _serialize_candidate_brief
|
|
from app.services.shipping_notice.query_service import serialize_task_detail
|
|
|
|
|
|
def test_serialize_task_detail_projects_parse_gates_and_source_candidate(app):
|
|
"""Task Detail 必须稳定顶层返回 lines 门禁与来源 Candidate 证据投影。"""
|
|
with app.app_context():
|
|
candidate = ShippingNoticeCandidate(
|
|
candidate_no='SNC-PARSED-PROJECTION-001',
|
|
intake_id=1,
|
|
primary_document_id=1,
|
|
candidate_key='parsed-projection-1',
|
|
missing_fields_json=['hub_code'],
|
|
field_confidence_json={'sku': 0.95, 'qty': 0.9},
|
|
evidence_json=[{
|
|
'field': 'lines[0].reservation_id',
|
|
'line_index': 0,
|
|
'reservation_id': '1000140351',
|
|
'quote': '1000140351',
|
|
'document_id': 1,
|
|
}],
|
|
conflicts_json=[],
|
|
)
|
|
db.session.add(candidate)
|
|
db.session.flush()
|
|
|
|
task = ShippingTask(
|
|
task_no='SN-PARSED-PROJECTION-001',
|
|
source_type='manual',
|
|
process_status='pending_review',
|
|
po_number='4510038672',
|
|
source_candidate_id=int(candidate.id),
|
|
lines_json=[{
|
|
'sku': 'P123456',
|
|
'qty': 24,
|
|
'reservation_id': '1000140351',
|
|
'source_type': 'DBS DUB',
|
|
'hub_code': 'DUB1',
|
|
}],
|
|
step_payload_json={
|
|
'parse_validation': {
|
|
'status': 'ready',
|
|
'issues': [],
|
|
'contract_version': 'shipping-line-v1',
|
|
},
|
|
'process_field_gate': {
|
|
'status': 'ready',
|
|
'missing_fields': [],
|
|
'issues': [],
|
|
},
|
|
},
|
|
)
|
|
db.session.add(task)
|
|
db.session.commit()
|
|
|
|
detail = serialize_task_detail(task)
|
|
assert detail['lines'][0]['reservation_id'] == '1000140351'
|
|
assert detail['lines'][0]['hub_code'] == 'DUB1'
|
|
assert detail['parse_validation']['status'] == 'ready'
|
|
assert detail['process_field_gate']['status'] == 'ready'
|
|
assert detail['source_candidate']['candidate_no'] == 'SNC-PARSED-PROJECTION-001'
|
|
assert detail['source_candidate']['missing_fields'] == ['hub_code']
|
|
assert detail['source_candidate']['evidence'][0]['reservation_id'] == '1000140351'
|
|
|
|
|
|
def test_serialize_candidate_brief_exposes_evidence_and_missing_fields(app):
|
|
"""Candidate brief 必须暴露已落库的 evidence / missing_fields / field_confidence。"""
|
|
with app.app_context():
|
|
candidate = ShippingNoticeCandidate(
|
|
candidate_no='SNC-BRIEF-EVIDENCE-001',
|
|
intake_id=1,
|
|
primary_document_id=1,
|
|
candidate_key='brief-evidence-1',
|
|
missing_fields_json=['reservation_id'],
|
|
field_confidence_json={'sku': 0.88},
|
|
evidence_json=[{'field': 'lines[0].sku', 'quote': 'SKU-A', 'document_id': 1}],
|
|
conflicts_json=[{'field': 'reservation_id', 'reason': 'source_conflict'}],
|
|
structured_result_json={'business_summary': {'po_number': 'PO-1', 'lines': []}},
|
|
)
|
|
brief = _serialize_candidate_brief(candidate)
|
|
assert brief['missing_fields'] == ['reservation_id']
|
|
assert brief['field_confidence']['sku'] == 0.88
|
|
assert brief['evidence'][0]['quote'] == 'SKU-A'
|
|
assert brief['conflicts'][0]['reason'] == 'source_conflict'
|
|
|
|
|
|
def test_normalize_notice_line_keeps_hub_code_independent_of_source_type():
|
|
"""Hub Code 不得折叠进 source_type;两者应同时保留。"""
|
|
from app.services.shipping_notice.candidate_validation import normalize_candidate_business_summary
|
|
from app.services.shipping_notice.notice_lines import normalize_notice_line
|
|
|
|
line = normalize_notice_line({
|
|
'sku': 'P123456',
|
|
'qty': 24,
|
|
'reservation_id': '1000140351',
|
|
'source_type': 'DBS DUB',
|
|
'hub_code': 'DUB1',
|
|
'Hub Code': 'SHOULD_NOT_OVERRIDE_EXPLICIT',
|
|
})
|
|
assert line is not None
|
|
assert line['source_type'] == 'DBS DUB'
|
|
assert line['hub_code'] == 'DUB1'
|
|
|
|
summary = normalize_candidate_business_summary({
|
|
'po_number': '4510038672',
|
|
'lines': [{
|
|
'sku': 'P123456',
|
|
'qty': 24,
|
|
'reservation_id': '1000140351',
|
|
'source_type': 'VMI-FRA01',
|
|
'hub_code': 'FRA01',
|
|
}],
|
|
})
|
|
assert summary['lines'][0]['source_type'] == 'VMI-FRA01'
|
|
assert summary['lines'][0]['hub_code'] == 'FRA01'
|
|
|
|
|
|
def test_candidate_update_persists_hub_code_round_trip(app):
|
|
"""Candidate 人工修订保存后,hub_code 必须仍在 business_summary 中。"""
|
|
from app.models import Agent, ChatSession, FileAsset, ShippingNoticeDocument, ShippingNoticeIntake, User
|
|
from app.services.shipping_notice.candidate_service import update_candidate
|
|
|
|
with app.app_context():
|
|
user = User(username='hub-code-user', email='hub-code@example.com', password_hash='x')
|
|
agent = Agent(
|
|
name='Shipping Notice',
|
|
agent_code='shipping_notice_hub_code',
|
|
dify_app_id='shipping-hub-code-test',
|
|
dify_api_key='test-key',
|
|
)
|
|
db.session.add_all([user, agent])
|
|
db.session.flush()
|
|
session = ChatSession(
|
|
session_id='sess-hub-code',
|
|
user_id=int(user.id),
|
|
agent_id=int(agent.id),
|
|
title='Hub code',
|
|
)
|
|
asset = FileAsset(
|
|
asset_no='FA-HUB-CODE-001',
|
|
owner_user_id=int(user.id),
|
|
original_filename='order.pdf',
|
|
storage_provider='minio',
|
|
storage_bucket='hxops-file-assets',
|
|
storage_key=f'temporary/{user.id}/hub-code.pdf',
|
|
content_hash='hash-hub-code',
|
|
file_size=12,
|
|
mime_type='application/pdf',
|
|
upload_status='ready',
|
|
lifecycle_status='temporary',
|
|
)
|
|
db.session.add_all([session, asset])
|
|
db.session.flush()
|
|
intake = ShippingNoticeIntake(
|
|
intake_no='SI-HUB-CODE-001',
|
|
asset_id=int(asset.id),
|
|
user_id=int(user.id),
|
|
purpose='shipping_notice',
|
|
parse_status='parsed',
|
|
parser_version='v6',
|
|
order_extraction_status='not_started',
|
|
)
|
|
db.session.add(intake)
|
|
db.session.flush()
|
|
document = ShippingNoticeDocument(
|
|
intake_id=int(intake.id),
|
|
asset_id=int(asset.id),
|
|
document_type='attachment',
|
|
attachment_key='order.pdf',
|
|
filename='order.pdf',
|
|
content_hash='hash-hub-code-doc',
|
|
classification='order_notice',
|
|
parse_status='parsed',
|
|
extracted_text='SKU P123456 qty 24',
|
|
)
|
|
db.session.add(document)
|
|
db.session.flush()
|
|
candidate = ShippingNoticeCandidate(
|
|
candidate_no='SNC-HUB-CODE-001',
|
|
intake_id=int(intake.id),
|
|
primary_document_id=int(document.id),
|
|
candidate_key='hub-code-1',
|
|
validation_status='needs_review',
|
|
selected=True,
|
|
structured_result_json={
|
|
'business_summary': {
|
|
'po_number': '4510038672',
|
|
'lines': [{'sku': 'P123456', 'qty': 24, 'uom': 'EA'}],
|
|
},
|
|
},
|
|
)
|
|
db.session.add(candidate)
|
|
db.session.commit()
|
|
|
|
updated = update_candidate(
|
|
user_id=int(user.id),
|
|
candidate_no='SNC-HUB-CODE-001',
|
|
business_summary={
|
|
'po_number': '4510038672',
|
|
'lines': [{
|
|
'sku': 'P123456',
|
|
'qty': 24,
|
|
'reservation_id': '1000140351',
|
|
'source_type': 'DBS DUB',
|
|
'hub_code': 'DUB1',
|
|
'uom': 'EA',
|
|
}],
|
|
},
|
|
)
|
|
line = updated['business_summary']['lines'][0]
|
|
assert line['source_type'] == 'DBS DUB'
|
|
assert line['hub_code'] == 'DUB1'
|
|
db.session.refresh(candidate)
|
|
persisted = candidate.structured_result_json['business_summary']['lines'][0]
|
|
assert persisted['hub_code'] == 'DUB1'
|
|
assert persisted['source_type'] == 'DBS DUB'
|