55 "context"
66 "errors"
77 "flag"
8+ "fmt"
89 "os"
910 "os/exec"
1011 "path/filepath"
@@ -186,17 +187,14 @@ func TestWriteSummary(t *testing.T) {
186187 writeSummary (& buf , sessions )
187188 out := buf .String ()
188189
189- if ! strings .Contains (out , "Found 3 sessions" ) {
190- t .Errorf ("missing session count: %s" , out )
191- }
192- if ! strings .Contains (out , "3.0 KB" ) {
193- t .Errorf ("missing total size: %s" , out )
194- }
195- if ! strings .Contains (out , "projA" ) {
196- t .Errorf ("missing projA: %s" , out )
197- }
198- if ! strings .Contains (out , "projB" ) {
199- t .Errorf ("missing projB: %s" , out )
190+ want := `Found 3 sessions (3.0 KB on disk)
191+
192+ By project:
193+ projA 2
194+ projB 1
195+ `
196+ if out != want {
197+ t .Errorf ("writeSummary() mismatch\n want:\n %s\n got:\n %s" , want , out )
200198 }
201199}
202200
@@ -214,7 +212,8 @@ func TestFormatBytes(t *testing.T) {
214212 }
215213
216214 for _ , tt := range tests {
217- t .Run (tt .want , func (t * testing.T ) {
215+ name := fmt .Sprintf ("%d_bytes" , tt .input )
216+ t .Run (name , func (t * testing.T ) {
218217 got := formatBytes (tt .input )
219218 if got != tt .want {
220219 t .Errorf (
@@ -278,135 +277,78 @@ func TestPrunerMaxMessagesCountsUserOnly(t *testing.T) {
278277 }
279278}
280279
281- func TestPrunerDryRun (t * testing.T ) {
282- d := dbtest .OpenTestDB (t )
283-
284- dbtest .SeedSession (t , d , "s1" , "test" , func (s * db.Session ) {
285- s .EndedAt = dbtest .Ptr ("2024-01-01T00:00:00Z" )
286- s .MessageCount = 0
287- })
288-
289- pruner , buf := newTestPruner (t , d , "" )
290- cfg := PruneConfig {
291- Filter : db.PruneFilter {Project : "test" },
292- DryRun : true ,
293- }
294-
295- if err := pruner .Prune (cfg ); err != nil {
296- t .Fatalf ("Prune: %v" , err )
297- }
298-
299- out := buf .String ()
300- if ! strings .Contains (out , "Dry run" ) {
301- t .Errorf ("expected dry run message: %s" , out )
302- }
303- if ! strings .Contains (out , "Found 1 sessions" ) {
304- t .Errorf ("expected summary: %s" , out )
305- }
306- }
307-
308- func TestPrunerNoMatches (t * testing.T ) {
309- d := dbtest .OpenTestDB (t )
310-
311- pruner , buf := newTestPruner (t , d , "" )
312- cfg := PruneConfig {
313- Filter : db.PruneFilter {Project : "nonexistent" },
314- }
315-
316- if err := pruner .Prune (cfg ); err != nil {
317- t .Fatalf ("Prune: %v" , err )
318- }
319-
320- if ! strings .Contains (buf .String (), "No sessions match" ) {
321- t .Errorf ("expected no-match message: %s" , buf .String ())
322- }
323- }
324-
325- func TestPrunerAbort (t * testing.T ) {
326- d := dbtest .OpenTestDB (t )
327-
328- dbtest .SeedSession (t , d , "s1" , "test" , func (s * db.Session ) {
329- s .EndedAt = dbtest .Ptr ("2024-01-01T00:00:00Z" )
330- s .MessageCount = 0
331- })
332-
333- pruner , buf := newTestPruner (t , d , "n\n " )
334- cfg := PruneConfig {
335- Filter : db.PruneFilter {Project : "test" },
336- }
337-
338- if err := pruner .Prune (cfg ); err != nil {
339- t .Fatalf ("Prune: %v" , err )
340- }
341-
342- if ! strings .Contains (buf .String (), "Aborted" ) {
343- t .Errorf ("expected abort message: %s" , buf .String ())
344- }
345-
346- // Session should still exist.
347- s , err := d .GetSession (context .Background (), "s1" )
348- if err != nil {
349- t .Fatalf ("GetSession: %v" , err )
350- }
351- if s == nil {
352- t .Error ("session was deleted despite abort" )
353- }
354- }
355-
356- func TestPrunerConfirmDelete (t * testing.T ) {
357- d := dbtest .OpenTestDB (t )
358-
359- dbtest .SeedSession (t , d , "s1" , "test" , func (s * db.Session ) {
360- s .EndedAt = dbtest .Ptr ("2024-01-01T00:00:00Z" )
361- s .MessageCount = 0
362- })
363-
364- pruner , buf := newTestPruner (t , d , "y\n " )
365- cfg := PruneConfig {
366- Filter : db.PruneFilter {Project : "test" },
367- }
368-
369- if err := pruner .Prune (cfg ); err != nil {
370- t .Fatalf ("Prune: %v" , err )
371- }
372-
373- if ! strings .Contains (buf .String (), "Deleted 1 sessions" ) {
374- t .Errorf ("expected deletion message: %s" , buf .String ())
375- }
376-
377- s , err := d .GetSession (context .Background (), "s1" )
378- if err != nil {
379- t .Fatalf ("GetSession: %v" , err )
380- }
381- if s != nil {
382- t .Error ("session still exists after confirmed delete" )
280+ func TestPruner_PruneScenarios (t * testing.T ) {
281+ tests := []struct {
282+ name string
283+ input string
284+ cfg PruneConfig
285+ wantOutput []string
286+ wantKept bool
287+ }{
288+ {
289+ name : "dry run" ,
290+ cfg : PruneConfig {Filter : db.PruneFilter {Project : "test" }, DryRun : true },
291+ wantOutput : []string {"Dry run" , "Found 1 sessions" },
292+ wantKept : true ,
293+ },
294+ {
295+ name : "no matches" ,
296+ cfg : PruneConfig {Filter : db.PruneFilter {Project : "nonexistent" }},
297+ wantOutput : []string {"No sessions match" },
298+ wantKept : true ,
299+ },
300+ {
301+ name : "abort" ,
302+ input : "n\n " ,
303+ cfg : PruneConfig {Filter : db.PruneFilter {Project : "test" }},
304+ wantOutput : []string {"Aborted" },
305+ wantKept : true ,
306+ },
307+ {
308+ name : "confirm delete" ,
309+ input : "y\n " ,
310+ cfg : PruneConfig {Filter : db.PruneFilter {Project : "test" }},
311+ wantOutput : []string {"Deleted 1 sessions" },
312+ wantKept : false ,
313+ },
314+ {
315+ name : "yes flag skips prompt" ,
316+ cfg : PruneConfig {Filter : db.PruneFilter {Project : "test" }, Yes : true },
317+ wantOutput : []string {"Deleted 1 sessions" },
318+ wantKept : false ,
319+ },
383320 }
384- }
385-
386- func TestPrunerYesFlag (t * testing.T ) {
387- d := dbtest .OpenTestDB (t )
388321
389- dbtest .SeedSession (t , d , "s1" , "test" , func (s * db.Session ) {
390- s .EndedAt = dbtest .Ptr ("2024-01-01T00:00:00Z" )
391- s .MessageCount = 0
392- })
393-
394- pruner , buf := newTestPruner (t , d , "" )
395- cfg := PruneConfig {
396- Filter : db.PruneFilter {Project : "test" },
397- Yes : true ,
398- }
322+ for _ , tt := range tests {
323+ t .Run (tt .name , func (t * testing.T ) {
324+ d := dbtest .OpenTestDB (t )
325+ dbtest .SeedSession (t , d , "s1" , "test" , func (s * db.Session ) {
326+ s .EndedAt = dbtest .Ptr ("2024-01-01T00:00:00Z" )
327+ s .MessageCount = 0
328+ })
329+
330+ pruner , buf := newTestPruner (t , d , tt .input )
331+ if err := pruner .Prune (tt .cfg ); err != nil {
332+ t .Fatalf ("Prune: %v" , err )
333+ }
399334
400- if err := pruner .Prune (cfg ); err != nil {
401- t .Fatalf ("Prune: %v" , err )
402- }
335+ out := buf .String ()
336+ for _ , want := range tt .wantOutput {
337+ if ! strings .Contains (out , want ) {
338+ t .Errorf ("expected output containing %q, got: %s" , want , out )
339+ }
340+ }
341+ if tt .cfg .Yes && strings .Contains (out , "[y/N]" ) {
342+ t .Error ("should not prompt when --yes is set" )
343+ }
403344
404- out := buf .String ()
405- if strings .Contains (out , "[y/N]" ) {
406- t .Error ("should not prompt when --yes is set" )
407- }
408- if ! strings .Contains (out , "Deleted 1 sessions" ) {
409- t .Errorf ("expected deletion message: %s" , out )
345+ s , _ := d .GetSession (context .Background (), "s1" )
346+ if tt .wantKept && s == nil {
347+ t .Error ("session was deleted unexpectedly" )
348+ } else if ! tt .wantKept && s != nil {
349+ t .Error ("session still exists" )
350+ }
351+ })
410352 }
411353}
412354
0 commit comments