718 lines
28 KiB
Python
718 lines
28 KiB
Python
"""
|
||
backend/tests/test_shipping_processing_projection.py
|
||
|
||
用途:验收会话级 processing 投影分组、OT 去重与任务—PDF 两层标题规则。
|
||
依赖:Flask/SQLite、create_order_extraction_run、project_conversation_processing。
|
||
逻辑:构造 queued / frozen-failed / completed;断言多 PDF 不占用首文件名、hash 去重计数。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
|
||
import pytest
|
||
|
||
from app import db
|
||
from app.models import ShippingOrderCandidate, ShippingNotice
|
||
from app.services.shipping_notice.conversation_processing_projection import (
|
||
project_conversation_processing,
|
||
)
|
||
from app.services.shipping_notice.order_extraction_run_service import (
|
||
create_order_extraction_run,
|
||
)
|
||
from app.services.shipping_notice.order_task_service import (
|
||
allocate_user_task_no_for_run,
|
||
upsert_order_task_for_candidate_formation,
|
||
)
|
||
from tests.test_shipping_order_extraction_runs import _seed_run_input
|
||
|
||
|
||
def _ensure_run_order_task(*, run, intake, document) -> None:
|
||
"""
|
||
为投影测试物化最小 Candidate + OT,并分配 Run.user_task_no。
|
||
|
||
核心逻辑:对齐 test_shipping_conversation_processing_api 模式;
|
||
Candidate 前 Run 无持久化 OT 时不应出现 order_task 卡;
|
||
重识别继承 OT 时走受控换绑。
|
||
"""
|
||
candidate = ShippingOrderCandidate.query.filter_by(
|
||
order_extraction_run_id=int(run.id),
|
||
).first()
|
||
if candidate is None:
|
||
candidate = ShippingOrderCandidate(
|
||
candidate_no=f'SNC-PROJ-{run.id}',
|
||
intake_id=int(intake.id),
|
||
primary_document_id=int(document.id),
|
||
candidate_key=f'key-proj-{run.id}',
|
||
order_extraction_run_id=int(run.id),
|
||
is_current_version=True,
|
||
candidate_status='provisional',
|
||
structured_result_json={},
|
||
)
|
||
db.session.add(candidate)
|
||
db.session.flush()
|
||
allocate_user_task_no_for_run(run)
|
||
upsert_order_task_for_candidate_formation(
|
||
run,
|
||
intake_id=int(intake.id),
|
||
candidate_id=int(candidate.id),
|
||
source_type='direct_pdf',
|
||
stage='candidate_created',
|
||
)
|
||
db.session.commit()
|
||
|
||
|
||
def test_project_conversation_processing_groups_and_dedupes_user_tasks(app):
|
||
"""同会话多 Run 投影:按状态分组;同 OT 只保留最新。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-A')
|
||
run_processing, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=run_processing, intake=intake, document=document)
|
||
|
||
_, _, _, intake_fail, document_fail = _seed_run_input(
|
||
suffix='PROJ-B',
|
||
agent_code='shipping_notice_proj_b',
|
||
)
|
||
intake_fail.user_id = user.id
|
||
intake_fail.conversation_id = session.session_id
|
||
db.session.commit()
|
||
run_failed, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake_fail.intake_no],
|
||
allow_parallel_conversation_runs=True,
|
||
)
|
||
_ensure_run_order_task(run=run_failed, intake=intake_fail, document=document_fail)
|
||
run_failed.status = 'failed'
|
||
run_failed.phase = 'failed'
|
||
run_failed.input_lifecycle = 'frozen'
|
||
run_failed.input_frozen_at = datetime.utcnow()
|
||
db.session.commit()
|
||
|
||
_, _, _, intake_done, document_done = _seed_run_input(
|
||
suffix='PROJ-C',
|
||
agent_code='shipping_notice_proj_c',
|
||
)
|
||
intake_done.user_id = user.id
|
||
intake_done.conversation_id = session.session_id
|
||
db.session.commit()
|
||
run_done, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake_done.intake_no],
|
||
allow_parallel_conversation_runs=True,
|
||
)
|
||
_ensure_run_order_task(run=run_done, intake=intake_done, document=document_done)
|
||
run_done.status = 'completed'
|
||
run_done.phase = 'completed'
|
||
run_done.input_lifecycle = 'closed'
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
focused_task_no=run_failed.user_task_no,
|
||
)
|
||
|
||
assert projection['summary']['total_tasks'] == 3
|
||
assert projection['summary']['processing'] >= 1
|
||
assert projection['summary']['needs_action'] >= 1
|
||
assert projection['summary'].get('completed', 0) >= 1
|
||
assert projection['summary']['history'] == 0
|
||
assert projection['history'] == []
|
||
assert projection['focused_task_no'] == run_failed.user_task_no
|
||
|
||
processing_nos = {row['user_task_no'] for row in projection['processing']}
|
||
needs_nos = {row['user_task_no'] for row in projection['needs_action']}
|
||
completed_nos = {row['user_task_no'] for row in projection.get('completed') or []}
|
||
assert run_processing.user_task_no in processing_nos
|
||
assert run_failed.user_task_no in needs_nos
|
||
# 未查看的 completed 作为成功通知展示在 completed 分组。
|
||
assert run_done.user_task_no in completed_nos
|
||
|
||
# 关键断言:每个活跃 OT 只出现一次。
|
||
all_nos = list(processing_nos | needs_nos | completed_nos)
|
||
assert len(all_nos) == len(set(all_nos)) == 3
|
||
|
||
failed_task = next(
|
||
row for row in projection['needs_action']
|
||
if row['user_task_no'] == run_failed.user_task_no
|
||
)
|
||
assert failed_task.get('status_label') == 'failed'
|
||
assert 'view_details' in (failed_task.get('available_actions') or [])
|
||
assert failed_task.get('primary_action')
|
||
|
||
done_task = next(
|
||
row for row in projection['completed']
|
||
if row['user_task_no'] == run_done.user_task_no
|
||
)
|
||
assert done_task.get('status_label') == 'completed'
|
||
assert done_task.get('group') == 'completed'
|
||
|
||
|
||
def test_fingerprint_merge_prefers_newer_processing_over_older_failed(app):
|
||
"""BE-02:同指纹旧失败 + 新处理中,必须展示新 Run。"""
|
||
with app.app_context():
|
||
from app.models import FileAsset, ShippingOrderDocument, ShippingOrderIntake
|
||
from datetime import timedelta
|
||
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-MERGE')
|
||
older, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
older.status = 'failed'
|
||
older.phase = 'failed'
|
||
older.input_lifecycle = 'frozen'
|
||
older.input_frozen_at = datetime.utcnow()
|
||
older.user_task_no = 'OT-OLD-FAIL'
|
||
db.session.commit()
|
||
|
||
second_asset = FileAsset(
|
||
asset_no='FA-PROJ-MERGE-2', owner_user_id=user.id,
|
||
original_filename=str(document.filename or 'merge.pdf'),
|
||
storage_provider='minio', storage_bucket='test',
|
||
storage_key=f'temporary/{user.id}/merge-2.pdf',
|
||
content_hash=db.session.get(FileAsset, intake.asset_id).content_hash,
|
||
file_size=100, mime_type='application/pdf', upload_status='ready',
|
||
lifecycle_status='temporary',
|
||
)
|
||
db.session.add(second_asset)
|
||
db.session.flush()
|
||
second_intake = ShippingOrderIntake(
|
||
intake_no='SNI-PROJ-MERGE-2', asset_id=second_asset.id, user_id=user.id,
|
||
conversation_id=session.session_id, upload_batch_id='proj-merge-2',
|
||
purpose='shipping_notice', parse_status='parsed', parser_version='v6',
|
||
extracted_text='same merge attachment',
|
||
expires_at=datetime.utcnow() + timedelta(hours=1),
|
||
)
|
||
db.session.add(second_intake)
|
||
db.session.flush()
|
||
db.session.add(ShippingOrderDocument(
|
||
intake_id=second_intake.id, asset_id=second_asset.id, document_type='attachment',
|
||
attachment_key=str(document.attachment_key or 'merge.pdf'),
|
||
filename=str(document.filename or 'merge.pdf'),
|
||
content_hash=document.content_hash, classification='order_notice',
|
||
parse_status='parsed', extracted_text=document.extracted_text,
|
||
))
|
||
db.session.commit()
|
||
|
||
newer, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[second_intake.intake_no],
|
||
upload_batch_id=second_intake.upload_batch_id,
|
||
keep_input_open=True,
|
||
allow_parallel_conversation_runs=True,
|
||
user_task_no='OT-NEW-PROC',
|
||
)
|
||
newer.status = 'processing'
|
||
newer.phase = 'extracting'
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
shown = projection['processing'] + projection['needs_action']
|
||
assert len(shown) == 1
|
||
assert shown[0]['user_task_no'] == newer.user_task_no
|
||
assert shown[0]['run_no'] == newer.run_no
|
||
|
||
|
||
def test_completed_current_run_hidden_after_viewed(app):
|
||
"""UI-04:当前 completed 查看后从右侧收起。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-DONE')
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
)
|
||
_ensure_run_order_task(run=run, intake=intake, document=document)
|
||
run.status = 'completed'
|
||
run.phase = 'completed'
|
||
run.input_lifecycle = 'closed'
|
||
db.session.commit()
|
||
|
||
before = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
assert any(row['user_task_no'] == run.user_task_no for row in (before.get('completed') or []))
|
||
|
||
run.seen_at = datetime.utcnow()
|
||
db.session.commit()
|
||
after = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
shown = after['processing'] + after['needs_action'] + list(after.get('completed') or [])
|
||
assert all(row['user_task_no'] != run.user_task_no for row in shown)
|
||
|
||
|
||
def _set_run_documents(run, documents: list[dict]) -> None:
|
||
"""覆盖 Run 快照 documents,便于断言任务—PDF 两层投影。"""
|
||
snapshot = dict(run.input_snapshot_json or {})
|
||
intakes = list(snapshot.get('intakes') or [])
|
||
if not intakes:
|
||
intakes = [{'intake_no': 'SNI-TEST', 'documents': []}]
|
||
intakes[0] = dict(intakes[0])
|
||
intakes[0]['documents'] = documents
|
||
snapshot['intakes'] = intakes
|
||
run.input_snapshot_json = snapshot
|
||
run.request_snapshot_json = snapshot
|
||
|
||
|
||
def test_single_pdf_task_uses_filename_title(app):
|
||
"""单 PDF 任务:title 为文件名,file_count=1,display_kind=single。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-1PDF')
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=run, intake=intake, document=document)
|
||
_set_run_documents(run, [{
|
||
'document_id': int(document.id),
|
||
'root_document_id': int(document.id),
|
||
'document_type': 'attachment',
|
||
'filename': 'only.pdf',
|
||
'content_hash': 'aaa111',
|
||
}])
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == run.user_task_no
|
||
)
|
||
assert task['title'] == 'only.pdf'
|
||
assert task['file_count'] == 1
|
||
assert task['display_kind'] == 'single_document_task'
|
||
doc_objects = [obj for obj in (task.get('objects') or []) if obj.get('kind') == 'document']
|
||
assert len(doc_objects) == 1
|
||
assert doc_objects[0]['filename'] == 'only.pdf'
|
||
|
||
|
||
def test_multi_pdf_task_does_not_use_first_filename_as_title(app):
|
||
"""多 PDF 任务:title 为空,file_count=2,objects 含两个当前 PDF。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-2PDF')
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=run, intake=intake, document=document)
|
||
_set_run_documents(run, [
|
||
{
|
||
'document_id': 91,
|
||
'root_document_id': 91,
|
||
'document_type': 'attachment',
|
||
'filename': '4518838152.pdf',
|
||
'content_hash': 'hash-a',
|
||
},
|
||
{
|
||
'document_id': 92,
|
||
'root_document_id': 92,
|
||
'document_type': 'attachment',
|
||
'filename': '4518838154.pdf',
|
||
'content_hash': 'hash-b',
|
||
},
|
||
])
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == run.user_task_no
|
||
)
|
||
assert task['title'] is None
|
||
assert task['display_kind'] == 'multi_document_task'
|
||
assert task['file_count'] == 2
|
||
assert task['document_names'] == ['4518838152.pdf', '4518838154.pdf']
|
||
doc_objects = [obj for obj in (task.get('objects') or []) if obj.get('kind') == 'document']
|
||
assert len(doc_objects) == 2
|
||
assert {obj['document_id'] for obj in doc_objects} == {91, 92}
|
||
|
||
|
||
def test_partial_reparse_keeps_unaffected_pdf_from_source_run(app):
|
||
"""同 OT 局部重识别:新版 PDF 替换自身,旧 Run 中未重识别 PDF 继续有效。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-PARTIAL-REPARSE')
|
||
source, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=source, intake=intake, document=document)
|
||
_set_run_documents(source, [
|
||
{
|
||
'document_id': 91,
|
||
'root_document_id': 91,
|
||
'document_type': 'attachment',
|
||
'filename': '4518838152.pdf',
|
||
'content_hash': 'hash-a',
|
||
},
|
||
{
|
||
'document_id': 92,
|
||
'root_document_id': 92,
|
||
'document_type': 'attachment',
|
||
'filename': '4518838154.pdf',
|
||
'content_hash': 'hash-b',
|
||
},
|
||
])
|
||
source.status = 'review_required'
|
||
source.phase = 'review_required'
|
||
source.input_lifecycle = 'frozen'
|
||
source.input_frozen_at = datetime.utcnow()
|
||
db.session.commit()
|
||
|
||
_, _, _, replacement_intake, replacement_document = _seed_run_input(
|
||
suffix='PROJ-PARTIAL-REPARSE-NEW',
|
||
agent_code='shipping_notice_proj_partial_reparse_new',
|
||
)
|
||
replacement_intake.user_id = user.id
|
||
replacement_intake.conversation_id = session.session_id
|
||
db.session.commit()
|
||
replacement, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[replacement_intake.intake_no],
|
||
allow_parallel_conversation_runs=True,
|
||
user_task_no=source.user_task_no,
|
||
)
|
||
replacement.reparse_of_run_id = source.id
|
||
replacement.reparse_reason = 'field_mapping_error'
|
||
# 投影用例无 Case 标记:用同 content_hash 满足严格文档谱系校验。
|
||
shared_hash = str(document.content_hash or 'hash-partial-shared')
|
||
document.content_hash = shared_hash
|
||
replacement_document.content_hash = shared_hash
|
||
db.session.flush()
|
||
_ensure_run_order_task(
|
||
run=replacement,
|
||
intake=replacement_intake,
|
||
document=replacement_document,
|
||
)
|
||
_set_run_documents(replacement, [{
|
||
'document_id': 191,
|
||
'root_document_id': 91,
|
||
'document_type': 'attachment',
|
||
'filename': '4518838152.pdf',
|
||
'content_hash': 'hash-a',
|
||
}])
|
||
source.superseded_by_run_id = replacement.id
|
||
source.superseded_at = datetime.utcnow()
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == source.user_task_no
|
||
)
|
||
doc_objects = [obj for obj in task['objects'] if obj.get('kind') == 'document']
|
||
|
||
# 关键断言:任务仍是两份 PDF;每份 PDF 保留其当前生效版本所属 Run。
|
||
assert task['file_count'] == 2
|
||
assert task['title'] is None
|
||
assert task['display_kind'] == 'multi_document_task'
|
||
assert set(task['document_names']) == {'4518838152.pdf', '4518838154.pdf'}
|
||
by_hash = {obj['content_hash']: obj for obj in doc_objects}
|
||
assert by_hash['hash-a']['document_id'] == 191
|
||
assert by_hash['hash-a']['current_run_no'] == replacement.run_no
|
||
assert by_hash['hash-b']['document_id'] == 92
|
||
assert by_hash['hash-b']['current_run_no'] == source.run_no
|
||
|
||
|
||
def test_same_filename_different_hash_counts_as_two_pdfs(app):
|
||
"""同名不同 hash:file_count=2,不得按文件名误合并。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SAME-NAME')
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=run, intake=intake, document=document)
|
||
_set_run_documents(run, [
|
||
{
|
||
'document_id': 11,
|
||
'document_type': 'attachment',
|
||
'filename': 'order.pdf',
|
||
'content_hash': 'hash-1',
|
||
},
|
||
{
|
||
'document_id': 12,
|
||
'document_type': 'attachment',
|
||
'filename': 'order.pdf',
|
||
'content_hash': 'hash-2',
|
||
},
|
||
])
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == run.user_task_no
|
||
)
|
||
assert task['file_count'] == 2
|
||
assert task['display_kind'] == 'multi_document_task'
|
||
|
||
|
||
def test_same_hash_different_document_id_counts_as_one_pdf(app):
|
||
"""同 hash 不同 document_id(重传):file_count=1。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SAME-HASH')
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=run, intake=intake, document=document)
|
||
_set_run_documents(run, [
|
||
{
|
||
'document_id': 21,
|
||
'root_document_id': 21,
|
||
'document_type': 'attachment',
|
||
'filename': 'order.pdf',
|
||
'content_hash': 'same-hash',
|
||
},
|
||
{
|
||
'document_id': 22,
|
||
'root_document_id': 22,
|
||
'document_type': 'attachment',
|
||
'filename': 'order.pdf',
|
||
'content_hash': 'same-hash',
|
||
},
|
||
])
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == run.user_task_no
|
||
)
|
||
assert task['file_count'] == 1
|
||
assert task['display_kind'] == 'single_document_task'
|
||
assert task['title'] == 'order.pdf'
|
||
doc_objects = [obj for obj in (task.get('objects') or []) if obj.get('kind') == 'document']
|
||
assert len(doc_objects) == 1
|
||
assert doc_objects[0]['document_id'] == 22
|
||
|
||
|
||
def test_sparse_snapshot_enriched_by_extraction_units(app):
|
||
"""快照仅 1 PDF 但 Unit 已拆 2 份时,投影须 multi + file_count=2。"""
|
||
from app.models import (
|
||
FileAsset,
|
||
ShippingOrderDocument,
|
||
ShippingOrderProcessingUnit,
|
||
)
|
||
from app.services.shipping_notice.order_extraction_plan import ensure_order_extraction_plan
|
||
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SPARSE-U')
|
||
asset2 = FileAsset(
|
||
asset_no='FA-RUN-PROJ-SPARSE-U-2',
|
||
owner_user_id=user.id,
|
||
original_filename='second.pdf',
|
||
storage_provider='minio',
|
||
storage_bucket='test',
|
||
storage_key=f'temporary/{user.id}/second.pdf',
|
||
content_hash='b' * 64,
|
||
file_size=100,
|
||
mime_type='application/pdf',
|
||
upload_status='ready',
|
||
lifecycle_status='temporary',
|
||
)
|
||
db.session.add(asset2)
|
||
db.session.flush()
|
||
document2 = ShippingOrderDocument(
|
||
intake_id=intake.id,
|
||
asset_id=asset2.id,
|
||
document_type='attachment',
|
||
attachment_key='second.pdf',
|
||
filename='second.pdf',
|
||
content_hash='C' * 64,
|
||
classification='order_notice',
|
||
parse_status='parsed',
|
||
extracted_text='PO 2',
|
||
)
|
||
db.session.add(document2)
|
||
db.session.flush()
|
||
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_ensure_run_order_task(run=run, intake=intake, document=document)
|
||
# 故意只写 1 份进快照,模拟生产侧栏仍显示单文件名的脏数据。
|
||
_set_run_documents(run, [{
|
||
'document_id': int(document.id),
|
||
'root_document_id': int(document.id),
|
||
'document_type': 'attachment',
|
||
'filename': '4518838152.pdf',
|
||
'content_hash': 'hash-only-one',
|
||
}])
|
||
ensure_order_extraction_plan(run)
|
||
plan = run.extraction_plan
|
||
assert plan is not None
|
||
db.session.add(ShippingOrderProcessingUnit(
|
||
plan_id=int(plan.id),
|
||
intake_id=int(intake.id),
|
||
document_id=int(document2.id),
|
||
position=max((int(u.position) for u in plan.units), default=-1) + 1,
|
||
unit_type='document_extraction',
|
||
document_type='attachment',
|
||
content_hash=str(document2.content_hash),
|
||
status='review_required',
|
||
))
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == run.user_task_no
|
||
)
|
||
assert task['file_count'] == 2
|
||
assert task['display_kind'] == 'multi_document_task'
|
||
assert task['title'] is None
|
||
names = set(task.get('document_names') or [])
|
||
assert '4518838152.pdf' in names or str(document.filename) in names
|
||
assert 'second.pdf' in names
|
||
|
||
|
||
@pytest.mark.skip(reason='workflow ledger removed')
|
||
def test_sparse_snapshot_enriched_by_workflow_items(app):
|
||
"""快照仅 1 PDF 但同 Run 已有 2 个 WorkflowItem 时,投影须 multi。"""
|
||
with app.app_context():
|
||
user, agent, session, intake, document = _seed_run_input(suffix='PROJ-SPARSE-W')
|
||
from app.models import FileAsset, ShippingOrderDocument
|
||
|
||
asset2 = FileAsset(
|
||
asset_no='FA-RUN-PROJ-SPARSE-W-2',
|
||
owner_user_id=user.id,
|
||
original_filename='peer.pdf',
|
||
storage_provider='minio',
|
||
storage_bucket='test',
|
||
storage_key=f'temporary/{user.id}/peer.pdf',
|
||
content_hash='d' * 64,
|
||
file_size=100,
|
||
mime_type='application/pdf',
|
||
upload_status='ready',
|
||
lifecycle_status='temporary',
|
||
)
|
||
db.session.add(asset2)
|
||
db.session.flush()
|
||
document2 = ShippingOrderDocument(
|
||
intake_id=intake.id,
|
||
asset_id=asset2.id,
|
||
document_type='attachment',
|
||
attachment_key='peer.pdf',
|
||
filename='peer.pdf',
|
||
content_hash='E' * 64,
|
||
classification='order_notice',
|
||
parse_status='parsed',
|
||
extracted_text='PO peer',
|
||
)
|
||
db.session.add(document2)
|
||
db.session.flush()
|
||
|
||
run, _ = create_order_extraction_run(
|
||
user=user,
|
||
agent_id=agent.id,
|
||
conversation_id=session.session_id,
|
||
intake_nos=[intake.intake_no],
|
||
keep_input_open=True,
|
||
)
|
||
_set_run_documents(run, [{
|
||
'document_id': int(document.id),
|
||
'root_document_id': int(document.id),
|
||
'document_type': 'attachment',
|
||
'filename': '4518838152.pdf',
|
||
'content_hash': 'hash-sparse-wf',
|
||
}])
|
||
workflow = ShippingNoticeWorkflow(
|
||
workflow_no=f'WF-SPARSE-{run.run_no}',
|
||
conversation_id=session.session_id,
|
||
owner_user_id=int(user.id),
|
||
status='open',
|
||
current_stage='extracting',
|
||
)
|
||
db.session.add(workflow)
|
||
db.session.flush()
|
||
for index, doc in enumerate((document, document2)):
|
||
db.session.add(ShippingNoticeWorkflowItem(
|
||
item_no=f'WFI-SPARSE-{run.run_no}-{index}',
|
||
workflow_id=int(workflow.id),
|
||
workflow_no=workflow.workflow_no,
|
||
conversation_id=session.session_id,
|
||
owner_user_id=int(user.id),
|
||
root_document_id=int(doc.id),
|
||
current_document_id=int(doc.id),
|
||
current_run_no=str(run.run_no),
|
||
lifecycle_status='pending_review',
|
||
current_stage='pending_review',
|
||
current_version_no=1,
|
||
))
|
||
db.session.commit()
|
||
|
||
projection = project_conversation_processing(
|
||
user_id=user.id,
|
||
conversation_id=session.session_id,
|
||
)
|
||
task = next(
|
||
row for row in projection['processing'] + projection['needs_action']
|
||
if row['user_task_no'] == run.user_task_no
|
||
)
|
||
assert task['file_count'] == 2
|
||
assert task['display_kind'] == 'multi_document_task'
|
||
assert task['title'] is None
|
||
assert 'peer.pdf' in (task.get('document_names') or [])
|