"""Tests for the WeChat voice-call scraper (Task 2.4). 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 4 records across 2 pages of size 2, plus a terminating empty page (the page-number paginator's primary stop signal): * page 1 -> 2 records, ``total=4`` * page 2 -> 2 records, ``total=4`` * page 3 -> empty ``data`` array (no more rows) The scraper must yield exactly the 4 records normalized to the ``PhoneCall`` column shape (``channel="voice_call"``), make exactly 3 calls, and stop without a 4th. Field mapping asserted end-to-end incl.: * ``id`` -> ``call_id`` (PK) * ``duration`` -> ``call_duration`` (string seconds coerced to int) * ``startTime`` -> ``start_call_time`` (``YYYY-MM-DD HH:MM:SS`` -> ms epoch) * ``ossFileName``-> ``record_file`` (may be null) * ``isSend`` -> ``incoming_call`` with INVERTED polarity (voice-call ``isSend`` is 0=incoming/1=outgoing; ``PhoneCall.incoming_call`` is 1=incoming/0=outgoing — so ``incoming_call = 1 - isSend``). * ``talker`` -> ``call_number`` (the customer / room id) * ``talkerNickName`` -> ``plan_customer_name`` """ from __future__ import annotations import datetime as dt import json as _json import httpx import respx from app.scraper.wechat import scrape_wechat_voice_calls from app.session.yunke_client import YunkeClient from tests.cassettes.voiceCall_fixture import ( DEFAULT_PAGE_SIZE, TOTAL, VOICE_RECORDS, make_voice_list_response, ) BASE = "https://crm2.yunkecn.com" VOICE_LIST_URL = f"{BASE}/wechatweb/callRecord/queryWeChatVoiceList" 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) def _ms(date_str: str) -> int: """Expected ms-epoch for a ``YYYY-MM-DD HH:MM:SS`` string parsed as UTC. Mirrors the scraper's documented parsing rule so the assertion is an independent recomputation (not a self-reference). """ return int( dt.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S") .replace(tzinfo=dt.UTC) .timestamp() * 1000 ) # --------------------------------------------------------------------------- # End-to-end: pagination + field mapping + raw_json # --------------------------------------------------------------------------- @respx.mock async def test_scrape_wechat_voice_calls_yields_records_and_paginates(): """Two full pages + a terminating empty page -> 4 records, 3 calls.""" page1 = make_voice_list_response( VOICE_RECORDS[:2], total=TOTAL, page_num=1, page_size=DEFAULT_PAGE_SIZE ) page2 = make_voice_list_response( VOICE_RECORDS[2:], total=TOTAL, page_num=2, page_size=DEFAULT_PAGE_SIZE ) page3 = make_voice_list_response( [], total=TOTAL, page_num=3, page_size=DEFAULT_PAGE_SIZE ) route = respx.post(VOICE_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_voice_calls( client, 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 4 fixture records (not duplicated, not dropped). assert len(results) == 4 assert {r["call_id"] for r in results} == {rec["id"] for rec in VOICE_RECORDS} # Field mapping for each record (compare against the source record). for result, record in zip(results, VOICE_RECORDS, strict=True): # Identity + channel. assert result["call_id"] == record["id"] assert result["channel"] == "voice_call" # Staff side. assert result["user_id"] == record["userId"] assert result["user_id_name"] == record["userName"] # Customer side: talker -> call_number, talkerNickName -> plan_customer_name. assert result["call_number"] == record["talker"] assert result["plan_customer_name"] == record["talkerNickName"] # Recording URL (may be None). assert result["record_file"] == record["ossFileName"] # Status + duration + start time coercions. assert result["call_status"] == record["callStatus"] assert result["call_duration"] == int(record["duration"]) assert result["start_call_time"] == _ms(record["startTime"]) # incoming_call polarity: isSend 0=incoming -> 1; 1=outgoing -> 0. if record["isSend"] == 0: assert result["incoming_call"] == 1 else: assert result["incoming_call"] == 0 # Version carried verbatim; raw_json preserves the original record. assert result["version"] == record["version"] assert result["raw_json"] == record # --------------------------------------------------------------------------- # Null ossFileName -> record_file None # --------------------------------------------------------------------------- @respx.mock async def test_scrape_wechat_voice_calls_null_oss_file_name_yields_none_record_file(): """A record with ``ossFileName=null`` -> ``record_file`` None.""" rec = { "id": "vc_null", "userId": "staff_user_001", "userName": "Alice Wang", "talker": "wxid_friend_x", "talkerNickName": "Customer X", "isSend": 1, "callStatus": 2, "ossFileName": None, "duration": "0", "startTime": "2026-01-15 10:00:00", "weChatId": "wxid_staff_alice01", } page1 = make_voice_list_response( [rec], total=1, page_num=1, page_size=DEFAULT_PAGE_SIZE ) page2 = make_voice_list_response( [], total=1, page_num=2, page_size=DEFAULT_PAGE_SIZE ) respx.post(VOICE_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_voice_calls( client, page_size=DEFAULT_PAGE_SIZE ) ] finally: await client.aclose() assert len(results) == 1 assert results[0]["record_file"] is None assert results[0]["call_status"] == 2 # Outgoing (isSend=1) -> incoming_call=0. assert results[0]["incoming_call"] == 0 # --------------------------------------------------------------------------- # Pagination body: pageNum/pageSize advanced correctly # --------------------------------------------------------------------------- @respx.mock async def test_scrape_wechat_voice_calls_sends_pagination_body(): """Outgoing request body carries pageNum/pageSize; advances per page.""" page1 = make_voice_list_response( VOICE_RECORDS[:1], total=TOTAL, page_num=1, page_size=1 ) page2 = make_voice_list_response( [], total=TOTAL, page_num=2, page_size=1 ) route = respx.post(VOICE_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_voice_calls(client, 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 body1 = _json.loads(route.calls[0].request.content.decode()) assert body1["pageNum"] == 1 assert body1["pageSize"] == 1 body2 = _json.loads(route.calls[1].request.content.decode()) assert body2["pageNum"] == 2 assert body2["pageSize"] == 1 # --------------------------------------------------------------------------- # Empty first page -> no yields, single call # --------------------------------------------------------------------------- @respx.mock async def test_scrape_wechat_voice_calls_stops_on_empty_first_page(): """If the very first page is empty, scraper stops without over-fetching.""" empty = make_voice_list_response([], total=0, page_num=1) route = respx.post(VOICE_LIST_URL).mock( return_value=httpx.Response(200, json=empty) ) client = _primed_client() try: results = [r async for r in scrape_wechat_voice_calls(client)] finally: await client.aclose() assert results == [] assert route.call_count == 1