1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| package search
|
| import (
| "path/filepath"
| "strings"
| "testing"
|
| "github.com/aisim/kb-cli/internal/graph"
| "github.com/aisim/kb-cli/internal/index"
| )
|
| func TestSearch(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()
|
| // 插入测试数据
| nodes := []*graph.Node{
| {
| ID: 1,
| Path: "FAQ/充装类/001-test.md",
| Title: "充装问题排查",
| Section: "FAQ",
| Tags: []string{"充装"},
| Content: "关于智能枪充装问题的排查方法",
| },
| {
| ID: 2,
| Path: "知识/002-config.md",
| Title: "充装规格配置",
| Section: "知识",
| Tags: []string{"配置"},
| Content: "充装规格配置说明",
| },
| }
|
| for _, n := range nodes {
| _, err := store.InsertNode(n)
| if err != nil {
| t.Fatalf("InsertNode failed: %v", err)
| }
| }
|
| // 创建 FTS 索引
| if err := store.CreateFTS(); err != nil {
| t.Fatalf("CreateFTS failed: %v", err)
| }
| if err := store.PopulateFTS(); err != nil {
| t.Fatalf("PopulateFTS failed: %v", err)
| }
|
| // 测试搜索
| results, err := Search(store, []string{"充装"}, SearchOptions{})
| if err != nil {
| t.Fatalf("Search failed: %v", err)
| }
|
| if len(results) == 0 {
| t.Error("Expected results, got none")
| }
|
| // 验证结果包含关键词
| found := false
| for _, r := range results {
| if strings.Contains(r.Title, "充装") || strings.Contains(r.Path, "充装") {
| found = true
| break
| }
| }
| if !found {
| t.Error("Expected results to contain '充装'")
| }
| }
|
| func TestIsGenericWord(t *testing.T) {
| tests := []struct {
| word string
| expected bool
| }{
| {"问题", true},
| {"故障", true},
| {"充装", false},
| {"智能枪", false},
| }
|
| for _, tt := range tests {
| result := IsGenericWord(tt.word)
| if result != tt.expected {
| t.Errorf("IsGenericWord(%q) = %v, want %v", tt.word, result, tt.expected)
| }
| }
| }
|
|