package docparser
import (
"encoding/base64"
"strings"
"testing"
)
func TestNewMinerUReaderResolvesParseMethod(t *testing.T) {
tests := []struct {
name string
overrides map[string]string
want string
}{
{name: "default is auto", overrides: map[string]string{}, want: "auto"},
{name: "legacy enabled becomes auto", overrides: map[string]string{"mineru_enable_ocr": "true"}, want: "auto"},
{name: "legacy disabled becomes text", overrides: map[string]string{"mineru_enable_ocr": "false"}, want: "txt"},
{name: "explicit method wins", overrides: map[string]string{"mineru_parse_method": "ocr", "mineru_enable_ocr": "false"}, want: "ocr"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := NewMinerUReader(tt.overrides)
if reader.parseMethod != tt.want {
t.Fatalf("parseMethod = %q, want %q", reader.parseMethod, tt.want)
}
})
}
}
func TestNormalizeMinerUMarkdownPreservesMarkdownAndHTML(t *testing.T) {
input := strings.Join([]string{
"# Heading",
"",
"",
"",
`text_image
caption
![]() |
`) {
t.Fatalf("expected html img tag to be preserved, got: %q", got)
}
}
func TestProcessImagesKeepsReferencedVariants(t *testing.T) {
reader := &MinerUReader{}
mdContent := strings.Join([]string{
"",
`
`,
``,
}, "\n")
png := createTestPNG(200, 150)
b64 := base64.StdEncoding.EncodeToString(png)
images := map[string]string{
"cover.jpg": "data:image/png;base64," + b64,
"profile.jpg": "data:image/png;base64," + b64,
"plain.jpg": "data:image/png;base64," + b64,
}
refs, gotMarkdown := reader.processImages(mdContent, images)
if gotMarkdown != mdContent {
t.Fatalf("processImages should not rewrite markdown content")
}
if len(refs) != 3 {
t.Fatalf("expected 3 image refs, got %d", len(refs))
}
}
// TestProcessImagesMatchesPathsWithSpaces guards against a regression where
// MinerU image filenames containing spaces (common on Chinese documents,
// e.g. "images/第 1 页.jpg") would be silently dropped because the markdown
// regex used to extract refs disallowed whitespace inside the URL group.
func TestProcessImagesMatchesPathsWithSpaces(t *testing.T) {
reader := &MinerUReader{}
mdContent := ""
png := createTestPNG(200, 150)
b64 := base64.StdEncoding.EncodeToString(png)
images := map[string]string{
"第 1 页.jpg": "data:image/png;base64," + b64,
}
refs, _ := reader.processImages(mdContent, images)
if len(refs) != 1 {
t.Fatalf("expected 1 image ref for path with spaces, got %d", len(refs))
}
if refs[0].OriginalRef != "images/第 1 页.jpg" {
t.Fatalf("unexpected OriginalRef: %q", refs[0].OriginalRef)
}
}