Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

2.6 KB
Raw Blame
package document

import (
	"testing"

	"ragflow/internal/entity"
)

// TestCloneParserConfigForDocument verifies that cloneParserConfigForDocument must
// deep-copy the nested maps a database load hands out, so writing a component
// entry onto one document's config cannot leak into the dataset's config or a
// sibling document's config.
func TestCloneParserConfigForDocument(t *testing.T) {
	if clone := cloneParserConfigForDocument(nil); clone == nil || len(clone) != 0 {
		t.Fatalf("nil config must clone to an empty object, got %#v", clone)
	}

	original := entity.JSONMap{
		"Parser:HipSignsRhyme": map[string]interface{}{
			"spreadsheet": map[string]interface{}{"output_format": "html"},
		},
		"topn": 10,
	}

	clone := cloneParserConfigForDocument(original)

	nested, ok := clone["Parser:HipSignsRhyme"].(map[string]interface{})
	if !ok {
		t.Fatalf("clone lost the component entry: %#v", clone["Parser:HipSignsRhyme"])
	}
	ss, ok := nested["spreadsheet"].(map[string]interface{})
	if !ok {
		t.Fatalf("clone lost the spreadsheet setup: %#v", nested["spreadsheet"])
	}
	ss["output_format"] = "json"
	clone["topn"] = 42

	origNested := original["Parser:HipSignsRhyme"].(map[string]interface{})["spreadsheet"].(map[string]interface{})
	if origNested["output_format"] != "html" {
		t.Fatalf("original nested map was mutated: %#v", origNested)
	}
	if original["topn"] != 10 {
		t.Fatalf("original top-level map was aliased: topn=%#v", original["topn"])
	}
}

func TestNormalizeWebDocumentName(t *testing.T) {
	pdfBlob := []byte("%PDF-1.4 fake")
	htmlBlob := []byte("<html><body>hi</body></html>")
	cases := []struct {
		name, filename, ct string
		blob               []byte
		want               string
		wantOK             bool
	}{
		{"pdf detected by blob", "report", "application/octet-stream", pdfBlob, "report.pdf", true},
		{"html detected by blob", "page", "application/octet-stream", htmlBlob, "page.html", true},
		{"dot stripped by utility, no type hint", "image.png", "application/octet-stream", []byte("plain"), "imagepng", true},
		{"no hint, no extension", "doc", "application/json", []byte("{}"), "doc", true},
		{"cjk-only name with pdf content", "界界", "application/pdf", pdfBlob, "", false},
		{"cjk-only name without type hint", "界界", "application/json", []byte("{}"), "", false},
	}
	for _, c := range cases {
		got, ok := normalizeWebDocumentName(c.filename, c.ct, c.blob)
		if ok != c.wantOK {
			t.Errorf("%s: normalizeWebDocumentName ok = %v, want %v", c.name, ok, c.wantOK)
		}
		if got != c.want {
			t.Errorf("%s: normalizeWebDocumentName = %q, want %q", c.name, got, c.want)
		}
	}
}