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,20 +1,23 @@
|
||||
"""shipping_notice query_service 读路径单测。"""
|
||||
"""
|
||||
backend/tests/test_shipping_notice_query_service.py
|
||||
|
||||
用途:shipping_notice query_service 读路径单测(任务/待办;co8 已移除批次排序用例)。
|
||||
依赖:pytest、ShippingNotice、query_service。
|
||||
逻辑:覆盖 task_no 解析、分页、投影与 my_todos UNION。
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.models import ShippingNotice
|
||||
|
||||
from app.models import ShippingBatch, ShippingTask
|
||||
from app.services.shipping_notice.query_service import (
|
||||
BatchProjection,
|
||||
TaskProjection,
|
||||
compute_batch_process_summary,
|
||||
extract_task_no_from_text,
|
||||
get_task_by_ref,
|
||||
get_task_in_batch,
|
||||
list_tasks_for_user,
|
||||
list_tasks_page_for_user,
|
||||
list_batch_task_summaries,
|
||||
resolve_task_from_payload,
|
||||
serialize_batch,
|
||||
serialize_task,
|
||||
sort_batch_tasks,
|
||||
task_export_error_message,
|
||||
task_sort_key,
|
||||
)
|
||||
@@ -24,7 +27,7 @@ from app.services.shipping_notice.user_facing_status import TOOL_TASK_DATA_OMIT
|
||||
def test_get_task_by_ref_supports_task_no_and_numeric_id(app):
|
||||
"""task_no 与数字主键均应能解析到同一任务。"""
|
||||
with app.app_context():
|
||||
task = ShippingTask(
|
||||
task = ShippingNotice(
|
||||
task_no="SN-TEST-QUERY-001",
|
||||
process_status="created",
|
||||
source_type="manual",
|
||||
@@ -41,45 +44,6 @@ def test_get_task_by_ref_supports_task_no_and_numeric_id(app):
|
||||
assert by_no.id == by_id.id
|
||||
|
||||
|
||||
def test_sort_batch_tasks_orders_by_batch_index_then_created_at(app):
|
||||
"""批次内排序应与 batch_index、created_at 规则一致。"""
|
||||
with app.app_context():
|
||||
batch = ShippingBatch(batch_no="SB-TEST-SORT", file_count=2)
|
||||
second = ShippingTask(task_no="SN-SORT-2", batch_index=2, process_status="created", source_type="manual")
|
||||
first = ShippingTask(task_no="SN-SORT-1", batch_index=1, process_status="created", source_type="manual")
|
||||
from app import db
|
||||
|
||||
db.session.add(batch)
|
||||
db.session.flush()
|
||||
second.batch_id = batch.id
|
||||
first.batch_id = batch.id
|
||||
db.session.add_all([first, second])
|
||||
db.session.commit()
|
||||
|
||||
ordered = sort_batch_tasks([second, first])
|
||||
assert [item.task_no for item in ordered] == ["SN-SORT-1", "SN-SORT-2"]
|
||||
assert task_sort_key(first) < task_sort_key(second)
|
||||
|
||||
summaries = list_batch_task_summaries(batch)
|
||||
assert [row["task_no"] for row in summaries] == ["SN-SORT-1", "SN-SORT-2"]
|
||||
|
||||
|
||||
def test_get_task_in_batch_rejects_foreign_task(app):
|
||||
"""不属于目标批次的任务应返回 None。"""
|
||||
with app.app_context():
|
||||
batch_a = ShippingBatch(batch_no="SB-A", file_count=1)
|
||||
batch_b = ShippingBatch(batch_no="SB-B", file_count=1)
|
||||
task = ShippingTask(task_no="SN-FOREIGN", process_status="created", source_type="manual")
|
||||
from app import db
|
||||
|
||||
db.session.add_all([batch_a, batch_b])
|
||||
db.session.flush()
|
||||
task.batch_id = batch_a.id
|
||||
db.session.add(task)
|
||||
db.session.commit()
|
||||
|
||||
assert get_task_in_batch("SB-A", "SN-FOREIGN") is not None
|
||||
assert get_task_in_batch("SB-B", "SN-FOREIGN") is None
|
||||
|
||||
|
||||
def test_list_tasks_for_user_filters_shortage_review_by_user_facing_state(app):
|
||||
@@ -87,14 +51,14 @@ def test_list_tasks_for_user_filters_shortage_review_by_user_facing_state(app):
|
||||
with app.app_context():
|
||||
from app import db
|
||||
|
||||
shortage = ShippingTask(
|
||||
shortage = ShippingNotice(
|
||||
task_no="SN-FILTER-SHORTAGE",
|
||||
process_status="pending_review",
|
||||
review_status="shortage_review",
|
||||
pending_human_reason="shortage",
|
||||
source_type="manual",
|
||||
)
|
||||
ordinary = ShippingTask(
|
||||
ordinary = ShippingNotice(
|
||||
task_no="SN-FILTER-PARSE",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
@@ -115,7 +79,7 @@ def test_list_tasks_page_for_user_returns_stable_page_and_total(app):
|
||||
from app import db
|
||||
|
||||
db.session.add_all([
|
||||
ShippingTask(task_no=f"SN-PAGE-{index:02d}", process_status="created", source_type="manual")
|
||||
ShippingNotice(task_no=f"SN-PAGE-{index:02d}", process_status="created", source_type="manual")
|
||||
for index in range(1, 6)
|
||||
])
|
||||
db.session.commit()
|
||||
@@ -141,9 +105,9 @@ def test_list_tasks_page_for_user_applies_status_filter_before_count(app):
|
||||
from app import db
|
||||
|
||||
db.session.add_all([
|
||||
ShippingTask(task_no="SN-PAGE-FILTER-1", process_status="created", source_type="manual"),
|
||||
ShippingTask(task_no="SN-PAGE-FILTER-2", process_status="created", source_type="manual"),
|
||||
ShippingTask(task_no="SN-PAGE-FILTER-3", process_status="uploaded", source_type="manual"),
|
||||
ShippingNotice(task_no="SN-PAGE-FILTER-1", process_status="created", source_type="manual"),
|
||||
ShippingNotice(task_no="SN-PAGE-FILTER-2", process_status="created", source_type="manual"),
|
||||
ShippingNotice(task_no="SN-PAGE-FILTER-3", process_status="uploaded", source_type="manual"),
|
||||
])
|
||||
db.session.commit()
|
||||
|
||||
@@ -159,10 +123,10 @@ def test_list_tasks_page_for_user_applies_status_filter_before_count(app):
|
||||
def test_task_export_error_message_distinct_for_missing_path(app):
|
||||
"""无导出路径与磁盘缺失应返回不同错误文案。"""
|
||||
with app.app_context():
|
||||
empty = ShippingTask(task_no="SN-NO-EXPORT", process_status="created", source_type="manual")
|
||||
empty = ShippingNotice(task_no="SN-NO-EXPORT", process_status="created", source_type="manual")
|
||||
assert task_export_error_message(empty) == "export file not found"
|
||||
|
||||
missing = ShippingTask(
|
||||
missing = ShippingNotice(
|
||||
task_no="SN-MISSING-FILE",
|
||||
process_status="matched",
|
||||
source_type="manual",
|
||||
@@ -174,7 +138,7 @@ def test_task_export_error_message_distinct_for_missing_path(app):
|
||||
def test_task_projection_tool_strips_internal_fields(app):
|
||||
"""TOOL 投影应剔除 Agent 不应复述的内部状态字段。"""
|
||||
with app.app_context():
|
||||
task = ShippingTask(
|
||||
task = ShippingNotice(
|
||||
task_no="SN-PROJ-TOOL",
|
||||
process_status="waiting_inventory",
|
||||
review_status="align_review",
|
||||
@@ -195,7 +159,7 @@ def test_task_projection_tool_strips_internal_fields(app):
|
||||
def test_task_projection_internal_keeps_status_fields(app):
|
||||
"""TOOL_INTERNAL 保留 create_task 复用场景所需的内部状态字段。"""
|
||||
with app.app_context():
|
||||
task = ShippingTask(
|
||||
task = ShippingNotice(
|
||||
task_no="SN-PROJ-INT",
|
||||
process_status="parsed",
|
||||
review_status="parse_review",
|
||||
@@ -206,39 +170,11 @@ def test_task_projection_internal_keeps_status_fields(app):
|
||||
assert data["review_status"] == "parse_review"
|
||||
|
||||
|
||||
def test_batch_process_summary_shared_formula(app):
|
||||
"""批次汇总公式在 serialize_batch 与 compute_batch_process_summary 间应一致。"""
|
||||
with app.app_context():
|
||||
batch = ShippingBatch(batch_no="SB-SUMMARY", file_count=2)
|
||||
done = ShippingTask(task_no="SN-DONE", process_status="uploaded", source_type="manual", batch_index=1)
|
||||
running = ShippingTask(task_no="SN-RUN", process_status="matched", source_type="manual", batch_index=2)
|
||||
from app import db
|
||||
|
||||
db.session.add(batch)
|
||||
db.session.flush()
|
||||
done.batch_id = batch.id
|
||||
running.batch_id = batch.id
|
||||
db.session.add_all([done, running])
|
||||
db.session.commit()
|
||||
|
||||
tasks = [done, running]
|
||||
summary = compute_batch_process_summary(tasks)
|
||||
assert summary["total"] == 2
|
||||
assert summary["completed"] == 1
|
||||
assert summary["failed"] == 0
|
||||
assert summary["running"] == 1
|
||||
assert summary["pending"] == 2
|
||||
assert summary["current_task_no"] == "SN-RUN"
|
||||
tool_batch = serialize_batch(batch, BatchProjection.TOOL)
|
||||
assert tool_batch["process_summary"]["completed"] == 1
|
||||
assert tool_batch["batch_no"] == "SB-SUMMARY"
|
||||
assert "id" not in tool_batch
|
||||
|
||||
|
||||
def test_resolve_task_from_payload_by_conversation_id_single_active_task(app):
|
||||
"""conversation_id 仅在同会话只有一票可操作任务时允许回退。"""
|
||||
with app.app_context():
|
||||
task = ShippingTask(
|
||||
task = ShippingNotice(
|
||||
task_no="SN-CONV-1",
|
||||
conversation_id="conv-abc",
|
||||
process_status="parsed",
|
||||
@@ -263,13 +199,13 @@ def test_resolve_task_from_payload_rejects_ambiguous_conversation_id(app):
|
||||
|
||||
db.session.add_all(
|
||||
[
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-CONV-MULTI-1",
|
||||
conversation_id="conv-multi",
|
||||
process_status="parsed",
|
||||
source_type="manual",
|
||||
),
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-CONV-MULTI-2",
|
||||
conversation_id="conv-multi",
|
||||
process_status="pending_review",
|
||||
@@ -294,13 +230,13 @@ def test_task_can_handle_only_creator(app):
|
||||
from app import db
|
||||
from app.services.shipping_notice.query_service import task_can_handle
|
||||
|
||||
owned = ShippingTask(
|
||||
owned = ShippingNotice(
|
||||
task_no="SN-HANDLE-OWN",
|
||||
process_status="created",
|
||||
source_type="manual",
|
||||
user_id=42,
|
||||
)
|
||||
orphan = ShippingTask(
|
||||
orphan = ShippingNotice(
|
||||
task_no="SN-HANDLE-ORPHAN",
|
||||
process_status="created",
|
||||
source_type="manual",
|
||||
@@ -325,13 +261,13 @@ def test_get_task_for_user_rejects_null_owner(app):
|
||||
get_task_visible_to_user,
|
||||
)
|
||||
|
||||
orphan = ShippingTask(
|
||||
orphan = ShippingNotice(
|
||||
task_no="SN-NULL-OWNER",
|
||||
process_status="created",
|
||||
source_type="manual",
|
||||
user_id=None,
|
||||
)
|
||||
owned = ShippingTask(
|
||||
owned = ShippingNotice(
|
||||
task_no="SN-OWNED",
|
||||
process_status="created",
|
||||
source_type="manual",
|
||||
@@ -353,7 +289,7 @@ def test_serialize_task_summary_enriches_todo_and_can_handle(app):
|
||||
from app import db
|
||||
from app.services.shipping_notice.query_service import serialize_task_summary
|
||||
|
||||
task = ShippingTask(
|
||||
task = ShippingNotice(
|
||||
task_no="SN-ENRICH-1",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
@@ -384,7 +320,7 @@ def test_list_my_todos_and_count_buckets(app):
|
||||
from app.services.shipping_notice.query_service import count_my_todos, list_my_todos
|
||||
|
||||
owner = 55
|
||||
review = ShippingTask(
|
||||
review = ShippingNotice(
|
||||
task_no="SN-TODO-REVIEW",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
@@ -392,7 +328,7 @@ def test_list_my_todos_and_count_buckets(app):
|
||||
source_type="manual",
|
||||
user_id=owner,
|
||||
)
|
||||
upload = ShippingTask(
|
||||
upload = ShippingNotice(
|
||||
task_no="SN-TODO-UPLOAD",
|
||||
process_status="matched",
|
||||
source_type="manual",
|
||||
@@ -400,13 +336,13 @@ def test_list_my_todos_and_count_buckets(app):
|
||||
export_file_path="/tmp/u.xlsx",
|
||||
export_file_name="u.xlsx",
|
||||
)
|
||||
done = ShippingTask(
|
||||
done = ShippingNotice(
|
||||
task_no="SN-TODO-DONE",
|
||||
process_status="uploaded",
|
||||
source_type="manual",
|
||||
user_id=owner,
|
||||
)
|
||||
other = ShippingTask(
|
||||
other = ShippingNotice(
|
||||
task_no="SN-TODO-OTHER",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
@@ -482,6 +418,70 @@ def test_list_my_todos_includes_open_review_case_with_run_deep_link(app):
|
||||
assert counts['total_pending_badge'] == counts['pending']
|
||||
|
||||
|
||||
def test_list_my_todos_includes_orphan_parse_failed_intake(app):
|
||||
"""
|
||||
未建 OT 且解析失败的 Intake 须进入 pending,并暴露 conversation 深链字段。
|
||||
|
||||
断言:item_kind=order_intake;todo_category=intake_parse;count.pending 计入。
|
||||
"""
|
||||
with app.app_context():
|
||||
from app import db
|
||||
from app.models import FileAsset, ShippingOrderIntake, User
|
||||
from app.services.shipping_notice.query_service import count_my_todos, list_my_todos
|
||||
|
||||
user = User(username='todo-orphan-intake', email='todo-orphan@example.com', password_hash='x')
|
||||
db.session.add(user)
|
||||
db.session.flush()
|
||||
asset = FileAsset(
|
||||
asset_no=f'FA-ORPHAN-{user.id}',
|
||||
owner_user_id=user.id,
|
||||
original_filename='orphan-fail.pdf',
|
||||
storage_provider='minio',
|
||||
storage_bucket='test',
|
||||
storage_key=f'temporary/{user.id}/orphan-fail.pdf',
|
||||
content_hash='b' * 64,
|
||||
file_size=12,
|
||||
mime_type='application/pdf',
|
||||
upload_status='ready',
|
||||
lifecycle_status='temporary',
|
||||
)
|
||||
db.session.add(asset)
|
||||
db.session.flush()
|
||||
intake = ShippingOrderIntake(
|
||||
intake_no=f'SNI-ORPHAN-{user.id}',
|
||||
asset_id=int(asset.id),
|
||||
user_id=int(user.id),
|
||||
conversation_id='c' * 32,
|
||||
upload_batch_id='batch-orphan-1',
|
||||
source_type='direct_pdf',
|
||||
purpose='shipping_notice',
|
||||
parse_status='parse_failed',
|
||||
parser_version='test-v1',
|
||||
error_code='SHIPPING_NOTICE.INTAKE_PARSE_FAILED',
|
||||
error_message='parse boom',
|
||||
)
|
||||
db.session.add(intake)
|
||||
db.session.commit()
|
||||
|
||||
pending = list_my_todos(user.id, bucket='pending', page=1, page_size=50)
|
||||
intake_items = [item for item in pending['items'] if item.get('item_kind') == 'order_intake']
|
||||
assert intake_items, 'pending 应包含未建 OT 的失败 Intake'
|
||||
hit = next(item for item in intake_items if item.get('intake_no') == intake.intake_no)
|
||||
assert hit['todo_category'] == 'intake_parse'
|
||||
assert hit['conversation_id'] == 'c' * 32
|
||||
assert hit['source_file_name'] == 'orphan-fail.pdf'
|
||||
assert hit['can_handle'] is True
|
||||
assert hit['ui_state']['summary']['entry_surface'] == 'conversation_processing'
|
||||
|
||||
filtered = list_my_todos(
|
||||
user.id, bucket='pending', page=1, page_size=50, todo_category='intake_parse',
|
||||
)
|
||||
assert any(item.get('intake_no') == intake.intake_no for item in filtered['items'])
|
||||
|
||||
counts = count_my_todos(user.id)
|
||||
assert counts['pending'] >= 1
|
||||
|
||||
|
||||
def test_list_my_todos_filters_review_case_by_customer_filename(app):
|
||||
"""Review Case 客户过滤按来源文件名/Case 号匹配,不能只筛 Task。"""
|
||||
with app.app_context():
|
||||
@@ -525,7 +525,7 @@ def test_list_my_todos_total_and_high_page_are_not_capped(app):
|
||||
# 520 > 旧 limit(500),用于证明截断不再作为 total 来源。
|
||||
total_tasks = 520
|
||||
tasks = [
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no=f"SN-TODO-PAGE-{index:04d}",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
@@ -576,7 +576,7 @@ def test_todo_category_lightweight_matches_orm_and_count(app):
|
||||
|
||||
owner = 88
|
||||
fixtures = [
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-LW-REVIEW",
|
||||
process_status="pending_review",
|
||||
review_status="parse_review",
|
||||
@@ -584,7 +584,7 @@ def test_todo_category_lightweight_matches_orm_and_count(app):
|
||||
source_type="manual",
|
||||
user_id=owner,
|
||||
),
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-LW-UPLOAD",
|
||||
process_status="matched",
|
||||
source_type="manual",
|
||||
@@ -592,7 +592,7 @@ def test_todo_category_lightweight_matches_orm_and_count(app):
|
||||
export_file_path="/tmp/lw.xlsx",
|
||||
export_file_name="lw.xlsx",
|
||||
),
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-LW-SHORTAGE",
|
||||
# 不可用 matched:状态机会先落到 ready_export,遮住 shortage 人审分支。
|
||||
process_status="parsed",
|
||||
@@ -601,13 +601,13 @@ def test_todo_category_lightweight_matches_orm_and_count(app):
|
||||
source_type="manual",
|
||||
user_id=owner,
|
||||
),
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-LW-DONE",
|
||||
process_status="uploaded",
|
||||
source_type="manual",
|
||||
user_id=owner,
|
||||
),
|
||||
ShippingTask(
|
||||
ShippingNotice(
|
||||
task_no="SN-LW-PROGRESS",
|
||||
process_status="parsed",
|
||||
source_type="manual",
|
||||
@@ -637,7 +637,7 @@ def test_todo_category_lightweight_matches_orm_and_count(app):
|
||||
# SQL CASE 与 ORM 路径同分类,保证 UNION 过滤不漂移。
|
||||
sql_category = (
|
||||
db.session.query(category_expr)
|
||||
.filter(ShippingTask.id == task.id)
|
||||
.filter(ShippingNotice.id == task.id)
|
||||
.scalar()
|
||||
)
|
||||
assert sql_category == expected
|
||||
|
||||
Reference in new issue
Block a user