fix(scraper): voice body 加 departmentId/isRoom/时间窗 + sync 解析 root department_id

实测 queryWeChatVoiceList 必须带 callSource+departmentId+isRoom+startTime+endTime,
否则 200 但 data 空(0 数据)。WA callSource=4 / 微信 callSource=0,宽窗抓全量。
sync_*_voice_calls 照 sync_wechat_accounts 模式解析 _find_root_department_id。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
浅念
2026-08-07 09:49:31 +08:00
parent 18620870c6
commit 79b89bc919
5 changed files with 210 additions and 21 deletions
@@ -91,10 +91,13 @@ def _ms(date_str: str) -> int:
async def test_scrape_wa_voice_calls_yields_records_and_paginates():
"""Two pages + a terminating empty page -> 3 records, 3 calls.
Body must carry ``callSource=4`` (the WA flag) plus the standard
``pageNum``/``pageSize`` pagination pair. Each yielded record is
normalized to the ``PhoneCall`` shape with ``channel="wa_voice_call"``
and the WA-specific fields preserved via ``raw_json``.
Body must carry the full filter set the live endpoint REQUIRES
(``callSource=4`` + ``departmentId`` + ``isRoom`` + ``startTime`` +
``endTime`` — omit any and the endpoint returns 200 with empty
``data.data``, 0 rows) plus the standard ``pageNum``/``pageSize``
pagination pair. Each yielded record is normalized to the ``PhoneCall``
shape with ``channel="wa_voice_call"`` and the WA-specific fields
preserved via ``raw_json``.
"""
page1 = make_wa_voice_list_response(
WA_VOICE_RECORDS[:2], total=TOTAL, page_num=1, page_size=DEFAULT_PAGE_SIZE
@@ -119,7 +122,7 @@ async def test_scrape_wa_voice_calls_yields_records_and_paginates():
results = [
r
async for r in scrape_wa_voice_calls(
client, page_size=DEFAULT_PAGE_SIZE
client, department_id="DEPT_TEST", page_size=DEFAULT_PAGE_SIZE
)
]
finally:
@@ -132,11 +135,29 @@ async def test_scrape_wa_voice_calls_yields_records_and_paginates():
assert len(results) == len(WA_VOICE_RECORDS)
assert {r["call_id"] for r in results} == {rec["id"] for rec in WA_VOICE_RECORDS}
# First request body: callSource=4 (WA) + standard pagination pair.
# First request body: full required filter set the live endpoint demands.
# callSource=4 (WA) + departmentId + isRoom="0" + wide time window +
# empty userId/sort/ids (the live endpoint accepts these blank).
body1 = _json.loads(route.calls[0].request.content.decode())
assert body1["callSource"] == "4"
assert body1["departmentId"] == "DEPT_TEST"
assert body1["isRoom"] == "0"
assert body1["startTime"] == "2000-01-01 00:00:00"
assert body1["endTime"] == "2099-12-31 23:59:59"
assert body1["pageNum"] == 1
assert body1["pageSize"] == DEFAULT_PAGE_SIZE
# The blank-but-required keys are present (the live endpoint expects
# them in the body even when empty).
assert body1["userId"] == ""
assert body1["sort"] == ""
assert body1["ids"] == []
# Second page advances pageNum and keeps the same filter set verbatim.
body2 = _json.loads(route.calls[1].request.content.decode())
assert body2["pageNum"] == 2
assert body2["pageSize"] == DEFAULT_PAGE_SIZE
assert body2["callSource"] == "4"
assert body2["departmentId"] == "DEPT_TEST"
# Field mapping for each record (compare against the source record).
for result, record in zip(results, WA_VOICE_RECORDS, strict=True):
@@ -205,7 +205,15 @@ async def test_scrape_wechat_voice_calls_null_oss_file_name_yields_none_record_f
@respx.mock
async def test_scrape_wechat_voice_calls_sends_pagination_body():
"""Outgoing request body carries pageNum/pageSize; advances per page."""
"""Body carries the full required filter set + advancing pageNum/pageSize.
Live diagnosis (2026-08-07) showed ``queryWeChatVoiceList`` REQUIRES
``callSource=0`` + ``departmentId`` + ``isRoom`` + ``startTime`` +
``endTime`` — omit any and the endpoint returns 200 with an empty
``data.data`` (0 rows). The WeChat variant uses ``callSource="0"``;
the wide time window ``2000-01-01 .. 2099-12-31`` pulls the full
history in one scrape.
"""
page1 = make_voice_list_response(
VOICE_RECORDS[:1], total=TOTAL, page_num=1, page_size=1
)
@@ -221,7 +229,12 @@ async def test_scrape_wechat_voice_calls_sends_pagination_body():
client = _primed_client()
try:
results = [r async for r in scrape_wechat_voice_calls(client, page_size=1)]
results = [
r
async for r in scrape_wechat_voice_calls(
client, department_id="DEPT_TEST", page_size=1
)
]
finally:
await client.aclose()
@@ -229,13 +242,30 @@ async def test_scrape_wechat_voice_calls_sends_pagination_body():
assert len(results) == 1
assert route.call_count == 2
# Page 1 body: full required filter set + standard pagination pair.
body1 = _json.loads(route.calls[0].request.content.decode())
assert body1["callSource"] == "0"
assert body1["departmentId"] == "DEPT_TEST"
assert body1["isRoom"] == "0"
assert body1["startTime"] == "2000-01-01 00:00:00"
assert body1["endTime"] == "2099-12-31 23:59:59"
assert body1["pageNum"] == 1
assert body1["pageSize"] == 1
# Blank-but-required keys are present (the live endpoint expects them
# in the body even when empty).
assert body1["userId"] == ""
assert body1["sort"] == ""
assert body1["ids"] == []
# Page 2 advances pageNum and keeps the same filter set verbatim.
body2 = _json.loads(route.calls[1].request.content.decode())
assert body2["pageNum"] == 2
assert body2["pageSize"] == 1
assert body2["callSource"] == "0"
assert body2["departmentId"] == "DEPT_TEST"
assert body2["isRoom"] == "0"
assert body2["startTime"] == "2000-01-01 00:00:00"
assert body2["endTime"] == "2099-12-31 23:59:59"
# ---------------------------------------------------------------------------