ForcePilot/backend/test/unit/channels/test_nextcloudtalk_chunking.py
Kris 69fe97a90d test: 批量修复并新增单元测试用例
1. 移除Telegram格式化测试中未使用的导入项
2. 修复Teams测试用例,添加monkeypatch参数并配置通配符开关
3. 更新钉钉适配器测试,替换弃用的流属性检查
4. 修正Twitch规范化测试,更新ROOMSTATE测试逻辑
5. 重构会话映射测试,完善数据库执行结果模拟
6. 格式化Slack块构建测试的长参数调用
7. 修复LINE适配器测试,更新能力断言和异步锁使用
8. 修正Slack会话解析测试,修复聊天类型判断错误
9. 更新能力测试,补充缺失的字段检查
10. 修复Matrix适配器测试,修正位置参数和配置校验逻辑
11. 为飞书分析模块测试添加跳过标记
12. 新增微信能力、限流、链接格式、会话路由等模块的单元测试
13. 修复Twitch适配器导入路径和测试断言
14. 新增Discord Webhook、Nextcloud Talk、Signal多账户等模块的单元测试
15. 修复Manager阶段测试的导入路径
16. 新增iMessage异常和命令处理的单元测试
17. 新增Nostr健康检查和相关模块的单元测试
18. 新增Signal守护进程和SSE重连相关测试
2026-05-13 16:43:01 +08:00

125 lines
4.5 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channels.adapters.nextcloudtalk.chunking import chunk_text
class TestChunkText:
def test_short_text_returns_single_chunk(self):
result = chunk_text("Hello World", limit=4000)
assert result == ["Hello World"]
def test_exact_limit_text(self):
text = "A" * 4000
result = chunk_text(text, limit=4000)
assert len(result) == 1
assert result[0] == text
def test_empty_string(self):
result = chunk_text("", limit=1000)
assert result == [""]
def test_chunk_markdown_paragraphs(self):
paragraphs = []
for i in range(100):
paragraphs.append(f"Paragraph {i}: " + "x" * 80)
text = "\n\n".join(paragraphs)
result = chunk_text(text, limit=500, mode="markdown")
assert len(result) > 1
assert all(len(c) <= 500 for c in result)
reconstructed = "".join(result)
assert reconstructed == text or all(p in reconstructed for p in paragraphs[:3])
def test_chunk_markdown_lines(self):
lines = []
for i in range(200):
lines.append(f"Line {i}: " + "y" * 60)
text = "\n".join(lines)
result = chunk_text(text, limit=500, mode="markdown")
assert len(result) > 1
assert all(len(c) <= 500 for c in result)
def test_chunk_markdown_sentences(self):
sentences = []
for i in range(200):
sentences.append(f"This is sentence number {i} with some content. ")
text = "".join(sentences)
result = chunk_text(text, limit=500, mode="markdown")
assert len(result) > 1
assert all(len(c) <= 500 for c in result)
def test_chunk_markdown_no_breaks_falls_back_to_length(self):
text = "A" * 5000
result = chunk_text(text, limit=1000, mode="markdown")
assert len(result) > 1
assert all(len(c) <= 1001 for c in result)
def test_chunk_newline_mode(self):
lines = [f"Line {i:03d}: " + "x" * 200 for i in range(30)]
text = "\n".join(lines)
result = chunk_text(text, limit=500, mode="newline")
assert len(result) > 1
assert all(len(c) <= 500 for c in result)
def test_chunk_newline_single_line_equals_limit(self):
text = "A" * 500
result = chunk_text(text, limit=500, mode="newline")
assert len(result) == 1
assert result[0] == text
def test_chunk_newline_long_line(self):
text = "A" * 600
result = chunk_text(text, limit=500, mode="newline")
assert len(result) >= 1
def test_chunk_by_length(self):
text = "B" * 2500
result = chunk_text(text, limit=500, mode="length")
assert len(result) == 5
for chunk in result:
assert len(chunk) == 500
def test_chunk_by_length_uneven(self):
text = "C" * 1234
result = chunk_text(text, limit=500, mode="length")
assert len(result) == 3
assert len(result[0]) == 500
assert len(result[1]) == 500
assert len(result[2]) == 234
def test_chunk_unknown_mode_falls_back_to_length(self):
text = "D" * 1500
result = chunk_text(text, limit=500, mode="unknown_mode")
assert len(result) == 3
assert all(len(c) == 500 for c in result)
def test_chunk_with_limit_zero_uses_default(self):
text = "Short text"
result = chunk_text(text, limit=4000)
assert result == ["Short text"]
def test_chunk_markdown_borderline(self):
text = "A" * 500 + "\n\n" + "B" * 500
result = chunk_text(text, limit=550, mode="markdown")
assert len(result) >= 1
assert all(len(c) <= 550 for c in result)
def test_chunk_markdown_small_limit_forces_sentence_split(self):
text = "First. Second very long sentence with lots of words. Third. Fourth."
result = chunk_text(text, limit=20, mode="markdown")
assert len(result) >= 2
def test_chunk_newline_empty_lines(self):
text = "\n\n\nA\n\nB\n\n"
result = chunk_text(text, limit=500, mode="newline")
assert "A" in "".join(result)
assert "B" in "".join(result)
def test_chunk_preserves_all_content(self):
text = "Hello World\n\nThis is a test.\n\nMore content here."
result = chunk_text(text, limit=20, mode="markdown")
reconstructed = "".join(result)
assert "Hello World" in reconstructed
assert "This is a test" in reconstructed
assert "More content here" in reconstructed