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
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
backend/tests/test_shipping_notice_business_dedup.py
|
||||
|
||||
用途:第三阶段业务指纹、重复检测与决策门闩核心用例。
|
||||
依赖:pytest、business_fingerprint、duplicate_detection、batch_from_intakes。
|
||||
逻辑:覆盖指纹稳定性、阻断门闩、open_existing / create_revision 分流。
|
||||
用途:业务指纹与 Intake/Candidate 重复检测核心用例(co8 已移除建批门闩用例)。
|
||||
依赖:pytest、business_fingerprint、duplicate_detection、intake_service。
|
||||
逻辑:覆盖指纹稳定性、候选阻断与 refresh。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -17,14 +17,12 @@ from app import db
|
||||
from app.core.errors import ConflictError
|
||||
from app.models import (
|
||||
FileAsset,
|
||||
ShippingBatch,
|
||||
ShippingNoticeCandidate,
|
||||
ShippingNoticeDocument,
|
||||
ShippingNoticeIntake,
|
||||
ShippingTask,
|
||||
ShippingOrderCandidate,
|
||||
ShippingOrderDocument,
|
||||
ShippingOrderIntake,
|
||||
ShippingNotice,
|
||||
User,
|
||||
)
|
||||
from app.services.shipping_notice.batch_from_intakes import create_shipping_batch_from_intakes
|
||||
from app.services.shipping_notice.business_fingerprint import (
|
||||
build_business_fingerprint,
|
||||
build_content_fingerprint,
|
||||
@@ -70,7 +68,7 @@ def _seed_parsed_intake(
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no=intake_no,
|
||||
asset_id=asset.id,
|
||||
user_id=user_id,
|
||||
@@ -144,237 +142,7 @@ def test_fingerprint_changes_with_qty():
|
||||
assert build_business_fingerprint(base)['fingerprint'] != build_business_fingerprint(changed)['fingerprint']
|
||||
|
||||
|
||||
def test_blocking_create_requires_resolution(app, dedup_user):
|
||||
with app.app_context():
|
||||
summary = {
|
||||
'customer_code': 'C1',
|
||||
'po_number': 'PO-9',
|
||||
'reservation_ids': ['1000140351'],
|
||||
'lines': [],
|
||||
}
|
||||
fp = build_business_fingerprint(summary)
|
||||
asset, intake = _seed_parsed_intake(
|
||||
dedup_user.id,
|
||||
intake_no='SNI-DEDUP-BLOCK',
|
||||
hash_value='hash-block-1',
|
||||
business_summary=summary,
|
||||
fingerprint=fp,
|
||||
)
|
||||
batch = ShippingBatch(batch_no='SB-DEDUP-OLD', user_id=dedup_user.id, file_count=1)
|
||||
db.session.add(batch)
|
||||
db.session.flush()
|
||||
existing = ShippingTask(
|
||||
task_no='SN-DEDUP-OLD',
|
||||
batch_id=batch.id,
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
process_status='waiting_inventory',
|
||||
business_fingerprint=fp['fingerprint'],
|
||||
fingerprint_version=fp['version'],
|
||||
document_version=1,
|
||||
)
|
||||
db.session.add(existing)
|
||||
db.session.commit()
|
||||
|
||||
result = detect_duplicates_for_intake(intake=intake, asset=asset, business_summary=summary, fingerprint_result=fp)
|
||||
assert result['blocking'] is True
|
||||
assert result['status'] == 'same_active_task'
|
||||
|
||||
with pytest.raises(ConflictError) as excinfo:
|
||||
create_shipping_batch_from_intakes(
|
||||
user_id=dedup_user.id,
|
||||
conversation_id='conv-dedup',
|
||||
intake_nos=[intake.intake_no],
|
||||
create_idempotency_key='idem-block',
|
||||
)
|
||||
assert excinfo.value.code == 'SHIPPING_NOTICE.DUPLICATE_RESOLUTION_REQUIRED'
|
||||
|
||||
|
||||
def test_open_existing_does_not_create_batch(app, dedup_user):
|
||||
with app.app_context():
|
||||
summary = {
|
||||
'customer_code': 'C1',
|
||||
'po_number': 'PO-OPEN',
|
||||
'reservation_ids': ['1000140352'],
|
||||
'lines': [],
|
||||
}
|
||||
fp = build_business_fingerprint(summary)
|
||||
asset, intake = _seed_parsed_intake(
|
||||
dedup_user.id,
|
||||
intake_no='SNI-DEDUP-OPEN',
|
||||
hash_value='hash-open-1',
|
||||
business_summary=summary,
|
||||
fingerprint=fp,
|
||||
)
|
||||
batch = ShippingBatch(batch_no='SB-OPEN-OLD', user_id=dedup_user.id, file_count=1)
|
||||
db.session.add(batch)
|
||||
db.session.flush()
|
||||
existing = ShippingTask(
|
||||
task_no='SN-OPEN-OLD',
|
||||
batch_id=batch.id,
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
process_status='waiting_inventory',
|
||||
business_fingerprint=fp['fingerprint'],
|
||||
fingerprint_version=fp['version'],
|
||||
document_version=1,
|
||||
)
|
||||
db.session.add(existing)
|
||||
db.session.commit()
|
||||
|
||||
# 模拟前端已决策:写入 resolution,避免 resolve 内 refresh 对 SQLite 时间敏感
|
||||
intake.duplicate_status = 'same_active_task'
|
||||
intake.duplicate_matches_json = [
|
||||
{
|
||||
'task_no': existing.task_no,
|
||||
'process_status': existing.process_status,
|
||||
'document_version': 1,
|
||||
'match_type': 'business_fingerprint',
|
||||
'matched_fields': ['customer_code', 'po_number'],
|
||||
'lifecycle': 'active',
|
||||
}
|
||||
]
|
||||
intake.duplicate_resolution = 'open_existing'
|
||||
intake.duplicate_target_task_no = existing.task_no
|
||||
intake.duplicate_resolved_by_id = dedup_user.id
|
||||
intake.duplicate_resolved_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
with patch(
|
||||
'app.services.shipping_notice.batch_from_intakes.create_shipping_batch_from_notice_items'
|
||||
) as create_mock:
|
||||
result = create_shipping_batch_from_intakes(
|
||||
user_id=dedup_user.id,
|
||||
conversation_id='conv-open',
|
||||
intake_nos=[intake.intake_no],
|
||||
create_idempotency_key='idem-open',
|
||||
)
|
||||
create_mock.assert_not_called()
|
||||
|
||||
assert result.get('batch_no') in (None, '')
|
||||
refreshed = db.session.get(ShippingNoticeIntake, intake.id)
|
||||
assert refreshed.parse_status == 'consumed'
|
||||
assert refreshed.consumed_task_id == existing.id
|
||||
|
||||
|
||||
def test_create_revision_sets_supersedes_and_version(app, dedup_user, monkeypatch):
|
||||
"""create_revision 应写入 supersedes_task_id 与 document_version+1。"""
|
||||
with app.app_context():
|
||||
summary = {
|
||||
'customer_code': 'C1',
|
||||
'po_number': 'PO-REV-1',
|
||||
'reservation_ids': ['12345678'],
|
||||
'lines': [{'part_number': 'A', 'qty': 1, 'reservation_id': '12345678'}],
|
||||
}
|
||||
fp = build_business_fingerprint(summary)
|
||||
asset, intake = _seed_parsed_intake(
|
||||
dedup_user.id,
|
||||
intake_no='SNI-REV-1',
|
||||
hash_value='hash-rev-1',
|
||||
business_summary=summary,
|
||||
fingerprint=fp,
|
||||
)
|
||||
existing = ShippingTask(
|
||||
task_no='SN-REV-BASE',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
process_status='uploaded',
|
||||
business_fingerprint=fp.get('fingerprint'),
|
||||
fingerprint_version=fp.get('version'),
|
||||
document_version=1,
|
||||
)
|
||||
db.session.add(existing)
|
||||
db.session.commit()
|
||||
|
||||
intake.duplicate_status = 'same_completed_task'
|
||||
intake.duplicate_matches_json = [
|
||||
{
|
||||
'task_no': existing.task_no,
|
||||
'process_status': 'uploaded',
|
||||
'document_version': 1,
|
||||
'match_type': 'business_fingerprint',
|
||||
'matched_fields': ['customer_code', 'po_number'],
|
||||
}
|
||||
]
|
||||
intake.duplicate_resolution = 'create_revision'
|
||||
intake.duplicate_target_task_no = existing.task_no
|
||||
intake.duplicate_resolved_by_id = dedup_user.id
|
||||
intake.duplicate_resolved_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
def _fake_create(**kwargs):
|
||||
notice_items = kwargs['notice_items']
|
||||
item = notice_items[0]
|
||||
batch = ShippingBatch(
|
||||
batch_no='SB-REV-1',
|
||||
user_id=dedup_user.id,
|
||||
file_count=1,
|
||||
create_idempotency_key=kwargs.get('create_idempotency_key'),
|
||||
)
|
||||
db.session.add(batch)
|
||||
db.session.flush()
|
||||
task = ShippingTask(
|
||||
task_no='SN-REV-NEW',
|
||||
batch_id=batch.id,
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
source_content_hash=item.get('source_content_hash'),
|
||||
source_asset_id=item.get('source_asset_id'),
|
||||
source_intake_id=item.get('source_intake_id'),
|
||||
business_fingerprint=item.get('business_fingerprint'),
|
||||
fingerprint_version=item.get('fingerprint_version'),
|
||||
document_version=int(item.get('document_version') or 1),
|
||||
supersedes_task_id=item.get('supersedes_task_id'),
|
||||
duplicate_resolution_type=item.get('duplicate_resolution_type'),
|
||||
raw_content=item.get('raw_text'),
|
||||
process_status='created',
|
||||
)
|
||||
db.session.add(task)
|
||||
db.session.commit()
|
||||
return {
|
||||
'status': 'ok',
|
||||
'batch_no': batch.batch_no,
|
||||
'data': {
|
||||
'batch': {'batch_no': batch.batch_no, 'id': batch.id},
|
||||
'tasks': [{'task_id': task.task_no}],
|
||||
'focus_task': {
|
||||
'task_no': task.task_no,
|
||||
'document_version': task.document_version,
|
||||
'duplicate_resolution_type': task.duplicate_resolution_type,
|
||||
'supersedes_task_no': existing.task_no,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.batch_from_intakes.create_shipping_batch_from_notice_items',
|
||||
_fake_create,
|
||||
)
|
||||
# 确保 detect 仍命中
|
||||
monkeypatch.setattr(
|
||||
'app.services.shipping_notice.batch_from_intakes.detect_duplicates_for_intake',
|
||||
lambda **kwargs: {
|
||||
'status': 'same_completed_task',
|
||||
'blocking': True,
|
||||
'fingerprint': fp.get('fingerprint'),
|
||||
'fingerprint_version': fp.get('version'),
|
||||
'fingerprint_strength': fp.get('strength'),
|
||||
'matches': intake.duplicate_matches_json,
|
||||
'allowed_actions': ['open_existing', 'create_revision', 'force_create'],
|
||||
},
|
||||
)
|
||||
|
||||
result = create_shipping_batch_from_intakes(
|
||||
user_id=dedup_user.id,
|
||||
conversation_id='conv-rev',
|
||||
intake_nos=[intake.intake_no],
|
||||
create_idempotency_key='idem-rev',
|
||||
)
|
||||
assert result['batch_no'] == 'SB-REV-1'
|
||||
created = db.session.query(ShippingTask).filter_by(task_no='SN-REV-NEW').one()
|
||||
assert created.supersedes_task_id == existing.id
|
||||
assert int(created.document_version) == 2
|
||||
assert created.duplicate_resolution_type == 'revision'
|
||||
|
||||
|
||||
def test_fingerprint_strong_with_customer_name_po_reservation():
|
||||
@@ -417,7 +185,7 @@ def test_pending_review_hit_is_same_active_blocking(app, dedup_user):
|
||||
business_summary=summary,
|
||||
fingerprint=fp,
|
||||
)
|
||||
existing = ShippingTask(
|
||||
existing = ShippingNotice(
|
||||
task_no='SN-PENDING-1',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
@@ -439,53 +207,6 @@ def test_pending_review_hit_is_same_active_blocking(app, dedup_user):
|
||||
assert result['blocking'] is True
|
||||
|
||||
|
||||
def test_assert_no_exact_duplicate_allows_revision_and_force(app, dedup_user):
|
||||
"""revision/force_create 可越过同 content_hash 的旧门闩;无决议仍阻断。"""
|
||||
from app.services.shipping_notice.batch_create_idempotency import assert_no_exact_duplicate_notices
|
||||
|
||||
with app.app_context():
|
||||
existing = ShippingTask(
|
||||
task_no='SN-HASH-GATE',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
source_content_hash='samehashabc',
|
||||
process_status='pending_review',
|
||||
)
|
||||
db.session.add(existing)
|
||||
db.session.commit()
|
||||
|
||||
assert_no_exact_duplicate_notices(
|
||||
dedup_user.id,
|
||||
[
|
||||
{
|
||||
'source_content_hash': 'samehashabc',
|
||||
'source_file_name': 'same.xlsx',
|
||||
'duplicate_resolution_type': 'revision',
|
||||
}
|
||||
],
|
||||
)
|
||||
assert_no_exact_duplicate_notices(
|
||||
dedup_user.id,
|
||||
[
|
||||
{
|
||||
'source_content_hash': 'samehashabc',
|
||||
'source_file_name': 'same.xlsx',
|
||||
'duplicate_resolution_type': 'force_create',
|
||||
}
|
||||
],
|
||||
)
|
||||
with pytest.raises(ConflictError) as excinfo:
|
||||
assert_no_exact_duplicate_notices(
|
||||
dedup_user.id,
|
||||
[
|
||||
{
|
||||
'source_content_hash': 'samehashabc',
|
||||
'source_file_name': 'same.xlsx',
|
||||
}
|
||||
],
|
||||
)
|
||||
assert excinfo.value.code == 'SHIPPING.NOTICE_EXACT_DUPLICATE'
|
||||
|
||||
|
||||
def test_serialize_intake_possible_duplicate_is_blocking(app, dedup_user):
|
||||
"""API 投影:possible_duplicate 必须 blocking,供前端弹窗确认。"""
|
||||
@@ -517,46 +238,6 @@ def test_serialize_intake_possible_duplicate_is_blocking(app, dedup_user):
|
||||
assert 'force_create' in payload['duplicate']['allowed_actions']
|
||||
|
||||
|
||||
def test_task_summary_includes_version_chain(app, dedup_user):
|
||||
"""query_service SUMMARY 须暴露版本链与 duplicate 决策字段。"""
|
||||
from app.services.shipping_notice.query_service import TaskProjection, serialize_task
|
||||
|
||||
with app.app_context():
|
||||
base = ShippingTask(
|
||||
task_no='SN-VER-BASE',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
process_status='uploaded',
|
||||
document_version=1,
|
||||
)
|
||||
db.session.add(base)
|
||||
db.session.flush()
|
||||
rev = ShippingTask(
|
||||
task_no='SN-VER-REV',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
process_status='pending_review',
|
||||
document_version=2,
|
||||
supersedes_task_id=base.id,
|
||||
duplicate_resolution_type='revision',
|
||||
duplicate_source_task_id=base.id,
|
||||
duplicate_override_reason=None,
|
||||
business_fingerprint='fp-x',
|
||||
fingerprint_version='shipping-v1',
|
||||
)
|
||||
db.session.add(rev)
|
||||
db.session.commit()
|
||||
|
||||
data = serialize_task(rev, TaskProjection.SUMMARY)
|
||||
assert data['document_version'] == 2
|
||||
assert data['supersedes_task_no'] == 'SN-VER-BASE'
|
||||
assert data['duplicate_source_task_no'] == 'SN-VER-BASE'
|
||||
assert data['duplicate_resolution_type'] == 'revision'
|
||||
assert data['business_fingerprint'] == 'fp-x'
|
||||
|
||||
base_data = serialize_task(base, TaskProjection.SUMMARY)
|
||||
assert base_data['superseded_by_task_no'] == 'SN-VER-REV'
|
||||
|
||||
|
||||
def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||||
"""
|
||||
@@ -571,7 +252,7 @@ def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||||
'lines': [],
|
||||
}
|
||||
)
|
||||
existing = ShippingTask(
|
||||
existing = ShippingNotice(
|
||||
task_no='SN-EXISTING-PO-A',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
@@ -598,7 +279,7 @@ def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||||
)
|
||||
db.session.add(parent)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no='SNI-STAGE2-DEDUP',
|
||||
asset_id=parent.id,
|
||||
user_id=dedup_user.id,
|
||||
@@ -613,7 +294,7 @@ def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||||
|
||||
docs = []
|
||||
for idx, po in enumerate(('4510039227', '4510039228', '4510039229'), start=1):
|
||||
doc = ShippingNoticeDocument(
|
||||
doc = ShippingOrderDocument(
|
||||
intake_id=intake.id,
|
||||
attachment_key=f'att-00{idx}',
|
||||
document_type='attachment',
|
||||
@@ -644,7 +325,7 @@ def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||||
business_summary=summary,
|
||||
fingerprint_result=fp,
|
||||
)
|
||||
cand = ShippingNoticeCandidate(
|
||||
cand = ShippingOrderCandidate(
|
||||
candidate_no=f'SNC-{po}',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=doc.id,
|
||||
@@ -671,9 +352,9 @@ def test_candidate_duplicate_blocks_only_matching_candidate(app, dedup_user):
|
||||
assert results[2]['status'] == 'none'
|
||||
|
||||
refreshed = (
|
||||
db.session.query(ShippingNoticeCandidate)
|
||||
db.session.query(ShippingOrderCandidate)
|
||||
.filter_by(intake_id=intake.id)
|
||||
.order_by(ShippingNoticeCandidate.id.asc())
|
||||
.order_by(ShippingOrderCandidate.id.asc())
|
||||
.all()
|
||||
)
|
||||
assert refreshed[0].duplicate_status != 'none'
|
||||
@@ -694,7 +375,7 @@ def test_refresh_duplicate_updates_candidate_statuses(app, dedup_user):
|
||||
'lines': [],
|
||||
}
|
||||
)
|
||||
existing = ShippingTask(
|
||||
existing = ShippingNotice(
|
||||
task_no='SN-REFRESH-EXISTING',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
@@ -721,7 +402,7 @@ def test_refresh_duplicate_updates_candidate_statuses(app, dedup_user):
|
||||
)
|
||||
db.session.add(parent)
|
||||
db.session.flush()
|
||||
intake = ShippingNoticeIntake(
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no='SNI-REFRESH-DEDUP',
|
||||
asset_id=parent.id,
|
||||
user_id=dedup_user.id,
|
||||
@@ -742,7 +423,7 @@ def test_refresh_duplicate_updates_candidate_statuses(app, dedup_user):
|
||||
'lines': [],
|
||||
}
|
||||
fp = build_business_fingerprint(summary)
|
||||
doc = ShippingNoticeDocument(
|
||||
doc = ShippingOrderDocument(
|
||||
intake_id=intake.id,
|
||||
attachment_key='att-001',
|
||||
document_type='attachment',
|
||||
@@ -754,7 +435,7 @@ def test_refresh_duplicate_updates_candidate_statuses(app, dedup_user):
|
||||
)
|
||||
db.session.add(doc)
|
||||
db.session.flush()
|
||||
cand = ShippingNoticeCandidate(
|
||||
cand = ShippingOrderCandidate(
|
||||
candidate_no='SNC-REFRESH-1',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=doc.id,
|
||||
@@ -779,7 +460,7 @@ def test_refresh_duplicate_updates_candidate_statuses(app, dedup_user):
|
||||
)
|
||||
assert payload['duplicate']['blocking'] is True
|
||||
db.session.expire_all()
|
||||
cand2 = db.session.query(ShippingNoticeCandidate).filter_by(candidate_no='SNC-REFRESH-1').one()
|
||||
cand2 = db.session.query(ShippingOrderCandidate).filter_by(candidate_no='SNC-REFRESH-1').one()
|
||||
assert cand2.duplicate_status != 'none'
|
||||
assert list(cand2.duplicate_matches_json or [])
|
||||
|
||||
@@ -802,7 +483,7 @@ def test_candidate_content_fingerprint_distinguishes_same_content_and_revision(a
|
||||
}
|
||||
identity = build_business_fingerprint(base)
|
||||
content = build_content_fingerprint(base)
|
||||
existing = ShippingTask(
|
||||
existing = ShippingNotice(
|
||||
task_no='SN-CONTENT-EXISTING',
|
||||
user_id=dedup_user.id,
|
||||
source_type='manual',
|
||||
@@ -820,7 +501,7 @@ def test_candidate_content_fingerprint_distinguishes_same_content_and_revision(a
|
||||
business_summary=base,
|
||||
fingerprint=identity,
|
||||
)
|
||||
doc = ShippingNoticeDocument(
|
||||
doc = ShippingOrderDocument(
|
||||
intake_id=intake.id,
|
||||
attachment_key='att-content',
|
||||
document_type='attachment',
|
||||
@@ -849,7 +530,7 @@ def test_candidate_content_fingerprint_distinguishes_same_content_and_revision(a
|
||||
)
|
||||
assert same['status'] == 'same_business_content'
|
||||
assert revision['status'] == 'possible_revision'
|
||||
candidate = ShippingNoticeCandidate(
|
||||
candidate = ShippingOrderCandidate(
|
||||
candidate_no='SNC-CONTENT-WRITEBACK',
|
||||
intake_id=intake.id,
|
||||
primary_document_id=doc.id,
|
||||
|
||||
Reference in new issue
Block a user