Files
ai_academy/internal/searchutil/imageinfo_match_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

222 lines
7.0 KiB
Go

package searchutil
import (
"encoding/json"
"strings"
"testing"
"academy/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 := "![p1](u1)\n\n![p2](u2)\n\n![p3](u3)"
matchStart := len([]rune("![p1](u1)\n\n"))
matchEnd := matchStart + len([]rune("![p2](u2)"))
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![page3](local://img3.jpg)\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 := "![p1](u1)\n\n![p2](u2)\n\n![p3](u3)"
matchStart := len([]rune("![p1](u1)\n\n"))
matchEnd := matchStart + len([]rune("![p2](u2)"))
got := PruneMarkdownImagesOutsideRange(parent, 0, matchStart, matchEnd)
if got != "![p2](u2)" {
t.Fatalf("prune: got %q", got)
}
}
func TestPruneMarkdownImagesByImageInfoIgnoresShiftedOffsets(t *testing.T) {
content := "a manually inserted prefix that shifts every parser offset\n\n" +
"![p1](u1)\n\nbody\n\n![p2](u2)"
raw, err := json.Marshal([]types.ImageInfo{{URL: "u2"}})
if err != nil {
t.Fatal(err)
}
got := PruneMarkdownImagesByImageInfo(content, string(raw))
if strings.Contains(got, "u1") {
t.Fatalf("unscoped image remained: %q", got)
}
if !strings.Contains(got, "![p2](u2)") {
t.Fatalf("scoped image was removed: %q", got)
}
}
func TestEnrichContentWithImageInfoForChat_SkipsUnmatched(t *testing.T) {
content := "![p1](u1)\n\n![p2](u2)"
raw, _ := json.Marshal([]types.ImageInfo{{URL: "u2", OCRText: "two"}})
got := EnrichContentWithImageInfoForChat(content, string(raw))
if strings.Contains(got, "<image") {
t.Fatalf("chat context should not contain internal image XML: %s", got)
}
if !strings.Contains(got, "![p1](u1)") {
t.Fatalf("unmatched markdown should remain: %s", got)
}
if !strings.Contains(got, "![p2](u2)") {
t.Fatalf("matched markdown should remain renderable: %s", got)
}
if !strings.Contains(got, "> **Image text (OCR):** two") {
t.Fatalf("matched image should be enriched: %s", got)
}
if strings.Count(got, "![") != 2 {
t.Fatalf("chat enrich should not duplicate markdown images: %s", got)
}
}
func TestEnrichContentWithImageInfoForChat_UsesMarkdownForMultilineMetadata(t *testing.T) {
content := "before\n\n![flow](resource://AbCdEfGhIjKlMnOpQrStUv)\n\nafter"
raw, _ := json.Marshal([]types.ImageInfo{
{
URL: "resource://AbCdEfGhIjKlMnOpQrStUv",
Caption: "目标说话人提取流程图",
OCRText: "输入\n目标说话人提取\n输出",
},
})
got := EnrichContentWithImageInfoForChat(content, string(raw))
for _, want := range []string{
"![flow](resource://AbCdEfGhIjKlMnOpQrStUv)",
"> **Image caption:** 目标说话人提取流程图",
"> **Image text (OCR):** 输入",
"> 目标说话人提取",
"> 输出",
} {
if !strings.Contains(got, want) {
t.Fatalf("expected %q in enriched Markdown:\n%s", want, got)
}
}
if strings.Contains(got, "<image") {
t.Fatalf("chat context should be Markdown-only for image content: %s", got)
}
}
func TestEnrichContentWithImageInfoForChat_EnrichesRepeatedImagesOnceEach(t *testing.T) {
content := "![same](u1)\n\n![same](u1)"
raw, _ := json.Marshal([]types.ImageInfo{{URL: "u1", Caption: "same caption"}})
got := EnrichContentWithImageInfoForChat(content, string(raw))
if strings.Count(got, "![same](u1)") != 2 {
t.Fatalf("expected both Markdown images to remain: %s", got)
}
if strings.Count(got, "> **Image caption:** same caption") != 2 {
t.Fatalf("expected each image to be enriched exactly once: %s", got)
}
}
func TestBuildImageInfoMarkdownWithURL(t *testing.T) {
got := BuildImageInfoMarkdownWithURL(
"resource://AbCdEfGhIjKlMnOpQrStUv",
&types.ImageInfo{Caption: "流程图 [测试]", OCRText: "输入\n输出"},
)
for _, want := range []string{
`![流程图 \[测试\]](resource://AbCdEfGhIjKlMnOpQrStUv)`,
"> **Image caption:** 流程图 [测试]",
"> **Image text (OCR):** 输入",
"> 输出",
} {
if !strings.Contains(got, want) {
t.Fatalf("expected %q in image Markdown:\n%s", want, got)
}
}
if strings.Contains(got, "<image") {
t.Fatalf("LLM-facing image context must not use image XML: %s", got)
}
}
func TestImageURLsInContent(t *testing.T) {
content := "![a](u1) x ![b](u2)"
urls := ImageURLsInContent(content)
if !urls["u1"] || !urls["u2"] || len(urls) != 2 {
t.Fatalf("urls: %#v", urls)
}
}
func TestClearImageInfoTextMatchingBody_ClearsOnlyHitField(t *testing.T) {
raw, err := json.Marshal([]types.ImageInfo{
{URL: "u1", OCRText: "page one ocr body", Caption: "page one caption"},
{URL: "u2", OCRText: "page two ocr body", Caption: "page two caption"},
})
if err != nil {
t.Fatal(err)
}
got := ClearImageInfoTextMatchingBody(string(raw), "page one ocr body", string(types.ChunkTypeImageOCR))
var infos []types.ImageInfo
if err := json.Unmarshal([]byte(got), &infos); err != nil {
t.Fatal(err)
}
if len(infos) != 2 {
t.Fatalf("entries: %d", len(infos))
}
if infos[0].URL != "u1" || infos[0].OCRText != "" || infos[0].Caption != "page one caption" {
t.Fatalf("hit entry: %+v", infos[0])
}
if infos[1].URL != "u2" || infos[1].OCRText != "page two ocr body" {
t.Fatalf("sibling entry stripped: %+v", infos[1])
}
gotCaption := ClearImageInfoTextMatchingBody(string(raw), "page two caption", string(types.ChunkTypeImageCaption))
if err := json.Unmarshal([]byte(gotCaption), &infos); err != nil {
t.Fatal(err)
}
if infos[1].Caption != "" || infos[1].OCRText != "page two ocr body" || infos[0].Caption != "page one caption" {
t.Fatalf("caption clear: %+v", infos)
}
}
func TestClearImageInfoTextMatchingBody_UnchangedWhenNoMatch(t *testing.T) {
raw, err := json.Marshal([]types.ImageInfo{{URL: "u1", OCRText: "kept ocr"}})
if err != nil {
t.Fatal(err)
}
got := ClearImageInfoTextMatchingBody(string(raw), "other body", string(types.ChunkTypeImageOCR))
if got != string(raw) {
t.Fatalf("expected original JSON, got %q", got)
}
if got := ClearImageInfoTextMatchingBody("", "body", string(types.ChunkTypeImageOCR)); got != "" {
t.Fatalf("empty JSON: %q", got)
}
}