129 lines
3.8 KiB
Go
129 lines
3.8 KiB
Go
package brain
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSplitByHeadings_H1(t *testing.T) {
|
|
text := "# Abschnitt A\nText A\n# Abschnitt B\nText B"
|
|
sections := splitByHeadings(text)
|
|
if len(sections) != 2 {
|
|
t.Fatalf("erwartet 2 Abschnitte, bekam %d", len(sections))
|
|
}
|
|
if !strings.Contains(sections[0], "Abschnitt A") {
|
|
t.Errorf("Abschnitt 0 enthält nicht 'Abschnitt A': %q", sections[0])
|
|
}
|
|
if !strings.Contains(sections[1], "Abschnitt B") {
|
|
t.Errorf("Abschnitt 1 enthält nicht 'Abschnitt B': %q", sections[1])
|
|
}
|
|
}
|
|
|
|
func TestSplitByHeadings_H2(t *testing.T) {
|
|
text := "## Intro\nText\n## Detail\nMehr Text"
|
|
sections := splitByHeadings(text)
|
|
if len(sections) != 2 {
|
|
t.Fatalf("erwartet 2 Abschnitte, bekam %d", len(sections))
|
|
}
|
|
}
|
|
|
|
func TestSplitByHeadings_H3(t *testing.T) {
|
|
text := "### Unterabschnitt A\nText A\n### Unterabschnitt B\nText B"
|
|
sections := splitByHeadings(text)
|
|
if len(sections) != 2 {
|
|
t.Fatalf("H3-Split: erwartet 2 Abschnitte, bekam %d", len(sections))
|
|
}
|
|
}
|
|
|
|
func TestSplitByHeadings_Mixed(t *testing.T) {
|
|
text := "# H1\nText\n## H2\nText2\n### H3\nText3"
|
|
sections := splitByHeadings(text)
|
|
if len(sections) != 3 {
|
|
t.Fatalf("gemischter Split: erwartet 3, bekam %d", len(sections))
|
|
}
|
|
}
|
|
|
|
func TestSplitByHeadings_NoHeadings(t *testing.T) {
|
|
text := "Kein Heading hier\nNur Text."
|
|
sections := splitByHeadings(text)
|
|
if len(sections) != 1 {
|
|
t.Fatalf("ohne Headings: erwartet 1 Abschnitt, bekam %d", len(sections))
|
|
}
|
|
}
|
|
|
|
func TestSplitLongSection_ShortPassthrough(t *testing.T) {
|
|
short := strings.Repeat("x", maxChunkSize-1)
|
|
chunks := splitLongSection(short)
|
|
if len(chunks) != 1 {
|
|
t.Fatalf("kurzer Abschnitt: erwartet 1 Chunk, bekam %d", len(chunks))
|
|
}
|
|
if chunks[0] != short {
|
|
t.Errorf("Inhalt verändert")
|
|
}
|
|
}
|
|
|
|
func TestSplitLongSection_SplitsByParagraph(t *testing.T) {
|
|
// Erzeuge zwei Paragraphen, die zusammen > maxChunkSize sind
|
|
para := strings.Repeat("a", maxChunkSize/2+10)
|
|
text := para + "\n\n" + para
|
|
chunks := splitLongSection(text)
|
|
if len(chunks) < 2 {
|
|
t.Fatalf("erwarte >= 2 Chunks für überlangen Text, bekam %d", len(chunks))
|
|
}
|
|
for _, c := range chunks {
|
|
if len(c) > maxChunkSize {
|
|
t.Errorf("Chunk überschreitet maxChunkSize: len=%d", len(c))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSplitLongSection_SingleLongParagraph(t *testing.T) {
|
|
// Ein einzelner Paragraph > maxChunkSize kann nicht weiter gesplittet werden
|
|
long := strings.Repeat("b", maxChunkSize+50)
|
|
chunks := splitLongSection(long)
|
|
if len(chunks) != 1 {
|
|
t.Fatalf("einzelner langer Paragraph: erwartet 1 Chunk, bekam %d", len(chunks))
|
|
}
|
|
}
|
|
|
|
func TestGenerateID_Deterministic(t *testing.T) {
|
|
id1 := generateID("text", "source.md")
|
|
id2 := generateID("text", "source.md")
|
|
if id1 != id2 {
|
|
t.Errorf("IDs nicht deterministisch: %s != %s", id1, id2)
|
|
}
|
|
}
|
|
|
|
func TestGenerateID_DifferentForDifferentInput(t *testing.T) {
|
|
id1 := generateID("text A", "source.md")
|
|
id2 := generateID("text B", "source.md")
|
|
if id1 == id2 {
|
|
t.Errorf("verschiedene Texte ergeben gleiche ID")
|
|
}
|
|
}
|
|
|
|
func TestGenerateID_DifferentSourceSameText(t *testing.T) {
|
|
id1 := generateID("same text", "file1.md")
|
|
id2 := generateID("same text", "file2.md")
|
|
if id1 == id2 {
|
|
t.Errorf("verschiedene Quellen mit gleichem Text ergeben gleiche ID")
|
|
}
|
|
}
|
|
|
|
func TestReadAndChunk_FiltersShortSections(t *testing.T) {
|
|
// readAndChunk filtert Abschnitte < 20 Zeichen
|
|
// Wir testen splitByHeadings + splitLongSection direkt
|
|
content := "# A\nKurz\n# Abschnitt mit genug Text zum Indizieren hier"
|
|
sections := splitByHeadings(content)
|
|
var kept []string
|
|
for _, s := range sections {
|
|
s = strings.TrimSpace(s)
|
|
if len(s) >= 20 {
|
|
kept = append(kept, s)
|
|
}
|
|
}
|
|
if len(kept) != 1 {
|
|
t.Fatalf("erwartet 1 beibehaltenen Abschnitt, bekam %d", len(kept))
|
|
}
|
|
}
|