"""Tests para split_text_into_chunks."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))

from split_text_into_chunks import split_text_into_chunks


def test_texto_corto_cabe_en_1_chunk():
    text = "Hello world. This is a short sentence."
    result = split_text_into_chunks(text, chunk_size=500)
    assert result == [text], f"Expected [text], got {result}"


def test_texto_largo_con_oraciones_corta_en_punto():
    # Dos oraciones claras que juntas superan chunk_size
    s1 = "This is the first sentence which ends with a period. "
    s2 = "This is the second sentence which also ends with a period."
    text = s1 + s2
    result = split_text_into_chunks(text, chunk_size=60, overlap=10)
    assert len(result) >= 2, f"Expected at least 2 chunks, got {result}"
    # El primer chunk debe terminar alrededor del punto de la primera oración
    assert result[0].endswith("period."), f"First chunk should end at sentence boundary: {result[0]!r}"


def test_texto_cjk_con_separador_ideografico():
    # Texto CJK con separador 。
    s1 = "これは最初の文です。"
    s2 = "これは二番目の文です。"
    text = s1 + s2
    # chunk_size pequeño para forzar el corte
    result = split_text_into_chunks(text, chunk_size=len(s1) + 2, overlap=2)
    assert len(result) >= 2, f"Expected at least 2 chunks for CJK text, got {result}"
    assert result[0].endswith("。"), f"First chunk should end with CJK period: {result[0]!r}"


def test_texto_sin_separadores_corta_en_chunk_size_exacto():
    # Texto sin ningún separador de oración
    text = "a" * 200
    result = split_text_into_chunks(text, chunk_size=50, overlap=0)
    # Cada chunk debe tener exactamente 50 chars (excepto el último)
    for chunk in result[:-1]:
        assert len(chunk) == 50, f"Chunk should be 50 chars, got {len(chunk)}: {chunk!r}"
    # Último puede ser menor
    assert len(result[-1]) <= 50


def test_overlap_funciona():
    # Verificar que el inicio del chunk N+1 solapa con el final del chunk N
    text = "a" * 300
    chunk_size = 100
    overlap = 20
    result = split_text_into_chunks(text, chunk_size=chunk_size, overlap=overlap)
    assert len(result) >= 2, "Need at least 2 chunks to test overlap"
    # La suma total de chars sin overlap debería ser < len(text) (hay solapamiento)
    total_chars = sum(len(c) for c in result)
    assert total_chars > len(text), f"With overlap, total chars ({total_chars}) should exceed text length ({len(text)})"


def test_texto_vacio_retorna_lista_vacia():
    assert split_text_into_chunks("") == []
    assert split_text_into_chunks("   ") == []
