-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathengine_test.go
More file actions
482 lines (456 loc) · 12.7 KB
/
Copy pathengine_test.go
File metadata and controls
482 lines (456 loc) · 12.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package engine
import (
"context"
"path/filepath"
"sync"
"testing"
"github.com/enola-labs/enola/internal/config"
"github.com/enola-labs/enola/internal/explainers/coverage"
"github.com/enola-labs/enola/internal/facts"
)
func TestIsIgnored(t *testing.T) {
tests := []struct {
name string
relPath string
isDir bool
patterns []string
want bool
}{
{
"vendor directory",
"vendor/foo/bar.go", false,
[]string{"vendor/**"},
true,
},
{
"vendor dir itself",
"vendor", true,
[]string{"vendor/**"},
true,
},
{
"node_modules",
"node_modules/react/index.js", false,
[]string{"node_modules/**"},
true,
},
{
// The reported bug: a repo-local .venv held the whole dependency tree
// and was indexed. The **/x/** form must prune the directory itself so
// the walk never descends into it.
"venv dir itself pruned at any depth",
"backend/.venv", true,
[]string{"**/.venv/**"},
true,
},
{
"site-packages under a nested venv",
"backend/.venv/lib/python3.12/site-packages/pandas/core/frame.py", false,
[]string{"**/.venv/**", "**/site-packages/**"},
true,
},
{
"plain venv dir",
"venv/lib/python3.11/site-packages/x.py", false,
[]string{"**/venv/**"},
true,
},
{
// "env" alone must NOT be treated as a venv (too common a name).
"env is not ignored",
"app/env/settings.py", false,
[]string{"**/.venv/**", "**/venv/**", "**/site-packages/**"},
false,
},
{
"git directory",
".git/HEAD", false,
[]string{".git/**"},
true,
},
{
"test files with ** prefix",
"src/main_test.go", false,
[]string{"**/*_test.go"},
true,
},
{
"non-test file not ignored",
"src/main.go", false,
[]string{"**/*_test.go"},
false,
},
{
"spec files",
"src/utils.spec.ts", false,
[]string{"**/*.spec.ts"},
true,
},
{
"enola output dir",
".enola/facts.jsonl", false,
[]string{".enola/**"},
true,
},
{
"normal source not ignored",
"src/app.go", false,
[]string{"vendor/**"},
false,
},
{
"build directory",
"build/output.kt", false,
[]string{"build/**"},
true,
},
{
"nested test file",
"internal/pkg/foo_test.go", false,
[]string{"**/*_test.go"},
true,
},
{
"deeply nested vendor",
"vendor/github.com/foo/bar/baz.go", false,
[]string{"vendor/**"},
true,
},
{
"nested build dir via **/build/**",
"data/build/kspCaches/devDebug/Gen.kt", false,
[]string{"**/build/**"},
true,
},
{
"top-level build dir via **/build/**",
"build/output.kt", false,
[]string{"**/build/**"},
true,
},
{
"build as filename is not a build dir",
"cmd/build.go", false,
[]string{"**/build/**"},
false,
},
{
"rebuild is a different segment",
"rebuild/main.go", false,
[]string{"**/build/**"},
false,
},
{
"cocoapods at any depth",
"Pods/Alamofire/Source/Request.swift", false,
[]string{"**/Pods/**"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.Default()
cfg.Ignore = tt.patterns
eng, _ := New(cfg)
got := eng.isIgnored(tt.relPath, tt.isDir)
if got != tt.want {
t.Errorf("isIgnored(%q, isDir=%v) with patterns %v = %v, want %v",
tt.relPath, tt.isDir, tt.patterns, got, tt.want)
}
})
}
}
// TestMatchAnyGlob_MidPatternDoublestar covers the "<prefix>/**/<fileglob>" form,
// which lets a pattern require BOTH a directory segment and a filename shape.
//
// The Ruby test globs need it. A bare "**/*_test.rb" is a filename-suffix match, so
// a production ActiveJob named cache_warmup_ab_test.rb was ignored AND routed to
// reference-only test-ref extraction — its class vanished from the graph. No
// filename-only rule can separate that file from lib/foo_test.rb: both end in the
// token "test". The directory segment is the only reliable signal, and Ruby supplies
// one (RSpec requires spec/, Minitest defaults to test/).
func TestMatchAnyGlob_MidPatternDoublestar(t *testing.T) {
rubyTestGlobs := []string{"**/spec/**/*_spec.rb", "**/test/**/*_test.rb"}
tests := []struct {
name string
relPath string
patterns []string
want bool
}{
{
// The reported bug: a production A/B-test job under app/jobs.
"production job whose name ends in _ab_test",
"app/jobs/reporting/cache_warmup_ab_test.rb",
rubyTestGlobs,
false,
},
{
"production model named ab_test",
"app/models/ab_test.rb",
rubyTestGlobs,
false,
},
{
// Zero intermediate directories. filepath.Match's "*" never crosses a
// separator, so the pre-existing "**/<glob>" branch could not match this.
"spec directly under spec/",
"spec/user_spec.rb",
rubyTestGlobs,
true,
},
{
"spec one level down",
"spec/services/report_worker_spec.rb",
rubyTestGlobs,
true,
},
{
"spec segment at any depth, several levels down",
"engines/billing/spec/models/nested/invoice_spec.rb",
rubyTestGlobs,
true,
},
{
"minitest file under test/",
"test/models/user_test.rb",
rubyTestGlobs,
true,
},
{
// The dir segment is present but the basename shape is wrong.
"support file under spec/ is not a spec",
"spec/rails_helper.rb",
rubyTestGlobs,
false,
},
{
// "spec" must be a DIRECTORY segment, not the basename stem.
"file named spec.rb outside a spec dir",
"app/models/spec.rb",
rubyTestGlobs,
false,
},
{
"anchored prefix form",
"spec/models/user_spec.rb",
[]string{"spec/**/*_spec.rb"},
true,
},
{
"anchored prefix form does not match a nested spec dir",
"engines/billing/spec/models/user_spec.rb",
[]string{"spec/**/*_spec.rb"},
false,
},
// The pre-existing pattern forms must keep their semantics — the new branch
// fires only on a literal "/**/" in the pattern, which none of them contain.
{
"**/build/** still matches a nested build dir",
"data/build/kspCaches/devDebug/Gen.kt",
[]string{"**/build/**"},
true,
},
{
"**/*_test.go still matches by filename at any depth",
"internal/pkg/foo_test.go",
[]string{"**/*_test.go"},
true,
},
{
"vendor/** still matches an anchored prefix",
"vendor/github.com/foo/bar.go",
[]string{"vendor/**"},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := matchAnyGlob(tt.relPath, tt.patterns); got != tt.want {
t.Errorf("matchAnyGlob(%q, %v) = %v, want %v",
tt.relPath, tt.patterns, got, tt.want)
}
})
}
}
func TestResolveFactFile_SingleRepo(t *testing.T) {
cfg := config.Default()
eng, _ := New(cfg)
eng.SetSnapshot(&facts.Snapshot{
Meta: facts.SnapshotMeta{RepoPath: "/Users/me/myrepo"},
})
f := &facts.Fact{File: "internal/server/server.go"}
got := eng.ResolveFactFile(f)
want := filepath.Join("/Users/me/myrepo", "internal/server/server.go")
if got != want {
t.Errorf("ResolveFactFile = %q, want %q", got, want)
}
}
func TestResolveFactFile_MultiRepo(t *testing.T) {
cfg := config.Default()
eng, _ := New(cfg)
eng.SetRepoPaths(map[string]string{
"go-service": "/Users/me/development/go-service",
"ruby-monolith": "/Users/me/development/ruby-monolith",
})
eng.SetSnapshot(&facts.Snapshot{
Meta: facts.SnapshotMeta{RepoPath: "/Users/me/workspace"},
})
tests := []struct {
name string
fact facts.Fact
want string
}{
{
"multi-repo go-service",
facts.Fact{File: "go-service/lib/foo.rb", Repo: "go-service"},
filepath.Join("/Users/me/development/go-service", "lib/foo.rb"),
},
{
"multi-repo ruby-monolith",
facts.Fact{File: "ruby-monolith/lib/bar.rb", Repo: "ruby-monolith"},
filepath.Join("/Users/me/development/ruby-monolith", "lib/bar.rb"),
},
{
"no repo label falls back to snapshot",
facts.Fact{File: "internal/server.go"},
filepath.Join("/Users/me/workspace", "internal/server.go"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := eng.ResolveFactFile(&tt.fact)
if got != tt.want {
t.Errorf("ResolveFactFile = %q, want %q", got, tt.want)
}
})
}
}
// TestLinkCrossRepo_ConnectsServicesInGraph verifies that the cross-repo linking
// step produces service nodes and dependency edges that make the graph traversable
// across repos, and that re-running it does not duplicate facts.
func TestLinkCrossRepo_ConnectsServicesInGraph(t *testing.T) {
cfg := config.Default()
eng, _ := New(cfg)
// Two repos: svc-alpha calls an endpoint svc-beta serves.
eng.Store().Add(
facts.Fact{
Kind: facts.KindRoute, Name: "/api/items/{id}", Repo: "svc-alpha",
Props: map[string]any{"method": "GET", "role": "client"},
},
facts.Fact{
Kind: facts.KindRoute, Name: "/api/items/{id}", Repo: "svc-beta",
Props: map[string]any{"method": "GET", "role": "server"},
},
)
eng.linkCrossRepo()
if got := eng.Store().ByKind(facts.KindService); len(got) != 2 {
t.Fatalf("service nodes = %d, want 2", len(got))
}
depFacts, _ := eng.Store().QueryAdvanced(facts.QueryOpts{Prop: "type", PropValue: "cross_repo"})
if len(depFacts) != 1 {
t.Fatalf("cross_repo dep facts = %d, want 1", len(depFacts))
}
if depFacts[0].Repo != "svc-alpha" || depFacts[0].Name != "svc-alpha -> svc-beta" {
t.Errorf("edge = %+v, want svc-alpha -> svc-beta", depFacts[0])
}
// The graph now connects the two service nodes.
eng.Store().BuildGraph()
g := eng.Store().Graph()
res := g.Traverse("svc-alpha", "forward", nil, nil, 5, 100)
reached := false
for _, n := range res.Nodes {
if n.Name == "svc-beta" {
reached = true
}
}
if !reached {
t.Errorf("traverse from svc-alpha did not reach svc-beta: %+v", res.Nodes)
}
if path := g.FindPath("svc-alpha", "svc-beta", nil, 10); !path.Found {
t.Errorf("FindPath(svc-alpha, svc-beta) not found")
}
// Idempotent: re-running linking keeps exactly one service node per repo
// and one edge (no duplicates).
eng.linkCrossRepo()
if got := eng.Store().ByKind(facts.KindService); len(got) != 2 {
t.Errorf("after relink, service nodes = %d, want 2", len(got))
}
deps2, _ := eng.Store().QueryAdvanced(facts.QueryOpts{Prop: "type", PropValue: "cross_repo"})
if len(deps2) != 1 {
t.Errorf("after relink, cross_repo dep facts = %d, want 1", len(deps2))
}
}
// TestLinkCrossRepo_CoverageGapInsight exercises the full coverage path through
// the engine: a service whose only outbound call site cannot be resolved must get
// edge_coverage props on its service node and a "Coverage gap" insight from the
// coverage explainer — so it reads as a blind spot, not a true isolate.
func TestLinkCrossRepo_CoverageGapInsight(t *testing.T) {
cfg := config.Default()
eng, _ := New(cfg)
// svc-alpha calls a path no loaded repo serves; svc-beta serves something else.
eng.Store().Add(
facts.Fact{
Kind: facts.KindRoute, Name: "/api/orders/{id}", Repo: "svc-alpha",
Props: map[string]any{"method": "GET", "role": "client"},
},
facts.Fact{
Kind: facts.KindRoute, Name: "/api/items/{id}", Repo: "svc-beta",
Props: map[string]any{"method": "GET", "role": "server"},
},
)
eng.linkCrossRepo()
// The svc-alpha service node carries edge_coverage with an unresolved call site.
var alpha *facts.Fact
for _, f := range eng.Store().ByKind(facts.KindService) {
if f.Name == "svc-alpha" {
ff := f
alpha = &ff
}
}
if alpha == nil {
t.Fatal("no svc-alpha service node")
}
if _, ok := alpha.Props["edge_coverage"]; !ok {
t.Errorf("svc-alpha service node missing edge_coverage prop: %+v", alpha.Props)
}
// The coverage explainer turns that into a "Coverage gap" insight.
insights, err := coverage.New().Explain(context.Background(), eng.Store())
if err != nil {
t.Fatalf("coverage Explain: %v", err)
}
found := false
for _, in := range insights {
if in.Title == "Coverage gap: service svc-alpha appears isolated but has 1 unresolved outbound call site(s)" {
found = true
}
}
if !found {
t.Errorf("expected a Coverage gap insight for svc-alpha, got %+v", insights)
}
}
// TestGenerateSnapshot_ConcurrentCallsSerialized verifies that the engine mutex
// prevents concurrent GenerateSnapshot calls from corrupting shared state.
func TestGenerateSnapshot_ConcurrentCallsSerialized(t *testing.T) {
cfg := config.Default()
eng, _ := New(cfg)
// Use a non-existent repo path — GenerateSnapshot will fail at walkRepo,
// but that's fine: we're testing that concurrent calls don't panic or
// produce a data race. The mutex should serialize them.
var wg sync.WaitGroup
errs := make([]error, 3)
for i := 0; i < 3; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
_, errs[idx] = eng.GenerateSnapshot(context.Background(), t.TempDir(), false)
}(i)
}
wg.Wait()
// All calls should complete without panic. Errors from missing extractors
// or empty repos are expected — the key thing is no race.
for i, err := range errs {
if err != nil {
t.Logf("goroutine %d error (expected): %v", i, err)
}
}
}