53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package materialindex_test
|
||
|
||
import (
|
||
"context"
|
||
"strings"
|
||
"testing"
|
||
|
||
"chat-agent-system/internal/materialindex"
|
||
)
|
||
|
||
func TestBuildTOCAndFormatRAG(t *testing.T) {
|
||
md := "<!-- page 1 -->\n## 需求概述\n正文A\n<!-- page 2 -->\n## 技术参数\n正文B\n"
|
||
toc := materialindex.BuildTOC("spec.pdf", md)
|
||
if !strings.Contains(toc, "[p.1]") || !strings.Contains(toc, "需求概述") {
|
||
t.Fatalf("toc=%s", toc)
|
||
}
|
||
rag := materialindex.FormatRAGMarkdown("spec.pdf", []materialindex.ChunkHit{{
|
||
FileName: "spec.pdf",
|
||
Title: "技术参数",
|
||
Content: "关键参数 220V",
|
||
PageStart: 2,
|
||
PageEnd: 2,
|
||
Score: 0.9,
|
||
}})
|
||
if !strings.Contains(rag, "〔材料: spec.pdf §技术参数 p.2〕") {
|
||
t.Fatalf("rag=%s", rag)
|
||
}
|
||
}
|
||
|
||
func TestStoreGatedHelpers(t *testing.T) {
|
||
// nil pool(内存降级模式):store 能力必须为 false,防止无状态机时打真实 API。
|
||
p := materialindex.NewPipeline(nil, nil)
|
||
if p.HasStore() {
|
||
t.Fatalf("nil pool must report HasStore=false")
|
||
}
|
||
ctx := context.Background()
|
||
if got := p.GetParseInfos(ctx, []string{"abc"}); len(got) != 0 {
|
||
t.Fatalf("GetParseInfos without store must be empty, got %v", got)
|
||
}
|
||
if p.ResetForReparse(ctx, "abc") {
|
||
t.Fatalf("ResetForReparse without store must be false")
|
||
}
|
||
// EnqueueAsync 无 store 时必须静默跳过(不 panic、不触网)。
|
||
p.EnqueueAsync("doc.pdf", "abc", []byte("%PDF-1.4"), 1)
|
||
}
|
||
|
||
func TestFormatRAG_Empty(t *testing.T) {
|
||
out := materialindex.FormatRAGMarkdown("x.pdf", nil)
|
||
if !strings.Contains(out, "未命中") {
|
||
t.Fatalf("out=%s", out)
|
||
}
|
||
}
|