import pytest
import importlib
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

chunk = importlib.import_module("contexthub.chunk")
chunk_by_paragraphs = chunk.chunk_by_paragraphs
chunk_by_sentences = chunk.chunk_by_sentences
chunk_by_separator = chunk.chunk_by_separator
chunk_by_tokens = chunk.chunk_by_tokens


def word_tokenizer(text: str) -> int:
    return len(text.split())


def test_chunk_by_tokens_basic_and_overlap():
    text = "one two three four five six seven"
    chunks = chunk_by_tokens(text, max_tokens=3, overlap=1, tokenizer=word_tokenizer)
    assert chunks == ["one two three", "three four five", "five six seven"]


def test_chunk_by_tokens_edge_cases():
    assert chunk_by_tokens("", max_tokens=3, tokenizer=word_tokenizer) == []
    assert chunk_by_tokens("single", max_tokens=3, tokenizer=word_tokenizer) == [
        "single"
    ]
    with pytest.raises(ValueError):
        chunk_by_tokens("a b", max_tokens=0, tokenizer=word_tokenizer)
    with pytest.raises(ValueError):
        chunk_by_tokens("a b", max_tokens=2, overlap=2, tokenizer=word_tokenizer)


def test_chunk_by_sentences():
    text = "First sentence. Second sentence! Third sentence? Fourth sentence."
    chunks = chunk_by_sentences(text, max_sentences=2)
    assert chunks == [
        "First sentence. Second sentence!",
        "Third sentence? Fourth sentence.",
    ]
    assert chunk_by_sentences("", max_sentences=2) == []


def test_chunk_by_paragraphs():
    text = "para1\n\npara2\n\n\npara3"
    assert chunk_by_paragraphs(text) == ["para1", "para2", "para3"]
    assert chunk_by_paragraphs("") == []


def test_chunk_by_separator():
    text = "a|b||c"
    assert chunk_by_separator(text, separator="|") == ["a", "b", "c"]
    assert chunk_by_separator("", separator="|") == []
    with pytest.raises(ValueError):
        chunk_by_separator("abc", separator="")
