from app import db from app.models import ShippingCase def _tool_headers(app): return { "Content-Type": "application/json", } def test_shipping_notice_tool_flow(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual", "raw_content": "客户:ACME 料号A001 x 10"}, ) assert create_res.status_code == 200 create_data = create_res.get_json() assert create_data["status"] == "ok" case_id = create_data["case_id"] parse_res = client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={"case_id": case_id, "raw_text": "客户:ACME 料号A001 x 10"}, ) assert parse_res.status_code == 200 assert parse_res.get_json()["status"] in {"ok", "needs_human"} inv_res = client.post( "/api/shipping-notice/tools/ensure_inventory", headers=headers, json={ "case_id": case_id, "inventory_lines": [{"sku": "A001", "available_qty": 10}], }, ) assert inv_res.status_code == 200 assert inv_res.get_json()["status"] == "ok" match_res = client.post( "/api/shipping-notice/tools/match_inventory", headers=headers, json={"case_id": case_id}, ) assert match_res.status_code == 200 assert match_res.get_json()["status"] == "ok" excel_res = client.post( "/api/shipping-notice/tools/generate_shipment_excel", headers=headers, json={"case_id": case_id}, ) assert excel_res.status_code == 200 assert excel_res.get_json()["status"] == "ok" with app.app_context(): case = ShippingCase.query.filter_by(case_no=case_id).first() assert case is not None assert case.export_file_path status_res = client.post( "/api/shipping-notice/tools/get_case_status", headers=headers, json={"case_id": case_id}, ) assert status_res.status_code == 200 assert status_res.get_json()["status"] == "ok" def test_shipping_notice_match_shortage_needs_human(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "ACME", "lines": [{"sku": "A001", "qty": 100, "uom": "EA"}], "confidence": 0.95, }, }, ) client.post( "/api/shipping-notice/tools/ensure_inventory", headers=headers, json={"case_id": case_id, "inventory_lines": [{"sku": "A001", "available_qty": 5}]}, ) match_res = client.post( "/api/shipping-notice/tools/match_inventory", headers=headers, json={"case_id": case_id}, ) data = match_res.get_json() assert data["status"] == "needs_human" assert "confirm" in (data.get("human_options") or []) confirm_res = client.post( "/api/shipping-notice/tools/submit_decision", headers=headers, json={"case_id": case_id, "decision": "confirm"}, ) assert confirm_res.get_json()["status"] == "ok" def test_openapi_spec_public(app, client): app.config["SHIPPING_NOTICE_TOOL_API_BASE"] = "http://tool-api.example.com" res = client.get("/api/shipping-notice/tools/openapi.json") assert res.status_code == 200 spec = res.get_json() assert "create_case" in str(spec) assert spec["servers"][0]["url"] == "http://tool-api.example.com/api/shipping-notice" props = spec["paths"]["/tools/create_case"]["post"]["requestBody"]["content"]["application/json"]["schema"]["properties"] assert "raw_content" in props assert "/rpa/" not in str(spec["paths"]) assert "/tools/parse_inventory" in spec["paths"] assert "/tools/get_inventory_schema_preview" in spec["paths"] assert "/tools/apply_inventory_mapping" in spec["paths"] def test_parse_inventory_from_chat_text(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "ACME", "lines": [{"sku": "A001", "qty": 10, "uom": "EA"}], "confidence": 0.95, }, }, ) inv_text = "料号\t可用数量\nA001\t10\nB002\t5" parse_inv = client.post( "/api/shipping-notice/tools/parse_inventory", headers=headers, json={"case_id": case_id, "raw_inventory_text": inv_text}, ) assert parse_inv.status_code == 200 data = parse_inv.get_json() assert data["status"] == "needs_human" assert data.get("checkpoint") == "inventory_field_mapping_review" assert data.get("stop_agent_turn") is True assert "match_inventory" in (data.get("forbidden_next_tools") or []) assert "field_alignment" in data["data"] confirm_res = client.post( "/api/shipping-notice/tools/submit_decision", headers=headers, json={"case_id": case_id, "decision": "confirm_inventory_mapping"}, ) assert confirm_res.get_json()["status"] == "ok" match_res = client.post( "/api/shipping-notice/tools/match_inventory", headers=headers, json={"case_id": case_id}, ) assert match_res.get_json()["status"] == "ok" def test_parse_inventory_empty_needs_human(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] res = client.post( "/api/shipping-notice/tools/parse_inventory", headers=headers, json={"case_id": case_id, "raw_inventory_text": " "}, ) assert res.get_json()["status"] == "needs_human" assert "reparse_inventory" in (res.get_json().get("human_options") or []) def test_match_by_aws_sku_and_reservation_id(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "AWS", "lines": [ {"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 2, "uom": "EA"}, {"sku": "10-40594-001", "reservation_id": "ORD-3002", "qty": 1, "uom": "EA"}, ], "confidence": 0.95, }, }, ) inv_text = ( "AWS Sku\tCOM ID\tQty Available\n" "10-40594-001\tAWS-311\t2\n" "10-40594-001\tORD-3002\t0\n" ) client.post( "/api/shipping-notice/tools/parse_inventory", headers=headers, json={"case_id": case_id, "raw_inventory_text": inv_text}, ) client.post( "/api/shipping-notice/tools/submit_decision", headers=headers, json={"case_id": case_id, "decision": "confirm_inventory_mapping"}, ) match_res = client.post( "/api/shipping-notice/tools/match_inventory", headers=headers, json={"case_id": case_id}, ) data = match_res.get_json() assert data["status"] == "needs_human" results = {r["reservation_id"]: r for r in data["data"]["line_results"]} assert results["AWS-311"]["line_status"] == "ok" assert results["ORD-3002"]["line_status"] == "shortage" def test_match_inventory_blocked_before_mapping_confirm(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "AWS", "lines": [{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 1, "uom": "EA"}], "confidence": 0.95, }, }, ) client.post( "/api/shipping-notice/tools/parse_inventory", headers=headers, json={ "case_id": case_id, "raw_inventory_text": "AWS Sku\tCOM ID\tQty Available\n10-40594-001\tAWS-311\t1\n", }, ) match_res = client.post( "/api/shipping-notice/tools/match_inventory", headers=headers, json={"case_id": case_id}, ) blocked = match_res.get_json() assert blocked["status"] == "needs_human" assert blocked.get("checkpoint") == "inventory_field_mapping_review" assert blocked.get("stop_agent_turn") is True assert "field_alignment" in blocked["data"] mapping = blocked["data"]["field_alignment"] assert mapping["inventory_columns"]["mapping"]["sku"]["source_column"] == "AWS Sku" assert mapping["inventory_columns"]["mapping"]["reservation_id"]["source_column"] == "COM ID" def test_correct_inventory_mapping_resets_to_inference(app, client, tmp_path): headers = _tool_headers(app) app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping") create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "AWS", "lines": [{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 1, "uom": "EA"}], "confidence": 0.95, }, }, ) client.post( "/api/shipping-notice/tools/parse_inventory", headers=headers, json={ "case_id": case_id, "raw_inventory_text": "AWS Sku\tCOM ID\tQty Available\n10-40594-001\tAWS-311\t1\n", }, ) correct_res = client.post( "/api/shipping-notice/tools/submit_decision", headers=headers, json={"case_id": case_id, "decision": "correct_inventory_mapping"}, ) data = correct_res.get_json() assert data["status"] == "ok" assert data.get("checkpoint") == "inventory_mapping_inference" assert "schema_preview" in data["data"] assert data["data"]["schema_preview"]["headers"] == ["AWS Sku", "COM ID", "Qty Available"] def test_rpa_inventory_callback(app, client, tmp_path, monkeypatch): headers = _tool_headers(app) drop = tmp_path / "drop" trigger = tmp_path / "triggers" drop.mkdir() trigger.mkdir() app.config["FREIGHT_INVENTORY_DROP_DIR"] = str(drop) app.config["FREIGHT_RPA_INVENTORY_TRIGGER_DIR"] = str(trigger) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] from openpyxl import Workbook inv_file = drop / "stock.xlsx" wb = Workbook() ws = wb.active ws.append(["料号", "可用数量"]) ws.append(["A001", 20]) wb.save(inv_file) wb.close() cb_res = client.post( "/api/shipping-notice/rpa/inventory-ready", headers=headers, json={"case_no": case_id, "success": True, "file_path": str(inv_file)}, ) assert cb_res.status_code == 200 data = cb_res.get_json() assert data["status"] == "needs_human" assert data["data"]["process_status"] == "inventory_ready" assert "confirm_inventory_mapping" in (data.get("human_options") or []) def test_rpa_upload_callback(app, client, tmp_path): headers = _tool_headers(app) queue = tmp_path / "queue" trigger = tmp_path / "upload_triggers" queue.mkdir() trigger.mkdir() app.config["FREIGHT_UPLOAD_QUEUE_DIR"] = str(queue) app.config["FREIGHT_RPA_UPLOAD_TRIGGER_DIR"] = str(trigger) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "ACME", "lines": [{"sku": "A001", "qty": 5, "uom": "EA"}], "confidence": 0.95, }, }, ) client.post( "/api/shipping-notice/tools/ensure_inventory", headers=headers, json={"case_id": case_id, "inventory_lines": [{"sku": "A001", "available_qty": 10}]}, ) client.post( "/api/shipping-notice/tools/match_inventory", headers=headers, json={"case_id": case_id}, ) client.post( "/api/shipping-notice/tools/generate_shipment_excel", headers=headers, json={"case_id": case_id}, ) cb_res = client.post( "/api/shipping-notice/rpa/upload-complete", headers=headers, json={"case_no": case_id, "success": True, "freight_ref_no": "FR-001"}, ) assert cb_res.status_code == 200 assert cb_res.get_json()["status"] == "ok" assert cb_res.get_json()["data"]["freight_ref_no"] == "FR-001" def test_upload_inventory_file_parses_server_side(app, client, tmp_path): headers = _tool_headers(app) app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping") app.config["SHIPPING_NOTICE_INVENTORY_TEXT_MAX_CHARS"] = 1000 create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "AWS", "lines": [{"sku": "10-40594-001", "reservation_id": "AWS-311", "qty": 1, "uom": "EA"}], "confidence": 0.95, }, }, ) from io import BytesIO from openpyxl import Workbook buf = BytesIO() wb = Workbook() ws = wb.active ws.append(["AWS Sku", "COM ID", "Qty Available"]) ws.append(["10-40594-001", "AWS-311", 5]) wb.save(buf) wb.close() buf.seek(0) upload_res = client.post( "/api/shipping-notice/tools/upload_inventory_file", headers={k: v for k, v in headers.items() if k.lower() != "content-type"}, data={ "case_id": case_id, "file": (buf, "inventory.xlsx"), }, ) assert upload_res.status_code == 200 upload_data = upload_res.get_json() assert upload_data["status"] == "ok" assert upload_data.get("checkpoint") == "inventory_mapping_inference" preview = upload_data["data"]["schema_preview"] assert preview["headers"] == ["AWS Sku", "COM ID", "Qty Available"] assert preview["suggested_mapping"]["sku"] == "AWS Sku" apply_res = client.post( "/api/shipping-notice/tools/apply_inventory_mapping", headers=headers, json={ "case_id": case_id, "column_mapping_json": preview["suggested_mapping"], }, ) apply_data = apply_res.get_json() assert apply_data["status"] == "needs_human" assert apply_data.get("checkpoint") == "inventory_field_mapping_review" assert apply_data["data"]["field_alignment"]["inventory_row_count"] == 1 def test_get_inventory_schema_preview(app, client, tmp_path): headers = _tool_headers(app) app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping") create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] missing = client.post( "/api/shipping-notice/tools/get_inventory_schema_preview", headers=headers, json={"case_id": case_id}, ) assert missing.get_json()["checkpoint"] == "inventory_upload_required" def test_apply_inventory_mapping_custom_headers(app, client, tmp_path): headers = _tool_headers(app) app.config["SHIPPING_NOTICE_STORAGE_ROOT"] = str(tmp_path / "shipping") create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] from io import BytesIO from openpyxl import Workbook buf = BytesIO() wb = Workbook() ws = wb.active ws.append(["Part No", "Resv", "On Hand"]) ws.append(["SKU-99", "R-1", 12]) wb.save(buf) wb.close() buf.seek(0) client.post( "/api/shipping-notice/tools/upload_inventory_file", headers={k: v for k, v in headers.items() if k.lower() != "content-type"}, data={"case_id": case_id, "file": (buf, "inv.xlsx")}, ) apply_res = client.post( "/api/shipping-notice/tools/apply_inventory_mapping", headers=headers, json={ "case_id": case_id, "column_mapping_json": { "sku": "Part No", "reservation_id": "Resv", "available_qty": "On Hand", }, }, ) apply_data = apply_res.get_json() assert apply_data["status"] == "needs_human" assert apply_data["data"]["inventory_row_count"] == 1 sample = apply_data["data"]["field_alignment"]["sample_inventory_lines"][0] assert sample["sku"] == "SKU-99" assert sample["available_qty"] == 12 def test_confirm_parse_does_not_skip_inventory_match(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "structured": { "customer_name": "Wistron", "lines": [{"sku": "504-010213-001", "qty": 9, "uom": "EA"}], "confidence": 0.95, }, }, ) confirm_res = client.post( "/api/shipping-notice/tools/submit_decision", headers=headers, json={"case_id": case_id, "decision": "confirm"}, ) confirm_data = confirm_res.get_json() assert confirm_data["status"] == "ok" assert confirm_data.get("checkpoint") == "parse_confirmed" assert "generate_shipment_excel" in (confirm_data.get("forbidden_next_tools") or []) excel_res = client.post( "/api/shipping-notice/tools/generate_shipment_excel", headers=headers, json={"case_id": case_id}, ) excel_data = excel_res.get_json() assert excel_data["status"] == "failed" assert "匹配" in excel_data["message"] with app.app_context(): case = ShippingCase.query.filter_by(case_no=case_id).first() assert case is not None assert case.process_status == "parsed" assert case.match_status is None def test_submit_decision_by_conversation_id(app, client): headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual", "conversation_id": "conv-test-001"}, ) case_id = create_res.get_json()["case_id"] client.post( "/api/shipping-notice/tools/parse_notice", headers=headers, json={ "case_id": case_id, "conversation_id": "conv-test-001", "structured": { "customer_name": "Wistron", "lines": [{"sku": "504-010213-001", "qty": 9, "uom": "EA"}], "confidence": 0.95, }, }, ) confirm_res = client.post( "/api/shipping-notice/tools/submit_decision", headers=headers, json={"conversation_id": "conv-test-001", "decision": "confirm"}, ) confirm_data = confirm_res.get_json() assert confirm_data["status"] == "ok" assert confirm_data["case_id"] == case_id assert confirm_data.get("checkpoint") == "parse_confirmed" dup_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual", "conversation_id": "conv-test-001"}, ) dup_data = dup_res.get_json() assert dup_data["case_id"] == case_id assert dup_data.get("checkpoint") == "case_already_exists" def test_parse_inventory_rejects_oversized_text(app, client): headers = _tool_headers(app) app.config["SHIPPING_NOTICE_INVENTORY_TEXT_MAX_CHARS"] = 100 create_res = client.post( "/api/shipping-notice/tools/create_case", headers=headers, json={"source_type": "manual"}, ) case_id = create_res.get_json()["case_id"] res = client.post( "/api/shipping-notice/tools/parse_inventory", headers=headers, json={"case_id": case_id, "raw_inventory_text": "x" * 200}, ) data = res.get_json() assert data["status"] == "needs_human" assert data.get("checkpoint") == "inventory_upload_required" assert "过大" in data["message"] def test_chat_actions_empty_when_case_rejected(app): from app.services.shipping_notice.chat_actions import build_chat_actions_payload, human_options_for_case with app.app_context(): case = ShippingCase( case_no="SN-20260101-TEST0001", source_type="manual", process_status="rejected", review_status="rejected", ) assert human_options_for_case(case) == [] payload = build_chat_actions_payload(case) assert payload["checkpoint"] == "rejected" assert payload["action_buttons"] == []