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重连相关测试
152 lines
5.1 KiB
Python
152 lines
5.1 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
from yuxi.channels.adapters.slack.chunker import (
|
|
ChunkMode,
|
|
TextChunk,
|
|
convert_table_to_bullets,
|
|
resolve_text_chunks,
|
|
)
|
|
|
|
|
|
class TestTextChunk:
|
|
def test_creation(self):
|
|
chunk = TextChunk(text="hello", index=0, is_last=True)
|
|
assert chunk.text == "hello"
|
|
assert chunk.index == 0
|
|
assert chunk.is_last is True
|
|
|
|
def test_is_last_false(self):
|
|
chunk = TextChunk(text="part1", index=0, is_last=False)
|
|
assert chunk.is_last is False
|
|
|
|
|
|
class TestChunkMode:
|
|
def test_values(self):
|
|
assert ChunkMode.LENGTH == "length"
|
|
assert ChunkMode.NEWLINE == "newline"
|
|
|
|
|
|
class TestConvertTableToBullets:
|
|
def test_basic_table(self):
|
|
text = "| Name | Age |\n|------|-----|\n| Alice | 30 |"
|
|
result = convert_table_to_bullets(text)
|
|
assert "*Name:* Alice | *Age:* 30" in result
|
|
|
|
def test_table_with_extra_spaces(self):
|
|
text = "| Col1 | Col2 |\n|--------|--------|\n| val1 | val2 |"
|
|
result = convert_table_to_bullets(text)
|
|
assert "*Col1:* val1" in result
|
|
assert "*Col2:* val2" in result
|
|
|
|
def test_not_a_table_no_pipe(self):
|
|
text = "Hello\nWorld"
|
|
assert convert_table_to_bullets(text) == "Hello\nWorld"
|
|
|
|
def test_not_a_table_no_separator(self):
|
|
text = "| A | B |\n| C | D |"
|
|
assert convert_table_to_bullets(text) == "| A | B |\n| C | D |"
|
|
|
|
def test_single_line(self):
|
|
text = "single line"
|
|
assert convert_table_to_bullets(text) == "single line"
|
|
|
|
def test_empty_text(self):
|
|
assert convert_table_to_bullets("") == ""
|
|
|
|
def test_three_column_table(self):
|
|
text = "| Name | Age | City |\n|------|-----|------|\n| Bob | 25 | NYC |"
|
|
result = convert_table_to_bullets(text)
|
|
assert "*Name:* Bob" in result
|
|
assert "*Age:* 25" in result
|
|
assert "*City:* NYC" in result
|
|
|
|
def test_table_with_empty_cells(self):
|
|
text = "| A | B |\n|---|---|\n| x | |"
|
|
result = convert_table_to_bullets(text)
|
|
assert "N/A" in result
|
|
|
|
|
|
class TestResolveTextChunks:
|
|
def test_empty_text(self):
|
|
assert resolve_text_chunks("", 100) == []
|
|
|
|
def test_text_within_limit(self):
|
|
chunks = resolve_text_chunks("short text", 100)
|
|
assert len(chunks) == 1
|
|
assert chunks[0].text == "short text"
|
|
assert chunks[0].index == 0
|
|
assert chunks[0].is_last is True
|
|
|
|
def test_text_at_exact_limit(self):
|
|
text = "A" * 10
|
|
chunks = resolve_text_chunks(text, 10)
|
|
assert len(chunks) == 1
|
|
assert chunks[0].text == text
|
|
|
|
def test_length_mode_chunks(self):
|
|
text = "A" * 50
|
|
chunks = resolve_text_chunks(text, 10, mode=ChunkMode.LENGTH)
|
|
assert len(chunks) == 5
|
|
for i, chunk in enumerate(chunks):
|
|
assert len(chunk.text) <= 10
|
|
assert chunk.index == i
|
|
assert chunks[-1].is_last is True
|
|
|
|
def test_newline_mode_chunks(self):
|
|
text = "line1\nline2\nline3\nline4\nline5"
|
|
chunks = resolve_text_chunks(text, 14, mode=ChunkMode.NEWLINE)
|
|
assert len(chunks) >= 2
|
|
for chunk in chunks:
|
|
assert len(chunk.text) <= 14
|
|
assert chunks[-1].is_last is True
|
|
|
|
def test_newline_mode_single_line(self):
|
|
text = "a" * 50
|
|
chunks = resolve_text_chunks(text, 10, mode=ChunkMode.NEWLINE)
|
|
assert len(chunks) >= 1
|
|
for chunk in chunks:
|
|
assert len(chunk.text) <= 10
|
|
|
|
def test_length_mode_prefers_newline_split(self):
|
|
text = "hello world\nfoo bar baz qux"
|
|
chunks = resolve_text_chunks(text, 15, mode=ChunkMode.LENGTH)
|
|
assert len(chunks) >= 2
|
|
assert chunks[0].text == "hello world"
|
|
|
|
def test_length_mode_falls_back_to_space_split(self):
|
|
text = "hello world q" * 10
|
|
chunks = resolve_text_chunks(text, 10, mode=ChunkMode.LENGTH)
|
|
for chunk in chunks:
|
|
assert len(chunk.text) <= 10
|
|
|
|
def test_table_conversion_disabled(self):
|
|
text = "| Name | Age |\n|------|-----|\n| Alice | 30 |"
|
|
chunks = resolve_text_chunks(text, 200, convert_table=False)
|
|
assert "| Name" in chunks[0].text
|
|
assert "*Name:*" not in chunks[0].text
|
|
|
|
def test_table_conversion_causes_expansion(self):
|
|
text = "| Name | Age |\n|------|-----|\n| Alice | 30 |"
|
|
chunks = resolve_text_chunks(text, 200, convert_table=True)
|
|
assert "*Name:* Alice" in chunks[0].text
|
|
|
|
def test_length_mode_long_text_no_newline(self):
|
|
text = "a" * 100
|
|
chunks = resolve_text_chunks(text, 10, mode=ChunkMode.LENGTH)
|
|
assert len(chunks) == 10
|
|
for chunk in chunks:
|
|
assert len(chunk.text) == 10
|
|
|
|
def test_single_chunk_is_last(self):
|
|
chunks = resolve_text_chunks("hello", 100)
|
|
assert len(chunks) == 1
|
|
assert chunks[0].is_last is True
|
|
|
|
def test_newline_exact_boundary(self):
|
|
text = "line1\nline2"
|
|
chunks = resolve_text_chunks(text, 6, mode=ChunkMode.NEWLINE)
|
|
for chunk in chunks:
|
|
assert len(chunk.text) <= 6
|
|
assert chunks[-1].is_last is True
|