233 lines
9.4 KiB
Python
233 lines
9.4 KiB
Python
"""
|
|
backend/tests/test_shipping_conversation_processing_api.py
|
|
|
|
用途:验收统一 shipping-processing / focus / viewed 工作台 API 信封。
|
|
依赖:Flask 测试客户端、JWT、_seed_run_input、create_order_extraction_run、order_task_service。
|
|
逻辑:Candidate 前仅 order_intake;分配 OT 后可 focus/viewed;旧 /processing GET 已移除。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from app import db
|
|
from app.core.auth.jwt import issue_local_access_token
|
|
from app.models import ShippingOrderCandidate, ShippingOrderProcessingRun
|
|
from sqlalchemy import event
|
|
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_from_run,
|
|
)
|
|
from tests.test_shipping_order_extraction_runs import _seed_run_input
|
|
|
|
|
|
def test_workspace_shipping_processing_and_focus_viewed_api(app, client):
|
|
"""Candidate 前仅 intake;形成 OT 后 focus/viewed 可用。"""
|
|
with app.app_context():
|
|
user, agent, session, intake, document = _seed_run_input(suffix='API-PROC')
|
|
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,
|
|
)
|
|
db.session.commit()
|
|
token = issue_local_access_token(user)
|
|
session_id = session.session_id
|
|
intake_no = intake.intake_no
|
|
run_id = int(run.id)
|
|
intake_id = int(intake.id)
|
|
document_id = int(document.id)
|
|
assert run.user_task_no is None
|
|
|
|
headers = {'Authorization': f'Bearer {token}'}
|
|
processing = client.get(
|
|
f'/api/workspace/shipping-processing/conversations/{session_id}',
|
|
headers=headers,
|
|
)
|
|
assert processing.status_code == 200, processing.get_json()
|
|
body = processing.get_json()
|
|
assert body['success'] is True
|
|
work_items = body['data'].get('work_items') or []
|
|
assert any(
|
|
str(row.get('work_item_type') or '') == 'order_intake'
|
|
and str(row.get('work_item_no') or '') == intake_no
|
|
for row in work_items
|
|
if isinstance(row, dict)
|
|
)
|
|
assert not any(
|
|
str(row.get('work_item_type') or '') == 'order_task'
|
|
for row in work_items
|
|
if isinstance(row, dict)
|
|
)
|
|
|
|
legacy = client.get(
|
|
f'/api/workspace/shipping-notice/conversations/{session_id}/processing',
|
|
headers=headers,
|
|
)
|
|
assert legacy.status_code == 404
|
|
|
|
with app.app_context():
|
|
run = db.session.get(ShippingOrderProcessingRun, run_id)
|
|
assert run is not None
|
|
candidate = ShippingOrderCandidate(
|
|
candidate_no=f'SNC-API-PROC-{run_id}',
|
|
intake_id=intake_id,
|
|
primary_document_id=document_id,
|
|
candidate_key=f'key-api-proc-{run_id}',
|
|
order_extraction_run_id=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_from_run(
|
|
run,
|
|
intake_id=intake_id,
|
|
candidate_id=int(candidate.id),
|
|
source_type='direct_pdf',
|
|
stage='candidate_created',
|
|
)
|
|
db.session.commit()
|
|
user_task_no = str(run.user_task_no)
|
|
|
|
focus = client.post(
|
|
f'/api/workspace/shipping-notice/conversation-tasks/{user_task_no}/focus',
|
|
json={'conversation_id': session_id},
|
|
headers=headers,
|
|
)
|
|
assert focus.status_code == 200, focus.get_json()
|
|
assert focus.get_json()['data']['focused_task_no'] == user_task_no
|
|
assert 'work_items' not in focus.get_json()['data']
|
|
assert set(focus.get_json()['data']['processing']['status_groups']) == {
|
|
'failed', 'waiting_human', 'processing', 'completed', 'cancelled', 'archived',
|
|
}
|
|
|
|
viewed = client.post(
|
|
f'/api/workspace/shipping-notice/conversation-tasks/{user_task_no}/viewed',
|
|
json={'conversation_id': session_id},
|
|
headers=headers,
|
|
)
|
|
assert viewed.status_code == 200, viewed.get_json()
|
|
assert 'work_items' not in viewed.get_json()['data']
|
|
|
|
|
|
def test_workspace_shipping_processing_summary_omits_detail_and_legacy_mirrors(app, client):
|
|
"""首屏会话摘要只返回状态→EML→PDF权威分组,详情必须按需读取。"""
|
|
with app.app_context():
|
|
user, agent, session, intake, _document = _seed_run_input(suffix='API-SUMMARY')
|
|
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,
|
|
)
|
|
db.session.commit()
|
|
token = issue_local_access_token(user)
|
|
session_id = session.session_id
|
|
assert run.id is not None
|
|
|
|
response = client.get(
|
|
f'/api/workspace/shipping-processing/conversations/{session_id}?view=summary',
|
|
headers={'Authorization': f'Bearer {token}'},
|
|
)
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
data = response.get_json()['data']
|
|
assert data['conversation_id'] == session_id
|
|
assert data['projection_mode'] == 'summary'
|
|
assert 'work_items' not in data
|
|
projection = data['conversation_processing']
|
|
assert set(projection['status_groups']) == {
|
|
'failed', 'waiting_human', 'processing', 'completed', 'cancelled', 'archived',
|
|
}
|
|
assert 'needs_action' not in projection
|
|
assert 'history' not in projection
|
|
for tasks in projection['status_groups'].values():
|
|
for task in tasks:
|
|
assert 'process_gate' not in task
|
|
assert 'objects' not in task
|
|
# PDF 叶子是首屏“状态→EML→PDF”树的一部分;但叶子同样不携带详情 Gate。
|
|
for child in task.get('aggregate_children') or []:
|
|
assert 'process_gate' not in child
|
|
|
|
|
|
def test_workspace_shipping_processing_summary_does_not_read_run_snapshots(app, client):
|
|
"""首屏摘要不得查询 processing run,避免 JSON 快照解码重新进入轮询路径。"""
|
|
with app.app_context():
|
|
user, _agent, session, _intake, _document = _seed_run_input(suffix='API-SUMMARY-FAST')
|
|
token = issue_local_access_token(user)
|
|
session_id = session.session_id
|
|
run_selects: list[str] = []
|
|
|
|
def capture_run_selects(_conn, _cursor, statement, _parameters, _context, _executemany):
|
|
if 'shipping_order_processing_runs' in statement.lower():
|
|
run_selects.append(statement)
|
|
|
|
event.listen(db.engine, 'before_cursor_execute', capture_run_selects)
|
|
try:
|
|
response = client.get(
|
|
f'/api/workspace/shipping-processing/conversations/{session_id}?view=summary',
|
|
headers={'Authorization': f'Bearer {token}'},
|
|
)
|
|
finally:
|
|
event.remove(db.engine, 'before_cursor_execute', capture_run_selects)
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
assert run_selects == []
|
|
|
|
|
|
def test_workspace_shipping_processing_summary_keeps_lightweight_action_entries(app, client):
|
|
"""摘要不加载重型详情,但必须保留后端裁决的最小操作入口。"""
|
|
with app.app_context():
|
|
user, agent, session, intake, document = _seed_run_input(suffix='API-SUMMARY-ACTIONS')
|
|
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,
|
|
)
|
|
run_no = run.run_no
|
|
db.session.commit()
|
|
token = issue_local_access_token(user)
|
|
session_id = session.session_id
|
|
document.parse_status = 'parsed'
|
|
document.order_extraction_status = 'review_required'
|
|
candidate = ShippingOrderCandidate(
|
|
candidate_no=f'SNC-API-SUMMARY-ACTIONS-{run.id}',
|
|
intake_id=intake.id,
|
|
primary_document_id=document.id,
|
|
candidate_key=f'key-api-summary-actions-{run.id}',
|
|
order_extraction_run_id=run.id,
|
|
is_current_version=True,
|
|
candidate_status='provisional',
|
|
structured_result_json={},
|
|
)
|
|
db.session.add(candidate)
|
|
db.session.flush()
|
|
db.session.commit()
|
|
|
|
response = client.get(
|
|
f'/api/workspace/shipping-processing/conversations/{session_id}?view=summary',
|
|
headers={'Authorization': f'Bearer {token}'},
|
|
)
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
projection = response.get_json()['data']['conversation_processing']
|
|
assert [node['code'] for node in projection['lifecycle_nodes']] == [
|
|
'intake_parse', 'order_review', 'fulfillment_prep', 'freight_upload', 'upload_complete',
|
|
]
|
|
tasks = projection['status_groups']['waiting_human']
|
|
assert len(tasks) == 1
|
|
leaf = tasks[0]['aggregate_children'][0]
|
|
assert leaf['run_no'] == run_no
|
|
assert leaf['source_summary']['po_number'] is None
|
|
assert leaf['ui_state']['lifecycle_stage'] == 'intake_parse'
|
|
assert leaf['ui_state']['primary_action']['code'] == 'open_order_review'
|
|
assert leaf['ui_state']['primary_action']['target_surface'] == 'order_lifecycle_workbench'
|
|
assert leaf['ui_state']['primary_action']['target_lifecycle_stage'] == 'intake_parse'
|