feat: wechat group+member and talker scrapers
Add scrape_wechat_groups (POST /wechatweb/chatroom/getChatroomList) and scrape_wechat_talkers (POST /wechatweb/wxFriends/talkerList), both reusing the page-number paginator. Groups parse the semicolon-joined memberIds into a member_ids_list so upsert_chat_group can mirror GroupMember rows (composite PK channel/room_id/member_id) idempotently alongside the ChatGroup row. Talkers are a sync-planning input (member_count>0 marks a group conversation); no dedicated table. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
"""Tests for the WeChat talker (conversation) list scraper (M2).
|
||||
|
||||
Drives the real page-number pagination + normalization logic through
|
||||
``respx`` by wiring a real ``YunkeClient`` (primed with a fake session so the
|
||||
login flow is skipped) against a mocked transport.
|
||||
|
||||
The fixture ships 3 conversations across 2 pages of size 2, plus a
|
||||
terminating empty page (the page-number paginator's primary stop signal):
|
||||
|
||||
* page 1 -> 2 records, ``hasMore=true``
|
||||
* page 2 -> 1 record, ``hasMore=false``
|
||||
* page 3 -> empty ``page`` array (no more rows)
|
||||
|
||||
The scraper must yield exactly the 3 talkers (normalized), send the owning
|
||||
``wechatId`` in every request body, advance ``pageNum``, and stop after the
|
||||
empty page. Both the direct-message (``memberCount == 0``) and group-chat
|
||||
(``memberCount > 0``) paths are exercised by the fixture.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json as _json
|
||||
|
||||
import httpx
|
||||
import respx
|
||||
|
||||
from app.scraper.wechat import scrape_wechat_talkers
|
||||
from app.session.yunke_client import YunkeClient
|
||||
from tests.cassettes.talkerList_fixture import (
|
||||
DEFAULT_PAGE_SIZE,
|
||||
OWNER_WECHAT_ID,
|
||||
TALKER_RECORDS,
|
||||
make_talker_list_response,
|
||||
)
|
||||
|
||||
BASE = "https://crm2.yunkecn.com"
|
||||
TALKER_LIST_URL = f"{BASE}/wechatweb/wxFriends/talkerList"
|
||||
|
||||
USERID = "a" * 32
|
||||
COMPANY = "8m0atd"
|
||||
|
||||
|
||||
def _primed_client() -> YunkeClient:
|
||||
"""A YunkeClient already in a logged-in state (login flow skipped)."""
|
||||
return YunkeClient(base_url=BASE, userid=USERID, company=COMPANY)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# End-to-end: pagination + field mapping + wechatId in request body
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_scrape_wechat_talkers_yields_records_and_paginates():
|
||||
"""Two full pages + a terminating empty page -> 3 talkers, 3 calls."""
|
||||
page1 = make_talker_list_response(
|
||||
TALKER_RECORDS[:2], page_num=1, page_size=DEFAULT_PAGE_SIZE, has_more=True
|
||||
)
|
||||
page2 = make_talker_list_response(
|
||||
TALKER_RECORDS[2:], page_num=2, page_size=DEFAULT_PAGE_SIZE, has_more=False
|
||||
)
|
||||
page3 = make_talker_list_response([], page_num=3, page_size=DEFAULT_PAGE_SIZE)
|
||||
|
||||
route = respx.post(TALKER_LIST_URL).mock(
|
||||
side_effect=[
|
||||
httpx.Response(200, json=page1),
|
||||
httpx.Response(200, json=page2),
|
||||
httpx.Response(200, json=page3),
|
||||
]
|
||||
)
|
||||
|
||||
client = _primed_client()
|
||||
try:
|
||||
results = [
|
||||
r
|
||||
async for r in scrape_wechat_talkers(
|
||||
client, OWNER_WECHAT_ID, page_size=DEFAULT_PAGE_SIZE
|
||||
)
|
||||
]
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
# Pagination: exactly 3 calls (page1, page2, empty-page stop).
|
||||
assert route.call_count == 3
|
||||
|
||||
# Exactly the 3 fixture talkers (not duplicated, not dropped).
|
||||
assert len(results) == 3
|
||||
assert {r["friend_wechat_id"] for r in results} == {
|
||||
rec["friendWechatId"] for rec in TALKER_RECORDS
|
||||
}
|
||||
|
||||
# Field mapping for each record (compare against the source record).
|
||||
for result, record in zip(results, TALKER_RECORDS, strict=True):
|
||||
assert result["friend_wechat_id"] == record["friendWechatId"]
|
||||
assert result["nick_name"] == record["nickName"]
|
||||
assert result["remark_name"] == record["remarkName"]
|
||||
assert result["message_count"] == record["messageCount"]
|
||||
assert result["last_chat_time"] == record["lastChatTime"]
|
||||
assert result["member_count"] == record["memberCount"]
|
||||
assert result["head_url"] == record["headUrl"]
|
||||
# The owning staff account is carried on every record.
|
||||
assert result["owner_account_id"] == OWNER_WECHAT_ID
|
||||
# raw_json preserves the original upstream record.
|
||||
assert result["raw_json"] == record
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_scrape_wechat_talkers_sends_wechat_id_and_pagination_body():
|
||||
"""Every request carries the staff wechatId + advancing pageNum/pageSize."""
|
||||
page1 = make_talker_list_response(
|
||||
TALKER_RECORDS[:1], page_num=1, page_size=1, has_more=True
|
||||
)
|
||||
page2 = make_talker_list_response(
|
||||
[], page_num=2, page_size=1, has_more=False
|
||||
)
|
||||
route = respx.post(TALKER_LIST_URL).mock(
|
||||
side_effect=[httpx.Response(200, json=page1), httpx.Response(200, json=page2)]
|
||||
)
|
||||
|
||||
client = _primed_client()
|
||||
try:
|
||||
results = [
|
||||
r async for r in scrape_wechat_talkers(client, OWNER_WECHAT_ID, page_size=1)
|
||||
]
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
# One record on page 1, empty page 2 stops the loop.
|
||||
assert len(results) == 1
|
||||
assert route.call_count == 2
|
||||
|
||||
# Both requests must carry the owning wechatId + page slice.
|
||||
body1 = _json.loads(route.calls[0].request.content.decode())
|
||||
assert body1["wechatId"] == OWNER_WECHAT_ID
|
||||
assert body1["pageNum"] == 1
|
||||
assert body1["pageSize"] == 1
|
||||
|
||||
body2 = _json.loads(route.calls[1].request.content.decode())
|
||||
assert body2["wechatId"] == OWNER_WECHAT_ID
|
||||
assert body2["pageNum"] == 2
|
||||
assert body2["pageSize"] == 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Empty first page -> no yields, single call
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_scrape_wechat_talkers_stops_on_empty_first_page():
|
||||
"""If the very first page is empty, scraper stops without over-fetching."""
|
||||
empty = make_talker_list_response([], page_num=1)
|
||||
route = respx.post(TALKER_LIST_URL).mock(
|
||||
return_value=httpx.Response(200, json=empty)
|
||||
)
|
||||
|
||||
client = _primed_client()
|
||||
try:
|
||||
results = [r async for r in scrape_wechat_talkers(client, OWNER_WECHAT_ID)]
|
||||
finally:
|
||||
await client.aclose()
|
||||
|
||||
assert results == []
|
||||
assert route.call_count == 1
|
||||
Reference in New Issue
Block a user