conftest.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """pytest 配置:对接 PC Express API(见 doc/开发/PC后端接口文档.md)"""
  2. from __future__ import annotations
  3. import os
  4. import uuid
  5. import pytest
  6. import requests
  7. PC_BASE = os.environ.get("PC_API_BASE_URL", "http://localhost:3101/api").rstrip("/")
  8. MOBILE_BASE = os.environ.get("MOBILE_API_BASE_URL", "http://localhost:3201/api").rstrip("/")
  9. RUN_INTEGRATION = os.environ.get("RUN_INTEGRATION", "1") != "0"
  10. def _server_alive(base: str) -> bool:
  11. try:
  12. r = requests.get(f"{base}/health", timeout=3)
  13. return r.status_code == 200
  14. except requests.RequestException:
  15. return False
  16. @pytest.fixture(scope="session")
  17. def pc_base() -> str:
  18. return PC_BASE
  19. @pytest.fixture(scope="session")
  20. def mobile_base() -> str:
  21. return MOBILE_BASE
  22. @pytest.fixture(scope="session")
  23. def require_pc_server(pc_base: str):
  24. if RUN_INTEGRATION and not _server_alive(pc_base):
  25. pytest.skip(
  26. f"PC 服务未启动,请先执行: cd backend && pnpm dev ({pc_base}/health)"
  27. )
  28. return pc_base
  29. @pytest.fixture
  30. def api(require_pc_server: str):
  31. from client import ApiClient
  32. return ApiClient(require_pc_server)
  33. @pytest.fixture(scope="session")
  34. def api_session(require_pc_server: str):
  35. from client import ApiClient
  36. return ApiClient(require_pc_server)
  37. @pytest.fixture
  38. def uid() -> str:
  39. """每条用例唯一后缀,避免内存库重复冲突"""
  40. return uuid.uuid4().hex[:8]
  41. # ---------- QiWe 真实联调(需 backend/.env:QIWEI_TOKEN + QIWEI_GUID)----------
  42. def _load_env() -> dict[str, str]:
  43. from load_env import load_backend_env
  44. return load_backend_env()
  45. def _parse_room_ids(env: dict[str, str]) -> list[str]:
  46. raw = env.get("QIWEI_ROOM_IDS", "").strip()
  47. if not raw:
  48. return []
  49. return [x.strip() for x in raw.split(",") if x.strip()]
  50. @pytest.fixture(scope="session")
  51. def live_guid(require_pc_server: str) -> str:
  52. guid = _load_env().get("QIWEI_GUID", "").strip()
  53. if not guid:
  54. pytest.skip(
  55. "未配置 QIWEI_GUID:在 backend/.env 填写 "
  56. "QIWEI_GUID=设备ID(https://manager.qiweapi.com/nodes)"
  57. )
  58. return guid
  59. @pytest.fixture(scope="session")
  60. def live_room_ids(api_session, live_guid: str) -> list[str]:
  61. """优先读 .env 的 QIWEI_ROOM_IDS;否则 rooms/sync(getRoomList)"""
  62. from_env = _parse_room_ids(_load_env())
  63. if from_env:
  64. return from_env
  65. data = api_session.assert_success(
  66. api_session.post("/rooms/sync", json={"guid": live_guid, "nextStartIndex": 0})
  67. )
  68. rooms = data.get("rooms") or []
  69. if not rooms:
  70. pytest.skip(
  71. "无 roomId:在 backend/.env 配置 QIWEI_ROOM_IDS=群id1,群id2 "
  72. "(从 getSessionList 里 sessionType=1 的 sessionId 复制)"
  73. )
  74. return [str(r["roomId"]) for r in rooms]
  75. @pytest.fixture(scope="session")
  76. def live_room_id(live_room_ids: list[str]) -> str:
  77. return live_room_ids[0]
  78. @pytest.fixture(scope="session")
  79. def live_rooms_seeded(api_session, live_guid: str, live_room_ids: list[str]) -> list[str]:
  80. """用 QIWEI_ROOM_IDS 写入本方群库(batchGetRoomDetail),供 GET /api/rooms 联调"""
  81. data = api_session.assert_success(
  82. api_session.post(
  83. "/rooms/sync",
  84. json={
  85. "guid": live_guid,
  86. "nextStartIndex": 0,
  87. "roomIdList": live_room_ids,
  88. },
  89. )
  90. )
  91. synced = [str(r["roomId"]) for r in data.get("rooms") or []]
  92. assert synced, "按 roomIdList 同步后 rooms 仍为空,请检查 QiWe 或 roomId 是否正确"
  93. return synced