-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboundary_test.go
More file actions
327 lines (296 loc) · 9.19 KB
/
Copy pathboundary_test.go
File metadata and controls
327 lines (296 loc) · 9.19 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package repomap
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClassifyBoundaries_Basic(t *testing.T) {
t.Parallel()
labels, bump := classifyBoundaries("go", []string{"net/http", "encoding/json"})
require.Equal(t, []string{"HTTP"}, labels)
assert.Equal(t, 5, bump)
}
func TestClassifyBoundaries_Multiple(t *testing.T) {
t.Parallel()
labels, bump := classifyBoundaries("go", []string{"net/http", "github.com/jackc/pgx/v5"})
require.Equal(t, []string{"HTTP", "Postgres"}, labels)
assert.Equal(t, 10, bump)
}
func TestClassifyBoundaries_Cap(t *testing.T) {
t.Parallel()
// HTTP(5) + Postgres(5) + Redis(5) + Kafka(5) = 20 → capped at 15
imports := []string{
"net/http",
"github.com/jackc/pgx/v5",
"github.com/redis/go-redis/v9",
"github.com/segmentio/kafka-go",
}
labels, bump := classifyBoundaries("go", imports)
assert.Equal(t, 4, len(labels), "expected four boundary labels")
assert.Equal(t, maxBoundaryBump, bump, "bump should be capped at maxBoundaryBump")
}
func TestClassifyBoundaries_Dedupe(t *testing.T) {
t.Parallel()
// Both net/http and chi match the HTTP rule — label emitted once, bump counted once.
labels, bump := classifyBoundaries("go", []string{"net/http", "github.com/go-chi/chi/v5"})
require.Equal(t, []string{"HTTP"}, labels)
assert.Equal(t, 5, bump)
}
func TestClassifyBoundaries_NoMatch(t *testing.T) {
t.Parallel()
labels, bump := classifyBoundaries("go", []string{"encoding/json", "fmt"})
assert.Empty(t, labels)
assert.Equal(t, 0, bump)
}
func TestApplyBoundaryBoost(t *testing.T) {
t.Parallel()
httpPgx := &FileSymbols{
Path: "handler.go",
Language: "go",
Imports: []string{"net/http", "github.com/jackc/pgx/v5"},
Symbols: []Symbol{{Name: "Handle", Kind: "function", Exported: true}},
}
plain := &FileSymbols{
Path: "util.go",
Language: "go",
Imports: []string{"encoding/json", "fmt"},
Symbols: []Symbol{{Name: "Format", Kind: "function", Exported: true}},
}
ranked := []RankedFile{
{FileSymbols: httpPgx, Score: 10},
{FileSymbols: plain, Score: 10},
}
applyBoundaryBoost(ranked)
// HTTP + Postgres = +10
assert.Equal(t, 20, ranked[0].Score)
assert.Equal(t, []string{"HTTP", "Postgres"}, ranked[0].Boundaries)
// no boundary match — score and Boundaries unchanged
assert.Equal(t, 10, ranked[1].Score)
assert.Empty(t, ranked[1].Boundaries)
}
func TestRenderDetail_BoundaryLabels(t *testing.T) {
t.Parallel()
// Use symbol/path names that don't overlap with boundary label strings,
// so compact-mode assertions are unambiguous.
sym := Symbol{Name: "Handle", Kind: "method", Exported: true, Receiver: "*Server"}
withBoundaries := RankedFile{
FileSymbols: &FileSymbols{
Path: "server/handler.go",
Language: "go",
Symbols: []Symbol{sym},
},
DetailLevel: 2,
Boundaries: []string{"HTTP", "Postgres"},
}
withoutBoundaries := RankedFile{
FileSymbols: &FileSymbols{
Path: "server/handler.go",
Language: "go",
Symbols: []Symbol{sym},
},
DetailLevel: 2,
}
t.Run("detail mode shows boundary labels", func(t *testing.T) {
t.Parallel()
out := formatFileBlockDetail(withBoundaries, false)
assert.Contains(t, out, "[HTTP, Postgres]")
})
t.Run("verbose mode shows boundary labels", func(t *testing.T) {
t.Parallel()
out := formatFileBlockVerbose(withBoundaries, false)
assert.Contains(t, out, "[HTTP, Postgres]")
})
t.Run("compact mode does not show boundary labels", func(t *testing.T) {
t.Parallel()
// withoutBoundaries: same file but no Boundaries set — compact never shows them.
out := formatFileBlockCompact(withoutBoundaries, nil, false)
assert.NotContains(t, out, "HTTP")
assert.NotContains(t, out, "Postgres")
// Also verify that even when Boundaries are set, compact does not render them.
outWithSet := formatFileBlockCompact(withBoundaries, nil, false)
assert.NotContains(t, outWithSet, "[HTTP")
})
}
// TestClassifyBoundaries_NonGoImports verifies that non-Go style import paths
// (TypeScript/Python/Rust package names) do not match Go-only boundary rules
// and return empty labels without panicking. The boundary rules table is
// intentionally Go-centric; future work may add per-language tables.
func TestClassifyBoundaries_NonGoImports(t *testing.T) {
t.Parallel()
tests := []struct {
name string
imports []string
}{
{
name: "TypeScript_HTTP",
imports: []string{"express", "fastify", "koa", "hono"},
},
{
name: "TypeScript_DB",
imports: []string{"prisma", "@prisma/client", "drizzle-orm", "pg"},
},
{
name: "Python_HTTP",
imports: []string{"fastapi", "flask", "django", "starlette"},
},
{
name: "Python_DB",
imports: []string{"sqlalchemy", "asyncpg", "psycopg2", "tortoise"},
},
{
name: "Rust_HTTP",
imports: []string{"axum", "actix-web", "warp", "rocket"},
},
{
name: "Rust_DB",
imports: []string{"sqlx", "diesel", "sea-orm"},
},
{
name: "empty",
imports: []string{},
},
{
name: "nil",
imports: nil,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
labels, bump := classifyBoundaries("", tc.imports)
assert.Empty(t, labels, "non-Go imports must not match Go boundary rules")
assert.Equal(t, 0, bump, "non-Go imports must produce zero score bump")
})
}
}
// TestApplyBoundaryBoost_LanguagesWithRules verifies that applyBoundaryBoost
// applies boundary labels for languages that have defined boundary rules
// (TypeScript, Python, Rust) and does not panic or produce labels for
// languages without rules (unknown "cobol").
func TestApplyBoundaryBoost_LanguagesWithRules(t *testing.T) {
t.Parallel()
tests := []struct {
name string
language string
imports []string
wantLabels []string
wantBump int
}{
{
name: "TypeScript express+prisma",
language: "typescript",
imports: []string{"express", "prisma"},
wantLabels: []string{"HTTP", "DB"},
wantBump: 10,
},
{
name: "Python fastapi+sqlalchemy",
language: "python",
imports: []string{"fastapi", "sqlalchemy"},
wantLabels: []string{"HTTP", "DB"},
wantBump: 10,
},
{
name: "Rust axum+sqlx",
language: "rust",
imports: []string{"axum", "sqlx"},
wantLabels: []string{"HTTP", "DB"},
wantBump: 10,
},
{
name: "unknown language no rules",
language: "cobol",
imports: []string{"anything"},
wantLabels: nil,
wantBump: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fs := &FileSymbols{
Path: "app." + tc.language,
Language: tc.language,
Imports: tc.imports,
Symbols: []Symbol{{Name: "Handler", Kind: "function", Exported: true}},
}
ranked := []RankedFile{{FileSymbols: fs, Score: 10}}
// Must not panic.
applyBoundaryBoost(ranked)
assert.Equal(t, tc.wantLabels, ranked[0].Boundaries,
"language %q boundary labels mismatch", tc.language)
assert.Equal(t, 10+tc.wantBump, ranked[0].Score,
"language %q score mismatch", tc.language)
})
}
}
// TestClassifyBoundaries_TableDriven exercises every label in the boundary rules
// table, ensuring each prefix produces the expected label and a positive score bump.
func TestClassifyBoundaries_TableDriven(t *testing.T) {
t.Parallel()
tests := []struct {
label string
imp string
wantBump int
}{
{"HTTP", "net/http", 5},
{"HTTP", "github.com/go-chi/chi/v5", 5},
{"HTTP", "github.com/gin-gonic/gin", 5},
{"HTTP", "github.com/gorilla/mux", 5},
{"Postgres", "github.com/jackc/pgx/v5", 5},
{"Postgres", "database/sql", 5},
{"Postgres", "github.com/lib/pq", 5},
{"Redis", "github.com/redis/go-redis/v9", 5},
{"Redis", "github.com/go-redis/redis/v8", 5},
{"Kafka", "github.com/segmentio/kafka-go", 5},
{"Kafka", "github.com/IBM/sarama", 5},
{"Kafka", "github.com/Shopify/sarama", 5},
{"gRPC", "google.golang.org/grpc", 5},
{"Shell", "os/exec", 3},
{"Crypto", "crypto/tls", 3},
{"Crypto", "golang.org/x/crypto/bcrypt", 3},
}
for _, tc := range tests {
t.Run(tc.label+"_"+tc.imp, func(t *testing.T) {
t.Parallel()
labels, bump := classifyBoundaries("go", []string{tc.imp})
require.Contains(t, labels, tc.label,
"import %q must produce label %q", tc.imp, tc.label)
assert.Equal(t, tc.wantBump, bump,
"single %q match must produce bump=%d", tc.label, tc.wantBump)
})
}
}
func TestRenderXML_BoundaryAttribute(t *testing.T) {
t.Parallel()
sym := Symbol{Name: "Handle", Kind: "function", Exported: true}
t.Run("boundaries attribute present when set", func(t *testing.T) {
t.Parallel()
f := RankedFile{
FileSymbols: &FileSymbols{
Path: "api/handler.go",
Language: "go",
Symbols: []Symbol{sym},
},
DetailLevel: 2,
Boundaries: []string{"HTTP", "Postgres"},
}
files := []RankedFile{f}
out := FormatXML(files, 0, nil)
assert.Contains(t, out, `boundaries="HTTP,Postgres"`)
})
t.Run("boundaries attribute absent when empty", func(t *testing.T) {
t.Parallel()
f := RankedFile{
FileSymbols: &FileSymbols{
Path: "util/format.go",
Language: "go",
Symbols: []Symbol{sym},
},
DetailLevel: 2,
}
files := []RankedFile{f}
out := FormatXML(files, 0, nil)
assert.NotContains(t, out, "boundaries=")
})
}