-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintent_test.go
More file actions
283 lines (267 loc) · 8.62 KB
/
Copy pathintent_test.go
File metadata and controls
283 lines (267 loc) · 8.62 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
272
273
274
275
276
277
278
279
280
281
282
283
package repomap
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// makeIntentRankedFile builds a minimal RankedFile for intent scoring tests.
func makeIntentRankedFile(path, pkg string, score int, symbols []Symbol, imports []string) RankedFile {
return RankedFile{
FileSymbols: &FileSymbols{
Path: path,
Package: pkg,
Symbols: symbols,
Imports: imports,
},
Score: score,
}
}
func TestIntentScorer_NoQuery(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, nil, nil),
makeIntentRankedFile("parser.go", "repomap", 80, nil, nil),
makeIntentRankedFile("ranker.go", "repomap", 60, nil, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "")
require.Len(t, result, 3)
assert.Equal(t, "scanner.go", result[0].Path)
assert.Equal(t, "parser.go", result[1].Path)
assert.Equal(t, "ranker.go", result[2].Path)
}
func TestIntentScorer_ExactMatch(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, nil, nil),
makeIntentRankedFile("parser.go", "repomap", 80, nil, nil),
makeIntentRankedFile("auth_token.go", "repomap", 50, nil, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "token auth")
require.Len(t, result, 3)
assert.Equal(t, "auth_token.go", result[0].Path, "auth_token.go should rank highest for 'token auth'")
}
func TestIntentScorer_SymbolMatch(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, []Symbol{
{Name: "ScanFiles", Exported: true},
}, nil),
makeIntentRankedFile("parser.go", "repomap", 80, []Symbol{
{Name: "ParseGoFile", Exported: true},
{Name: "ParseSymbols", Exported: true},
}, nil),
makeIntentRankedFile("ranker.go", "repomap", 60, []Symbol{
{Name: "RankFiles", Exported: true},
}, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "parse symbols")
require.Len(t, result, 3)
assert.Equal(t, "parser.go", result[0].Path, "parser.go should rank highest for 'parse symbols'")
}
func TestIntentScorer_PluralMatch(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, nil, nil),
makeIntentRankedFile("token.go", "repomap", 80, []Symbol{
{Name: "Token", Exported: true},
{Name: "TokenType", Exported: true},
}, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "tokens")
require.Len(t, result, 2)
assert.Equal(t, "token.go", result[0].Path, "token.go should rank highest for 'tokens' (plural match)")
}
func TestIntentScorer_PrefixMatch(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, nil, nil),
makeIntentRankedFile("authentication.go", "repomap", 50, []Symbol{
{Name: "Authenticate", Exported: true},
{Name: "Authorize", Exported: true},
}, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "auth")
// "auth" is only 4 chars, so prefix matching kicks in with min 4 chars
require.Len(t, result, 2)
assert.Equal(t, "authentication.go", result[0].Path, "authentication.go should rank highest for 'auth' (prefix match)")
}
func TestIntentScorer_Negation(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("parser.go", "repomap", 100, []Symbol{
{Name: "ParseGoFile", Exported: true},
}, nil),
makeIntentRankedFile("parser_test.go", "repomap", 80, []Symbol{
{Name: "TestParseGoFile", Exported: false},
}, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "parser not test")
require.Len(t, result, 2)
assert.Equal(t, "parser.go", result[0].Path, "parser.go should rank above parser_test.go when negating 'test'")
}
func TestIntentScorer_BigramBonus(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("token.go", "repomap", 100, []Symbol{
{Name: "Token", Exported: true},
}, nil),
makeIntentRankedFile("refresh.go", "repomap", 100, []Symbol{
{Name: "Refresh", Exported: true},
}, nil),
makeIntentRankedFile("token_refresh.go", "repomap", 100, []Symbol{
{Name: "TokenRefresh", Exported: true},
{Name: "RefreshToken", Exported: true},
}, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "token refresh")
require.Len(t, result, 3)
assert.Equal(t, "token_refresh.go", result[0].Path, "token_refresh.go should rank highest for bigram 'token refresh'")
}
func TestIntentScorer_PreservesStructuralRanking(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("main.go", "main", 200, []Symbol{
{Name: "Run", Exported: true},
}, nil),
makeIntentRankedFile("obscure.go", "repomap", 10, nil, nil),
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "zzzzunmatchedquery")
require.Len(t, result, 2)
// main.go should still be first — no BM25 match means multiplier is 1.0 for all
assert.Equal(t, "main.go", result[0].Path, "high structural score should be preserved when no BM25 match")
}
func TestIntentScorer_CamelCaseSplit(t *testing.T) {
t.Parallel()
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, nil, nil),
makeIntentRankedFile("parser.go", "repomap", 80, []Symbol{
{Name: "ParseGoFile", Exported: true},
}, nil),
}
scorer := NewIntentScorer(ranked)
// "parse" should match "ParseGoFile" via camelCase split
result := scorer.Score(ranked, "parse")
require.Len(t, result, 2)
assert.Equal(t, "parser.go", result[0].Path, "parser.go should rank highest via CamelCase 'parse' from ParseGoFile")
}
func TestTokenizeIntent(t *testing.T) {
t.Parallel()
tests := []struct {
input string
want []string
}{
{"parse symbols", []string{"parse", "symbols"}},
{"token-refresh handler", []string{"token-refresh", "handler"}},
{"fix the parser", []string{"parser"}}, // "fix" and "the" are stopwords; "parser" is kept
{"BM25 ranking", []string{"bm25", "ranking"}},
{"", nil},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
t.Parallel()
got := tokenizeIntent(tc.input)
assert.Equal(t, tc.want, got)
})
}
}
func TestTokenizeCamelCase(t *testing.T) {
t.Parallel()
tests := []struct {
input string
want []string
}{
{"ParseGoFile", []string{"parse", "go", "file"}},
{"http_client", []string{"http", "client"}},
{"ALLCAPS", []string{"allcaps"}},
{"NewIntentScorer", []string{"new", "intent", "scorer"}},
{"BM25Score", []string{"bm25score"}},
{"simple", []string{"simple"}},
{"", nil},
}
for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
t.Parallel()
got := tokenizeCamelCase(tc.input)
assert.Equal(t, tc.want, got)
})
}
}
func TestIntentScorer_DocOnlyMatch(t *testing.T) {
t.Parallel()
// All three files have equal base scores; the query terms appear ONLY in the
// Doc string of session.go's exported symbol — not in any basename, package
// name, symbol name, or import path. The BM25 doc-field boost must be
// sufficient to rank session.go first.
ranked := []RankedFile{
makeIntentRankedFile("scanner.go", "repomap", 100, []Symbol{
{Name: "ScanFiles", Exported: true},
}, nil),
makeIntentRankedFile("ranker.go", "repomap", 100, []Symbol{
{Name: "RankFiles", Exported: true},
}, nil),
{
FileSymbols: &FileSymbols{
Path: "session.go",
Package: "repomap",
Symbols: []Symbol{
{Name: "Retry", Exported: true, Doc: "validates jwt tokens and refreshes sessions"},
},
},
Score: 100,
},
}
scorer := NewIntentScorer(ranked)
result := scorer.Score(ranked, "jwt refresh")
require.Len(t, result, 3)
assert.Equal(t, "session.go", result[0].Path, "session.go should rank first: doc string is the only source of jwt/refresh terms")
}
func TestExtractNegated(t *testing.T) {
t.Parallel()
tests := []struct {
input []string
keep []string
negated []string
}{
{
input: []string{"parser", "not", "test"},
keep: []string{"parser"},
negated: []string{"test"},
},
{
input: []string{"auth", "without", "vendor"},
keep: []string{"auth"},
negated: []string{"vendor"},
},
{
input: []string{"scanner", "except", "bench"},
keep: []string{"scanner"},
negated: []string{"bench"},
},
{
input: []string{"token", "no", "cache"},
keep: []string{"token"},
negated: []string{"cache"},
},
{
input: []string{"parser", "scanner"},
keep: []string{"parser", "scanner"},
negated: nil,
},
}
for _, tc := range tests {
t.Run(tc.input[0], func(t *testing.T) {
t.Parallel()
keep, negated := extractNegated(tc.input)
assert.Equal(t, tc.keep, keep)
assert.Equal(t, tc.negated, negated)
})
}
}