"""新增两模板从真实业务快照到发送器的集成测试;微信传输使用假客户端。""" import json from datetime import timedelta import pytest from test_wechat_cert_notices import cert_notices from test_wechat_completion_notices import db as completion_db, seed, sync, NOW class Client: def __init__(self): self.calls = [] def send(self, **kwargs): self.calls.append(kwargs) return 'wx-message' def binding(db, user_id, tenant_no='t1'): from app.core.config import settings from app.models.wechat_notice import WechatBinding WechatBinding.__table__.create(db.connection()) db.add(WechatBinding(id='binding', user_id=user_id, tenant_no=tenant_no, appid=settings.WECHAT_OA_APPID, subscribed=1, openid='oa-openid')) db.commit() def test_daily_worker_refreshes_real_counts_and_send_time(cert_notices, monkeypatch): from app.services.wechat_approval_notices import deliver_pending svc, db, now, add_cert, Notice = cert_notices add_cert('old') binding(db, 't1-a') assert svc.enqueue_daily_cert_notices(db, now=now) == 1 db.commit() add_cert('new', days=-1) db.commit() send_time = now + timedelta(minutes=5) monkeypatch.setattr(svc, '_local_time', lambda value: send_time) client = Client() assert deliver_pending(db, client=client) == 1 assert client.calls[0]['data'] == { 'thing5': {'value': '1项即将到期,1项已到期'}, 'thing3': {'value': '系统默认发出'}, 'time6': {'value': '2026-10-26 10:05:00'}, } assert db.query(Notice).one().status == 'sent' assert deliver_pending(db, client=client) == 0 @pytest.mark.parametrize('kind', ['bx_advance', 'bx_reimburse', 'pl_freight', 'pl_recharge']) def test_completion_delivered_once_without_approval_cycle(completion_db, kind): from app.services.wechat_approval_notices import deliver_pending from app.models.wechat_notice import WechatNotice db = completion_db seed(db, kind) kwargs = {'event_id': 'record', 'occurred_at': NOW} if kind.startswith('pl_') else {} row = sync(db, kind, **kwargs) assert row is not None recipient = row.recipient_id expected = json.loads(row.payload) binding(db, recipient) client = Client() assert deliver_pending(db, client=client) == 1 assert client.calls[0]['data'] == expected assert 'amount' not in json.dumps(expected) assert db.query(WechatNotice).one().status == 'sent' assert deliver_pending(db, client=client) == 0