""" backend/tests/test_openapi_snapshots.py 用途:校验 docs/openapi 下的静态快照与后端 OpenAPI builder 输出一致。 依赖:shipping_notice.open_api_spec、agent_runtime_context.open_api_spec、标准 json/pathlib。 逻辑:直接 import builder 生成动态字典,再与静态 JSON 做对象级比对,避免 Dify 导入文件漂移。 """ from __future__ import annotations import json from pathlib import Path from app.services.agent_runtime_context.openapi_spec import build_agent_runtime_context_openapi_spec from app.services.shipping_notice.openapi_spec import build_dify_openapi_spec DEFAULT_BASE_URL = "http://192.168.36.108:5052" ROOT = Path(__file__).resolve().parents[2] OPENAPI_DIR = ROOT / "docs" / "openapi" def _load_snapshot(name: str) -> dict: """ 读取静态 OpenAPI 快照。 Args: name: docs/openapi 下的文件名。 Returns: 解析后的 JSON 对象。 """ return json.loads((OPENAPI_DIR / name).read_text(encoding="utf-8")) def test_shipping_notice_snapshot_matches_builder_output(): """发货通知静态快照应与当前 builder 输出完全一致。""" snapshot = _load_snapshot("shipping_notice_tools.dify.json") generated = build_dify_openapi_spec(DEFAULT_BASE_URL) assert snapshot == generated assert snapshot["paths"]["/tools/submit_decision"]["post"]["operationId"] == "submit_decision" snapshot_text = json.dumps(snapshot, ensure_ascii=False) assert "reference_context" in snapshot_text assert "状态机" in snapshot_text assert "唯一 task_id" in snapshot_text assert "不得自行宣布完成" in snapshot_text def test_agent_runtime_context_snapshot_matches_builder_output(): """运行态上下文静态快照应与当前 builder 输出完全一致。""" snapshot = _load_snapshot("agent_runtime_context_tools.dify.json") generated = build_agent_runtime_context_openapi_spec(DEFAULT_BASE_URL) assert snapshot == generated assert snapshot["paths"]["/tools/parse"]["post"]["operationId"] == "parse_agent_runtime_context" snapshot_text = json.dumps(snapshot, ensure_ascii=False) assert "reference_context" in snapshot_text assert "状态机" in snapshot_text assert "追问" in snapshot_text