-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbudget_test.go
More file actions
631 lines (571 loc) · 24.6 KB
/
Copy pathbudget_test.go
File metadata and controls
631 lines (571 loc) · 24.6 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package repomap
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// mkSym builds a minimal Symbol for cost/budget tests.
func mkSym(name, kind, sig, doc string, exported bool) Symbol {
return Symbol{Name: name, Kind: kind, Signature: sig, Doc: doc, Exported: exported}
}
// mkRankedFunc builds a minimal RankedFile with n exported function symbols.
func mkRankedFunc(path string, n int, sigLen, docLen int) RankedFile {
syms := make([]Symbol, n)
sig := strings.Repeat("x", sigLen)
doc := strings.Repeat("d", docLen)
for i := range syms {
syms[i] = mkSym(string(rune('A'+i)), "function", "("+sig+") error", doc, true)
}
return RankedFile{FileSymbols: &FileSymbols{Path: path, Symbols: syms}}
}
// TestEnrichedCost_ZeroSymbols verifies that a nil symbol slice costs zero.
func TestEnrichedCost_ZeroSymbols(t *testing.T) {
t.Parallel()
assert.Equal(t, 0, enrichedCost(nil))
}
// TestEnrichedCost_UnexportedIgnored verifies that unexported symbols contribute zero cost.
func TestEnrichedCost_UnexportedIgnored(t *testing.T) {
t.Parallel()
syms := []Symbol{
mkSym("a", "function", "()", "", false),
mkSym("b", "function", "()", "doc", false),
mkSym("c", "struct", "{ X int }", "", false),
mkSym("d", "variable", "", "", false),
mkSym("e", "constant", "", "", false),
}
assert.Equal(t, 0, enrichedCost(syms))
}
// TestEnrichedCost_ExportedCounted verifies a single exported function without doc.
func TestEnrichedCost_ExportedCounted(t *testing.T) {
t.Parallel()
name := "Run"
sig := "(ctx context.Context) error"
sym := mkSym(name, "function", sig, "", true)
got := enrichedCost([]Symbol{sym})
// Minimum: 8 + len(name) + len(sig)
assert.GreaterOrEqual(t, got, 8+len(name)+len(sig))
}
// TestEnrichedCost_DocAdded verifies that a non-empty Doc increases the cost.
func TestEnrichedCost_DocAdded(t *testing.T) {
t.Parallel()
name := "Run"
sig := "(ctx context.Context) error"
doc := "starts the loop"
withoutDoc := enrichedCost([]Symbol{mkSym(name, "function", sig, "", true)})
withDoc := enrichedCost([]Symbol{mkSym(name, "function", sig, doc, true)})
// Doc adds at least 8 + len(doc) bytes.
assert.GreaterOrEqual(t, withDoc, withoutDoc+8+len(doc))
}
// TestEnrichedCost_StructFields verifies that a struct's typed field list is counted
// as part of the name line only — no separate field-block term.
//
// formatFileBlockDefault renders struct symbols inline: " type Config{Name string, ID int}\n"
// The signature appears once on the name line; enrichedCost must not double-count it.
func TestEnrichedCost_StructFields(t *testing.T) {
t.Parallel()
// Use the typed field format that parser_go.go now produces (fields with types).
sig := "{Name string, ID int}"
sym := mkSym("Config", "struct", sig, "", true)
require.True(t, sym.HasFields(), "test setup: HasFields must be true for this symbol")
// Name line only: 8 + len(Name) + len(Signature)
// No separate field block — the signature appears inline on the name line.
expected := 8 + len(sym.Name) + len(sig)
assert.Equal(t, expected, enrichedCost([]Symbol{sym}))
}
// TestEnrichedCost_MixedSymbols verifies that only exported symbols contribute cost.
func TestEnrichedCost_MixedSymbols(t *testing.T) {
t.Parallel()
exported := []Symbol{
mkSym("A", "function", "()", "", true),
mkSym("B", "function", "()", "", true),
mkSym("C", "function", "()", "", true),
}
unexported := []Symbol{
mkSym("a", "function", "()", "", false),
mkSym("b", "function", "()", "", false),
}
mixed := append(exported, unexported...)
exportedCost := enrichedCost(exported)
mixedCost := enrichedCost(mixed)
assert.Equal(t, exportedCost, mixedCost, "unexported symbols must not affect cost")
assert.Greater(t, exportedCost, 0, "exported symbols must contribute positive cost")
}
// TestBudgetAllOrNothing_FitsEnriched verifies a small file under a generous budget gets level 2.
func TestBudgetAllOrNothing_FitsEnriched(t *testing.T) {
t.Parallel()
f := mkRankedFunc("small.go", 2, 10, 0)
ranked := BudgetFiles([]RankedFile{f}, 2048, nil)
require.Len(t, ranked, 1)
assert.Equal(t, 2, ranked[0].DetailLevel, "file with cost well within budget must be level 2")
}
// TestBudgetAllOrNothing_FallsToSummary verifies demotion to level 1 when enriched cost overflows.
func TestBudgetAllOrNothing_FallsToSummary(t *testing.T) {
t.Parallel()
// Build a file whose enriched cost exceeds the budget but whose summary fits.
// Budget: 100 tokens = 400 bytes.
// File has 30 symbols with sig=20 chars, no doc → enrichedCost ≈ 30*(8+1+20) = 870 bytes > 400.
// countGroups returns 1 (all "function") → summaryCost = 30 bytes ≤ 400.
f := mkRankedFunc("large.go", 30, 20, 0)
ranked := BudgetFiles([]RankedFile{f}, 100, nil)
require.Len(t, ranked, 1)
assert.Equal(t, 1, ranked[0].DetailLevel, "file whose enriched cost exceeds budget must fall to level 1")
}
// TestBudgetAllOrNothing_Omitted verifies level -1 when neither enriched nor summary fit.
func TestBudgetAllOrNothing_Omitted(t *testing.T) {
t.Parallel()
// Budget: 2 tokens = 8 bytes. Any file with symbols will have enrichedCost > 8.
// summaryCost = groups*30 > 8 as well.
// Phase 1 headerCap = 8*70/100 = 5 bytes; path "x.go" = 4+30 = 34 bytes > 5,
// so cutoff = 0 and the file gets level -1 via the beyond-cutoff loop.
f := mkRankedFunc("x.go", 1, 5, 0)
ranked := BudgetFiles([]RankedFile{f}, 2, nil)
require.Len(t, ranked, 1)
assert.Equal(t, -1, ranked[0].DetailLevel, "file that overflows even summary budget must be omitted")
}
// TestBudgetAllOrNothing_VerboseNoInvariant verifies that maxTokens=0 gives all files level 2.
func TestBudgetAllOrNothing_VerboseNoInvariant(t *testing.T) {
t.Parallel()
files := []RankedFile{
mkRankedFunc("a.go", 5, 50, 20),
mkRankedFunc("b.go", 5, 50, 20),
mkRankedFunc("c.go", 5, 50, 20),
}
ranked := BudgetFiles(files, 0, nil)
require.Len(t, ranked, 3)
for i, f := range ranked {
assert.Equal(t, 2, f.DetailLevel, "maxTokens=0 must give DetailLevel=2 for file %d", i)
}
}
// TestBudgetAllOrNothing_ZeroSymbols verifies that a file with no symbols gets level 0.
func TestBudgetAllOrNothing_ZeroSymbols(t *testing.T) {
t.Parallel()
f := RankedFile{FileSymbols: &FileSymbols{Path: "empty.go", Symbols: nil}}
ranked := BudgetFiles([]RankedFile{f}, 2048, nil)
require.Len(t, ranked, 1)
assert.Equal(t, 0, ranked[0].DetailLevel, "file with no symbols must be level 0 regardless of budget")
}
// TestBudgetAllOrNothing_MultiFile verifies mixed detail levels across 3 files under a tight budget.
func TestBudgetAllOrNothing_MultiFile(t *testing.T) {
t.Parallel()
// File 1: 2 funcs, sig=5 chars → enrichedCost ≈ 2*(8+1+6) = 30 bytes.
// File 2: 10 funcs, sig=10 chars → enrichedCost ≈ 10*(8+1+11) = 200 bytes.
// File 3: 30 funcs, sig=20 chars → enrichedCost ≈ 30*(8+1+21) = 900 bytes.
//
// Budget: 200 tokens = 800 bytes.
// Phase 1 headerCap = 560 bytes; all 3 paths fit (each ~4+30=34 bytes each → 102 total).
// headerCost ≈ 102 bytes. used = 102.
// File 1: enrichedCost ≈ 30 → 102+30=132 ≤ 800 → level 2. used=132.
// File 2: enrichedCost ≈ 200 → 132+200=332 ≤ 800 → level 2. used=332.
// File 3: enrichedCost ≈ 900 → 332+900=1232 > 800. summaryCost=30 → 332+30=362 ≤ 800 → level 1.
f1 := mkRankedFunc("a.go", 2, 5, 0)
f2 := mkRankedFunc("b.go", 10, 10, 0)
f3 := mkRankedFunc("c.go", 30, 20, 0)
ranked := BudgetFiles([]RankedFile{f1, f2, f3}, 200, nil)
require.Len(t, ranked, 3)
assert.Equal(t, 2, ranked[0].DetailLevel, "file 1 must be level 2")
assert.Equal(t, 2, ranked[1].DetailLevel, "file 2 must be level 2")
assert.Equal(t, 1, ranked[2].DetailLevel, "file 3 must fall to level 1")
}
// TestBudgetAllOrNothing_NoPartialLevel2 verifies that no file gets DetailLevel=2
// when its enriched cost exceeds the remaining budget. Proves the invariant.
func TestBudgetAllOrNothing_NoPartialLevel2(t *testing.T) {
t.Parallel()
// 5 identical files each with enriched cost that won't fit under a 50-token budget.
// Budget: 50 tokens = 200 bytes.
// Each file: 10 funcs, sig=20 chars → enrichedCost ≈ 10*(8+1+21) = 300 bytes > 200.
// summaryCost = 1 group * 30 = 30 bytes.
// So each file should end up at level 1 (summary) or -1, never 2.
files := make([]RankedFile, 5)
for i := range files {
files[i] = mkRankedFunc("file.go", 10, 20, 0)
}
ranked := BudgetFiles(files, 50, nil)
require.Len(t, ranked, 5)
level2Count := 0
for _, f := range ranked {
if f.DetailLevel == 2 {
level2Count++
}
}
assert.Equal(t, 0, level2Count, "no file should get DetailLevel=2 when enriched cost exceeds budget")
}
// TestEnrichedCost_MatchesRenderer verifies enrichedCost stays within ±10% of
// formatFileBlockDefault's actual output length. This is the regression guard
// that ensures budget decisions match what the renderer actually produces.
//
// Three symbol mixes are tested: functions only, functions+doc, and a struct+func mix.
// The path overhead (e.g. "test.go\n") is excluded from the comparison because
// enrichedCost estimates symbol cost only — the caller adds the path separately.
func TestEnrichedCost_MatchesRenderer(t *testing.T) {
t.Parallel()
// helper strips the first line (file path header) from formatFileBlockDefault output
// so we compare only the symbol lines against enrichedCost.
symbolLines := func(rendered string) string {
idx := strings.IndexByte(rendered, '\n')
if idx < 0 {
return rendered
}
return rendered[idx+1:]
}
checkWithin10Pct := func(t *testing.T, syms []Symbol, path string) {
t.Helper()
f := RankedFile{FileSymbols: &FileSymbols{Path: path, Language: "go", Symbols: syms}}
rendered := symbolLines(formatFileBlockDefault(f, false))
cost := enrichedCost(syms)
actual := len(rendered)
lo := int(float64(cost) * 0.90)
hi := int(float64(cost) * 1.10)
assert.GreaterOrEqual(t, actual, lo,
"rendered len %d is below 90%% of enrichedCost %d (lo=%d)", actual, cost, lo)
assert.LessOrEqual(t, actual, hi,
"rendered len %d exceeds 110%% of enrichedCost %d (hi=%d)", actual, cost, hi)
}
t.Run("functions only", func(t *testing.T) {
t.Parallel()
syms := []Symbol{
mkSym("Run", "function", "(ctx context.Context) error", "", true),
mkSym("Stop", "function", "()", "", true),
mkSym("New", "function", "(cfg Config) *Server", "", true),
}
checkWithin10Pct(t, syms, "server.go")
})
t.Run("functions with doc", func(t *testing.T) {
t.Parallel()
syms := []Symbol{
mkSym("Start", "function", "(addr string) error", "listens and serves HTTP", true),
mkSym("Shutdown", "function", "(ctx context.Context) error", "gracefully stops the server", true),
}
checkWithin10Pct(t, syms, "http.go")
})
t.Run("struct and function mix", func(t *testing.T) {
t.Parallel()
syms := []Symbol{
mkSym("Config", "struct", "{Host string, Port int}", "holds server settings", true),
mkSym("New", "function", "(cfg Config) *Server", "creates a new server", true),
mkSym("helper", "function", "()", "", false), // unexported — excluded from both sides
}
checkWithin10Pct(t, syms, "pkg.go")
})
t.Run("mixed with receiver method", func(t *testing.T) {
t.Parallel()
syms := []Symbol{
{Name: "Build", Kind: "method", Receiver: "*Builder", Signature: "(opts Options) error", Exported: true},
mkSym("Options", "struct", "{Timeout time.Duration, MaxRetry int}", "", true),
}
checkWithin10Pct(t, syms, "builder.go")
})
}
// TestCompactCost_StrictlyLessThanEnriched verifies that compactCost is always
// strictly less than enrichedCost for files with exported symbols that have signatures or doc.
func TestCompactCost_StrictlyLessThanEnriched(t *testing.T) {
t.Parallel()
tests := []struct {
name string
syms []Symbol
}{
{
name: "function with signature",
syms: []Symbol{mkSym("Run", "function", "(ctx context.Context) error", "", true)},
},
{
name: "function with signature and doc",
syms: []Symbol{mkSym("Start", "function", "(addr string) error", "starts the server", true)},
},
{
name: "struct with fields",
syms: []Symbol{mkSym("Config", "struct", "{Host string, Port int}", "", true)},
},
{
name: "mixed exported symbols",
syms: []Symbol{
mkSym("New", "function", "(cfg Config) *Server", "creates a server", true),
mkSym("Config", "struct", "{Host string, Port int}", "", true),
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
compact := compactCost(tc.syms)
enriched := enrichedCost(tc.syms)
assert.Less(t, compact, enriched,
"compactCost (%d) must be strictly less than enrichedCost (%d)", compact, enriched)
})
}
}
// TestCompactCost_ZeroUnexported verifies that unexported symbols contribute zero cost.
func TestCompactCost_ZeroUnexported(t *testing.T) {
t.Parallel()
syms := []Symbol{
mkSym("a", "function", "()", "", false),
mkSym("b", "struct", "{X int}", "", false),
}
assert.Equal(t, 0, compactCost(syms), "unexported symbols must not contribute to compactCost")
}
// TestCompactCost_ExportedCounted verifies basic counting for a single exported symbol.
func TestCompactCost_ExportedCounted(t *testing.T) {
t.Parallel()
sym := mkSym("Run", "function", "(ctx context.Context) error", "starts the loop", true)
cost := compactCost([]Symbol{sym})
// Minimum: 8 + len("Run") = 11
assert.GreaterOrEqual(t, cost, 8+len(sym.Name),
"compactCost must include name overhead")
// Must NOT include signature or doc bytes.
assert.Less(t, cost, 8+len(sym.Name)+len(sym.Signature)+len(sym.Doc),
"compactCost must not include signature or doc bytes")
}
// TestFileAllDead verifies the fileAllDead helper.
func TestFileAllDead(t *testing.T) {
t.Parallel()
tests := []struct {
name string
syms []Symbol
want bool
}{
{
name: "all exported dead",
syms: []Symbol{
{Name: "A", Kind: "function", Exported: true, Dead: true},
{Name: "B", Kind: "struct", Exported: true, Dead: true},
},
want: true,
},
{
name: "some exported not dead",
syms: []Symbol{
{Name: "A", Kind: "function", Exported: true, Dead: true},
{Name: "B", Kind: "struct", Exported: true, Dead: false},
},
want: false,
},
{
name: "no exported symbols",
syms: []Symbol{
{Name: "a", Kind: "function", Exported: false, Dead: false},
},
want: false,
},
{
name: "single dead exported",
syms: []Symbol{
{Name: "A", Kind: "function", Exported: true, Dead: true},
},
want: true,
},
{
name: "empty symbols",
syms: []Symbol{},
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := fileAllDead(tc.syms)
assert.Equal(t, tc.want, got)
})
}
}
// TestDeadExportBudgetCost verifies that files with all-dead exports get
// lower effective enriched cost, making them less competitive for Level 2 slots.
// Under a tight budget, the live-export file should get a higher DetailLevel
// than the all-dead file when both would otherwise compete for the same slot.
func TestDeadExportBudgetCost(t *testing.T) {
t.Parallel()
// Two identical files except one has all-dead exports.
// Under a budget that can only afford one file at level 2,
// the dead file's halved cost may let it fit — or both may fit
// but the live file should never get a lower level than the dead one.
//
// More concretely: build a scenario where the live file's enriched cost
// exceeds the remaining budget but the dead file's halved cost fits.
// Each file: 5 exported funcs, sig=50 chars → enrichedCost ≈ 5*(8+1+51) = 300 bytes.
// Dead file: cost/2 = 150 bytes.
// Budget: 100 tokens = 400 bytes.
// Phase 1: 2 files × 34 bytes = 68 bytes header. used=68.
// Live file first: enrichedCost=300 → 68+300=368 ≤ 400 → level 2. used=368.
// Dead file: enrichedCost=300, halved=150 → 368+150=518 > 400.
// summaryCost=1*30=30 → 368+30=398 ≤ 400 → level 1.
//
// With dead file first: halved cost 150 → 68+150=218 ≤ 400 → level 2. used=218.
// Live file: 218+300=518 > 400. summaryCost=30 → 218+30=248 ≤ 400 → level 1.
// The live file gets demoted while the dead file gets level 2 — that's the
// intended behaviour: dead-export files are less competitive for Level 2.
//
// We verify the invariant: live file DetailLevel ≤ dead file DetailLevel.
// (Dead files with halved cost can afford level 2 more easily.)
docs := strings.Repeat("d", 10)
liveSyms := []Symbol{
mkSym("A", "function", "("+strings.Repeat("x", 50)+")", docs, true),
mkSym("B", "function", "("+strings.Repeat("x", 50)+")", docs, true),
mkSym("C", "function", "("+strings.Repeat("x", 50)+")", docs, true),
}
deadSyms := []Symbol{
{Name: "A", Kind: "function", Signature: "(" + strings.Repeat("x", 50) + ")", Doc: docs, Exported: true, Dead: true},
{Name: "B", Kind: "function", Signature: "(" + strings.Repeat("x", 50) + ")", Doc: docs, Exported: true, Dead: true},
{Name: "C", Kind: "function", Signature: "(" + strings.Repeat("x", 50) + ")", Doc: docs, Exported: true, Dead: true},
}
liveFile := RankedFile{FileSymbols: &FileSymbols{Path: "live.go", Symbols: liveSyms}}
deadFile := RankedFile{FileSymbols: &FileSymbols{Path: "dead.go", Symbols: deadSyms}}
// Run with live file first (higher rank).
ranked := BudgetFiles([]RankedFile{liveFile, deadFile}, 100, nil)
require.Len(t, ranked, 2)
// The live file gets level 2 (cost fits), dead file gets level 1 (halved cost still overflows remaining).
// The exact levels depend on budget arithmetic, but we verify the cost-halving
// is actually applied by checking that the dead file's effective cost is lower.
liveCost := enrichedCost(liveSyms)
deadEffective := enrichedCost(deadSyms) / 2
assert.Less(t, deadEffective, liveCost, "dead file's effective cost must be halved")
}
// TestBudgetFiles_BeyondCutoffOmitted verifies that files ranked beyond the header
// cutoff receive DetailLevel=-1 (omitted), not summary or full.
func TestBudgetFiles_BeyondCutoffOmitted(t *testing.T) {
t.Parallel()
// Budget: 5 tokens = 20 bytes. Phase 1 headerCap = 14 bytes.
// Each path "fileN.go" = 8+30=38 bytes — the very first file exceeds the cap,
// so cutoff=0 and all files are beyond cutoff.
files := []RankedFile{
mkRankedFunc("file0.go", 2, 5, 0),
mkRankedFunc("file1.go", 3, 5, 0),
mkRankedFunc("file2.go", 1, 5, 0),
}
ranked := BudgetFiles(files, 5, nil)
require.Len(t, ranked, 3)
for i, f := range ranked {
assert.Equal(t, -1, f.DetailLevel,
"file %d beyond header cutoff must be omitted", i)
}
}
// TestBudgetFiles_HighRankNotOmittedUnderBreadthFirst verifies that under
// breadth-first allocation a high-rank file with a very large enriched cost stays
// VISIBLE (level 0 or 1) rather than being omitted (-1) when the budget is
// exhausted by depth-promotion of earlier files. Only the top file gets level 2.
func TestBudgetFiles_HighRankNotOmittedUnderBreadthFirst(t *testing.T) {
t.Parallel()
// Budget: 150 tokens = 600 bytes.
// File 1 (ranked highest): 3 symbols, sig=5 → enrichedCost ≈ 3*(8+1+6)=45 bytes → level 2.
// File 2: 40 symbols, sig=20 → enrichedCost ≈ 40*(8+1+21)=1200 bytes > remaining;
// stays at summary (level 1) under breadth-first.
// File 3: lower-ranked large file stays at level 1 (visible summary), never -1.
f1 := mkRankedFunc("top.go", 3, 5, 0)
f2 := mkRankedFunc("large.go", 40, 20, 0)
f3 := mkRankedFunc("other.go", 40, 20, 0)
ranked := BudgetFiles([]RankedFile{f1, f2, f3}, 150, nil)
require.Len(t, ranked, 3)
assert.Equal(t, 2, ranked[0].DetailLevel, "top.go must be level 2 (fits enriched)")
// large.go and other.go stay VISIBLE: level 0 or 1, never omitted (-1).
assert.GreaterOrEqual(t, ranked[1].DetailLevel, 0, "large.go must stay visible, not omitted")
assert.LessOrEqual(t, ranked[1].DetailLevel, 1, "large.go must not reach level 2 with exhausted budget")
assert.GreaterOrEqual(t, ranked[2].DetailLevel, 0,
"lower-ranked large file must stay visible (level 0 or 1), never omitted")
assert.LessOrEqual(t, ranked[2].DetailLevel, 1,
"third large file must not get level 2 with exhausted budget")
}
// TestBudgetBreadthFirst_AllVisible verifies the breadth-first guarantee: under a
// tight budget across many files, every file stays VISIBLE (DetailLevel >= 0) and
// the top-ranked file is promoted to level 2. No file is omitted (-1).
func TestBudgetBreadthFirst_AllVisible(t *testing.T) {
t.Parallel()
// 6 files of varying size under a tight budget. The top (smallest) file fits
// enriched (level 2); the rest stay at summary or header, never omitted.
files := []RankedFile{
mkRankedFunc("top.go", 2, 5, 0),
mkRankedFunc("a.go", 20, 20, 0),
mkRankedFunc("b.go", 20, 20, 0),
mkRankedFunc("c.go", 20, 20, 0),
mkRankedFunc("d.go", 20, 20, 0),
mkRankedFunc("e.go", 20, 20, 0),
}
ranked := BudgetFiles(files, 300, nil)
require.Len(t, ranked, 6)
for i, f := range ranked {
assert.GreaterOrEqual(t, f.DetailLevel, 0,
"file %d (%s) must stay visible under breadth-first — never omitted (-1)", i, f.Path)
}
assert.Equal(t, 2, ranked[0].DetailLevel, "top file must be promoted to level 2")
}
// TestBudgetFiles_OmitSingleHighRankFile verifies the headerCap guard: when a
// file's PATH header alone can't fit within the budget, it is omitted (-1).
// Breadth-first keeps the headerCap guard so pathological tiny budgets stay bounded.
func TestBudgetFiles_OmitSingleHighRankFile(t *testing.T) {
t.Parallel()
// Budget: 10 tokens = 40 bytes. headerCap = 28 bytes.
// path "big.go" = 6+30 = 36 bytes > 28 → beyond cutoff → level -1.
f := mkRankedFunc("big.go", 50, 30, 0)
ranked := BudgetFiles([]RankedFile{f}, 10, nil)
require.Len(t, ranked, 1)
assert.Equal(t, -1, ranked[0].DetailLevel,
"file whose path alone exceeds headerCap must be omitted")
}
// TestBudgetFiles_MultipleOverrides verifies mixed detail levels when different files
// land on different paths through the budget algorithm: level 2, level 1, and level -1.
func TestBudgetFiles_MultipleOverrides(t *testing.T) {
t.Parallel()
// Budget: 120 tokens = 480 bytes.
// f1: 2 symbols, sig=5 → enrichedCost ≈ 2*(8+1+6)=30 bytes → level 2.
// f2: 20 symbols, sig=10 → enrichedCost ≈ 20*(8+1+11)=400 bytes; remaining ≈ 480-headerCost-30.
// f3: 40 symbols, sig=20 → must overflow both enriched and summary.
f1 := mkRankedFunc("a.go", 2, 5, 0)
f2 := mkRankedFunc("b.go", 20, 10, 0)
f3 := mkRankedFunc("c.go", 40, 20, 0)
ranked := BudgetFiles([]RankedFile{f1, f2, f3}, 120, nil)
require.Len(t, ranked, 3)
// f1 must be level 2 (small enriched cost fits easily).
assert.Equal(t, 2, ranked[0].DetailLevel, "f1 must be level 2")
// f2 and f3 must not both get level 2 — at least one must be demoted.
// Under breadth-first, "demoted" means level 1 (visible summary), never -1.
level2Count := 0
for _, r := range ranked {
if r.DetailLevel == 2 {
level2Count++
}
assert.GreaterOrEqual(t, r.DetailLevel, 0,
"breadth-first keeps every file visible — no file may be omitted (-1)")
}
assert.LessOrEqual(t, level2Count, 2,
"tight budget must prevent all three files from reaching level 2")
}
// TestBudgetFiles_ZeroSymbolFilesAlwaysLevel0 verifies that zero-symbol files always
// receive DetailLevel=0 regardless of budget, even when surrounded by heavy files.
func TestBudgetFiles_ZeroSymbolFilesAlwaysLevel0(t *testing.T) {
t.Parallel()
empty := RankedFile{FileSymbols: &FileSymbols{Path: "empty.go", Symbols: nil}, Score: 100}
heavy := mkRankedFunc("heavy.go", 30, 20, 0)
heavy.Score = 50
ranked := BudgetFiles([]RankedFile{empty, heavy}, 2048, nil)
require.Len(t, ranked, 2)
assert.Equal(t, 0, ranked[0].DetailLevel, "zero-symbol file must always be level 0")
assert.Equal(t, 2, ranked[1].DetailLevel, "symbol-rich file under generous budget must be level 2")
}
// TestBudgetFilesCompact_FitsMoreThanEnriched verifies that compact budgeting
// fits more files at level 2 within the same token budget compared to enriched budgeting.
func TestBudgetFilesCompact_FitsMoreThanEnriched(t *testing.T) {
t.Parallel()
// Build 5 files each with 10 functions with long signatures and doc.
// These will overflow enrichedCost budget but fit under compactCost budget.
files := make([]RankedFile, 5)
for i := range files {
files[i] = mkRankedFunc("file.go", 10, 30, 20)
}
// Clone for each budget call (BudgetFiles mutates the slice).
enrichedInput := make([]RankedFile, len(files))
compactInput := make([]RankedFile, len(files))
copy(enrichedInput, files)
copy(compactInput, files)
const budget = 150 // tokens — tight enough to stress the difference
enrichedResult := BudgetFiles(enrichedInput, budget, nil)
compactResult := BudgetFilesCompact(compactInput, budget, nil)
enrichedLevel2 := 0
compactLevel2 := 0
for i := range files {
if enrichedResult[i].DetailLevel == 2 {
enrichedLevel2++
}
if compactResult[i].DetailLevel == 2 {
compactLevel2++
}
}
assert.GreaterOrEqual(t, compactLevel2, enrichedLevel2,
"compact budget must fit at least as many files at level 2 as enriched budget")
}