github.com/derat/nup@v0.0.0-20230418113745-15592ba7c620/cmd/nup/client/files/gains_test.go (about) 1 // Copyright 2022 Daniel Erat. 2 // All rights reserved. 3 4 package files 5 6 import ( 7 "path/filepath" 8 "testing" 9 10 "github.com/derat/nup/cmd/nup/client" 11 "github.com/derat/nup/cmd/nup/mp3gain" 12 "github.com/derat/nup/test" 13 ) 14 15 func TestGainsCache(t *testing.T) { 16 dir := t.TempDir() 17 18 // Song0s and 1s are both from the same album, while 5s is from its own. 19 s0, s1, s5 := test.Song0s, test.Song1s, test.Song5s 20 if err := test.CopySongs(dir, s0.Filename, s1.Filename, s5.Filename); err != nil { 21 t.Fatal(err) 22 } 23 p0 := filepath.Join(dir, s0.Filename) 24 p1 := filepath.Join(dir, s1.Filename) 25 p5 := filepath.Join(dir, s5.Filename) 26 27 info := mp3gain.Info{ 28 TrackGain: -7.25, 29 AlbumGain: -4.5, 30 PeakAmp: 1.125, 31 } 32 mp3gain.SetInfoForTest(&info) 33 defer mp3gain.SetInfoForTest(nil) 34 35 cfg := client.Config{MusicDir: dir} 36 gc, err := NewGainsCache(&cfg, "") 37 if err != nil { 38 t.Fatal("NewGainsCache failed: ", err) 39 } 40 41 // Compute adjustments for a song. 42 if got, err := gc.get(p0, s0.Album, s0.AlbumID); err != nil { 43 t.Fatalf("gc.get(%q, %q, %q) failed: %v", p0, s0.Album, s0.AlbumID, err) 44 } else if got != info { 45 t.Errorf("gc.get(%q, %q, %q) = %+v; want %+v", p0, s0.Album, s0.AlbumID, got, info) 46 } 47 48 // We should've also saved adjustments for the other song in the album. 49 if sz := gc.cache.Size(); sz != 2 { 50 t.Errorf("Computed gain adjustments for %v file(s); want 2", sz) 51 } else if got, ok := gc.cache.GetIfExists(p1); !ok { 52 t.Errorf("Didn't compute gain adjustment for %v", p1) 53 } else if got != info { 54 t.Errorf("Gain adjustment for %v is %+v; want %+v", p1, got, info) 55 } 56 57 // Now compute adjustments for a song in a different album. 58 if got, err := gc.get(p5, s5.Album, s5.AlbumID); err != nil { 59 t.Fatalf("gc.get(%q, %q, %q) failed: %v", p5, s5.Album, s5.AlbumID, err) 60 } else if got != info { 61 t.Errorf("gc.get(%q, %q, %q) = %+v; want %+v", p5, s5.Album, s5.AlbumID, got, info) 62 } 63 if sz := gc.cache.Size(); sz != 3 { 64 t.Errorf("Computed gain adjustments for %v file(s); want 3", sz) 65 } 66 }