feat(scraper): map customer add_time from raw_json

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
浅念
2026-08-05 18:44:04 +08:00
parent b5733f15d2
commit 8494d564ef
3 changed files with 81 additions and 0 deletions
@@ -152,3 +152,49 @@ async def test_scrape_wechat_friends_stops_on_empty_first_page():
assert results == []
assert route.call_count == 1
# ---------------------------------------------------------------------------
# add_time mapping: addTime (string 'YYYY-MM-DD HH:mm:ss' OR ms-epoch int)
# ---------------------------------------------------------------------------
def test_normalize_maps_add_time_string_to_epoch():
"""addTime as a wall-clock string parses to a plausible recent ms epoch.
The CRM delivers wechat ``addTime`` as ``'YYYY-MM-DD HH:mm:ss'`` (confirmed
in smoke.db raw_json). ``parse_datetime_to_ms`` parses naive UTC; we assert
only that the result is an int in the right magnitude for a 2026 date
(any 2026 moment > 1.76e12) rather than pinning a specific epoch so the
test is not brittle to the exact format-resolution / tz convention.
"""
from app.scraper.wechat import _normalize
record = {
"friendWxId": "x",
"addTime": "2026-04-28 16:24:39",
"lastChatTime": 1700000000000,
}
out = _normalize(record)
assert isinstance(out["add_time"], int)
assert out["add_time"] > 1_760_000_000_000
def test_normalize_maps_add_time_int_passthrough():
"""addTime as a numeric ms-epoch passes through verbatim (int or float)."""
from app.scraper.wechat import _normalize
record = {"friendWxId": "x", "addTime": 1690000000000, "lastChatTime": None}
out = _normalize(record)
assert out["add_time"] == 1690000000000
def test_normalize_add_time_none_when_missing():
"""Missing / empty addTime maps to None (None-safe field)."""
from app.scraper.wechat import _normalize
out = _normalize({"friendWxId": "x"})
assert out["add_time"] is None
out_empty = _normalize({"friendWxId": "x", "addTime": ""})
assert out_empty["add_time"] is None