# --------------------------------------------------------------------------- # GET /api/customers/declared-total — 云客口径合计(页脚对齐云客显示,2026-08-20) # --------------------------------------------------------------------------- async def _seed_accounts_for_declared(session): from app.db.models import WechatAccount session.add_all([ WechatAccount(wx_id="w1", contacts_num=100), WechatAccount(wx_id="w2", contacts_num=23), WechatAccount(wx_id="w3", contacts_num=None), ]) await session.commit() async def test_declared_total_sums_contacts_num(make_client): """分渠道 declared_total = Σ 员工账号表 contacts_num(云客含重复口径)。""" from app.db.models import WhatsappAccount async def seed_both(session): await _seed_accounts_for_declared(session) session.add_all([ WhatsappAccount(uid="a1@s.whatsapp.net", contacts_num=10), WhatsappAccount(uid="a2@s.whatsapp.net", contacts_num=5), ]) await session.commit() client = await make_client(seed_both) r = await client.get("/api/customers/declared-total") assert r.status_code == 200 body = r.json() assert body["wechat"]["declared_total"] == 123 assert body["wechat"]["account_count"] == 2 # 只计有值的员工 assert body["whatsapp"]["declared_total"] == 15 assert body["whatsapp"]["account_count"] == 2 # --------------------------------------------------------------------------- # send_today / receive_today — 今日发送/接收条数(对齐云客口径,2026-08-22) # --------------------------------------------------------------------------- async def _seed_today_counts(session): import datetime as dt from app.db.models import Customer as C, Message as M now_ms = int(dt.datetime.now(dt.UTC).timestamp() * 1000) yesterday_ms = now_ms - 86_400_000 session.add_all([ C(channel="wechat", owner_account_id="o1", friend_id="f1"), C(channel="wechat", owner_account_id="o1", friend_id="f2"), ]) # f1: 今日 3发2收 + 昨日 5发(不应计入) for i in range(3): session.add(M(msg_id=f"t1s{i}", channel="wechat", owner_account_id="o1", talker_id="f1", is_send=True, timestamp=now_ms - i * 1000)) for i in range(2): session.add(M(msg_id=f"t1r{i}", channel="wechat", owner_account_id="o1", talker_id="f1", is_send=False, timestamp=now_ms - i * 1000)) for i in range(5): session.add(M(msg_id=f"t0s{i}", channel="wechat", owner_account_id="o1", talker_id="f1", is_send=True, timestamp=yesterday_ms)) # f2: 无消息 await session.commit() async def test_customers_today_send_receive(make_client): """发送/接收今日列 = 本地消息表按 Asia/Shanghai 今日聚合。""" client = await make_client(_seed_today_counts) r = await client.get("/api/customers", params={"channel": "wechat"}) assert r.status_code == 200 by_fid = {it["friend_id"]: it for it in r.json()["items"]} assert by_fid["f1"]["send_today"] == 3 assert by_fid["f1"]["receive_today"] == 2 assert by_fid["f2"]["send_today"] == 0 assert by_fid["f2"]["receive_today"] == 0