-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_test.go
More file actions
271 lines (248 loc) · 6.7 KB
/
Copy pathfind_test.go
File metadata and controls
271 lines (248 loc) · 6.7 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package repomap
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// findTestRoot walks up from cwd to find the repo root (go.mod).
func findTestRoot(t *testing.T) string {
t.Helper()
dir, err := os.Getwd()
require.NoError(t, err)
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
t.Skip("cannot find repo root")
}
dir = parent
}
}
func TestParseFindQuery(t *testing.T) {
t.Parallel()
cases := []struct {
input string
wantName string
wantKind string
wantFile string
}{
{"Config", "Config", "", ""},
{"kind:struct:Config", "Config", "struct", ""},
{"file:parser:Parse", "Parse", "", "parser"},
{"kind:struct:file:cli:Root", "Root", "struct", "cli"},
{"file:cli:kind:struct:Root", "Root", "struct", "cli"},
{"", "", "", ""},
{" ", "", "", ""},
{"kind:func:New", "New", "func", ""},
}
for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
t.Parallel()
name, kind, file := ParseFindQuery(tc.input)
assert.Equal(t, tc.wantName, name, "name")
assert.Equal(t, tc.wantKind, kind, "kind")
assert.Equal(t, tc.wantFile, file, "file")
})
}
}
func TestFindSymbol(t *testing.T) {
t.Parallel()
root := findTestRoot(t)
m := New(root, DefaultConfig())
require.NoError(t, m.Build(context.Background()))
t.Run("exact match", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("Map", "", "")
require.NotEmpty(t, hits)
// First result must be exact (score=100).
assert.Equal(t, float64(100), hits[0].Score)
// Should come from repomap.go.
found := false
for _, h := range hits {
if h.Score == 100 && strings.Contains(h.File, "repomap.go") {
found = true
break
}
}
assert.True(t, found, "expected exact Map hit in repomap.go")
})
t.Run("case-insensitive exact", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("map", "", "")
require.NotEmpty(t, hits)
hasCI := false
for _, h := range hits {
if h.Score == 75 {
hasCI = true
break
}
}
assert.True(t, hasCI, "expected at least one score=75 case-insensitive hit")
})
t.Run("prefix match", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("Find", "", "")
require.NotEmpty(t, hits)
// FindSymbol itself should appear with score >= 50 (prefix match).
found := false
for _, h := range hits {
if h.Symbol.Name == "FindSymbol" {
assert.GreaterOrEqual(t, h.Score, float64(50), "FindSymbol must score as prefix or better")
found = true
break
}
}
assert.True(t, found, "expected FindSymbol in prefix results")
// The first result must be prefix-or-better (sorted by score desc).
assert.GreaterOrEqual(t, hits[0].Score, float64(50), "top result must be prefix or better")
})
t.Run("contains match", func(t *testing.T) {
t.Parallel()
// "Rank" is contained in RankFiles, RankedFile, etc.
hits := m.FindSymbol("ank", "", "")
require.NotEmpty(t, hits)
for _, h := range hits {
assert.Equal(t, float64(25), h.Score)
}
})
t.Run("kind filter", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("Config", "struct", "")
require.NotEmpty(t, hits)
for _, h := range hits {
assert.Equal(t, "struct", h.Symbol.Kind)
}
})
t.Run("file filter", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("New", "", "ranker")
for _, h := range hits {
assert.Contains(t, h.File, "ranker")
}
})
t.Run("combined kind and file", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("Map", "struct", "repomap.go")
require.NotEmpty(t, hits)
for _, h := range hits {
assert.Equal(t, "struct", h.Symbol.Kind)
assert.Contains(t, h.File, "repomap.go")
}
})
t.Run("empty name returns empty slice", func(t *testing.T) {
t.Parallel()
hits := m.FindSymbol("", "", "")
assert.NotNil(t, hits)
assert.Empty(t, hits)
})
t.Run("unbuilt map no panic", func(t *testing.T) {
t.Parallel()
fresh := New(".", DefaultConfig())
hits := fresh.FindSymbol("X", "", "")
assert.NotNil(t, hits)
assert.Empty(t, hits)
})
t.Run("tiebreaker ordering", func(t *testing.T) {
t.Parallel()
// Build a minimal in-memory Map with two same-named symbols in two files
// with different scores to verify sort order.
lowFile := &FileSymbols{
Path: "z_low_score.go",
Symbols: []Symbol{{Name: "Foo", Kind: "func"}},
}
highFile := &FileSymbols{
Path: "a_high_score.go",
Symbols: []Symbol{{Name: "Foo", Kind: "func"}},
}
tm := &Map{}
tm.ranked = []RankedFile{
{FileSymbols: lowFile, Score: 10},
{FileSymbols: highFile, Score: 100},
}
hits := tm.FindSymbol("Foo", "", "")
require.Len(t, hits, 2)
// Higher-scored file must come first when symbol score ties.
assert.Equal(t, "a_high_score.go", hits[0].File)
assert.Equal(t, "z_low_score.go", hits[1].File)
assert.Empty(t, hits[0].Handle)
assert.Equal(t, "file:a_high_score.go", hits[0].FileHandle)
})
}
func TestFindSymbolHandle(t *testing.T) {
t.Parallel()
tm := &Map{}
tm.ranked = []RankedFile{{
FileSymbols: &FileSymbols{
Path: "service.go",
Symbols: []Symbol{{
Name: "Run",
Kind: "function",
Line: 12,
}},
},
Score: 100,
DetailLevel: 2,
}}
hits := tm.FindSymbol("symbol:service.go::Run#function@12", "", "")
require.Len(t, hits, 1)
assert.Equal(t, "service.go", hits[0].File)
assert.Equal(t, "Run", hits[0].Symbol.Name)
assert.Equal(t, "symbol:service.go::Run#function@12", hits[0].Handle)
}
func TestFindSymbolConcurrentIncrementalBuild(t *testing.T) {
dir := newGitRepo(t)
cacheDir := t.TempDir()
for i := range 4 {
path := filepath.Join(dir, fmt.Sprintf("lib%d.py", i))
src := fmt.Sprintf("def helper_%d():\n return %d\n", i, i)
require.NoError(t, os.WriteFile(path, []byte(src), 0o644))
}
gitCommitAll(t, dir, "add non-Go fixtures")
m := buildWithCache(t, dir, cacheDir)
done := make(chan struct{})
errCh := make(chan error, 1)
var readers sync.WaitGroup
for range 2 {
readers.Add(1)
go func() {
defer readers.Done()
for {
select {
case <-done:
return
default:
}
m.FindSymbol("Hello", "", "")
m.FindSymbolHandle("main.go", "Hello", "function", 3)
}
}()
}
for i := range 20 {
src := fmt.Sprintf("def helper_0():\n return %d\n", i)
if err := os.WriteFile(filepath.Join(dir, "lib0.py"), []byte(src), 0o644); err != nil {
errCh <- err
break
}
if err := m.Build(context.Background()); err != nil {
errCh <- err
break
}
}
close(done)
readers.Wait()
select {
case err := <-errCh:
require.NoError(t, err)
default:
}
require.NotEmpty(t, m.FindSymbol("Hello", "", ""))
require.Len(t, m.FindSymbolHandle("main.go", "Hello", "function", 3), 1)
}