test_09_module_live.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """Swagger 模块 9(KOC)— 真实联调(QiWe 同步联系人 + 真实 roomId 意向识别)"""
  2. from __future__ import annotations
  3. import uuid
  4. import pytest
  5. from load_env import load_backend_env
  6. pytestmark = [pytest.mark.integration, pytest.mark.qiwei_live]
  7. @pytest.fixture(scope="module")
  8. def m09_channel(api_session):
  9. suffix = uuid.uuid4().hex[:6]
  10. ch = api_session.assert_success(
  11. api_session.post(
  12. "/channels",
  13. json={
  14. "name": f"联调渠道_{suffix}",
  15. "code": f"live-ch-{suffix}",
  16. "type": "offline",
  17. "remark": "module9-live",
  18. },
  19. ),
  20. status=201,
  21. )
  22. yield ch
  23. api_session.assert_success(api_session.delete(f"/channels/{ch['id']}"))
  24. @pytest.fixture(scope="module")
  25. def m09_contacts(api_session, live_guid):
  26. data = api_session.assert_success(
  27. api_session.post(
  28. "/contacts/sync",
  29. json={"guid": live_guid, "currentSeq": 0, "limit": 20},
  30. )
  31. )
  32. return {
  33. "sync": data,
  34. "contacts": data.get("contacts") or [],
  35. "user_id": (data.get("contacts") or [{}])[0].get("userId") if data.get("contacts") else None,
  36. }
  37. @pytest.fixture(scope="module")
  38. def m09_intent(api_session, live_room_id):
  39. suffix = uuid.uuid4().hex[:8]
  40. detect = api_session.assert_success(
  41. api_session.post(
  42. "/intent-leads/detect",
  43. json={
  44. "userId": f"live-user-{suffix}",
  45. "roomId": live_room_id,
  46. "content": "这个全屋定制方案多少钱?能约量房看样板间吗?",
  47. },
  48. )
  49. )
  50. return {"detect": detect, "room_id": live_room_id}
  51. def test_m09_channels_list(api, m09_channel):
  52. listed = api.assert_success(api.get("/channels"))
  53. assert any(c["id"] == m09_channel["id"] for c in listed)
  54. def test_m09_update_channel(api, m09_channel):
  55. updated = api.assert_success(
  56. api.put(f"/channels/{m09_channel['id']}", json={"remark": "联调已更新"})
  57. )
  58. assert updated["remark"] == "联调已更新"
  59. def test_m09_contacts_sync(m09_contacts):
  60. assert "contacts" in m09_contacts["sync"]
  61. assert "hasMore" in m09_contacts["sync"]
  62. def test_m09_contacts_list(api, m09_contacts):
  63. listed = api.assert_success(api.get("/contacts"))
  64. assert isinstance(listed, list)
  65. if m09_contacts["contacts"]:
  66. assert len(listed) >= len(m09_contacts["contacts"])
  67. def test_m09_contact_detail_if_any(api, m09_contacts):
  68. uid = m09_contacts.get("user_id")
  69. if not uid:
  70. pytest.skip("QiWe 同步未返回外部联系人,跳过详情")
  71. detail = api.assert_success(api.get(f"/contacts/{uid}"))
  72. assert detail["userId"] == uid
  73. def test_m09_koc_candidates(api, m09_contacts):
  74. candidates = api.assert_success(api.get("/koc/candidates"))
  75. assert isinstance(candidates, list)
  76. def test_m09_koc_label_optional(api, live_guid, m09_contacts):
  77. candidates = api.assert_success(api.get("/koc/candidates"))
  78. if not candidates:
  79. pytest.skip("无 KOC 候选人(需先 sync 联系人且随机规则命中)")
  80. env = load_backend_env()
  81. label_id = env.get("QIWEI_KOC_LABEL_ID", "").strip() or "1"
  82. if env.get("QIWEI_LIVE_KOC_LABEL", "").strip() != "1":
  83. pytest.skip("未开启真实打标签:.env 设置 QIWEI_LIVE_KOC_LABEL=1 与 QIWEI_KOC_LABEL_ID")
  84. user_id = candidates[0]["userId"]
  85. resp = api.post(
  86. "/koc/label",
  87. json={"userId": user_id, "guid": live_guid, "labelId": label_id},
  88. )
  89. body = api.parse_json(resp)
  90. assert body.get("success") or body.get("error", {}).get("code") == "QIWEI_API_ERROR"
  91. def test_m09_intent_detect(m09_intent):
  92. d = m09_intent["detect"]
  93. assert d.get("matched") is True
  94. assert d.get("lead") is not None
  95. def test_m09_intent_leads_list(api, m09_intent):
  96. leads = api.assert_success(api.get("/intent-leads"))
  97. assert isinstance(leads, list)
  98. if m09_intent["detect"].get("lead"):
  99. lid = m09_intent["detect"]["lead"]["id"]
  100. assert any(l["id"] == lid for l in leads)
  101. def test_m09_intent_lead_update_if_any(api, m09_intent):
  102. lead = m09_intent["detect"].get("lead")
  103. if not lead:
  104. pytest.skip("未识别到意向线索")
  105. updated = api.assert_success(
  106. api.put(
  107. f"/intent-leads/{lead['id']}",
  108. json={"status": "assigned", "assigneeId": 1},
  109. )
  110. )
  111. assert updated["status"] == "assigned"
  112. def test_m09_intent_tasks(api, m09_intent):
  113. tasks = api.assert_success(api.get("/intent-tasks"))
  114. assert isinstance(tasks, list)