2026-07-29 02:19:57 +00:00
|
|
|
package docparser
|
|
|
|
|
|
2026-08-31 06:23:39 +00:00
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/infrastructure/docparser/anydoc"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
)
|
2026-07-29 02:19:57 +00:00
|
|
|
|
2026-08-20 11:49:48 +00:00
|
|
|
func TestListAllEnginesBuiltinIncludesDocumentFormats(t *testing.T) {
|
2026-07-29 02:19:57 +00:00
|
|
|
engines := ListAllEngines(true, nil, nil)
|
|
|
|
|
for _, engine := range engines {
|
|
|
|
|
if engine.Name != "builtin" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !engine.Available {
|
|
|
|
|
t.Fatalf("builtin engine is unavailable: %s", engine.UnavailableReason)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileTypes := make(map[string]bool, len(engine.FileTypes))
|
|
|
|
|
for _, fileType := range engine.FileTypes {
|
|
|
|
|
fileTypes[fileType] = true
|
|
|
|
|
}
|
2026-08-31 05:51:12 +00:00
|
|
|
for _, want := range []string{"html", "htm", "xmind", "ppt", "pptx"} {
|
2026-07-29 02:19:57 +00:00
|
|
|
if !fileTypes[want] {
|
|
|
|
|
t.Errorf("builtin engine file types do not include %q: %v", want, engine.FileTypes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Fatal("builtin engine not found")
|
|
|
|
|
}
|
2026-08-31 06:23:39 +00:00
|
|
|
|
|
|
|
|
func TestDefaultParserEnginePrefersAnydocWhenLinked(t *testing.T) {
|
fix(parser): keep PDFs on builtin instead of defaulting to anydoc
anydoc has no document model for PDF: Convert force-disables asset
extraction, and AnydocReader falls back to builtin only when the whole
file yields no text. A born-digital PDF with figures and tables that
still yields a few characters is therefore reported as parsed, with its
images, tables and layout silently dropped.
35275d56 widened the type-level preference from ppt/pptx to every type
anydoc claims, and pdf is on that list. The predicate asks whether
anydoc can convert a type, not whether it is the better parser for it.
60207997, one commit earlier, recorded the original intent: "Types
without a fallback (pdf, docx, ...) never consult this".
The builtin PDF parser classifies pages individually and routes scanned
ones through OCR/VLM, so it stays the default for PDF. Every other
anydoc format is unchanged, and an explicit pdf -> anydoc engine rule
still wins.
2026-09-21 06:25:39 +00:00
|
|
|
cases := []string{"pptx", "ppt", "docx"}
|
2026-08-31 06:23:39 +00:00
|
|
|
if anydoc.Available() {
|
2026-08-31 06:26:58 +00:00
|
|
|
for _, ft := range cases {
|
|
|
|
|
if got := types.DefaultParserEngine(ft); got != AnydocEngineName {
|
|
|
|
|
t.Errorf("DefaultParserEngine(%s) = %q, want anydoc when the binding is linked", ft, got)
|
|
|
|
|
}
|
2026-08-31 06:23:39 +00:00
|
|
|
}
|
2026-08-31 06:26:58 +00:00
|
|
|
if got := types.DefaultParserEngine("csv"); got != "" {
|
|
|
|
|
t.Errorf("DefaultParserEngine(csv) = %q, want empty so the Go simple reader stays default", got)
|
2026-08-31 06:23:39 +00:00
|
|
|
}
|
fix(parser): keep PDFs on builtin instead of defaulting to anydoc
anydoc has no document model for PDF: Convert force-disables asset
extraction, and AnydocReader falls back to builtin only when the whole
file yields no text. A born-digital PDF with figures and tables that
still yields a few characters is therefore reported as parsed, with its
images, tables and layout silently dropped.
35275d56 widened the type-level preference from ppt/pptx to every type
anydoc claims, and pdf is on that list. The predicate asks whether
anydoc can convert a type, not whether it is the better parser for it.
60207997, one commit earlier, recorded the original intent: "Types
without a fallback (pdf, docx, ...) never consult this".
The builtin PDF parser classifies pages individually and routes scanned
ones through OCR/VLM, so it stays the default for PDF. Every other
anydoc format is unchanged, and an explicit pdf -> anydoc engine rule
still wins.
2026-09-21 06:25:39 +00:00
|
|
|
// anydoc claims pdf but cannot model one, so linking the binding must
|
|
|
|
|
// not move PDFs off builtin. This branch is where that is observable.
|
|
|
|
|
for _, ft := range []string{"pdf", ".PDF"} {
|
|
|
|
|
if got := types.DefaultParserEngine(ft); got != "" {
|
|
|
|
|
t.Errorf("DefaultParserEngine(%q) = %q, want empty so PDFs stay on builtin", ft, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-31 06:23:39 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-08-31 06:26:58 +00:00
|
|
|
if got := types.DefaultParserEngine("pptx"); got != "markitdown" {
|
2026-08-31 06:23:39 +00:00
|
|
|
t.Fatalf("DefaultParserEngine(pptx) = %q, want markitdown when anydoc is unavailable", got)
|
|
|
|
|
}
|
2026-08-31 06:26:58 +00:00
|
|
|
if got := types.DefaultParserEngine("docx"); got != "" {
|
|
|
|
|
t.Fatalf("DefaultParserEngine(docx) = %q, want empty when anydoc is unavailable", got)
|
|
|
|
|
}
|
2026-08-31 06:23:39 +00:00
|
|
|
}
|
fix(parser): keep PDFs on builtin instead of defaulting to anydoc
anydoc has no document model for PDF: Convert force-disables asset
extraction, and AnydocReader falls back to builtin only when the whole
file yields no text. A born-digital PDF with figures and tables that
still yields a few characters is therefore reported as parsed, with its
images, tables and layout silently dropped.
35275d56 widened the type-level preference from ppt/pptx to every type
anydoc claims, and pdf is on that list. The predicate asks whether
anydoc can convert a type, not whether it is the better parser for it.
60207997, one commit earlier, recorded the original intent: "Types
without a fallback (pdf, docx, ...) never consult this".
The builtin PDF parser classifies pages individually and routes scanned
ones through OCR/VLM, so it stays the default for PDF. Every other
anydoc format is unchanged, and an explicit pdf -> anydoc engine rule
still wins.
2026-09-21 06:25:39 +00:00
|
|
|
|
|
|
|
|
// The PDF exclusion only matters because anydoc still claims the type. If a
|
|
|
|
|
// future anydoc drops pdf, the exclusion becomes dead code and should go with
|
|
|
|
|
// it — this guard runs in every build, tagged or not.
|
|
|
|
|
func TestPreferAnydocSkipsPDF(t *testing.T) {
|
|
|
|
|
if !anydoc.Supports("pdf", "") {
|
|
|
|
|
t.Fatal("anydoc no longer claims pdf; drop the explicit exclusion in preferAnydocWhenAvailable")
|
|
|
|
|
}
|
|
|
|
|
for _, ft := range []string{"pdf", ".PDF", " Pdf "} {
|
|
|
|
|
if got := preferAnydocWhenAvailable(ft); got != "" {
|
|
|
|
|
t.Errorf("preferAnydocWhenAvailable(%q) = %q, want empty so PDFs stay on builtin", ft, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|