test_team_service.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from pathlib import Path
  2. from tests.conftest import build_fastapi_test_client, prepare_known_service_import
  3. def test_team_service_post_contract_supports_team_configs_and_runs(
  4. tmp_path: Path,
  5. monkeypatch,
  6. ) -> None:
  7. prepare_known_service_import("team-service")
  8. from app.bootstrap.app import create_app
  9. from app.db.models import Base
  10. from core_db import create_session_factory
  11. from sqlalchemy import create_engine
  12. database_url = f"sqlite:///{tmp_path / 'teams.db'}"
  13. monkeypatch.setenv("AGENT_PLATFORM_DATABASE_URL", database_url)
  14. monkeypatch.setenv("AGENT_PLATFORM_REDIS_URL", "")
  15. monkeypatch.setenv("AGENT_PLATFORM_AUTO_WORKER_ENABLED", "false")
  16. engine = create_engine(database_url, connect_args={"check_same_thread": False})
  17. Base.metadata.create_all(engine)
  18. app = create_app()
  19. app.state.session_factory = create_session_factory(engine)
  20. client = build_fastapi_test_client(app)
  21. team_response = client.post(
  22. "/teams/create",
  23. json={
  24. "name": "Support Team",
  25. "description": "Handles support escalations",
  26. "teamType": "collaborative",
  27. "ownerUserId": "demo-user",
  28. },
  29. )
  30. assert team_response.status_code == 200
  31. team_payload = team_response.json()["data"]
  32. assert team_payload["name"] == "Support Team"
  33. assert team_payload["teamType"] == "collaborative"
  34. assert "code" not in team_payload
  35. config_response = client.post(
  36. "/teams/configs/create",
  37. json={
  38. "teamId": team_payload["id"],
  39. "coordinationMode": "supervisor",
  40. "objective": "Resolve the customer request",
  41. "memberRefs": [
  42. {
  43. "role": "worker",
  44. "agentId": "agent_support",
  45. "responsibility": "Draft the answer",
  46. }
  47. ],
  48. "policy": {
  49. "max_rounds": 3,
  50. "handoff": "supervisor",
  51. },
  52. },
  53. )
  54. assert config_response.status_code == 200
  55. config_payload = config_response.json()["data"]
  56. assert config_payload["teamId"] == team_payload["id"]
  57. assert config_payload["memberRefs"][0]["role"] == "executor"
  58. assert config_payload["memberRefs"][0]["member_key"] == "member_1"
  59. list_response = client.post(
  60. "/teams/list",
  61. json={"page": 1, "pageSize": 20, "keyword": "support"},
  62. )
  63. assert list_response.status_code == 200
  64. assert list_response.json()["data"]["total"] == 1
  65. configs_response = client.post(
  66. "/teams/configs/list",
  67. json={"page": 1, "pageSize": 20, "teamId": team_payload["id"]},
  68. )
  69. assert configs_response.status_code == 200
  70. assert configs_response.json()["data"]["total"] == 1
  71. run_response = client.post(
  72. "/teams/runs/create",
  73. json={
  74. "teamId": team_payload["id"],
  75. "teamConfigId": config_payload["id"],
  76. "inputText": "Help the customer reset MFA",
  77. },
  78. )
  79. assert run_response.status_code == 200
  80. run_payload = run_response.json()["data"]
  81. assert run_payload["teamId"] == team_payload["id"]
  82. assert run_payload["teamConfigId"] == config_payload["id"]
  83. assert run_payload["status"] == "queued"
  84. status_response = client.post(
  85. "/teams/runs/status",
  86. json={
  87. "teamRunId": run_payload["id"],
  88. "status": "completed",
  89. "workerKey": "test-worker",
  90. "outputText": "MFA reset steps prepared.",
  91. },
  92. )
  93. assert status_response.status_code == 200
  94. assert status_response.json()["data"]["status"] == "completed"
  95. runs_response = client.post(
  96. "/teams/runs/list",
  97. json={"page": 1, "pageSize": 20, "teamId": team_payload["id"]},
  98. )
  99. assert runs_response.status_code == 200
  100. assert runs_response.json()["data"]["total"] == 1
  101. update_response = client.post(
  102. "/teams/update",
  103. json={
  104. "teamId": team_payload["id"],
  105. "name": "Support Team Updated",
  106. "status": "active",
  107. },
  108. )
  109. assert update_response.status_code == 200
  110. assert update_response.json()["data"]["name"] == "Support Team Updated"
  111. assert update_response.json()["data"]["status"] == "active"
  112. delete_run_response = client.post(
  113. "/teams/runs/delete",
  114. json={"teamRunId": run_payload["id"]},
  115. )
  116. assert delete_run_response.status_code == 200
  117. assert delete_run_response.json()["data"]["deleted"] is True
  118. delete_config_response = client.post(
  119. "/teams/configs/delete",
  120. json={"configId": config_payload["id"]},
  121. )
  122. assert delete_config_response.status_code == 200
  123. assert delete_config_response.json()["data"]["deleted"] is True
  124. delete_team_response = client.post(
  125. "/teams/delete",
  126. json={"teamId": team_payload["id"]},
  127. )
  128. assert delete_team_response.status_code == 200
  129. assert delete_team_response.json()["data"]["deleted"] is True