github.com/derat/nup@v0.0.0-20230418113745-15592ba7c620/cmd/nup/client/files/song_test.go (about)

     1  // Copyright 2023 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/test"
    12  	"github.com/google/go-cmp/cmp"
    13  )
    14  
    15  func TestReadSong_Override(t *testing.T) {
    16  	dir := t.TempDir()
    17  	cfg := &client.Config{
    18  		MusicDir:    filepath.Join(dir, "music"),
    19  		MetadataDir: filepath.Join(dir, "metadata"),
    20  	}
    21  
    22  	want := test.Song0s
    23  	want.TrackGain = 0
    24  	want.AlbumGain = 0
    25  	want.PeakAmp = 0
    26  	want.Artist = "Overridden Artist"
    27  	test.Must(t, test.CopySongs(cfg.MusicDir, want.Filename))
    28  	p := filepath.Join(cfg.MusicDir, want.Filename)
    29  
    30  	// Test that metadata can be overridden.
    31  	// The overriding logic is tested in more detail in override_test.go.
    32  	if err := UpdateMetadataOverride(cfg, &want); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	if got, err := ReadSong(cfg, p, nil /* fi */, 0, nil /* gc */); err != nil {
    37  		t.Fatalf("ReadSong(cfg, %q, nil, 0, nil) failed: %v", p, err)
    38  	} else if diff := cmp.Diff(want, *got); diff != "" {
    39  		t.Errorf("ReadSong(cfg, %q, nil, 0, nil) returned bad data:\n%s", p, diff)
    40  	}
    41  
    42  	// Also check that the SHA1 and duration are omitted when SkipAudioData is passed.
    43  	want.SHA1 = ""
    44  	want.Length = 0
    45  	if got, err := ReadSong(cfg, p, nil /* fi */, SkipAudioData, nil /* gc */); err != nil {
    46  		t.Fatalf("ReadSong(cfg, %q, nil, SkipAudioData, nil) failed: %v", p, err)
    47  	} else if diff := cmp.Diff(want, *got); diff != "" {
    48  		t.Errorf("ReadSong(cfg, %q, nil, SkipAudioData, nil) returned bad data:\n%s", p, diff)
    49  	}
    50  
    51  	// Check that OnlyFileMetadata works too.
    52  	want.Artist = test.Song0s.Artist
    53  	if got, err := ReadSong(cfg, p, nil /* fi */, SkipAudioData|OnlyFileMetadata, nil /* gc */); err != nil {
    54  		t.Fatalf("ReadSong(cfg, %q, nil, SkipAudioData|OnlyFileMetadata, nil) failed: %v", p, err)
    55  	} else if diff := cmp.Diff(want, *got); diff != "" {
    56  		t.Errorf("ReadSong(cfg, %q, nil, SkipAudioData|OnlyFileMetadata, nil) returned bad data:\n%s", p, diff)
    57  	}
    58  }
    59  
    60  func TestReadSong_ID3V1(t *testing.T) {
    61  	dir := t.TempDir()
    62  	want := test.ID3V1Song
    63  	want.TrackGain = 0
    64  	want.AlbumGain = 0
    65  	want.PeakAmp = 0
    66  	test.Must(t, test.CopySongs(dir, want.Filename))
    67  	p := filepath.Join(dir, want.Filename)
    68  
    69  	cfg := client.Config{MusicDir: dir}
    70  	if got, err := ReadSong(&cfg, p, nil /* fi */, 0, nil /* gc */); err != nil {
    71  		t.Fatalf("ReadSong(cfg, %q, ...) failed: %v", p, err)
    72  	} else if diff := cmp.Diff(want, *got); diff != "" {
    73  		t.Errorf("ReadSong(cfg, %q, ...) returned bad data:\n%s", p, diff)
    74  	}
    75  }
    76  
    77  func TestExtractAlbumDisc(t *testing.T) {
    78  	for _, tc := range []struct {
    79  		orig      string
    80  		album     string
    81  		discNum   int
    82  		discTitle string
    83  	}{
    84  		{"Abbey Road", "Abbey Road", 0, ""},
    85  		{"The Beatles (disc 1)", "The Beatles", 1, ""},
    86  		{"The Beatles (disc 2)", "The Beatles", 2, ""},
    87  		{"The Beatles  (disc 200)", "The Beatles", 200, ""},
    88  		{"The Fragile (disc 1: Left)", "The Fragile", 1, "Left"},
    89  		{"The Fragile (disc 2: Right)", "The Fragile", 2, "Right"},
    90  		{"Indiana Jones: The Soundtracks Collection (disc 1: Raiders of the Lost Ark)",
    91  			"Indiana Jones: The Soundtracks Collection", 1, "Raiders of the Lost Ark"},
    92  		{"Speakerboxxx / The Love Below (disc 2: The Love Below)",
    93  			"Speakerboxxx / The Love Below", 2, "The Love Below"},
    94  	} {
    95  		album, discNum, discTitle := extractAlbumDisc(tc.orig)
    96  		if album != tc.album || discNum != tc.discNum || discTitle != tc.discTitle {
    97  			t.Errorf("extractAlbumDisc(%q) = %q, %d, %q; want %q, %d, %q",
    98  				tc.orig, album, discNum, discTitle, tc.album, tc.discNum, tc.discTitle)
    99  		}
   100  	}
   101  }