|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/yoanbernabeu/grepai/config" |
| 9 | + "github.com/yoanbernabeu/grepai/search" |
| 10 | + "github.com/yoanbernabeu/grepai/store" |
| 11 | +) |
| 12 | + |
| 13 | +// MockStore is a test mock for VectorStore |
| 14 | +type MockStore struct { |
| 15 | + chunks map[string]store.Chunk |
| 16 | +} |
| 17 | + |
| 18 | +func NewMockStore() *MockStore { |
| 19 | + return &MockStore{ |
| 20 | + chunks: make(map[string]store.Chunk), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func (m *MockStore) SaveChunks(ctx context.Context, chunks []store.Chunk) error { |
| 25 | + for _, chunk := range chunks { |
| 26 | + m.chunks[chunk.ID] = chunk |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func (m *MockStore) DeleteByFile(ctx context.Context, filePath string) error { |
| 32 | + for id, chunk := range m.chunks { |
| 33 | + if chunk.FilePath == filePath { |
| 34 | + delete(m.chunks, id) |
| 35 | + } |
| 36 | + } |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func (m *MockStore) Search(ctx context.Context, queryVector []float32, limit int, opts store.SearchOptions) ([]store.SearchResult, error) { |
| 41 | + results := make([]store.SearchResult, 0) |
| 42 | + for _, chunk := range m.chunks { |
| 43 | + // Filter by path prefix if provided |
| 44 | + if opts.PathPrefix != "" && len(chunk.FilePath) < len(opts.PathPrefix) { |
| 45 | + continue |
| 46 | + } |
| 47 | + if opts.PathPrefix != "" && chunk.FilePath[:len(opts.PathPrefix)] != opts.PathPrefix { |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + // Simple similarity score based on vector |
| 52 | + score := float32(0) |
| 53 | + for i := range queryVector { |
| 54 | + if i < len(chunk.Vector) { |
| 55 | + score += queryVector[i] * chunk.Vector[i] |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + results = append(results, store.SearchResult{ |
| 60 | + Chunk: chunk, |
| 61 | + Score: score, |
| 62 | + }) |
| 63 | + |
| 64 | + if len(results) >= limit && limit > 0 { |
| 65 | + break |
| 66 | + } |
| 67 | + } |
| 68 | + return results, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (m *MockStore) GetDocument(ctx context.Context, filePath string) (*store.Document, error) { |
| 72 | + return nil, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (m *MockStore) SaveDocument(ctx context.Context, doc store.Document) error { |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (m *MockStore) DeleteDocument(ctx context.Context, filePath string) error { |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (m *MockStore) ListDocuments(ctx context.Context) ([]string, error) { |
| 84 | + return nil, nil |
| 85 | +} |
| 86 | + |
| 87 | +func (m *MockStore) Load(ctx context.Context) error { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (m *MockStore) Persist(ctx context.Context) error { |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (m *MockStore) Close() error { |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func (m *MockStore) GetStats(ctx context.Context) (*store.IndexStats, error) { |
| 100 | + return nil, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (m *MockStore) ListFilesWithStats(ctx context.Context) ([]store.FileStats, error) { |
| 104 | + return nil, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (m *MockStore) GetChunksForFile(ctx context.Context, filePath string) ([]store.Chunk, error) { |
| 108 | + return nil, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (m *MockStore) GetAllChunks(ctx context.Context) ([]store.Chunk, error) { |
| 112 | + chunks := make([]store.Chunk, 0, len(m.chunks)) |
| 113 | + for _, chunk := range m.chunks { |
| 114 | + chunks = append(chunks, chunk) |
| 115 | + } |
| 116 | + return chunks, nil |
| 117 | +} |
| 118 | + |
| 119 | +// MockEmbedder is a test mock for Embedder |
| 120 | +type MockEmbedder struct{} |
| 121 | + |
| 122 | +func (m *MockEmbedder) Embed(ctx context.Context, text string) ([]float32, error) { |
| 123 | + // Return a simple constant vector for testing |
| 124 | + return []float32{0.9, 0.1, 0.0}, nil |
| 125 | +} |
| 126 | + |
| 127 | +func (m *MockEmbedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error) { |
| 128 | + // Return constant vectors for all texts |
| 129 | + result := make([][]float32, len(texts)) |
| 130 | + for i := range texts { |
| 131 | + result[i] = []float32{0.9, 0.1, 0.0} |
| 132 | + } |
| 133 | + return result, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (m *MockEmbedder) Dimensions() int { |
| 137 | + return 3 |
| 138 | +} |
| 139 | + |
| 140 | +func (m *MockEmbedder) Close() error { |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +// TestSearcherWithPathPrefix tests the searcher integration with path prefix filtering |
| 145 | +func TestSearcherWithPathPrefix(t *testing.T) { |
| 146 | + ctx := context.Background() |
| 147 | + mockStore := NewMockStore() |
| 148 | + mockEmbedder := &MockEmbedder{} |
| 149 | + |
| 150 | + // Add test chunks |
| 151 | + chunks := []store.Chunk{ |
| 152 | + { |
| 153 | + ID: "1", |
| 154 | + FilePath: "src/handlers/user.go", |
| 155 | + StartLine: 1, |
| 156 | + EndLine: 10, |
| 157 | + Content: "func HandleUser() {}", |
| 158 | + Vector: []float32{0.9, 0.1, 0.0}, |
| 159 | + Hash: "hash1", |
| 160 | + UpdatedAt: time.Now(), |
| 161 | + }, |
| 162 | + { |
| 163 | + ID: "2", |
| 164 | + FilePath: "src/models/user.go", |
| 165 | + StartLine: 1, |
| 166 | + EndLine: 15, |
| 167 | + Content: "type User struct {}", |
| 168 | + Vector: []float32{0.8, 0.2, 0.0}, |
| 169 | + Hash: "hash2", |
| 170 | + UpdatedAt: time.Now(), |
| 171 | + }, |
| 172 | + { |
| 173 | + ID: "3", |
| 174 | + FilePath: "test/user_test.go", |
| 175 | + StartLine: 1, |
| 176 | + EndLine: 20, |
| 177 | + Content: "func TestUser() {}", |
| 178 | + Vector: []float32{0.85, 0.15, 0.0}, |
| 179 | + Hash: "hash3", |
| 180 | + UpdatedAt: time.Now(), |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + if err := mockStore.SaveChunks(ctx, chunks); err != nil { |
| 185 | + t.Fatalf("failed to save chunks: %v", err) |
| 186 | + } |
| 187 | + |
| 188 | + // Create searcher |
| 189 | + cfg := config.SearchConfig{ |
| 190 | + Boost: config.BoostConfig{}, |
| 191 | + Hybrid: config.HybridConfig{Enabled: false}, |
| 192 | + } |
| 193 | + searcher := search.NewSearcher(mockStore, mockEmbedder, cfg) |
| 194 | + |
| 195 | + tests := []struct { |
| 196 | + name string |
| 197 | + pathPrefix string |
| 198 | + wantCount int |
| 199 | + }{ |
| 200 | + { |
| 201 | + name: "no path filter returns all results", |
| 202 | + pathPrefix: "", |
| 203 | + wantCount: 3, |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "filter by src/ directory", |
| 207 | + pathPrefix: "src/", |
| 208 | + wantCount: 2, |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "filter by src/handlers/ subdirectory", |
| 212 | + pathPrefix: "src/handlers/", |
| 213 | + wantCount: 1, |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "filter by test/ directory", |
| 217 | + pathPrefix: "test/", |
| 218 | + wantCount: 1, |
| 219 | + }, |
| 220 | + { |
| 221 | + name: "filter with non-existent path", |
| 222 | + pathPrefix: "nonexistent/", |
| 223 | + wantCount: 0, |
| 224 | + }, |
| 225 | + } |
| 226 | + |
| 227 | + for _, tt := range tests { |
| 228 | + t.Run(tt.name, func(t *testing.T) { |
| 229 | + results, err := searcher.Search(ctx, "test query", 10, tt.pathPrefix) |
| 230 | + if err != nil { |
| 231 | + t.Fatalf("search failed: %v", err) |
| 232 | + } |
| 233 | + |
| 234 | + if len(results) != tt.wantCount { |
| 235 | + t.Errorf("got %d results, want %d", len(results), tt.wantCount) |
| 236 | + } |
| 237 | + |
| 238 | + // Verify all results match the path prefix |
| 239 | + for _, result := range results { |
| 240 | + if tt.pathPrefix != "" { |
| 241 | + if len(result.Chunk.FilePath) < len(tt.pathPrefix) || |
| 242 | + result.Chunk.FilePath[:len(tt.pathPrefix)] != tt.pathPrefix { |
| 243 | + t.Errorf("result %s doesn't start with prefix %s", result.Chunk.FilePath, tt.pathPrefix) |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + }) |
| 248 | + } |
| 249 | +} |
0 commit comments