"""shipping_notice query_service 读路径单测。""" 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_batch_task_summaries, resolve_task_from_payload, serialize_batch, serialize_task, sort_batch_tasks, task_export_error_message, task_sort_key, ) 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_no="SN-TEST-QUERY-001", process_status="created", source_type="manual", ) from app import db db.session.add(task) db.session.commit() by_no = get_task_by_ref("SN-TEST-QUERY-001") by_id = get_task_by_ref(str(task.id)) assert by_no is not None assert by_id is not None 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_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") assert task_export_error_message(empty) == "export file not found" missing = ShippingTask( task_no="SN-MISSING-FILE", process_status="matched", source_type="manual", export_file_path="/tmp/not-a-real-shipping-export.xlsx", ) assert task_export_error_message(missing) == "export file missing on disk" def test_task_projection_tool_strips_internal_fields(app): """TOOL 投影应剔除 Agent 不应复述的内部状态字段。""" with app.app_context(): task = ShippingTask( task_no="SN-PROJ-TOOL", process_status="waiting_inventory", review_status="align_review", match_status="pending", pending_human_reason="rpa_pending", error_message="boom", source_type="manual", customer_name="ACME", lines_json=[{"sku": "A001"}], ) data = serialize_task(task, TaskProjection.TOOL) assert data["task_no"] == "SN-PROJ-TOOL" assert data["customer_name"] == "ACME" assert data["lines"] == [{"sku": "A001"}] assert not TOOL_TASK_DATA_OMIT.intersection(data.keys()) def test_task_projection_internal_keeps_status_fields(app): """TOOL_INTERNAL 保留 create_task 复用场景所需的内部状态字段。""" with app.app_context(): task = ShippingTask( task_no="SN-PROJ-INT", process_status="parsed", review_status="parse_review", source_type="manual", ) data = serialize_task(task, TaskProjection.TOOL_INTERNAL) assert data["process_status"] == "parsed" 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(app): """conversation_id 应能解析到最近非 rejected 任务。""" with app.app_context(): task = ShippingTask( task_no="SN-CONV-1", conversation_id="conv-abc", process_status="parsed", source_type="manual", ) from app import db db.session.add(task) db.session.commit() resolved = resolve_task_from_payload({"conversation_id": "conv-abc"}) assert resolved is not None assert resolved.task_no == "SN-CONV-1" assert extract_task_no_from_text("请处理 SN-20260617-ABCDEF01 这票") == "SN-20260617-ABCDEF01"