111 lines
1.8 KiB
Go
111 lines
1.8 KiB
Go
|
|
package portfolio
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSplitMarkdownSections(t *testing.T) {
|
||
|
|
cases := []struct {
|
||
|
|
name string
|
||
|
|
input string
|
||
|
|
wantCount int
|
||
|
|
wantHeading []string
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
name: "frontmatter only",
|
||
|
|
input: `---
|
||
|
|
title: "Foo"
|
||
|
|
tags: ["go"]
|
||
|
|
---
|
||
|
|
|
||
|
|
# Foo
|
||
|
|
Body of foo.`,
|
||
|
|
wantCount: 2,
|
||
|
|
wantHeading: []string{"frontmatter", "Foo"},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "no frontmatter, multiple H2s",
|
||
|
|
input: `# Title
|
||
|
|
|
||
|
|
intro paragraph
|
||
|
|
|
||
|
|
## Description
|
||
|
|
|
||
|
|
description body
|
||
|
|
|
||
|
|
## Tech stack
|
||
|
|
|
||
|
|
- Go
|
||
|
|
- SQLite`,
|
||
|
|
wantCount: 3,
|
||
|
|
wantHeading: []string{"Title", "Description", "Tech stack"},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "H3 stays under parent H2",
|
||
|
|
input: `# T
|
||
|
|
|
||
|
|
## Section
|
||
|
|
|
||
|
|
content
|
||
|
|
|
||
|
|
### H3 detail
|
||
|
|
|
||
|
|
H3 content stays here`,
|
||
|
|
wantCount: 1, // T has no body → dropped; H3 content folds into Section
|
||
|
|
wantHeading: []string{"Section"},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "drop empty section",
|
||
|
|
input: `## X
|
||
|
|
|
||
|
|
ok
|
||
|
|
|
||
|
|
## Empty
|
||
|
|
|
||
|
|
## Y
|
||
|
|
|
||
|
|
content`,
|
||
|
|
wantCount: 2,
|
||
|
|
wantHeading: []string{"X", "Y"},
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: "long H2 sub-splits",
|
||
|
|
input: "## Long\n" + strings.Repeat("a ", 800),
|
||
|
|
wantCount: 2, // 2 sub-splits of Long
|
||
|
|
},
|
||
|
|
}
|
||
|
|
for _, c := range cases {
|
||
|
|
t.Run(c.name, func(t *testing.T) {
|
||
|
|
got := SplitMarkdownSections(c.input, DefaultChunkerConfig())
|
||
|
|
if len(got) != c.wantCount {
|
||
|
|
t.Errorf("got %d chunks, want %d. Headings: %v", len(got), c.wantCount, headings(got))
|
||
|
|
}
|
||
|
|
if c.wantHeading != nil {
|
||
|
|
if !equalSlice(headings(got), c.wantHeading) {
|
||
|
|
t.Errorf("headings = %v, want %v", headings(got), c.wantHeading)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func headings(sections []section) []string {
|
||
|
|
out := make([]string, len(sections))
|
||
|
|
for i, s := range sections {
|
||
|
|
out[i] = s.Heading
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
func equalSlice(a, b []string) bool {
|
||
|
|
if len(a) != len(b) {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
for i := range a {
|
||
|
|
if a[i] != b[i] {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true
|
||
|
|
}
|