package com.cloudhandson.vpdbackoffice.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

class VectorChunkerTest {

  @Test
  void keepsParagraphsTogetherUntilTheConfiguredLimit() {
    var chunks = VectorChunker.chunk(
        "첫 번째 문단입니다.\n\n두 번째 문단은 같은 지식자료입니다.", 80);

    assertThat(chunks).hasSize(1);
    assertThat(chunks.get(0).chunkNo()).isEqualTo(1);
    assertThat(chunks.get(0).text()).contains("첫 번째", "두 번째");
  }

  @Test
  void splitsLongTextAndNumbersChunksFromOne() {
    var chunks = VectorChunker.chunk("문장 하나입니다. ".repeat(30), 100);

    assertThat(chunks).hasSizeGreaterThan(1);
    assertThat(chunks).extracting("chunkNo").containsExactlyElementsOf(
        java.util.stream.IntStream.rangeClosed(1, chunks.size()).boxed().toList());
    assertThat(chunks).allSatisfy(chunk -> assertThat(chunk.text()).isNotBlank());
  }

  @Test
  void rejectsBlankContent() {
    assertThatThrownBy(() -> VectorChunker.chunk("  ", 600))
        .isInstanceOf(AppException.class)
        .hasMessageContaining("청킹할 문서 본문");
  }
}
