75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""
|
||
tests/test_shipping_notice_openapi_contract.py
|
||
|
||
用途:锁定发货通知聊天 Tool 边界与库存连接器 OpenAPI 命名契约。
|
||
依赖:Flask 测试 client、静态 Dify OpenAPI JSON。
|
||
逻辑:分别校验动态 builder 与静态快照,确保旧订单抽取 Tool 不再暴露且连接器契约不漂移。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
from pathlib import Path
|
||
from typing import Any
|
||
|
||
|
||
def _assert_connector_contract(spec: dict[str, Any]) -> None:
|
||
"""
|
||
校验订单抽取 Tool 已下线且库存连接器字段边界保持稳定。
|
||
|
||
核心逻辑:订单行只接受动态 fields 数组,每个字段携带稳定 field_id 和 observations;
|
||
任务状态说明使用 inventory_connector_type,并拒绝旧 inventory_source_type 命名。
|
||
|
||
Args:
|
||
spec: 动态接口或静态快照读取出的 OpenAPI 对象。
|
||
|
||
Returns:
|
||
None;契约不符时由 pytest assertion 报错。
|
||
"""
|
||
paths = spec['paths']
|
||
assert '/tools/get_order_extraction_context' not in paths
|
||
assert '/tools/submit_order_extraction' not in paths
|
||
|
||
status_description = spec["paths"]["/tools/get_task_status"]["post"]["description"]
|
||
assert "inventory_connector_type" in status_description
|
||
assert "inventory_source_type" not in status_description
|
||
|
||
confirm_description = spec["paths"]["/tools/confirm_inventory_source"]["post"]["description"]
|
||
assert "connector_type" in confirm_description
|
||
assert "source_code、source_type" not in confirm_description
|
||
|
||
|
||
def test_dynamic_openapi_uses_connector_contract(app, client) -> None:
|
||
"""
|
||
校验公开 OpenAPI 接口使用新的连接器命名。
|
||
|
||
核心逻辑:通过 Flask 路由获取 builder 实时结果,再复用统一契约断言。
|
||
|
||
Args:
|
||
app: pytest Flask 应用 fixture。
|
||
client: pytest Flask 测试客户端 fixture。
|
||
|
||
Returns:
|
||
None。
|
||
"""
|
||
app.config["HXOPS_TOOLS_API_BASE"] = "http://tool-api.example.com"
|
||
response = client.get("/api/shipping-notice/tools/openapi.json")
|
||
|
||
assert response.status_code == 200
|
||
_assert_connector_contract(response.get_json())
|
||
|
||
|
||
def test_static_openapi_uses_connector_contract() -> None:
|
||
"""
|
||
校验仓库脚本生成的 Dify 静态快照与动态契约一致。
|
||
|
||
核心逻辑:读取 docs/openapi 下的 JSON,并复用统一契约断言。
|
||
|
||
Returns:
|
||
None。
|
||
"""
|
||
spec_path = Path(__file__).resolve().parents[2] / "docs" / "openapi" / "shipping_notice_tools.dify.json"
|
||
spec = json.loads(spec_path.read_text(encoding="utf-8"))
|
||
|
||
_assert_connector_contract(spec)
|