package search
|
|
import (
|
"path/filepath"
|
"strings"
|
"testing"
|
|
"github.com/aisim/kb-cli/internal/graph"
|
"github.com/aisim/kb-cli/internal/index"
|
)
|
|
// TestExploreParagraphExtraction 段落截取:只输出命中关键词的段落,整段不截半句
|
func TestExploreParagraphExtraction(t *testing.T) {
|
content := "# 标题\n\n第一段讲称重。\n\n## 补气流程\n\n补气失败时先检查阀门。\n\n## 其他\n\n无关内容。\n"
|
got := extractRelevantParagraphs(content, []string{"补气"}, 100)
|
want := "## 补气流程\n\n补气失败时先检查阀门。\n"
|
if got != want {
|
t.Errorf("段落截取:\n got=%q\nwant=%q", got, want)
|
}
|
}
|
|
// TestExploreWholeDocWhenSmall 文档短于预算时整篇输出
|
func TestExploreWholeDocWhenSmall(t *testing.T) {
|
got := extractRelevantParagraphs("短文档\n", []string{"不存在"}, 10000)
|
if got != "短文档\n" {
|
t.Errorf("应整篇输出: %q", got)
|
}
|
}
|
|
// TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆;末尾去重保序)
|
func TestExploreKeywordExpansion(t *testing.T) {
|
got := expandKeywords([]string{"电子秤补气失败", "补气", "abc"})
|
want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "abc"}
|
if len(got) != len(want) {
|
t.Fatalf("展开数量: got=%d want=%d (%v)", len(got), len(want), got)
|
}
|
for i := range want {
|
if got[i] != want[i] {
|
t.Errorf("第 %d 项: got=%q want=%q", i, got[i], want[i])
|
}
|
}
|
}
|
|
func TestExploreBudget(t *testing.T) {
|
// 用 extractRelevantParagraphs 的预算版验证:预算 20 字节,命中段落 30 字节 → 输出空(宁缺毋滥,不截半段)
|
got := extractWithBudget("## 段落\n\n这是一段超过预算的内容啊\n", []string{"段落"}, 20)
|
if got != "" {
|
t.Errorf("超预算段落应跳过: %q", got)
|
}
|
}
|
|
// TestExploreFallbackToExpanded 回归(审查 Important):单复合 CJK 词在大文档中
|
// 原文不连续出现(bigram 召回入选,但原词在任何段落都不出现)时,
|
// 段落截取必须回退到展开词,不能静默丢弃文档。
|
func TestExploreFallbackToExpanded(t *testing.T) {
|
tmpDir := t.TempDir()
|
dbPath := filepath.Join(tmpDir, "test.db")
|
store, err := index.Open(dbPath)
|
if err != nil {
|
t.Fatalf("Open failed: %v", err)
|
}
|
defer store.Close()
|
|
// 大文档(> 2000 字节预算):某段落只含「电子秤补气」片段,不含完整原词「电子秤补气失败」
|
filler := strings.Repeat("填充段落内容,用于把文档撑过预算。\n", 60)
|
content := "# 称重故障排查\n\n" + filler + "\n## 称重故障\n\n电子秤补气 时先检查阀门,失败则断电重启。\n"
|
|
n := &graph.Node{
|
ID: 1,
|
Path: "FAQ/称重/001-test.md",
|
Title: "称重故障排查",
|
Section: "FAQ",
|
Content: content,
|
}
|
if _, err := store.InsertNode(n); err != nil {
|
t.Fatalf("InsertNode failed: %v", err)
|
}
|
if err := store.CreateFTS(); err != nil {
|
t.Fatalf("CreateFTS failed: %v", err)
|
}
|
if err := store.PopulateFTS(); err != nil {
|
t.Fatalf("PopulateFTS failed: %v", err)
|
}
|
|
// 原词提取必为空(内容里没有完整的「电子秤补气失败」)
|
if b := extractRelevantParagraphs(content, []string{"电子秤补气失败"}, 2000); b != "" {
|
t.Fatalf("前提不成立:原词提取应为空,got=%q", b)
|
}
|
|
res, err := Explore(store, []string{"电子秤补气失败"}, ExploreOptions{Budget: 2000, TopN: 5})
|
if err != nil {
|
t.Fatalf("Explore failed: %v", err)
|
}
|
if len(res.Docs) != 1 {
|
t.Fatalf("应召回 1 篇文档(bigram 命中),got=%d", len(res.Docs))
|
}
|
if !strings.Contains(res.Docs[0].Body, "电子秤补气") {
|
t.Errorf("回退展开词后应提取含片段段落: body=%q", res.Docs[0].Body)
|
}
|
}
|