ForcePilot/backend/test/unit/channels/test_telegram_format.py
Kris 3264900bc9 test: 新增多渠道单元测试用例并配置测试环境变量
新增了Twitch、Telegram、Discord、Slack、Mattermost、WeChat、Zalo等多渠道的单元测试用例,覆盖了令牌处理、速率限制、消息去重、会话解析、格式转换、安全策略等模块
同时在测试配置中添加了测试用的OpenAI API密钥环境变量
2026-05-12 00:56:47 +08:00

76 lines
2.3 KiB
Python

from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from yuxi.channels.adapters.telegram.format import (
markdown_to_html,
strip_all_tags,
_convert_bold,
_convert_italic,
_convert_code_blocks,
_convert_inline_code,
_convert_links,
_sanitize_html,
)
class TestMarkdownToHTML:
def test_bold_conversion(self):
result = markdown_to_html("**hello**")
assert result == "<b>hello</b>"
def test_italic_conversion(self):
result = markdown_to_html("*hello*")
assert "<i>hello</i>" in result
def test_italic_underscore(self):
result = markdown_to_html("_hello_")
assert "<i>hello</i>" in result
def test_bold_and_italic(self):
result = markdown_to_html("**bold** and *italic*")
assert "<b>bold</b>" in result
assert "<i>italic</i>" in result
def test_strikethrough(self):
result = markdown_to_html("~~deleted~~")
assert "<s>deleted</s>" in result
def test_inline_code(self):
result = markdown_to_html("use `print()`")
assert "<code>print()</code>" in result
def test_code_block(self):
result = markdown_to_html("```python\nprint('hello')\n```")
assert "<pre>" in result
assert "print" in result
def test_link_conversion(self):
result = markdown_to_html("[click here](https://example.com)")
assert '<a href="https://example.com">click here</a>' in result
def test_spoiler_conversion(self):
result = markdown_to_html("||secret||")
assert "<tg-spoiler>secret</tg-spoiler>" in result
def test_plain_text_passthrough(self):
result = markdown_to_html("hello world")
assert "hello world" in result
def test_escape_special_chars(self):
result = markdown_to_html("price &amp; 5")
assert "&amp;" in result
def test_sanitize_html_input(self):
result = markdown_to_html("<b>clean</b>")
assert "clean" in result
def test_strip_all_tags(self):
result = strip_all_tags("<b>hello</b> <i>world</i>")
assert result == "hello world"
def test_strip_all_tags_no_tags(self):
result = strip_all_tags("plain text")
assert result == "plain text"