From 60eb89c12ee0661785395bff90940204b16dcb3a Mon Sep 17 00:00:00 2001
From: ax_rd <ax_rd@aisim.cn>
Date: Thu, 03 Sep 2026 13:21:39 +0800
Subject: [PATCH] docs: e2e 验证脚本 + README 更新(增量同步/explore/构建要求)

---
 internal/search/explore_test.go |   61 +++++++++++++++++++++++++++++-
 1 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/internal/search/explore_test.go b/internal/search/explore_test.go
index 125fcac..bfe2677 100644
--- a/internal/search/explore_test.go
+++ b/internal/search/explore_test.go
@@ -1,7 +1,12 @@
 package search
 
 import (
+	"path/filepath"
+	"strings"
 	"testing"
+
+	"github.com/aisim/kb-cli/internal/graph"
+	"github.com/aisim/kb-cli/internal/index"
 )
 
 // TestExploreParagraphExtraction 段落截取:只输出命中关键词的段落,整段不截半句
@@ -22,10 +27,10 @@
 	}
 }
 
-// TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆)
+// TestExploreKeywordExpansion 长 CJK 词拆 bigram(原词保留,ASCII/短词不拆;末尾去重保序)
 func TestExploreKeywordExpansion(t *testing.T) {
-	got := expandKeywords([]string{"电子秤补气失败", "补气", "abc"})
-	want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "补气", "abc"}
+	got := ExpandCJKKeywords([]string{"电子秤补气失败", "补气", "abc"})
+	want := []string{"电子秤补气失败", "电子", "子秤", "秤补", "补气", "气失", "失败", "abc"}
 	if len(got) != len(want) {
 		t.Fatalf("展开数量: got=%d want=%d (%v)", len(got), len(want), got)
 	}
@@ -43,3 +48,53 @@
 		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)
+	}
+}

--
Gitblit v1.10.0