Repository navigation
Expand file tree
/
Copy pathtool_filters_git.test.ts
More file actions
1678 lines (1472 loc) · 83.9 KB
/
Copy pathtool_filters_git.test.ts
File metadata and controls
1678 lines (1472 loc) · 83.9 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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Batch D golden tests — git filter family. Faithfully ported from the Python suite (test_bash_compress_git.py and test_bash_compress_git_commit_push.py). These are the regression spec for the 7 filters in src/tool_filters/git.ts.
import * as fs from 'node:fs'
import * as path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { defaultConfig, invalidateConfigCache, saveConfig } from '../src/config.js'
import { configPath } from '../src/constants.js'
import {
CAPTURE_NAME_ONLY_12,
CAPTURE_NUMSTAT_12,
CAPTURE_ONELINE_NAME_ONLY_8,
CAPTURE_STAT_12,
} from './fixtures/git_log_real_captures.js'
import { CAPTURE_DIFF_9_FILES } from './fixtures/git_diff_real_captures.js'
import { compressOutput } from '../src/tool_filters/dispatch.js'
import {
GIT_FILTERS,
GitBlameFilter,
GitCommitFilter,
GitDiffFilter,
GitFilter,
GitLogFilter,
GitPushFilter,
GitStatusVerboseFilter,
TOOL_FILTERS,
selectFilter,
} from '../src/tool_filters/index.js'
// --------------------------------------------------------------------------- Helpers ---------------------------------------------------------------------------
function apply(
filter: { apply: (a: string, b: string, c: number, d: string[]) => { text: string } },
stdout: string,
argv: string[],
opts: { stderr?: string; exitCode?: number } = {},
): string {
return filter.apply(stdout, opts.stderr ?? '', opts.exitCode ?? 0, argv).text
}
const gitLogFilter = new GitLogFilter()
const gitDiffFilter = new GitDiffFilter()
const gitStatusFilter = new GitStatusVerboseFilter()
const gitBlameFilter = new GitBlameFilter()
const gitCommitFilter = new GitCommitFilter()
const gitPushFilter = new GitPushFilter()
const _gitFilter = new GitFilter()
// --------------------------------------------------------------------------- GIT_FILTERS array ---------------------------------------------------------------------------
describe('GIT_FILTERS', () => {
it('exports 7 filter entries', () => {
expect(GIT_FILTERS).toHaveLength(7)
})
it('GitFilter (generic) is last in GIT_FILTERS', () => {
expect(GIT_FILTERS[GIT_FILTERS.length - 1]?.name).toBe('git')
})
it('specific filters precede GitFilter', () => {
const genericIdx = GIT_FILTERS.findIndex((f) => f.name === 'git')
for (const f of GIT_FILTERS.slice(0, genericIdx)) {
expect(f.name).not.toBe('git')
}
})
it('all filters are registered in TOOL_FILTERS', () => {
for (const f of GIT_FILTERS) {
expect(TOOL_FILTERS).toContain(f)
}
})
it('git filters appear after linter filters in TOOL_FILTERS', () => {
const linterIdx = TOOL_FILTERS.findIndex((f) => f.name === 'tsc')
const gitIdx = TOOL_FILTERS.findIndex((f) => f.name === 'git-log')
expect(gitIdx).toBeGreaterThan(linterIdx)
})
})
// --------------------------------------------------------------------------- GitLogFilter — dispatch ---------------------------------------------------------------------------
describe('GitLogFilter dispatch', () => {
it('is registered for git log', () => {
const f = selectFilter(['git', 'log'])
expect(f?.name).toBe('git-log')
})
it('does not match other git subcommands', () => {
const f = selectFilter(['git', 'status'])
expect(f?.name).not.toBe('git-log')
})
it('does not match non-git commands', () => {
expect(gitLogFilter.matches(['hg', 'log'])).toBe(false)
})
})
// --------------------------------------------------------------------------- GitLogFilter — oneline format ---------------------------------------------------------------------------
function makeOneline(n: number): string {
return Array.from({ length: n }, (_, i) => `abc${String(i).padStart(4, '0')}ef Short commit message ${i}`).join('\n')
}
describe('GitLogFilter oneline', () => {
it('short oneline passthrough (≤50)', () => {
const text = makeOneline(10)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline'])
for (let i = 0; i < 10; i++) {
expect(result).toContain(`Short commit message ${i}`)
}
})
it('long oneline truncated to 50 with elided count', () => {
const text = makeOneline(80)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline'])
expect(result).toContain('+30 more commits')
expect(result).toContain('abc0000ef')
expect(result).not.toContain('abc0079ef')
})
it('oneline autodetected without flag (60 commits > cap of 50)', () => {
const text = makeOneline(60)
const result = apply(gitLogFilter, text, ['git', 'log'])
expect(result).toContain('more commits')
})
it('49 oneline commits passthrough without truncation', () => {
const text = makeOneline(49)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline'])
expect(result).not.toContain('more commits')
for (let i = 0; i < 49; i++) {
expect(result).toContain(`Short commit message ${i}`)
}
})
it('exactly 50 oneline commits passthrough', () => {
const text = makeOneline(50)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline'])
expect(result).not.toContain('more commits')
})
it('51 oneline commits triggers truncation', () => {
const text = makeOneline(51)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline'])
expect(result).toContain('+1 more commit]')
})
// Regression: `git log --graph --oneline` intersperses real commit lines (each prefixed with ASCII-art connectors like `* `/`| * `) with connector-only lines that carry no commit hash (e.g. `|\ `, `|/ `, from a merge). The old cap counted every non-empty line as a commit, so those connector-only lines inflated both the truncation point and the "+N more commits" tally past the real commit count.
it('--graph --oneline: connector-only lines are not counted as commits', () => {
const commitLines = Array.from(
{ length: 55 },
(_, i) => `* abc${String(i).padStart(4, '0')}ef Short commit message ${i}`,
)
// Splice in graph connector-only lines (no commit hash) that a real merge produces.
const lines = [...commitLines]
lines.splice(10, 0, '|\\ ')
lines.splice(30, 0, '|/ ')
const text = lines.join('\n')
const result = apply(gitLogFilter, text, ['git', 'log', '--graph', '--oneline'])
// 55 real commits, cap 50 -> exactly 5 elided, not 7 (which the connector-line-inflated count would have reported).
expect(result).toContain('+5 more commits')
expect(result).not.toContain('+7 more commits')
})
})
// --------------------------------------------------------------------------- GitLogFilter — --oneline combined with --stat/-p ---------------------------------------------------------------------------
function makeOnelineStat(n: number): string {
const blocks = Array.from({ length: n }, (_, i) => {
const hash = `abc${String(i).padStart(4, '0')}ef`
return [
`${hash} Commit subject ${i}`,
` src/module/file${i}.ts | 3 +++`,
' 1 file changed, 3 insertions(+)',
'',
].join('\n')
})
return blocks.join('\n').replace(/\n$/, '')
}
describe('GitLogFilter --oneline --stat combo', () => {
// Regression: --oneline combined with --stat interleaves each commit's one-line header with its own stat lines, so the isOneline branch used to cap by raw non-empty LINE count instead of commit count -- with ~20 real commits (well under the 50-commit cap) but ~80 raw lines (over the 50-LINE cap this branch used to apply), it wrongly elided commits that should have been shown in full.
it('~20 commits: no bogus elision (commit count, not raw line count, drives the cap)', () => {
const text = makeOnelineStat(20)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline', '--stat'])
expect(result).not.toContain('more commits')
for (let i = 0; i < 20; i++) {
expect(result).toContain(`Commit subject ${i}`)
expect(result).toContain(`file${i}.ts`)
}
})
// 60 real commits, ~240 raw lines. The old line-based cap would report "+190 more commits" (240 - 50) -- wildly wrong for only 60 actual commits. The fixed commit-block-based cap must report exactly the 10 commits actually elided (60 - 50).
it('60 commits: "+N more commits" reflects actual commit count, not raw stat-line count', () => {
const text = makeOnelineStat(60)
const result = apply(gitLogFilter, text, ['git', 'log', '--oneline', '--stat'])
expect(result).toContain('+10 more commits')
expect(result).toContain('Commit subject 0')
expect(result).not.toContain('Commit subject 59')
})
})
// --------------------------------------------------------------------------- GitLogFilter — full format ---------------------------------------------------------------------------
function makeFullCommits(n: number): string {
const blocks: string[] = []
for (let i = 0; i < n; i++) {
blocks.push(
`commit abc${String(i).padStart(4, '0')}ef1234567890\n` +
`Author: Dev User <dev@example.com>\n` +
`Date: Mon Jan ${String(i + 1).padStart(2, '0')} 10:00:00 2025 +0000\n` +
`\n` +
` Fix bug number ${i}\n`,
)
}
return blocks.join('\n')
}
describe('GitLogFilter full format', () => {
it('short log passthrough (≤10 commits)', () => {
const text = makeFullCommits(5)
const result = apply(gitLogFilter, text, ['git', 'log'])
expect(result).toContain('Fix bug number 0')
expect(result).toContain('Fix bug number 4')
})
it('long log (20 commits) collapsed to fewer lines', () => {
const text = makeFullCommits(20)
const result = apply(gitLogFilter, text, ['git', 'log'])
const origNonEmpty = text.split('\n').filter((l) => l.trim()).length
const resNonEmpty = result.split('\n').filter((l) => l.trim()).length
expect(resNonEmpty).toBeLessThan(origNonEmpty)
expect(result).toContain('abc0000ef')
})
it('merge commits preserved in collapsed output', () => {
const block =
'commit abcdef1234567890\n' +
'Merge: aaa bbb\n' +
'Author: User <u@e.com>\n' +
'Date: Mon Jan 01 10:00:00 2025 +0000\n\n Merge branch feature\n'
const text = Array.from({ length: 15 }, () => block).join('')
const result = apply(gitLogFilter, text, ['git', 'log'])
expect(result).toContain('Merge:')
})
})
// --------------------------------------------------------------------------- GitLogFilter — patch format ---------------------------------------------------------------------------
function makePatchLog(nPatchLines: number): string {
const diffLines = Array.from({ length: nPatchLines }, (_, i) => `+line ${i}`).join('\n')
return (
'commit abcdef1234567890\n' +
'Author: User <u@e.com>\n' +
'Date: Mon Jan 01 10:00:00 2025 +0000\n\n Big change\n\n' +
'diff --git a/foo.py b/foo.py\n' +
'--- a/foo.py\n+++ b/foo.py\n@@ -1,5 +1,5 @@\n' +
diffLines
)
}
describe('GitLogFilter patch format', () => {
it('small patch (10 lines) passthrough', () => {
const text = makePatchLog(10)
const result = apply(gitLogFilter, text, ['git', 'log', '-p'])
expect(result).not.toContain('omitted by token-goat')
})
it('large patch (60 lines) collapsed with omission marker', () => {
const text = makePatchLog(60)
const result = apply(gitLogFilter, text, ['git', 'log', '-p'])
expect(result).toMatch(/patch:.*omitted by token-goat/)
})
})
// --------------------------------------------------------------------------- GitLogFilter — stat format ---------------------------------------------------------------------------
function makeStatLog(nFiles: number): string {
const statLines = Array.from({ length: nFiles }, (_, i) => ` src/file${i}.py | 5 +++++`).join('\n')
return (
'commit abcdef1234567890\n' +
'Author: User <u@e.com>\n' +
'Date: Mon Jan 01 10:00:00 2025 +0000\n\n Refactor many files\n\n' +
statLines +
`\n ${nFiles} files changed, ${nFiles * 5} insertions(+)`
)
}
describe('GitLogFilter stat format', () => {
it('small stat (5 files) passthrough', () => {
const text = makeStatLog(5)
const result = apply(gitLogFilter, text, ['git', 'log', '--stat'])
expect(result).toContain('file0.py')
})
it('large stat (30 files) collapsed with omission marker', () => {
const text = makeStatLog(30)
const result = apply(gitLogFilter, text, ['git', 'log', '--stat'])
expect(result).toContain('more stat lines omitted')
})
})
// --------------------------------------------------------------------------- GitLogFilter — --name-only / --numstat / --stat=<width> ---------------------------------------------------------------------------
// Regression: `_compressGitLogEnhanced`'s isStat set named `--stat`, `--shortstat`, `--name-status` but not `--name-only` or `--numstat`, so both fell through to the full-format collapser (above 10 commits) or the oneline hash-only branch (at any commit count), which discard every line that is neither a commit header nor an indented subject -- a bare file path is neither, so every filename vanished. `--stat=<width>` (a variable-width stat header) also missed the set because it is stored in argv as a single `--stat=200` token, which `flags.has('--stat')` never matches.
describe('GitLogFilter --name-only / --numstat', () => {
// CAPTURE: `git log --name-only -12 --no-color` in this repo (token-goat), 2026-09-17, git 2.x. 12 commits is deliberate: it sits above the `commits.length <= 10` full-format collapse threshold at which the router used to discard every path, while every other full-format fixture in this file uses 5. A path line here sits in the exact position a full-format log puts a body paragraph, so this pins that the router keys off the `--name-only` flag and not off the shape of the line.
it('keeps every filename across a 12-commit --name-only log', () => {
const result = apply(gitLogFilter, CAPTURE_NAME_ONLY_12, ['git', 'log', '--name-only', '-12', '--no-color'])
expect(result).toContain('src/tool_filters/package_managers.ts')
expect(result).toContain('tests/tool_filters_package_managers.test.ts')
expect(result).toContain('src/tool_filters/pytest.ts')
expect(result).toContain('src/tool_filters/git.ts')
expect(result).toContain('src/xlsx_reader.ts')
expect(result).toContain('src/transcript_extract.ts')
expect(result).toContain('CHANGELOG.md')
// Commit 1 and commit 12 (oldest kept commit) subjects, to prove the header survives too.
expect(result).toContain("anchor the general deprecation regex to npm's own warning line")
expect(result).toContain('index a Groovy generic method whose type-parameter list precedes its return type')
})
// CAPTURE: `git log --oneline --name-only -8 --no-color` in this repo, 2026-09-17, git 2.x. 8 commits sits below the 10-commit full-format threshold, so this pins that the SECOND dropper -- the oneline branch's hash-only filter -- was fixed independently of the first: fixture A alone cannot prove this, since it never enters the oneline branch at all.
it('keeps every filename across an 8-commit --oneline --name-only log', () => {
const result = apply(gitLogFilter, CAPTURE_ONELINE_NAME_ONLY_8, [
'git',
'log',
'--oneline',
'--name-only',
'-8',
'--no-color',
])
expect(result).toContain('src/tool_filters/package_managers.ts')
expect(result).toContain('tests/tool_filters_package_managers.test.ts')
expect(result).toContain('src/tool_filters/pytest.ts')
expect(result).toContain('src/docx_extract.ts')
expect(result).not.toContain('more commits')
})
// CAPTURE: `git log --numstat -12 --no-color` in this repo, 2026-09-17, git 2.x. No binary-file line (`-\t-\tpath`) appeared in this capture, so only the real added/deleted numstat shape is asserted here.
it('keeps real numstat lines (tab-separated added/deleted/path) across a 12-commit log', () => {
const result = apply(gitLogFilter, CAPTURE_NUMSTAT_12, ['git', 'log', '--numstat', '-12', '--no-color'])
expect(result).toContain('5\t18\tsrc/tool_filters/package_managers.ts')
expect(result).toContain('7\t2\tsrc/tool_filters/git.ts')
})
// 12 commits, each with its own stat lines: above the full-format collapser's 10-commit threshold, so this proves `--stat=200` is routed to the stat compressor and not the full-format collapser, which would discard every stat line above that threshold.
it('--stat=<width> is treated as a stat shape, not the full-format collapser', () => {
const commits = Array.from({ length: 12 }, (_, i) =>
`commit abc${String(i).padStart(4, '0')}ef1234567890\n` +
`Author: Dev User <dev@example.com>\n` +
`Date: Mon Jan ${String(i + 1).padStart(2, '0')} 10:00:00 2025 +0000\n\n` +
` Refactor pass ${i}\n\n` +
` src/wide/commit${i}_file0.py | 5 +++++\n` +
` src/wide/commit${i}_file1.py | 5 +++++\n` +
` 2 files changed, 10 insertions(+)`,
).join('\n\n')
const result = apply(gitLogFilter, commits, ['git', 'log', '--stat=200', '-12', '--no-color'])
expect(result).toContain('src/wide/commit0_file0.py')
expect(result).toContain('src/wide/commit11_file1.py')
})
})
// --------------------------------------------------------------------------- GitLogFilter stat shapes under the shipping line cap ---------------------------------------------------------------------------
// `_compressGitLogCapped` (the shared body of --stat/--name-only/--numstat/--name-status/-p) caps only the stat/patch lines per commit and left the message body uncapped, so a repo with multi-paragraph commit bodies (this one) pushed a 12-commit stat-shaped log to 335-347 raw lines -- past the 200-line cap the `balanced` profile actually ships into -- and the generic tail-truncation cap then picked survivors by an error-keyword regex matching commit-message prose, not by git structure, dropping 39 of 43 file lines. Every existing stat/name-only/numstat test above drives the filter through the bare `apply` helper, whose default cap is `DEFAULT_MAX_LINES` (1000) -- never the 200 the shipping path (`compressOutput` at the `balanced` profile) actually passes, so none of them could have caught this. These tests drive `compressOutput` directly so the cap under test is the one the CLI ships.
describe('GitLogFilter stat shapes under the shipping line cap', () => {
it('keeps every filename across a 12-commit --name-only log under the balanced (200-line) cap', () => {
const result = compressOutput(gitLogFilter, CAPTURE_NAME_ONLY_12, '', 0, ['git', 'log', '--name-only', '-12', '--no-color'], {
compressionProfile: 'balanced',
}).text
const lines = result.split('\n')
expect(lines.length).toBeLessThanOrEqual(200)
expect(result).not.toContain('lines omitted ---')
expect(result).toContain('src/tool_filters/package_managers.ts')
expect(result).toContain('tests/tool_filters_package_managers.test.ts')
expect(result).toContain('src/tool_filters/pytest.ts')
expect(result).toContain('src/xlsx_reader.ts')
expect(result).toContain('src/transcript_extract.ts')
expect(result).toContain('src/languages/groovy.ts')
const fileLineCount = lines.filter((ln) => /^[A-Za-z0-9_./-]+\.[A-Za-z0-9]+$/.test(ln)).length
expect(fileLineCount).toBe(43)
})
it('keeps every numstat line across a 12-commit --numstat log under the balanced (200-line) cap', () => {
const result = compressOutput(gitLogFilter, CAPTURE_NUMSTAT_12, '', 0, ['git', 'log', '--numstat', '-12', '--no-color'], {
compressionProfile: 'balanced',
}).text
const lines = result.split('\n')
expect(lines.length).toBeLessThanOrEqual(200)
expect(result).not.toContain('lines omitted ---')
expect(result).toContain('5\t18\tsrc/tool_filters/package_managers.ts')
expect(result).toContain('7\t2\tsrc/tool_filters/git.ts')
const fileLineCount = lines.filter((ln) => /^\d+\t\d+\t\S+$/.test(ln)).length
expect(fileLineCount).toBe(43)
})
it('keeps every stat line across a 12-commit --stat log under the balanced (200-line) cap', () => {
const result = compressOutput(gitLogFilter, CAPTURE_STAT_12, '', 0, ['git', 'log', '--stat', '-12', '--no-color'], {
compressionProfile: 'balanced',
}).text
const lines = result.split('\n')
expect(lines.length).toBeLessThanOrEqual(200)
expect(result).not.toContain('lines omitted ---')
expect(result).toContain('src/tool_filters/git.ts | 9 +++++++--')
expect(result).toContain('3 files changed, 52 insertions(+), 2 deletions(-)')
// Commit 1 (newest) and commit 12 (oldest kept) subjects, so the summary header survives.
expect(result).toContain('route --name-only, --numstat, and --stat=<width> logs as stat shapes')
expect(result).toContain('keep a marker that follows a rejected kind word on the same line')
// A real diffstat line is `<path> | <count> <+/->`; the synthesized commit-header detail line (` author | date | "subject"`) also contains ` | ` and a `-` from the timezone offset (e.g. `-0500`), so this is anchored to the trailing `<digits><+/->` diffstat shape to avoid double-counting header lines as stat lines.
const statLineCount = lines.filter((ln) => /^\s*\S+\s*\|\s*\d+\s*[+-]*$/.test(ln)).length
expect(statLineCount).toBe(43)
})
// Mutating the message-line predicate from `/^ {4}/` to `/^ /` also eats stat lines (they start with a single leading space), so this test is the one that would go red on that mutation -- the name-only/numstat tests above would not, since those shapes have no leading-space lines.
it('the --stat capture is required: a one-space predicate would eat stat lines too', () => {
expect(CAPTURE_STAT_12.split('\n').some((ln) => /^ \S/.test(ln) && ln.includes(' | '))).toBe(true)
})
it('a stat log under the cap passes through unchanged (the collapse never fires)', () => {
const text = makeStatLog(5)
const uncapped = apply(gitLogFilter, text, ['git', 'log', '--stat'])
const capped = compressOutput(gitLogFilter, text, '', 0, ['git', 'log', '--stat'], { compressionProfile: 'balanced' }).text
expect(capped).toBe(uncapped)
})
})
// --------------------------------------------------------------------------- GitDiffFilter — dispatch ---------------------------------------------------------------------------
describe('GitDiffFilter dispatch', () => {
it('is registered for git diff', () => {
expect(selectFilter(['git', 'diff'])?.name).toBe('git-diff')
})
it('is registered for git show', () => {
expect(selectFilter(['git', 'show'])?.name).toBe('git-diff')
})
it('does not match git log', () => {
expect(selectFilter(['git', 'log'])?.name).not.toBe('git-diff')
})
})
// --------------------------------------------------------------------------- GitDiffFilter — binary files ---------------------------------------------------------------------------
describe('GitDiffFilter binary', () => {
it('binary file collapsed to header + summary', () => {
const text =
'diff --git a/image.png b/image.png\n' +
'index abc123..def456 100644\n' +
'Binary files a/image.png and b/image.png differ\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('Binary files a/image.png and b/image.png differ')
expect(result).toContain('diff --git a/image.png')
})
it('non-binary diff unchanged', () => {
const text =
'diff --git a/foo.py b/foo.py\n' +
'--- a/foo.py\n+++ b/foo.py\n@@ -1,3 +1,3 @@\n-old\n+new\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('-old')
expect(result).toContain('+new')
})
})
// --------------------------------------------------------------------------- GitDiffFilter — large hunks ---------------------------------------------------------------------------
function makeLargeHunkDiff(nChanged: number): string {
const hunkLines = Array.from({ length: nChanged }, (_, i) => `+line ${i}`).join('\n')
return (
'diff --git a/big.py b/big.py\n--- a/big.py\n+++ b/big.py\n@@ -1,100 +1,100 @@\n context\n' +
hunkLines
)
}
describe('GitDiffFilter large hunk', () => {
it('small hunk (10 changed) passthrough', () => {
const text = makeLargeHunkDiff(10)
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).not.toContain('omitted by token-goat')
})
it('large hunk (80 changed) truncated', () => {
const text = makeLargeHunkDiff(80)
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('omitted by token-goat')
})
it('diff header lines preserved after large-hunk truncation', () => {
const text = makeLargeHunkDiff(80)
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('diff --git a/big.py')
expect(result).toContain('--- a/big.py')
expect(result).toContain('+++ b/big.py')
})
// Regression: `git diff --cc` (combined diffs from a merge-commit conflict resolution) use a `diff --cc <path>` file-boundary line instead of `diff --git a/<path> b/<path>`. The file boundary regex used to only recognize `diff --git `, so combined diffs never got split into per-file blocks here at all -- the whole diff fell through as a single unprocessed block and large-hunk compression never applied, even for a single huge hunk.
it('git diff --cc gets per-file compression (binary collapse) like a normal diff', () => {
const text =
'diff --cc image.png\n' +
'index abc123,def456..0000000\n' +
'Binary files a/image.png and b/image.png differ\n'
const result = apply(gitDiffFilter, text, ['git', 'diff', '--cc', 'HEAD'])
expect(result).toContain('diff --cc image.png')
expect(result).toContain('Binary files a/image.png and b/image.png differ')
// The `index` metadata line is dropped only when the block was recognized as a combined-diff file boundary and ran through binary collapse (header + binary-summary line only). Before the fix, `diff --cc` never matched the file-boundary regex, so the whole block (including this line) fell through unprocessed.
expect(result).not.toContain('index abc123,def456..0000000')
})
// Regression: _GIT_DIFF_BINARY_RE only matched the plain two-filename binary message ("Binary files a/x and b/x differ"). Real `git diff --cc`/`git show --cc` binary-conflict output omits filenames entirely ("Binary files differ"), so that combined-diff form never matched and binary collapse never triggered for --cc binary conflicts.
it('collapses a combined-diff (--cc) binary message that omits filenames', () => {
const text =
'diff --cc image.png\n' +
'index abc123,def456..0000000\n' +
'Binary files differ\n'
const result = apply(gitDiffFilter, text, ['git', 'diff', '--cc', 'HEAD'])
expect(result).toContain('diff --cc image.png')
expect(result).toContain('Binary files differ')
expect(result).not.toContain('index abc123,def456..0000000')
})
// Regression: real `git diff --cc`/`git show --cc` output for a text conflict uses a triple-`@` combined-diff hunk header (`@@@ -a,b -c,d +e,f @@@`), one extra `@` per merged parent -- not the plain-diff `@@ -a,b +c,d @@`. Confirmed against a real merge-commit combined diff (`git show --cc <merge-sha>`) during dogfooding: with only the file-boundary fix, a large conflict hunk still passed through byte-identical to the pre-fix output, because _GIT_DIFF_HUNK_RE never split it into hunks in the first place. Both the file-boundary AND the hunk-boundary regex need to recognize the combined-diff format for large-hunk truncation to actually engage on `--cc` output.
it('git diff --cc: large combined-diff hunk (triple-@ header) gets truncated like a normal diff', () => {
const changedLines = Array.from({ length: 80 }, (_, i) => `+line ${i}`).join('\n')
const text =
'diff --cc conflict.py\n' +
'index abc123,def456..0000000\n' +
'--- a/conflict.py\n+++ b/conflict.py\n@@@ -1,3 -1,3 +1,83 @@@\n context\n' +
changedLines
const result = apply(gitDiffFilter, text, ['git', 'diff', '--cc', 'HEAD'])
expect(result).toContain('diff --cc conflict.py')
expect(result).toContain('omitted by token-goat')
})
})
// --------------------------------------------------------------------------- GitDiffFilter — JSONL hunk (semantic summary) ---------------------------------------------------------------------------
function makeJsonlDiff(n: number): string {
const record = { ts: '2026-01-01T00:00:00Z', entity: 'campaign', op: 'create', success: true }
const added = Array.from({ length: n }, (_, i) => `+${JSON.stringify({ ...record, i })}`).join('\n')
return (
'diff --git a/audit.jsonl b/audit.jsonl\n--- a/audit.jsonl\n+++ b/audit.jsonl\n@@ -1,3 +1,100 @@\n existing_line\n' +
added
)
}
describe('GitDiffFilter JSONL hunk', () => {
it('large JSONL hunk (100 records) gets semantic summary', () => {
const diff = makeJsonlDiff(100)
const result = apply(gitDiffFilter, diff, ['git', 'diff'])
expect(result).toContain('repetitive JSON/JSONL block')
expect(result).toContain('+100 JSON records added')
})
it('semantic summary includes sample lines', () => {
const diff = makeJsonlDiff(100)
const result = apply(gitDiffFilter, diff, ['git', 'diff'])
expect(result).toMatch(/"ts":/)
})
it('semantic summary includes bash-output recall hint', () => {
const diff = makeJsonlDiff(100)
const result = apply(gitDiffFilter, diff, ['git', 'diff'])
expect(result).toContain('bash-output')
})
it('small JSONL hunk (5 records) uses normal truncation (no semantic summary)', () => {
const diff = makeJsonlDiff(5)
const result = apply(gitDiffFilter, diff, ['git', 'diff'])
expect(result).not.toContain('repetitive JSON/JSONL block')
})
it('diff header preserved in semantic summary', () => {
const diff = makeJsonlDiff(100)
const result = apply(gitDiffFilter, diff, ['git', 'diff'])
expect(result).toContain('diff --git a/audit.jsonl')
})
it('regression: 611-line JSONL append compressed to <10% of original', () => {
const record = {
ts: '2026-06-08T22:46:10.327Z',
run_id: 'local-1780958770327',
platform: 'google_ads',
entity_type: 'campaign',
operation: 'create',
resource_name: null,
campaign_name: null,
before: null,
after: null,
module: 'TestModule',
success: true,
}
const added = Array.from({ length: 611 }, (_, i) => `+${JSON.stringify({ ...record, i })}`).join('\n')
const diff =
'diff --git a/memory/ads/mutation-audit-log.jsonl b/memory/ads/mutation-audit-log.jsonl\n' +
'--- a/memory/ads/mutation-audit-log.jsonl\n' +
'+++ b/memory/ads/mutation-audit-log.jsonl\n' +
'@@ -2403,3 +2403,611 @@\n existing_record\n' +
added
const result = apply(gitDiffFilter, diff, ['git', 'diff'])
expect(result).toContain('repetitive JSON/JSONL block')
expect(result.length).toBeLessThan(diff.length * 0.1)
})
})
// --------------------------------------------------------------------------- GitDiffFilter — whitespace/EOL-only hunk collapse ---------------------------------------------------------------------------
describe('GitDiffFilter whitespace/EOL-only hunk collapse', () => {
it('trailing-whitespace-only hunk collapses to a marker', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,3 +1,3 @@\n context\n-line one \n+line one\n context\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('whitespace/EOL-only change, collapsed')
expect(result).not.toContain('-line one')
expect(result).not.toContain('+line one')
// File header and hunk header are preserved even though the hunk body is elided.
expect(result).toContain('diff --git a/foo.py')
expect(result).toContain('@@ -1,3 +1,3 @@')
})
it('CRLF-vs-LF-only hunk collapses to a marker', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,2 +1,2 @@\n-line one\r\n+line one\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('whitespace/EOL-only change, collapsed')
expect(result).not.toContain('-line one')
})
it('internal-whitespace-only hunk (reformatted spacing, identical stripped content) collapses', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,2 +1,2 @@\n-x = 1+2\n+x = 1 + 2\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('whitespace/EOL-only change, collapsed')
})
it('mixed real+whitespace hunk does NOT collapse (passes through unmodified)', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,4 +1,4 @@\n context\n-line one \n+line one\n-real change\n+different content\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).not.toContain('whitespace/EOL-only change, collapsed')
expect(result).toContain('-line one ')
expect(result).toContain('+line one')
expect(result).toContain('-real change')
expect(result).toContain('+different content')
})
it('unrelated-content hunk does NOT collapse', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,2 +1,2 @@\n-old value\n+new value\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).not.toContain('whitespace/EOL-only change, collapsed')
expect(result).toContain('-old value')
expect(result).toContain('+new value')
})
it('unequal -/+ counts within a hunk does NOT collapse, even if all lines are whitespace-only individually', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,2 +1,3 @@\n context\n-line one \n+line one\n+line two\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).not.toContain('whitespace/EOL-only change, collapsed')
expect(result).toContain('-line one ')
expect(result).toContain('+line two')
})
it('multi-hunk file: whitespace-only hunk collapses while a mixed hunk in the same file does not', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,3 +1,3 @@\n context\n-line one \n+line one\n context\n' +
'@@ -50,2 +50,2 @@\n-old value\n+new value\n'
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('whitespace/EOL-only change, collapsed')
expect(result).not.toContain('-line one')
expect(result).toContain('-old value')
expect(result).toContain('+new value')
})
it('collapses via the real selectFilter dispatch path (git diff), not just a directly-instantiated filter', () => {
const text =
'diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n' +
'@@ -1,3 +1,3 @@\n context\n-line one \n+line one\n context\n'
const filter = selectFilter(['git', 'diff'])
expect(filter?.name).toBe('git-diff')
const result = apply(filter as typeof gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('whitespace/EOL-only change, collapsed')
expect(result).not.toContain('-line one')
})
it('does not misfire on a large all-whitespace hunk that would otherwise hit large-hunk truncation', () => {
const nPairs = 80
const removed = Array.from({ length: nPairs }, (_, i) => `-line ${i} `).join('\n')
const added = Array.from({ length: nPairs }, (_, i) => `+line ${i}`).join('\n')
const text =
'diff --git a/big.py b/big.py\n--- a/big.py\n+++ b/big.py\n@@ -1,80 +1,80 @@\n context\n' +
removed +
'\n' +
added
const result = apply(gitDiffFilter, text, ['git', 'diff'])
expect(result).toContain('whitespace/EOL-only change, collapsed')
expect(result).not.toContain('omitted by token-goat')
})
})
// --------------------------------------------------------------------------- GitDiffFilter — stat rollup ---------------------------------------------------------------------------
function makeStatDiff(nFiles: number): string {
const statLines = Array.from(
{ length: nFiles },
(_, i) => ` src/module/file${i}.py | ${i + 1} ${'+'.repeat(i + 1)}`,
).join('\n')
const adds = Array.from({ length: nFiles }, (_, i) => i + 1).reduce((a, b) => a + b, 0)
return statLines + `\n ${nFiles} files changed, ${adds} insertions(+)`
}
describe('GitDiffFilter stat rollup', () => {
it('small stat (5 files) passthrough', () => {
const text = makeStatDiff(5)
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('file0.py')
})
it('large stat (25 files) rolls up to directory summary', () => {
const text = makeStatDiff(25)
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('src/ (25 files,')
expect(result).not.toContain('file0.py')
})
it('large stat summary line always present', () => {
const text = makeStatDiff(25)
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('files changed')
})
// PROVENANCE: CAPTURE. The three row spellings are real `git diff --stat` output, run on 2026-09-22 in a throwaway repository built for this: a binary file whose size changed (` b.bin | Bin 3 -> 5 bytes`), a binary file whose size did not (` same.bin | Bin 2 -> 2 bytes`), and an ordinary text row (` t.txt | 2 +-`) for contrast. Reading the `Bin` spelling off our own regex would have proved only that the regex agrees with itself -- and it did not agree with git, which is the defect: the pattern required a digit immediately after the bar, so every binary row failed it and went to otherLines. That bucket is excluded from the rollup threshold AND spliced ahead of the rollup, so binary rows both failed to count toward the file total and jumped above the summary they belong in.
it('counts binary rows as files and rolls them up with the rest', () => {
const lines = [
...Array.from({ length: 25 }, (_v, i) => ` src/file${i}.py | 2 +-`),
' src/b.bin | Bin 3 -> 5 bytes',
' src/same.bin | Bin 2 -> 2 bytes',
' 27 files changed, 25 insertions(+), 25 deletions(-)',
].join('\n')
const result = apply(gitDiffFilter, lines, ['git', 'diff', '--stat'])
expect(result, 'binary rows must be counted, not hoisted above the rollup').toContain('src/ (27 files,')
expect(result, 'a rolled-up binary row must not survive verbatim').not.toContain('b.bin')
// A binary change has no line counts to attribute, so the totals must come from the 25 text rows alone (each ` 2 +-` is one add and one delete) rather than picking up the `-` in `3 -> 5`.
expect(result).toContain('+25/-25')
expect(result).toContain('files changed')
})
it('large stat with pathspec -- uses truncation not rollup', () => {
const text = makeStatDiff(25)
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat', '--', 'src/'])
expect(result).toContain('more files changed')
expect(result).toContain('file0.py')
expect(result).not.toContain('src/ (')
})
it('multi-directory rollup produces one line per top-level dir', () => {
const lines = [
' alpha/a.py | 3 +++',
' alpha/b.py | 2 ++',
' beta/c.py | 5 +++++',
' beta/d.py | 1 +',
' gamma/e.py | 4 ++++',
]
const repeated = Array.from({ length: 5 }, () => lines).flat()
const summary = ' 25 files changed, 75 insertions(+)'
const text = [...repeated, summary].join('\n')
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('alpha/ (')
expect(result).toContain('beta/ (')
expect(result).toContain('gamma/ (')
expect(result).not.toContain('a.py')
})
it('root files (no slash) grouped under (root)', () => {
const rootFiles = Array.from({ length: 25 }, (_, i) => ` file${i}.txt | 1 +`)
const text = [...rootFiles, ' 25 files changed, 25 insertions(+)'].join('\n')
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('(root) (25 files,')
expect(result).not.toContain('file0.txt')
})
// Regression: git's brace-compressed rename notation ("prefix/{old => new}/suffix") only had its stray leading "{" / trailing "}" stripped when the braces spanned the ENTIRE path column (no prefix or suffix outside them) -- the common real-world case of a prefix and/or suffix left the braces mid-string, so e.g. "src/{old => new}/file.py" resolved to "new}/file.py" and got rolled up under a garbage "new}/" directory instead of the correct "src/". The full two-path form ("old/path => new/path", no braces) already worked and must keep working.
it('rename notation (brace-compressed and full two-path) rolls up under the correct new-path directory', () => {
const bracedWithPrefixSuffix = Array.from(
{ length: 11 },
(_, i) => ` src/{old => new}/file${i}.py | ${i + 1} ${'+'.repeat(i + 1)}`,
)
const bracedWholeTopDir = Array.from(
{ length: 10 },
(_, i) => ` {old-root => new-root}/file${i}.py | ${i + 1} ${'+'.repeat(i + 1)}`,
)
const summary = ' 21 files changed, 231 insertions(+)'
const text = [...bracedWithPrefixSuffix, ...bracedWholeTopDir, summary].join('\n')
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('src/ (11 files,')
expect(result).toContain('new-root/ (10 files,')
// Not corrupted into a garbage directory carrying a stray brace character.
expect(result).not.toMatch(/new\}/)
expect(result).not.toMatch(/\{old/)
})
it('full two-path rename notation (no braces) still rolls up under the new path directory', () => {
const renamed = Array.from(
{ length: 21 },
(_, i) => ` old/dir/file${i}.py => new/dir/file${i}.py | ${i + 1} ${'+'.repeat(i + 1)}`,
)
const summary = ' 21 files changed, 231 insertions(+)'
const text = [...renamed, summary].join('\n')
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('new/ (21 files,')
expect(result).not.toContain('old/')
})
// Regression: git's --stat bar-graph glyphs are WIDTH-SCALED once a file's true change count exceeds --stat-width -- the +/- ratio survives scaling but the absolute glyph count does not (e.g. a real 900/100 split can render as only 45 '+' / 5 '-' characters). Counting bar glyphs directly silently undercounted large diffs by up to ~20x; the fix recovers the true split from the line's numeric total column instead.
it('recovers real insert/delete counts from a width-scaled stat bar instead of undercounting', () => {
const smallFiles = Array.from({ length: 20 }, (_, i) => ` small/file${i}.py | 1 +`)
const bigFile = ` big/file.ts | 1000 ${'+'.repeat(45)}${'-'.repeat(5)}`
const summary = ' 21 files changed, 1020 insertions(+), 100 deletions(-)'
const text = [...smallFiles, bigFile, summary].join('\n')
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat'])
expect(result).toContain('big/ (1 file, +900/-100)')
expect(result).toContain('small/ (20 files, +20/-0)')
})
it('pathspec truncation branch also recovers real counts instead of undercounting the scaled bar', () => {
const headFiles = Array.from({ length: 10 }, (_, i) => ` src/head${i}.py | 1 +`)
const elidedSmall = Array.from({ length: 14 }, (_, i) => ` src/mid${i}.py | 1 +`)
const bigFile = ` src/big.ts | 1000 ${'+'.repeat(45)}${'-'.repeat(5)}`
const summary = ' 25 files changed, 1029 insertions(+), 100 deletions(-)'
const text = [...headFiles, ...elidedSmall, bigFile, summary].join('\n')
const result = apply(gitDiffFilter, text, ['git', 'diff', '--stat', '--', 'src/'])
expect(result).toContain('+15 more files changed, +914 -100 lines')
})
})
// --------------------------------------------------------------------------- GitDiffFilter multi-file diffs under the shipping line cap ---------------------------------------------------------------------------
// _compressGitDiffBody compressed per hunk and per file but never measured its own output against the line cap it ships into: any diff whose per-hunk-compressed form still exceeded 200 lines (the `balanced` profile's cap) was handed whole to the generic tail-truncation truncator, which keeps 10 head + 10 tail lines plus context around error-signal keyword matches -- in a diff, survivors were chosen by which hunks happened to contain words like `Error`/`failed`, so the file list, the one thing a diff reader needs first, was what got dropped. A real 9-file, 346-line capture from this repo shipped as 58 lines carrying 1 of 9 `diff --git` headers. Every existing diff test above drives the bare `apply` helper, whose default cap is 1000, never the 200 the shipping path (`compressOutput` at the `balanced` profile) actually passes, so none of them could have caught this. These tests drive `compressOutput` directly so the cap under test is the one the CLI ships. Loop-37's `makeMultiHunkDiff(60)` fallback-parity fixture above collapses to ~43 lines after per-hunk capping and would stay green under a reverted fix, so it is not a substitute for the capture-based assertions here.
describe('GitDiffFilter multi-file diffs under the shipping line cap', () => {
it('the capture is required: per-hunk compression alone still exceeds the 200-line cap', () => {
// Precondition, not the bug itself: without a maxLines in ctx the new cap-aware reducer never runs, so this is what the filter's own per-hunk compression alone produces. If a later hunk-cap change shrinks that below 200 lines, this goes red before the assertions below could go vacuous.
const direct = gitDiffFilter.compress(CAPTURE_DIFF_9_FILES, '', 0, ['git', 'diff', '--no-color'])
expect(direct.split('\n').length).toBeGreaterThan(200)
})
it('keeps every file header across a real 9-file diff under the balanced (200-line) cap', () => {
const result = compressOutput(gitDiffFilter, CAPTURE_DIFF_9_FILES, '', 0, ['git', 'diff', '--no-color'], {
compressionProfile: 'balanced',
}).text
const lines = result.split('\n')
expect(lines.length).toBeLessThanOrEqual(200)
expect(result).not.toContain('lines omitted ---')
expect(result).toContain('collapsed to fit the line cap')
for (const file of [
'diff --git a/CHANGELOG.md b/CHANGELOG.md',
'diff --git a/src/doc_embed_extract.ts b/src/doc_embed_extract.ts',
'diff --git a/src/embed_fingerprint.ts b/src/embed_fingerprint.ts',
'diff --git a/src/tool_filters/cloud.ts b/src/tool_filters/cloud.ts',
'diff --git a/src/tool_filters/containers.ts b/src/tool_filters/containers.ts',
'diff --git a/src/tool_filters/git.ts b/src/tool_filters/git.ts',
'diff --git a/tests/tool_filters_cloud.test.ts b/tests/tool_filters_cloud.test.ts',
'diff --git a/tests/tool_filters_containers.test.ts b/tests/tool_filters_containers.test.ts',
'diff --git a/tests/tool_filters_git.test.ts b/tests/tool_filters_git.test.ts',
]) {
expect(result).toContain(file)
}
// A content line from the first file's hunk must survive verbatim, not just its header -- reverting the "keep earlier files whole" ordering (collapsing every file, including the first) would leave the headers intact while dropping this.
expect(result).toContain(
"+- **The PDF, Word, PowerPoint and Excel readers no longer load on every hook call**: a hook fires before every tool call and parses the whole eagerly reachable half of its bundle before running any of it, and the four document readers (pdfjs, the OOXML and zip machinery, the spreadsheet reader) were reachable from the indexer's document bridge even though the predicate that decides whether a file is one of those formats only looks at its extension. They are now imported where they are used, which takes 69 KB off what every hook call has to parse.",
)
})
it('a small diff under the cap ships byte-identical through compressOutput (the collapse never fires)', () => {
// HAND-DERIVED: a synthetic 3-file diff, well under 200 lines, so this pins that an under-cap diff is untouched by the new reducer.
const diff = [
'diff --git a/a.py b/a.py',
'index aaa1111..aaa2222 100644',
'--- a/a.py',
'+++ b/a.py',
'@@ -1,2 +1,2 @@',
'-old a',
'+new a',
'diff --git a/b.py b/b.py',
'index bbb1111..bbb2222 100644',
'--- a/b.py',
'+++ b/b.py',
'@@ -1,2 +1,2 @@',
'-old b',
'+new b',
'diff --git a/c.py b/c.py',
'index ccc1111..ccc2222 100644',
'--- a/c.py',
'+++ b/c.py',
'@@ -1,2 +1,2 @@',
'-old c',
'+new c',
].join('\n')
const uncapped = apply(gitDiffFilter, diff, ['git', 'diff', '--no-color'])
const capped = compressOutput(gitDiffFilter, diff, '', 0, ['git', 'diff', '--no-color'], { compressionProfile: 'balanced' }).text
expect(capped).toBe(uncapped)
})
})
// --------------------------------------------------------------------------- GitStatusVerboseFilter — dispatch ---------------------------------------------------------------------------
describe('GitStatusVerboseFilter dispatch', () => {
it('is registered for git status', () => {
expect(selectFilter(['git', 'status'])?.name).toBe('git-status')
})
})
// --------------------------------------------------------------------------- GitStatusVerboseFilter — short format passthrough ---------------------------------------------------------------------------
describe('GitStatusVerboseFilter short format', () => {
it('short/porcelain format passes through unchanged', () => {
const text = 'M src/foo.py\n?? src/bar.py\nD src/old.py\n'
const result = apply(gitStatusFilter, text, ['git', 'status'])
expect(result).toContain('src/foo.py')
expect(result).toContain('src/bar.py')
expect(result).toContain('src/old.py')
})
})
// --------------------------------------------------------------------------- GitStatusVerboseFilter — verbose format compression ---------------------------------------------------------------------------
describe('GitStatusVerboseFilter verbose format', () => {
// CAPTURE: real `git status` output from a real scratch repo on this machine (git's own long format, one tracked file modified plus untracked files), pasted verbatim.
it('keeps the literal file lines on a small change set, strips only advice and the trailer', () => {
const text =
'On branch master\n' +
'Changes not staged for commit:\n' +
' (use "git add <file>..." to update what will be committed)\n' +
' (use "git restore <file>..." to discard changes in working directory)\n' +
'\tmodified: tracked.ts\n\n' +
'Untracked files:\n' +
' (use "git add <file>..." to include in what will be committed)\n' +
'\treal_git_status.txt\n' +
'\ts3.sh\n' +
'\tuntracked1.txt\n' +
'\tuntracked2.txt\n\n' +
'no changes added to commit (use "git add" and/or "git commit -a")\n'
const result = apply(gitStatusFilter, text, ['git', 'status'])
// must-not-drop: the actual paths are the entire point of running `git status` on a small tree
expect(result).toContain('modified: tracked.ts')
expect(result).toContain('real_git_status.txt')
expect(result).toContain('s3.sh')
expect(result).toContain('untracked1.txt')
expect(result).toContain('untracked2.txt')
expect(result).not.toContain('use "git add')
expect(result).not.toContain('use "git restore')
expect(result).not.toContain('no changes added to commit')
})
it('nothing to commit line preserved (clean tree)', () => {
const text = 'On branch main\nnothing to commit, working tree clean\n'
const result = apply(gitStatusFilter, text, ['git', 'status'])
expect(result).toContain('nothing to commit, working tree clean')
expect(result).toContain('On branch main')
})
// HAND-DERIVED: 3 files is well under `_DIFF_STAT_DIR_ROLLUP_THRESHOLD` (20, shared with `_compressGitDiffStat`'s directory-rollup gate), so the literal paths must survive -- a small untracked list is exactly the case that names the files, not just their count.
it('small untracked list (3 files) keeps the literal paths', () => {
const files = Array.from({ length: 3 }, (_, i) => `\t new_file_${i}.py`).join('\n')
const text =
'On branch main\n' +
'Untracked files:\n' +
' (use "git add <file>..." to include in what will be committed)\n' +
files +
'\n'
const result = apply(gitStatusFilter, text, ['git', 'status'])
expect(result).toContain('new_file_0.py')
expect(result).toContain('new_file_1.py')
expect(result).toContain('new_file_2.py')
})
// HAND-DERIVED: 25 files exceeds the same 20-file threshold, so this is the case the collapse-to-count path exists for.
it('large untracked list (25 files) grouped to count', () => {
const files = Array.from({ length: 25 }, (_, i) => `\tnew_file_${i}.py`).join('\n')
const text =
'On branch main\n' +
'Untracked files:\n' +
' (use "git add <file>..." to include in what will be committed)\n' +
files +
'\n'
const result = apply(gitStatusFilter, text, ['git', 'status'])
expect(result).toContain('25 untracked')
expect(result).not.toContain('new_file_0.py')
expect(result).not.toContain('new_file_24.py')
})