ForcePilot/backend/test/unit/channels/test_nextcloudtalk_chunking.py

125 lines
4.5 KiB
Python
Raw Normal View History

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