feat(shipping-notice): 添加订单文档 Profile 盲测集和样本管理功能,支持创建、执行和批准盲测,增强订单冻结和库存核验功能

This commit is contained in:
hugo committed 2026-07-23 00:15:34 +08:00
1 parent c35223d49d
commit abd8fc1e16
98 files changed
+4629 -528

No files matched your search

+75 -12
View File
@@ -30,6 +30,7 @@ from app.models import (
ShippingOrderExtraction,
ShippingOrderFieldDecision,
ShippingOrderFieldValue,
ShippingOrderSnapshot,
ShippingTask,
ShippingTaskEvent,
ShippingTemporaryInventorySession,
@@ -418,6 +419,59 @@ def test_workspace_create_batch_from_notices_defers_parse_to_dify(app, client):
assert len(task.lines_json or []) == 0
def test_workspace_create_batch_rejects_configured_file_limit_before_intake(app, client, tmp_path):
"""超过请求级文件数限制必须明确失败,且不创建批次、任务或临时上传目录。"""
headers = _workspace_headers(app)
app.config['SHIPPING_NOTICE_BATCH_MAX_FILES'] = 1
app.config['SHIPPING_NOTICE_STORAGE_ROOT'] = str(tmp_path)
with app.app_context():
batch_count_before = ShippingBatch.query.count()
task_count_before = ShippingTask.query.count()
response = client.post(
'/api/workspace/shipping-batches/create-from-notices',
headers=headers,
data={
'files': [
(BytesIO(b'first'), 'notice-a.txt'),
(BytesIO(b'second'), 'notice-b.txt'),
],
},
content_type='multipart/form-data',
)
assert response.status_code == 400
payload = response.get_json()
# 关键断言:请求在抽取前被阻断,避免超限文件占用临时目录或部分建单。
assert payload['error']['code'] == 'SHIPPING_NOTICE.BATCH_FILE_LIMIT_EXCEEDED'
assert payload['error']['details'] == {'max_files': 1, 'received_files': 2}
with app.app_context():
assert ShippingBatch.query.count() == batch_count_before
assert ShippingTask.query.count() == task_count_before
assert not (tmp_path / 'notice_uploads' / '_tmp').exists()
def test_workspace_inventory_upload_requires_authenticated_task_and_file(app, client):
"""独立工作台库存上传端点必须保持任务归属校验,并拒绝缺少 multipart 文件的调用。"""
headers = _workspace_headers(app)
response = client.post('/api/workspace/shipping-tasks/SN-NOT-OWNED/upload-inventory', headers=headers)
assert response.status_code == 404
with app.app_context():
user = User.query.filter_by(username='shipping_workspace_user').order_by(User.id.desc()).first()
assert user is not None
task = ShippingTask(task_no='SN-WORKSPACE-INVENTORY-UPLOAD', user_id=user.id)
db.session.add(task)
db.session.commit()
response = client.post(
'/api/workspace/shipping-tasks/SN-WORKSPACE-INVENTORY-UPLOAD/upload-inventory', headers=headers,
data={}, content_type='multipart/form-data',
)
assert response.status_code == 400
assert response.get_json()['error']['message'] == 'file is required'
def test_workspace_create_batch_from_notices_starts_orchestrator_when_enabled(app, client, monkeypatch):
"""开启编排器时,工作台上传接口应返回 orchestrator_started。"""
headers = _workspace_headers(app)
@@ -627,8 +681,8 @@ def test_workspace_replace_notice_overwrites_current_task_without_new_batch(app,
assert task.export_file_name is None
def test_auto_progress_inventory_trigger_failure_marks_step3(app, tmp_path):
"""库存自动获取失败时应进入第 3 步库存准备,而不是继续显示解析确认。"""
def test_auto_progress_requires_order_freeze_before_inventory_trigger(app, tmp_path):
"""关键断言:库存自动推进前必须先阻断到订单冻结,不能消费动态订单投影。"""
app.config["SHIPPING_NOTICE_AUTO_PROGRESS_ENABLED"] = True
app.config["SHIPPING_NOTICE_AUTO_RPA_INVENTORY_ENABLED"] = True
app.config["SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR"] = str(tmp_path / "empty_inventory_drop")
@@ -664,16 +718,13 @@ def test_auto_progress_inventory_trigger_failure_marks_step3(app, tmp_path):
db.session.refresh(task)
assert result["status"] == "needs_human"
assert result["checkpoint"] == "upload_req"
assert task.process_status == "waiting_inventory"
assert task.pending_human_reason == "inv_unavail"
assert task.current_step == "step3_inventory_prepare"
assert task.step_payload_json["current_step_progress"]["status"] == "needs_human"
assert result["checkpoint"] == "order_freeze"
assert task.process_status == "parsed"
from app.services.shipping_notice.ui_state import build_task_ui_state
ui_state = build_task_ui_state(task)
inventory_step = next(step for step in ui_state["steps"] if step["code"] == "inventory_prepare")
assert inventory_step["status"] == "blocked"
assert ui_state["checkpoint"] == "order_freeze"
assert ui_state["current_step"]["code"] == "parse_confirm"
def test_workspace_task_detail_is_read_only_for_timed_out_inventory_rpa(app, client, tmp_path):
@@ -736,8 +787,8 @@ def test_workspace_task_detail_is_read_only_for_timed_out_inventory_rpa(app, cli
assert not list((tmp_path / "inventory_triggers").glob("inventory_export_*.json"))
def test_auto_progress_resolves_dub_source_without_implicitly_triggering_rpa(app, tmp_path):
"""自动推进可解析 Dublin 库存源,但正式 RPA 刷新必须由用户明确触发。"""
def test_auto_progress_requires_order_freeze_before_inventory_source_resolution(app, tmp_path):
"""关键断言:库存源解析也不得绕过冻结 Snapshot 门禁。"""
app.config["SHIPPING_NOTICE_AUTO_PROGRESS_ENABLED"] = True
app.config["SHIPPING_NOTICE_AUTO_RPA_INVENTORY_ENABLED"] = True
app.config["SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR"] = str(tmp_path / "empty_inventory_drop")
@@ -769,7 +820,8 @@ def test_auto_progress_resolves_dub_source_without_implicitly_triggering_rpa(app
db.session.refresh(task)
assert result["status"] == "needs_human"
assert result["checkpoint"] == "upload_req"
assert result["checkpoint"] == "order_freeze"
# 预置的历史 Source 提示可以保留,但不可据此触发库存/RPA。
assert task.inventory_source_code == "DSV360_AWSDU"
trigger_path = tmp_path / "inventory_triggers" / f"inventory_export_{task.task_no}.json"
assert not trigger_path.exists()
@@ -845,6 +897,17 @@ def test_inventory_source_rules_resolve_configured_dublin_source(app):
value_json='DUB',
))
task.current_order_extraction_id = extraction.id
snapshot = ShippingOrderSnapshot(
task_id=task.id,
extraction_id=extraction.id,
schema_version_id=published['id'],
order_data_version=1,
header_values_json={str(destination_field['id']): 'DUB'},
rule_versions_json={},
)
db.session.add(snapshot)
db.session.flush()
task.order_snapshot_id = snapshot.id
db.session.add(ShippingInventoryRoutingRule(
rule_code='BATCH_TEST_DUB',
rule_name='批次测试 Dublin 路由',