| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- """pytest 配置:对接 PC Express API(见 doc/开发/PC后端接口文档.md)"""
- from __future__ import annotations
- import os
- import uuid
- import pytest
- import requests
- PC_BASE = os.environ.get("PC_API_BASE_URL", "http://localhost:3101/api").rstrip("/")
- MOBILE_BASE = os.environ.get("MOBILE_API_BASE_URL", "http://localhost:3201/api").rstrip("/")
- RUN_INTEGRATION = os.environ.get("RUN_INTEGRATION", "1") != "0"
- def _server_alive(base: str) -> bool:
- try:
- r = requests.get(f"{base}/health", timeout=3)
- return r.status_code == 200
- except requests.RequestException:
- return False
- @pytest.fixture(scope="session")
- def pc_base() -> str:
- return PC_BASE
- @pytest.fixture(scope="session")
- def mobile_base() -> str:
- return MOBILE_BASE
- @pytest.fixture(scope="session")
- def require_pc_server(pc_base: str):
- if RUN_INTEGRATION and not _server_alive(pc_base):
- pytest.skip(
- f"PC 服务未启动,请先执行: cd backend && pnpm dev ({pc_base}/health)"
- )
- return pc_base
- @pytest.fixture
- def api(require_pc_server: str):
- from client import ApiClient
- return ApiClient(require_pc_server)
- @pytest.fixture(scope="session")
- def api_session(require_pc_server: str):
- from client import ApiClient
- return ApiClient(require_pc_server)
- @pytest.fixture
- def uid() -> str:
- """每条用例唯一后缀,避免内存库重复冲突"""
- return uuid.uuid4().hex[:8]
- # ---------- QiWe 真实联调(需 backend/.env:QIWEI_TOKEN + QIWEI_GUID)----------
- def _load_env() -> dict[str, str]:
- from load_env import load_backend_env
- return load_backend_env()
- def _parse_room_ids(env: dict[str, str]) -> list[str]:
- raw = env.get("QIWEI_ROOM_IDS", "").strip()
- if not raw:
- return []
- return [x.strip() for x in raw.split(",") if x.strip()]
- @pytest.fixture(scope="session")
- def live_guid(require_pc_server: str) -> str:
- guid = _load_env().get("QIWEI_GUID", "").strip()
- if not guid:
- pytest.skip(
- "未配置 QIWEI_GUID:在 backend/.env 填写 "
- "QIWEI_GUID=设备ID(https://manager.qiweapi.com/nodes)"
- )
- return guid
- @pytest.fixture(scope="session")
- def live_room_ids(api_session, live_guid: str) -> list[str]:
- """优先读 .env 的 QIWEI_ROOM_IDS;否则 rooms/sync(getRoomList)"""
- from_env = _parse_room_ids(_load_env())
- if from_env:
- return from_env
- data = api_session.assert_success(
- api_session.post("/rooms/sync", json={"guid": live_guid, "nextStartIndex": 0})
- )
- rooms = data.get("rooms") or []
- if not rooms:
- pytest.skip(
- "无 roomId:在 backend/.env 配置 QIWEI_ROOM_IDS=群id1,群id2 "
- "(从 getSessionList 里 sessionType=1 的 sessionId 复制)"
- )
- return [str(r["roomId"]) for r in rooms]
- @pytest.fixture(scope="session")
- def live_room_id(live_room_ids: list[str]) -> str:
- return live_room_ids[0]
- @pytest.fixture(scope="session")
- def live_rooms_seeded(api_session, live_guid: str, live_room_ids: list[str]) -> list[str]:
- """用 QIWEI_ROOM_IDS 写入本方群库(batchGetRoomDetail),供 GET /api/rooms 联调"""
- data = api_session.assert_success(
- api_session.post(
- "/rooms/sync",
- json={
- "guid": live_guid,
- "nextStartIndex": 0,
- "roomIdList": live_room_ids,
- },
- )
- )
- synced = [str(r["roomId"]) for r in data.get("rooms") or []]
- assert synced, "按 roomIdList 同步后 rooms 仍为空,请检查 QiWe 或 roomId 是否正确"
- return synced
|