"""shipping_notice batch tests. 覆盖 batch 新增能力的最小闭环: 1. 单/多通知可通过 create_batch 一次建批并生成多 task。 2. get_batch_status 返回 focus task 与任务列表。 3. switch_focus_task 可在同批次内切换当前任务。 4. 工作台批量上传通知时仅保存 raw_text,首轮解析延后到 Dify。 """ from app import db from app.models import ShippingBatch, ShippingTask, User from app.auth_jwt import issue_local_access_token def _tool_headers(app): app.config["INTEGRATION_API_KEY"] = "test-integration-key" return { "Content-Type": "application/json", "X-Integration-Key": "test-integration-key", } def _workspace_headers(app): with app.app_context(): user = User( username="shipping_workspace_user", email="shipping_workspace_user@example.com", role="user", status="active", ) user.set_password("password") db.session.add(user) db.session.commit() token = issue_local_access_token(user) return { "Authorization": f"Bearer {token}", } def test_create_batch_tasks_and_switch_focus(app, client): """验证 batch 建单、状态查询、focus task 切换主流程。""" headers = _tool_headers(app) create_res = client.post( "/api/shipping-notice/tools/create_batch", headers=headers, json={ "source_type": "manual", "notice_items": [ { "source_file_name": "notice-a.txt", "raw_text": "客户:ACME 料号A001 x 10", "structured": { "customer_name": "ACME", "lines": [{"sku": "A001", "reservation_id": "R-1", "qty": 10, "uom": "EA"}], "confidence": 0.95, }, }, { "source_file_name": "notice-b.txt", "raw_text": "客户:Wistron 料号B002 x 5", "structured": { "customer_name": "Wistron", "lines": [{"sku": "B002", "reservation_id": "R-2", "qty": 5, "uom": "EA"}], "confidence": 0.95, }, }, ], }, ) assert create_res.status_code == 200 create_data = create_res.get_json() assert create_data["status"] == "ok" batch = create_data["data"]["batch"] tasks = create_data["data"]["tasks"] batch_no = batch["batch_no"] assert batch_no.startswith("SB-") assert len(tasks) == 2 assert batch["current_focus_task_no"] == tasks[0]["task_id"] assert batch["process_summary"]["total"] == 2 status_res = client.post( "/api/shipping-notice/tools/get_batch_status", headers=headers, json={"batch_no": batch_no}, ) assert status_res.status_code == 200 status_data = status_res.get_json() assert status_data["status"] == "ok" assert status_data["data"]["batch_no"] == batch_no assert len(status_data["data"]["tasks"]) == 2 assert status_data["data"]["focus_task"] is None assert status_data["data"]["tasks"][0]["task_no"] == tasks[0]["task_id"] target_task_no = tasks[1]["task_id"] switch_res = client.post( "/api/shipping-notice/tools/switch_focus_task", headers=headers, json={"batch_no": batch_no, "task_no": target_task_no}, ) assert switch_res.status_code == 200 switch_data = switch_res.get_json() assert switch_data["status"] == "ok" assert switch_data["task_no"] == target_task_no assert switch_data["data"]["batch"]["current_focus_task_no"] == target_task_no assert switch_data["data"]["focus_task"]["task_no"] == target_task_no with app.app_context(): batch_row = ShippingBatch.query.filter_by(batch_no=batch_no).first() task_rows = ShippingTask.query.filter_by(batch_id=batch_row.id).order_by(ShippingTask.batch_index.asc()).all() assert batch_row is not None assert len(task_rows) == 2 assert batch_row.current_focus_task_no == target_task_no assert task_rows[0].batch_total == 2 assert task_rows[1].batch_total == 2 def test_create_batch_defers_parse_without_structured(app, client): """默认将 parse_notice 延后到 Dify,仅保存 raw_text。""" headers = _tool_headers(app) res = client.post( "/api/shipping-notice/tools/create_batch", headers=headers, json={ "notice_items": [ { "source_file_name": "notice-a.txt", "raw_text": "客户:ACME 料号A001 x 10", }, ], }, ) assert res.status_code == 200 data = res.get_json() assert data["data"]["tasks"][0]["parse_status"] == "deferred" with app.app_context(): task = ShippingTask.query.filter_by(task_no=data["data"]["tasks"][0]["task_id"]).first() assert task is not None assert task.raw_content assert not (task.lines_json or []) def test_workspace_create_batch_from_notices_defers_parse_to_dify(app, client): """聊天/工作台批量上传通知时应仅落 raw_text,首轮解析延后到 Dify。""" headers = _workspace_headers(app) res = client.post( "/api/workspace/shipping-batches/create-from-notices", headers=headers, data={ "files": (__import__("io").BytesIO("客户:ACME\nA001 x 10".encode("utf-8")), "notice-a.txt"), "conversation_id": "conv-workspace-batch-001", }, content_type="multipart/form-data", ) assert res.status_code == 200 data = res.get_json() assert data["status"] == "ok" assert data["batch_no"].startswith("SB-") assert data["data"]["batch"]["batch_no"] == data["batch_no"] assert data["data"]["tasks"][0]["parse_status"] == "deferred" assert data["data"]["focus_task"]["task_no"] == data["data"]["tasks"][0]["task_id"] with app.app_context(): task = ShippingTask.query.filter_by(task_no=data["data"]["tasks"][0]["task_id"]).first() assert task is not None assert task.raw_content assert task.process_status == "created" assert len(task.lines_json or []) == 0 def test_create_empty_batch_returns_batch_shell(app, client): """验证无 notice_items 时仍可创建批次壳,供后续续办。""" headers = _tool_headers(app) res = client.post( "/api/shipping-notice/tools/create_batch", headers=headers, json={"file_count": 1}, ) assert res.status_code == 200 data = res.get_json() assert data["status"] == "ok" assert data["batch_no"].startswith("SB-") assert data["data"]["file_count"] == 1