2906c6656e
Scope image info to matched text children during parent resolution and add range/URL filtering utilities with unit tests.
96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package searchutil
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
func TestSliceContentByDocumentRange(t *testing.T) {
|
|
parent := "aaaPAGE1bbbPAGE2ccc"
|
|
got := SliceContentByDocumentRange(parent, 100, 103, 108)
|
|
want := "PAGE1"
|
|
if got != want {
|
|
t.Fatalf("slice: got %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestFilterImageInfoByMatchRange(t *testing.T) {
|
|
parent := "\n\n\n\n"
|
|
matchStart := len([]rune("\n\n"))
|
|
matchEnd := matchStart + len([]rune(""))
|
|
all := []types.ImageInfo{
|
|
{URL: "u1"}, {URL: "u2"}, {URL: "u3"},
|
|
}
|
|
raw, err := json.Marshal(all)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := FilterImageInfoByMatchRange(parent, 0, matchStart, matchEnd, string(raw))
|
|
var filtered []types.ImageInfo
|
|
if err := json.Unmarshal([]byte(got), &filtered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(filtered) != 1 || filtered[0].URL != "u2" {
|
|
t.Fatalf("filtered: %+v", filtered)
|
|
}
|
|
}
|
|
|
|
func TestFilterImageInfoByContentURLs(t *testing.T) {
|
|
content := "intro\n\noutro"
|
|
all := []types.ImageInfo{
|
|
{URL: "local://img1.jpg", OCRText: "one"},
|
|
{URL: "local://img3.jpg", OCRText: "three"},
|
|
}
|
|
raw, err := json.Marshal(all)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := FilterImageInfoByContentURLs(content, string(raw))
|
|
var filtered []types.ImageInfo
|
|
if err := json.Unmarshal([]byte(got), &filtered); err != nil {
|
|
t.Fatalf("unmarshal filtered: %v", err)
|
|
}
|
|
if len(filtered) != 1 || filtered[0].URL != "local://img3.jpg" {
|
|
t.Fatalf("filtered: %+v", filtered)
|
|
}
|
|
}
|
|
|
|
func TestPruneMarkdownImagesOutsideRange(t *testing.T) {
|
|
parent := "\n\n\n\n"
|
|
matchStart := len([]rune("\n\n"))
|
|
matchEnd := matchStart + len([]rune(""))
|
|
got := PruneMarkdownImagesOutsideRange(parent, 0, matchStart, matchEnd)
|
|
if got != "" {
|
|
t.Fatalf("prune: got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestEnrichContentWithImageInfoForChat_SkipsUnmatched(t *testing.T) {
|
|
content := "\n\n"
|
|
raw, _ := json.Marshal([]types.ImageInfo{{URL: "u2", OCRText: "two"}})
|
|
got := EnrichContentWithImageInfoForChat(content, string(raw))
|
|
if strings.Count(got, "<image url=") != 1 {
|
|
t.Fatalf("expected one image block, got: %s", got)
|
|
}
|
|
if !strings.Contains(got, "") {
|
|
t.Fatalf("unmatched markdown should remain: %s", got)
|
|
}
|
|
if !strings.Contains(got, "<image_ocr>two</image_ocr>") {
|
|
t.Fatalf("matched image should be enriched: %s", got)
|
|
}
|
|
if strings.Contains(got, "<image_original>") {
|
|
t.Fatalf("chat enrich should not duplicate markdown in image_original: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestImageURLsInContent(t *testing.T) {
|
|
content := " x "
|
|
urls := ImageURLsInContent(content)
|
|
if !urls["u1"] || !urls["u2"] || len(urls) != 2 {
|
|
t.Fatalf("urls: %#v", urls)
|
|
}
|
|
}
|