import numpy as np

from veena3modal.processing.long_text_processor import LongTextProcessor


def _pcm_bytes(value: int, samples: int) -> bytes:
    arr = (np.ones(samples, dtype=np.int16) * value).astype(np.int16)
    return arr.tobytes()


def test_should_chunk_threshold_behavior():
    p = LongTextProcessor(pipeline=object())
    assert p.should_chunk("a" * p.CHUNKING_THRESHOLD) is False
    assert p.should_chunk("a" * (p.CHUNKING_THRESHOLD + 1)) is True


def test_chunk_text_delegates_to_internal_chunker():
    p = LongTextProcessor(pipeline=object())
    chunks = p.chunk_text("Hello world.")
    assert chunks == ["Hello world."]


def test_stitch_audio_chunks_crossfades_and_reduces_length():
    p = LongTextProcessor(pipeline=object())

    sample_rate = 1000
    crossfade_ms = 10  # -> 10 samples -> 20 bytes overlap
    crossfade_bytes = int((crossfade_ms / 1000.0) * sample_rate) * 2

    c1 = _pcm_bytes(100, samples=100)  # 200 bytes
    c2 = _pcm_bytes(200, samples=100)  # 200 bytes

    out = p.stitch_audio_chunks([c1, c2], sample_rate=sample_rate, crossfade_ms=crossfade_ms)
    # Output should be shorter than concatenation by exactly the overlap.
    assert len(out) == len(c1) + len(c2) - crossfade_bytes


