WeKnora/internal/searchutil/imageinfo_html_test.go

194 lines
6.4 KiB
Go
Raw Normal View History

fix(docparser): resolve remote images referenced by HTML img tags (#2355) Remote http(s) images written as <img src="..."> reached no resolver. Markdown images went through ResolveRemoteImages, HTML data-URIs and HTML relative paths each had their own pass, but nothing claimed the remaining combination, so those images were dropped without a log line and the multimodal stage was skipped for documents that rely on HTML for layout. ResolveRemoteImages now scans HTML img tags with a quoted src as a second pass and rewrites only the src attribute value, leaving the tag intact because the surrounding markup is why these documents use HTML at all. The per-image work (whitelist, SSRF, download, icon filter, upload) moves into one helper so the two syntaxes cannot drift apart on policy. Each pass carries its own image budget; sharing one would let HTML images eat into the number of Markdown images a document already gets resolved. An attribute value is trimmed, HTML-unescaped and its scheme lowercased before use, because HTML permits padding, spells a query separator &amp; and tolerates any scheme case, while every fetcher downstream compares the scheme byte-for-byte. Markdown targets get none of that normalization, so no Markdown reference resolves differently than it does today. A whitelisted host keeps serving the image from where it already lives, and its span is rewritten with the normalized URL rather than the raw attribute text: later stages both locate the image by searching the document for its serving URL and fetch that same string, so the two have to agree. The src attribute must now be preceded by whitespace, so a hyphenated attribute name is no longer mistaken for it. Among HTML tags this cuts two ways: one carrying both a hyphenated *-src and a real src now resolves the real src instead of the placeholder, while one carrying only a hyphenated *-src is no longer resolved at all. Recovering the latter needs an attribute parser rather than a regex, and resolving a lazy-loading placeholder while the real image goes unvisited is the worse of the two failures. Both halves are pinned by tests. Image-derived text also has to survive into the answer, which is why this touches internal/searchutil. The chat enricher and the image_info filters matched Markdown links only, so an HTML-only passage looked image-free: every entry was dropped and the model received bare markup with the caption and OCR text stripped, even though the analysis had run. Both now recognise HTML img tags through one shared regex, so a tag the parser stores can always be matched back. Both syntaxes are located against the original passage and spliced in a single pass; scanning one regex over the other's output let a metadata block that itself quotes an image reference be rescanned and duplicated. The chat variant still enriches only images present in the passage, so orphan entries stay out. Known limits: - An unquoted src and a srcset-only tag are not matched, nor is a tag whose attribute values hold a bare > or a quoted src= of their own. - imgHTMLDataURI still matches src without a boundary, so a data-src holding a base64 placeholder is consumed as if it were the src and the whole tag replaced. Worth the same fix, but it changes what an existing lazy-loading document resolves, so it belongs on its own. - A reference shown as a documentation example is rewritten like any other. Recognising code spans needs a Markdown parser; a hand-rolled scanner risks the far worse failure of treating a document's real screenshots as examples. - Rewriting a whitelisted src writes the unescaped URL back into an HTML attribute, so a query parameter whose name spells an entity renders differently than the author wrote it. Keeping the encoded form would mean unescaping on every matcher instead. - Image text reaches the model only when the whole reference falls inside the retrieved window; an img tag is several times longer than a Markdown link and straddles a chunk boundary more often. - A reference repeated in one document is fetched once per occurrence, which is what gives each chunk its own analysed copy. - Nothing bounds how long one document's fetches may take in total. - Parent-context pruning removes only Markdown images. Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:08:31 +00:00
package searchutil
import (
"encoding/json"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
func imageInfoJSON(t *testing.T, infos ...types.ImageInfo) string {
t.Helper()
data, err := json.Marshal(infos)
if err != nil {
t.Fatalf("marshal image info: %v", err)
}
return string(data)
}
func TestImageURLsInContent_CollectsHTMLSrc(t *testing.T) {
cases := []struct {
name string
content string
want string
}{
{"markdown", "![a](local://images/a.png)", "local://images/a.png"},
{"html double quotes", `<img src="local://images/b.png">`, "local://images/b.png"},
{"html single quotes", `<img src='local://images/c.png'>`, "local://images/c.png"},
{"html with attributes", `<img class="x" src="local://images/d.png" alt="y">`, "local://images/d.png"},
{"html uppercase", `<IMG SRC="local://images/e.png">`, "local://images/e.png"},
{"html padded", `<img src=" local://images/f.png ">`, "local://images/f.png"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
urls := ImageURLsInContent(tc.content)
if !urls[tc.want] {
t.Errorf("expected %q to be collected from %q, got %v", tc.want, tc.content, urls)
}
})
}
}
func TestFilterImageInfoByContentURLs_KeepsHTMLReferencedEntry(t *testing.T) {
info := types.ImageInfo{
URL: "local://images/shot.png",
Caption: "the login screen",
OCRText: "Sign in with SSO",
}
// A passage whose images are HTML tags used to look image-free, which made
// the filter drop every entry it was given.
got := FilterImageInfoByContentURLs(
`<p><img src="local://images/shot.png" alt="login"></p>`,
imageInfoJSON(t, info),
)
if got == "" {
t.Fatal("expected the entry to be kept for an HTML-only passage")
}
var kept []types.ImageInfo
if err := json.Unmarshal([]byte(got), &kept); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if len(kept) != 1 || kept[0].URL != info.URL {
t.Errorf("unexpected filter result: %+v", kept)
}
}
func TestFilterImageInfoByContentURLs_StillDropsUnreferenced(t *testing.T) {
got := FilterImageInfoByContentURLs(
`<p><img src="local://images/present.png"></p>`,
imageInfoJSON(t, types.ImageInfo{URL: "local://images/absent.png", OCRText: "elsewhere"}),
)
if got != "" {
t.Errorf("expected an unreferenced entry to be dropped, got %s", got)
}
}
func TestEnrichContentWithImageInfoForChat_HTMLTag(t *testing.T) {
info := types.ImageInfo{
URL: "local://images/shot.png",
Caption: "the login screen",
OCRText: "Sign in with SSO",
}
content := `<p><img src="local://images/shot.png" alt="login"></p>`
got := EnrichContentWithImageInfoForChat(content, imageInfoJSON(t, info))
if !strings.Contains(got, "Sign in with SSO") {
t.Errorf("OCR text was not injected for an HTML image: %s", got)
}
if !strings.Contains(got, "the login screen") {
t.Errorf("caption was not injected for an HTML image: %s", got)
}
if !strings.Contains(got, `<img src="local://images/shot.png"`) {
t.Errorf("the original tag should be preserved: %s", got)
}
}
func TestEnrichContentWithImageInfoForChat_MarkdownUnchanged(t *testing.T) {
info := types.ImageInfo{
URL: "local://images/shot.png",
OCRText: "Sign in with SSO",
}
content := "![login](local://images/shot.png)"
got := EnrichContentWithImageInfoForChat(content, imageInfoJSON(t, info))
if !strings.Contains(got, "![login](local://images/shot.png)") {
t.Errorf("markdown image should be preserved: %s", got)
}
if !strings.Contains(got, "Sign in with SSO") {
t.Errorf("markdown enrichment regressed: %s", got)
}
}
func TestFilterImageInfoByMatchRange_HTMLTagInsideWindow(t *testing.T) {
info := types.ImageInfo{URL: "local://images/shot.png", OCRText: "Sign in with SSO"}
parent := `intro text
<img src="local://images/shot.png">
tail text`
// Window covering the tag: the entry survives.
got := FilterImageInfoByMatchRange(parent, 0, 0, len([]rune(parent)), imageInfoJSON(t, info))
if got == "" {
t.Error("expected the entry to survive a window containing the whole tag")
}
// Window that cuts the tag in half: nothing matches, which is the known
// limitation — an HTML tag is several times longer than ![](url) and
// straddles a child window far more often.
if got := FilterImageInfoByMatchRange(parent, 0, 0, 20, imageInfoJSON(t, info)); got != "" {
t.Errorf("expected a truncated tag not to match, got %s", got)
}
}
func TestEnrichContentWithImageInfoForChat_DoesNotRescanInjectedMetadata(t *testing.T) {
// The OCR text of a screenshot that documents an <img> tag contains one.
// Scanning the injected metadata would splice a second block into the middle
// of the first.
info := types.ImageInfo{
URL: "local://images/x.png",
OCRText: `use <img src="local://images/x.png"> to embed`,
}
content := "![a](local://images/x.png)"
got := EnrichContentWithImageInfoForChat(content, imageInfoJSON(t, info))
if n := strings.Count(got, "**Image text (OCR):**"); n != 1 {
t.Errorf("expected exactly 1 metadata block, got %d:\n%s", n, got)
}
if !strings.Contains(got, "to embed") {
t.Errorf("OCR sentence was truncated:\n%s", got)
}
}
func TestEnrichContentWithImageInfoForChat_BothSyntaxesOneBlockEach(t *testing.T) {
info := types.ImageInfo{URL: "local://images/x.png", Caption: "shot"}
content := "![a](local://images/x.png)\n\n<img src=\"local://images/x.png\">"
got := EnrichContentWithImageInfoForChat(content, imageInfoJSON(t, info))
if n := strings.Count(got, "**Image caption:** shot"); n != 2 {
t.Errorf("expected one block per reference (2), got %d:\n%s", n, got)
}
}
func TestHTMLImageSrcRegex_HyphenatedAttributeIsNotSrc(t *testing.T) {
urls := ImageURLsInContent(`<img data-src="local://images/placeholder.png" src="local://images/real.png">`)
if urls["local://images/placeholder.png"] {
t.Error("data-src must not be collected as src")
}
if !urls["local://images/real.png"] {
t.Errorf("src was not collected: %v", urls)
}
}
func TestEnrichContentWithImageInfoForChat_UnmatchedImageStaysBare(t *testing.T) {
// The chat variant deliberately enriches only images that appear in the
// passage, so an entry for some other page must not be appended.
info := types.ImageInfo{URL: "local://images/other.png", OCRText: "another page"}
content := `<p><img src="local://images/shot.png"></p>`
got := EnrichContentWithImageInfoForChat(content, imageInfoJSON(t, info))
if got != content {
t.Errorf("expected content untouched, got %s", got)
}
if strings.Contains(got, "another page") {
t.Errorf("an orphan entry leaked into the passage: %s", got)
}
}