""" tests/test_wecom_textcard_source.py 用途:断言企微 textcard 标题带来源,以及账号类通知不加来源。 依赖:app fixture、integrations.wecom.notify。 逻辑:直接调用标题函数与发送出口,不经过具体业务模块。 """ from app import db from app.models import User from app.services.integrations.wecom.notify import ( _with_agent_source_title, send_wecom_event_message, send_wecom_textcard_message, ) def test_agent_source_prefix_stays_when_title_is_truncated(): """超长业务标题截断后,来源前缀仍完整留在最前面。""" title = _with_agent_source_title('缺' * 200) # [断言意图] 前缀不被 120 字上限切掉,业务标题从末尾被截断。 assert title.startswith('AI智能体 · ') assert len(title) == 120 assert title == f"AI智能体 · {'缺' * 112}" def test_agent_source_prefix_is_not_repeated(): """已经带来源的标题不再加第二次。""" once = _with_agent_source_title('精益管理培训逾期提醒') twice = _with_agent_source_title(once) # [断言意图] 重复经过发送出口时标题保持单层来源。 assert once == 'AI智能体 · 精益管理培训逾期提醒' assert twice == once def test_textcard_payload_uses_agent_source_title(app, monkeypatch): """实际发给企业微信的 textcard.title 带来源,调用方传入的仍是业务标题。""" captured = {} def _fake_post(_url, params=None, json=None, timeout=None): captured['json'] = json class _Resp: def json(self): return {'errcode': 0} return _Resp() monkeypatch.setattr( 'app.services.integrations.wecom.notify._get_access_token', lambda: 'token', ) monkeypatch.setattr( 'app.services.integrations.wecom.notify.requests.post', _fake_post, ) with app.app_context(): app.config['WECOM_AGENT_ID'] = '1000002' send_wecom_textcard_message( 'wx-user', '销售开票附件缺件提醒', '正文', 'https://example.com/detail', ) # [断言意图] 企微载荷标题带来源,正文和按钮保持原样。 assert captured['json']['textcard']['title'] == 'AI智能体 · 销售开票附件缺件提醒' assert captured['json']['textcard']['description'] == '正文' assert captured['json']['textcard']['btntxt'] == '查看详情' def test_account_events_keep_business_title(app, monkeypatch): """密码重置和权限变更不加来源;业务提醒仍然加上。""" captured = {} def _fake_send(_userid, title, _description, _jump_url, *, with_agent_source=True): captured['title'] = title captured['with_agent_source'] = with_agent_source return {'errcode': 0} monkeypatch.setattr( 'app.services.integrations.wecom.notify.send_wecom_textcard_message', _fake_send, ) with app.app_context(): app.config['WECOM_NOTIFY_ENABLED'] = True app.config['WECOM_NOTIFY_EVENTS'] = ( 'password_reset,permission_request_approved,lean_training_overdue' ) user = User( username='source-user', email='source@example.com', role='user', wecom_userid='wx-source', ) db.session.add(user) db.session.commit() send_wecom_event_message( event_type='password_reset', user=user, title='和信AI智能工作台 密码重置', content='请重置密码', jump_url='https://example.com/reset', ) # [断言意图] 密码重置不是智能体推送,发送出口关闭来源前缀。 assert captured['with_agent_source'] is False assert captured['title'] == '和信AI智能工作台 密码重置' send_wecom_event_message( event_type='permission_request_approved', user=user, title='权限申请已通过', content='已通过', ) # [断言意图] 权限变更与密码重置同一排除名单。 assert captured['with_agent_source'] is False send_wecom_event_message( event_type='lean_training_overdue', user=user, title='精益管理培训逾期提醒', content='请完成学习', ) # [断言意图] 业务提醒仍把来源交给 textcard 出口添加。 assert captured['with_agent_source'] is True assert captured['title'] == '精益管理培训逾期提醒'