feat(shipping-notice): 落地字段资产治理与唯一 Schema,自动建单随版本冻结

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-07-29 16:23:15 +08:00
1 parent b7307fc573
commit 3a1dbf7ce5
144 files changed
+15167 -4480

No files matched your search

@@ -286,3 +286,366 @@ def test_resolve_task_from_payload_rejects_ambiguous_conversation_id(app):
)
assert resolved is not None
assert resolved.task_no == "SN-CONV-MULTI-1"
def test_task_can_handle_only_creator(app):
"""仅创建者 can_handle;无主任务与非创建者均为 False。"""
with app.app_context():
from app import db
from app.services.shipping_notice.query_service import task_can_handle
owned = ShippingTask(
task_no="SN-HANDLE-OWN",
process_status="created",
source_type="manual",
user_id=42,
)
orphan = ShippingTask(
task_no="SN-HANDLE-ORPHAN",
process_status="created",
source_type="manual",
user_id=None,
)
db.session.add_all([owned, orphan])
db.session.commit()
assert task_can_handle(owned, 42) is True
assert task_can_handle(owned, 42, is_admin=True) is True
assert task_can_handle(owned, 99) is False
assert task_can_handle(owned, 99, is_admin=True) is False
assert task_can_handle(orphan, 42) is False
def test_get_task_for_user_rejects_null_owner(app):
"""写路径 get_task_for_user 拒绝 user_id IS NULL;管理员只读走 visible。"""
with app.app_context():
from app import db
from app.services.shipping_notice.query_service import (
get_task_for_user,
get_task_visible_to_user,
)
orphan = ShippingTask(
task_no="SN-NULL-OWNER",
process_status="created",
source_type="manual",
user_id=None,
)
owned = ShippingTask(
task_no="SN-OWNED",
process_status="created",
source_type="manual",
user_id=7,
)
db.session.add_all([orphan, owned])
db.session.commit()
assert get_task_for_user("SN-NULL-OWNER", 7) is None
assert get_task_for_user("SN-OWNED", 7) is not None
assert get_task_for_user("SN-OWNED", 8) is None
assert get_task_visible_to_user("SN-OWNED", 8, is_admin=True) is not None
assert get_task_visible_to_user("SN-NULL-OWNER", 7, is_admin=True) is not None
def test_serialize_task_summary_enriches_todo_and_can_handle(app):
"""SUMMARY enrich 含 todo_category / created_by / can_handle / ui_state。"""
with app.app_context():
from app import db
from app.services.shipping_notice.query_service import serialize_task_summary
task = ShippingTask(
task_no="SN-ENRICH-1",
process_status="pending_review",
review_status="parse_review",
pending_human_reason="low_confidence",
source_type="manual",
user_id=11,
export_file_name="a.xlsx",
export_file_path="/tmp/a.xlsx",
)
db.session.add(task)
db.session.commit()
data = serialize_task_summary(task, viewer_user_id=11)
assert data["todo_category"] == "review"
assert data["created_by"] == 11
assert data["can_handle"] is True
assert data["ui_state"]["checkpoint"] == "parse_review"
assert data["artifact_summary"]["export_file_name"] == "a.xlsx"
no_viewer = serialize_task_summary(task)
assert "can_handle" not in no_viewer
def test_list_my_todos_and_count_buckets(app):
"""list_my_todos 按 bucket 分类;count 徽标与 pending 一致。"""
with app.app_context():
from app import db
from app.services.shipping_notice.query_service import count_my_todos, list_my_todos
owner = 55
review = ShippingTask(
task_no="SN-TODO-REVIEW",
process_status="pending_review",
review_status="parse_review",
pending_human_reason="low_confidence",
source_type="manual",
user_id=owner,
)
upload = ShippingTask(
task_no="SN-TODO-UPLOAD",
process_status="matched",
source_type="manual",
user_id=owner,
export_file_path="/tmp/u.xlsx",
export_file_name="u.xlsx",
)
done = ShippingTask(
task_no="SN-TODO-DONE",
process_status="uploaded",
source_type="manual",
user_id=owner,
)
other = ShippingTask(
task_no="SN-TODO-OTHER",
process_status="pending_review",
review_status="parse_review",
pending_human_reason="low_confidence",
source_type="manual",
user_id=99,
)
db.session.add_all([review, upload, done, other])
db.session.commit()
pending = list_my_todos(owner, bucket="pending", page=1, page_size=20)
assert pending["total"] == 2
assert {item["task_no"] for item in pending["items"]} == {
"SN-TODO-REVIEW",
"SN-TODO-UPLOAD",
}
assert all(item["can_handle"] is True for item in pending["items"])
done_page = list_my_todos(owner, bucket="done")
assert done_page["total"] == 1
assert done_page["items"][0]["task_no"] == "SN-TODO-DONE"
counts = count_my_todos(owner)
assert counts["pending"] == 2
assert counts["done"] == 1
assert counts["total_pending_badge"] == 2
admin_pending = list_my_todos(owner, bucket="pending", is_admin=True)
assert admin_pending["total"] >= 3
other_item = next(
item for item in admin_pending["items"] if item["task_no"] == "SN-TODO-OTHER"
)
assert other_item["can_handle"] is False
def test_list_my_todos_includes_open_review_case_with_run_deep_link(app):
"""开放 Review Case 进入 pending,并暴露 review_run_no 供人工复核深链。"""
with app.app_context():
from app import db
from app.services.shipping_notice.order_review_service import ensure_run_review_cases
from app.services.shipping_notice.query_service import count_my_todos, list_my_todos
from tests.test_shipping_order_review_service import _materialized_run
from tests.test_shipping_order_extraction_runs import _seed_run_input
user, agent, session, intake, _document = _seed_run_input(suffix='TODO-REV-DL')
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
unit = run.extraction_plan.units[0]
unit.result_json = {
'orders': [{
'header_fields': [{'field_id': 'po', 'observations': [{'value': 'PO-DL'}]}],
'lines': [{'fields': [{'field_id': 'part', 'observations': [{'value': 'A1'}]}]}],
'conflicts': [],
}],
}
db.session.commit()
cases = ensure_run_review_cases(run=run)
assert cases
case = cases[0]
pending = list_my_todos(user.id, bucket='pending', page=1, page_size=50)
review_items = [item for item in pending['items'] if item.get('item_kind') == 'review_case']
assert review_items, 'pending 应包含建单前复核 Case'
hit = next(item for item in review_items if item.get('review_case_no') == case.case_no)
assert hit['todo_category'] == 'review'
assert hit['review_run_no'] == run.run_no
assert hit['owner_agent_id'] == agent.id
assert hit['conversation_id'] == session.session_id
assert hit['can_handle'] is True
assert hit['ui_state']['summary']['entry_surface'] == 'review_workbench'
counts = count_my_todos(user.id)
assert counts['pending'] >= 1
assert counts['total_pending_badge'] == counts['pending']
def test_list_my_todos_filters_review_case_by_customer_filename(app):
"""Review Case 客户过滤按来源文件名/Case 号匹配,不能只筛 Task。"""
with app.app_context():
from app import db
from app.services.shipping_notice.order_review_service import ensure_run_review_cases
from app.services.shipping_notice.query_service import list_my_todos
from tests.test_shipping_order_review_service import _materialized_run
from tests.test_shipping_order_extraction_runs import _seed_run_input
user, agent, session, intake, document = _seed_run_input(suffix='TODO-REV-CUST')
document.filename = 'ACME-PO-450099.pdf'
db.session.flush()
run = _materialized_run(user=user, agent=agent, session=session, intake=intake)
unit = run.extraction_plan.units[0]
unit.result_json = {
'orders': [{
'header_fields': [{'field_id': 'po', 'observations': [{'value': 'PO-CUST'}]}],
'lines': [{'fields': [{'field_id': 'part', 'observations': [{'value': 'B1'}]}]}],
'conflicts': [],
}],
}
db.session.commit()
cases = ensure_run_review_cases(run=run)
assert cases
hit = list_my_todos(user.id, bucket='pending', page=1, page_size=50, customer='ACME')
review_hits = [item for item in hit['items'] if item.get('item_kind') == 'review_case']
assert review_hits, '按文件名关键字应命中 Review Case'
miss = list_my_todos(user.id, bucket='pending', page=1, page_size=50, customer='NO-SUCH-CUSTOMER')
assert not any(item.get('item_kind') == 'review_case' for item in miss['items'])
def test_list_my_todos_total_and_high_page_are_not_capped(app):
"""total 必须是完整匹配数;超过旧 500 截断后高页码仍可访问。"""
with app.app_context():
from app import db
from app.services.shipping_notice.query_service import list_my_todos
owner = 77
# 520 > 旧 limit(500),用于证明截断不再作为 total 来源。
total_tasks = 520
tasks = [
ShippingTask(
task_no=f"SN-TODO-PAGE-{index:04d}",
process_status="pending_review",
review_status="parse_review",
pending_human_reason="low_confidence",
source_type="manual",
user_id=owner,
)
for index in range(total_tasks)
]
db.session.add_all(tasks)
db.session.commit()
page1 = list_my_todos(owner, bucket="pending", page=1, page_size=20)
assert page1["total"] == total_tasks
assert len(page1["items"]) == 20
# 第 26 页:offset=500,恰好是旧截断边界之后;必须仍能返回数据。
page26 = list_my_todos(owner, bucket="pending", page=26, page_size=20)
assert page26["total"] == total_tasks
assert len(page26["items"]) == 20
page27 = list_my_todos(owner, bucket="pending", page=27, page_size=20)
assert page27["total"] == total_tasks
assert len(page27["items"]) == 0
far = list_my_todos(owner, bucket="pending", page=30, page_size=20)
assert far["total"] == total_tasks
assert far["items"] == []
def test_todo_category_lightweight_matches_orm_and_count(app):
"""
轻量行分类 / SQL CASE 与 ORM task_todo_category 一致;count 徽标与 list total 同源。
断言意图:UNION 分页下推的 SQL 分类不得漂移口径。
"""
with app.app_context():
from types import SimpleNamespace
from app import db
from app.services.shipping_notice.query_service import (
_task_todo_category_sql_expr,
_todo_category_from_task_row,
count_my_todos,
list_my_todos,
task_todo_category,
)
owner = 88
fixtures = [
ShippingTask(
task_no="SN-LW-REVIEW",
process_status="pending_review",
review_status="parse_review",
pending_human_reason="low_confidence",
source_type="manual",
user_id=owner,
),
ShippingTask(
task_no="SN-LW-UPLOAD",
process_status="matched",
source_type="manual",
user_id=owner,
export_file_path="/tmp/lw.xlsx",
export_file_name="lw.xlsx",
),
ShippingTask(
task_no="SN-LW-SHORTAGE",
# 不可用 matched:状态机会先落到 ready_export,遮住 shortage 人审分支。
process_status="parsed",
review_status="shortage_review",
pending_human_reason="shortage",
source_type="manual",
user_id=owner,
),
ShippingTask(
task_no="SN-LW-DONE",
process_status="uploaded",
source_type="manual",
user_id=owner,
),
ShippingTask(
task_no="SN-LW-PROGRESS",
process_status="parsed",
source_type="manual",
user_id=owner,
),
]
db.session.add_all(fixtures)
db.session.commit()
category_expr = _task_todo_category_sql_expr()
for task in fixtures:
row = SimpleNamespace(
process_status=task.process_status,
review_status=task.review_status,
pending_human_reason=task.pending_human_reason,
shipping_case_id=task.shipping_case_id,
customer_resolution_status=task.customer_resolution_status,
order_snapshot_id=task.order_snapshot_id,
current_order_extraction_id=task.current_order_extraction_id,
inventory_lines_json=task.inventory_lines_json,
match_status=task.match_status,
export_file_path=task.export_file_path,
)
expected = task_todo_category(task)
# 轻量路径与 ORM 路径同分类。
assert _todo_category_from_task_row(row, extractions_with_lines=set()) == expected
# SQL CASE 与 ORM 路径同分类,保证 UNION 过滤不漂移。
sql_category = (
db.session.query(category_expr)
.filter(ShippingTask.id == task.id)
.scalar()
)
assert sql_category == expected
pending = list_my_todos(owner, bucket="pending", page=1, page_size=50)
assert pending["total"] == 3
counts = count_my_todos(owner)
assert counts["pending"] == 3
assert counts["done"] == 1
assert counts["processing"] == 1
assert counts["total_pending_badge"] == 3