Files
ragflow/internal/service/file/file_content_test.go
Jack 965590ccbe Refactor: dataset/document/file service (#17071)
### Summary

Refactor dataset.go document.do file.go file2document.go in
internal/service.
2026-07-20 09:48:24 +08:00

61 lines
1.8 KiB
Go

package file
import (
"ragflow/internal/utility"
"testing"
)
func TestBytesLooksLikePDF(t *testing.T) {
cases := []struct {
name string
data []byte
want bool
}{
{"valid header", []byte("%PDF-1.4 content"), true},
{"too short", []byte("%PD"), false},
{"plain text", []byte("hello"), false},
{"nil", nil, false},
}
for _, c := range cases {
if got := utility.BytesLooksLikePDF(c.data); got != c.want {
t.Errorf("%s: utility.BytesLooksLikePDF = %v, want %v", c.name, got, c.want)
}
}
}
func TestLooksLikeHTML(t *testing.T) {
if !utility.LooksLikeHTML([]byte("<html><body>x</body></html>")) {
t.Error("should detect <html>")
}
if !utility.LooksLikeHTML([]byte("<DIV>x</DIV>")) {
t.Error("should detect <div> case-insensitively")
}
if !utility.LooksLikeHTML([]byte("<body>x</body>")) {
t.Error("should detect <body>")
}
if utility.LooksLikeHTML([]byte("just text")) {
t.Error("should not detect plain text")
}
}
func TestParseFileContent_HTMLOutputFormat(t *testing.T) {
// CSV parser produces OutputFormat "html".
result := parseFileContent("data.csv", []byte("a,b,c\n1,2,3\n"))
if result == "" || result == string([]byte("a,b,c\n1,2,3\n")) {
t.Skip("CSV parser not available or returned raw text; integration-only test")
}
// CSV parser emits an HTML table; must not contain raw CSV comma-separated rows.
if result == "a,b,c\n1,2,3\n" {
t.Errorf("CSV should produce HTML output, got raw CSV: %q", result)
}
}
func TestParseFileContent_MarkdownOutputFormat(t *testing.T) {
// .md files fall through to default parser or produce text/markdown.
result := parseFileContent("doc.md", []byte("# Title\n\nBody"))
// This is integration-dependent; verify it doesn't crash and returns something.
if result == "" {
t.Skip("markdown parser returned empty (integration-only)")
}
}