test_skill_service_api.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from pathlib import Path
  2. from tests.conftest import (
  3. build_fastapi_test_client,
  4. build_postgres_database_url,
  5. build_postgres_engine,
  6. prepare_known_service_import,
  7. )
  8. def test_skill_service_post_contract_matches_frontend(
  9. tmp_path: Path,
  10. monkeypatch,
  11. ) -> None:
  12. prepare_known_service_import("skill-service")
  13. from app.bootstrap.app import create_app
  14. from app.db.models import Base
  15. from core_db import create_session_factory
  16. database_url = build_postgres_database_url(tmp_path, "skill-api")
  17. monkeypatch.setenv("AGENT_PLATFORM_DATABASE_URL", database_url)
  18. engine = build_postgres_engine(database_url)
  19. Base.metadata.create_all(engine)
  20. app = create_app()
  21. app.state.session_factory = create_session_factory(engine)
  22. client = build_fastapi_test_client(app)
  23. create_response = client.post(
  24. "/skills/create",
  25. json={
  26. "name": "订单查询",
  27. "description": "查询订单并组织回复",
  28. "category": "service",
  29. "instruction": "使用订单工具查询状态,然后回复客户。",
  30. "toolIds": ["tool_lookup_order"],
  31. },
  32. )
  33. assert create_response.status_code == 200
  34. skill = create_response.json()["data"]
  35. assert skill["name"] == "订单查询"
  36. assert skill["category"] == "service"
  37. assert skill["toolIds"] == ["tool_lookup_order"]
  38. assert skill["createdTime"]
  39. assert "code" not in skill
  40. assert "skill_type" not in skill
  41. list_response = client.post(
  42. "/skills/list",
  43. json={"page": 1, "pageSize": 20, "keyword": "订单"},
  44. )
  45. assert list_response.status_code == 200
  46. list_payload = list_response.json()["data"]
  47. assert list_payload["total"] == 1
  48. assert list_payload["items"][0]["id"] == skill["id"]
  49. update_response = client.post(
  50. "/skills/update",
  51. json={
  52. "skillId": skill["id"],
  53. "name": "订单状态查询",
  54. "instruction": "先查询订单,再解释当前状态。",
  55. "toolIds": ["tool_lookup_order", "tool_create_ticket"],
  56. },
  57. )
  58. assert update_response.status_code == 200
  59. updated = update_response.json()["data"]
  60. assert updated["name"] == "订单状态查询"
  61. assert updated["toolIds"] == ["tool_lookup_order", "tool_create_ticket"]
  62. assert skill["runtimeType"] == "template"
  63. assert skill["implementation"]["template"]
  64. install_response = client.post(
  65. "/skills/installations/create",
  66. json={
  67. "skillId": skill["id"],
  68. "installScope": "global",
  69. "scopeId": "global",
  70. "config": {"priority": "normal"},
  71. },
  72. )
  73. assert install_response.status_code == 200
  74. installation = install_response.json()["data"]
  75. assert installation["skillId"] == skill["id"]
  76. assert installation["status"] == "installed"
  77. installations_response = client.post(
  78. "/skills/installations/list",
  79. json={"page": 1, "pageSize": 20, "installScope": "global"},
  80. )
  81. assert installations_response.status_code == 200
  82. assert installations_response.json()["data"]["total"] == 1
  83. run_create_response = client.post(
  84. "/skills/runs/create",
  85. json={
  86. "skillId": skill["id"],
  87. "installationId": installation["id"],
  88. "input": {"orderNo": "SO-1001"},
  89. },
  90. )
  91. assert run_create_response.status_code == 200
  92. run = run_create_response.json()["data"]
  93. assert run["skillId"] == skill["id"]
  94. assert run["installationId"] == installation["id"]
  95. assert run["input"] == {"orderNo": "SO-1001"}
  96. assert "skill_id" not in run
  97. assert "input_json" not in run
  98. run_execute_response = client.post(
  99. "/skills/runs/execute",
  100. json={"skillRunId": run["id"], "workerKey": "pytest-worker"},
  101. )
  102. assert run_execute_response.status_code == 200
  103. executed_run = run_execute_response.json()["data"]
  104. assert executed_run["status"] == "completed"
  105. assert executed_run["workerKey"] == "pytest-worker"
  106. assert executed_run["finishedTime"]
  107. assert "worker_key" not in executed_run
  108. delete_response = client.post("/skills/delete", json={"skillId": skill["id"]})
  109. assert delete_response.status_code == 200
  110. assert delete_response.json()["data"] == {"deleted": True, "skillId": skill["id"], "installationId": None}