release / go (push) Successful in 1m57s
release / web (push) Successful in 1m8s
release / integration (push) Successful in 2m42s
release / govulncheck (push) Successful in 20s
release / android (push) Successful in 1m53s
release / Build signed APK (push) Successful in 1m43s
release / Build + push container image (push) Successful in 24s
The Title Case sweep of the export found real song titles and artists in fixtures and comments: three songs from one real album, a real song in a release fixture, real artists in a tag test, a link test and a genre test, a real song as a notification example, and a count from a real folder. Each is now invented. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
375 lines
14 KiB
Go
375 lines
14 KiB
Go
package library
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/dhowden/tag"
|
|
)
|
|
|
|
// rawFrame is a frame with a byte-exact payload, so tests can express encoding
|
|
// bytes and embedded nulls that a string-keyed helper can't.
|
|
type rawFrame struct {
|
|
id string
|
|
payload []byte
|
|
}
|
|
|
|
// buildID3v2 assembles a tag for the given major version. Frame size encoding
|
|
// differs per version (2.4 is synchsafe, 2.2/2.3 are plain), which is exactly
|
|
// the kind of detail a parser gets subtly wrong, so tests build all three.
|
|
func buildID3v2(t *testing.T, major byte, frames ...rawFrame) []byte {
|
|
t.Helper()
|
|
var body bytes.Buffer
|
|
for _, f := range frames {
|
|
switch major {
|
|
case 2:
|
|
if len(f.id) != 3 {
|
|
t.Fatalf("v2.2 frame id %q must be 3 bytes", f.id)
|
|
}
|
|
body.WriteString(f.id)
|
|
n := len(f.payload)
|
|
body.Write([]byte{byte(n >> 16), byte(n >> 8), byte(n)})
|
|
case 3:
|
|
body.WriteString(f.id)
|
|
_ = binary.Write(&body, binary.BigEndian, uint32(len(f.payload)))
|
|
body.Write([]byte{0x00, 0x00})
|
|
case 4:
|
|
body.WriteString(f.id)
|
|
body.Write(synchsafeBytes(len(f.payload)))
|
|
body.Write([]byte{0x00, 0x00})
|
|
}
|
|
body.Write(f.payload)
|
|
}
|
|
var out bytes.Buffer
|
|
out.WriteString("ID3")
|
|
out.Write([]byte{major, 0x00, 0x00})
|
|
out.Write(synchsafeBytes(body.Len()))
|
|
out.Write(body.Bytes())
|
|
// A few bytes of MPEG sync so dhowden/tag accepts the file shape.
|
|
out.Write([]byte{0xFF, 0xFB, 0x90, 0x00})
|
|
return out.Bytes()
|
|
}
|
|
|
|
func synchsafeBytes(n int) []byte {
|
|
return []byte{
|
|
byte((n >> 21) & 0x7F),
|
|
byte((n >> 14) & 0x7F),
|
|
byte((n >> 7) & 0x7F),
|
|
byte(n & 0x7F),
|
|
}
|
|
}
|
|
|
|
// utf8Frame builds a text-frame payload: encoding byte 3 (UTF-8) followed by
|
|
// values joined with the null separator ID3v2 uses for multiple values.
|
|
func utf8Frame(values ...string) []byte {
|
|
return append([]byte{0x03}, []byte(strings.Join(values, "\x00"))...)
|
|
}
|
|
|
|
// paddedTag is a v2.4 tag holding one TCON frame followed by 32 bytes of
|
|
// padding, the way taggers leave room to grow.
|
|
func paddedTag(t *testing.T) []byte {
|
|
tagged := buildID3v2(t, 4, rawFrame{"TCON", utf8Frame("Rock")})
|
|
body := tagged[10 : len(tagged)-4]
|
|
padded := append(append([]byte{}, body...), make([]byte, 32)...)
|
|
var out bytes.Buffer
|
|
out.WriteString("ID3")
|
|
out.Write([]byte{4, 0x00, 0x00})
|
|
out.Write(synchsafeBytes(len(padded)))
|
|
out.Write(padded)
|
|
return out.Bytes()
|
|
}
|
|
|
|
// unsynchronisedTag is a v2.3 tag with the unsynchronisation flag set, whose
|
|
// Latin-1 genre holds the byte 0xFF. Once unsynchronised that becomes
|
|
// 0xFF 0x00, indistinguishable from a value separator until the collapse runs.
|
|
func unsynchronisedTag(t *testing.T) []byte {
|
|
payload := concat([]byte{0x00}, []byte("Ro\xffck"), []byte{0x00}, []byte("Pop"))
|
|
inner := buildID3v2(t, 3, rawFrame{"TCON", payload})
|
|
body := inner[10 : len(inner)-4]
|
|
encoded := bytes.ReplaceAll(body, []byte{0xFF}, []byte{0xFF, 0x00})
|
|
if bytes.Equal(encoded, body) {
|
|
t.Fatal("fixture is vacuous: nothing was unsynchronised")
|
|
}
|
|
var out bytes.Buffer
|
|
out.WriteString("ID3")
|
|
out.Write([]byte{3, 0x00, 0x80}) // 0x80 = unsynchronisation
|
|
out.Write(synchsafeBytes(len(encoded)))
|
|
out.Write(encoded)
|
|
return out.Bytes()
|
|
}
|
|
|
|
// Minstrel reads ID3v2 genre frames itself because dhowden/tag rejoins
|
|
// multiple values with the empty string ("Alternative RockRock"). Each case is
|
|
// a detail of the format a reader gets subtly wrong.
|
|
func TestReadID3v2GenreValues(t *testing.T) {
|
|
multi := utf8Frame("Alternative Rock", "Rock")
|
|
for _, tc := range []struct {
|
|
name string
|
|
data func(t *testing.T) []byte
|
|
want []string
|
|
err bool
|
|
}{
|
|
// Frame size encoding differs per version: 2.4 is synchsafe, 2.2 and
|
|
// 2.3 are plain.
|
|
{"v2.2 multi-value", func(t *testing.T) []byte { return buildID3v2(t, 2, rawFrame{"TCO", multi}) }, []string{"Alternative Rock", "Rock"}, false},
|
|
{"v2.3 multi-value", func(t *testing.T) []byte { return buildID3v2(t, 3, rawFrame{"TCON", multi}) }, []string{"Alternative Rock", "Rock"}, false},
|
|
{"v2.4 multi-value", func(t *testing.T) []byte { return buildID3v2(t, 4, rawFrame{"TCON", multi}) }, []string{"Alternative Rock", "Rock"}, false},
|
|
{"a trailing terminator adds no empty value", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 4, rawFrame{"TCON", append(utf8Frame("Jazz"), 0x00)})
|
|
}, []string{"Jazz"}, false},
|
|
// UTF-16 uses a two-byte separator; splitting on single nulls would
|
|
// cut every ASCII character in half.
|
|
{"utf16le, BOM on each value", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 4, rawFrame{"TCON", concat([]byte{0x01},
|
|
[]byte{0xFF, 0xFE}, utf16LE("Rock"), []byte{0x00, 0x00}, []byte{0xFF, 0xFE}, utf16LE("Pop"))})
|
|
}, []string{"Rock", "Pop"}, false},
|
|
// Sloppy but common; without carrying the byte order forward, "Pop"
|
|
// decodes byte-swapped to CJK.
|
|
{"utf16le, BOM only on the first value", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 4, rawFrame{"TCON", concat([]byte{0x01},
|
|
[]byte{0xFF, 0xFE}, utf16LE("Rock"), []byte{0x00, 0x00}, utf16LE("Pop"))})
|
|
}, []string{"Rock", "Pop"}, false},
|
|
{"utf16be, no BOM", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 4, rawFrame{"TCON", concat([]byte{0x02}, utf16BE("Rock"), []byte{0x00, 0x00}, utf16BE("Pop"))})
|
|
}, []string{"Rock", "Pop"}, false},
|
|
// ISO-8859-1 is widened, not reinterpreted as UTF-8.
|
|
{"latin-1", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 3, rawFrame{"TCON", append([]byte{0x00}, []byte("Chanson Fran\xe7aise")...)})
|
|
}, []string{"Chanson Française"}, false},
|
|
// A wrongly decoded size lands the walk mid-frame and misses TCON.
|
|
{"v2.3 frames before TCON", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 3, rawFrame{"TIT2", utf8Frame("Some Title")}, rawFrame{"TPE1", utf8Frame("Some Artist")},
|
|
rawFrame{"TCON", utf8Frame("Shoegaze", "Dream Pop")})
|
|
}, []string{"Shoegaze", "Dream Pop"}, false},
|
|
{"v2.4 frames before TCON", func(t *testing.T) []byte {
|
|
return buildID3v2(t, 4, rawFrame{"TIT2", utf8Frame("Some Title")}, rawFrame{"TPE1", utf8Frame("Some Artist")},
|
|
rawFrame{"TCON", utf8Frame("Shoegaze", "Dream Pop")})
|
|
}, []string{"Shoegaze", "Dream Pop"}, false},
|
|
// The walk stops at padding rather than reading a frame id of zeros.
|
|
{"padding after the last frame", paddedTag, []string{"Rock"}, false},
|
|
{"tag unsynchronisation", unsynchronisedTag, []string{"Roÿck", "Pop"}, false},
|
|
{"no genre frame", func(t *testing.T) []byte { return buildID3v2(t, 4, rawFrame{"TIT2", utf8Frame("Only A Title")}) }, nil, true},
|
|
{"not an ID3 file", func(*testing.T) []byte { return []byte("not a tag at all") }, nil, true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, err := readID3v2GenreValues(bytes.NewReader(tc.data(t)))
|
|
if (err != nil) != tc.err {
|
|
t.Fatalf("err = %v, want error: %v", err, tc.err)
|
|
}
|
|
if !equalStrings(got, tc.want) {
|
|
t.Errorf("genres = %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNormaliseGenreValue(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want []string
|
|
}{
|
|
{"plain text", "Alternative Rock", []string{"Alternative Rock"}},
|
|
{"trims whitespace", " Jazz ", []string{"Jazz"}},
|
|
{"empty", "", nil},
|
|
{"whitespace only", " ", nil},
|
|
|
|
// Numeric references, one value at a time.
|
|
{"bare numeric", "17", []string{"Rock"}},
|
|
{"bare numeric pop", "13", []string{"Pop"}},
|
|
{"bare numeric electronic", "52", []string{"Electronic"}},
|
|
{"winamp extension range", "187", []string{"Indie Rock"}},
|
|
{"numeric out of range", "9999", nil},
|
|
{"negative", "-1", nil},
|
|
|
|
// ID3v2.3 refinement syntax.
|
|
{"parenthesised", "(17)", []string{"Rock"}},
|
|
{"parenthesised repeated", "(51)(39)", []string{"Techno-Industrial", "Noise"}},
|
|
{"parenthesised with refinement", "(17)Hard Rock", []string{"Rock", "Hard Rock"}},
|
|
{"remix", "(RX)", []string{"Remix"}},
|
|
{"cover", "(CR)", []string{"Cover"}},
|
|
{"escaped open paren", "((Weird", []string{"(Weird"}},
|
|
{"parenthesised non-numeric", "(Live)", []string{"(Live)"}},
|
|
|
|
// A label that merely starts with digits is text, not a reference.
|
|
{"digits in a name", "1980s", []string{"1980s"}},
|
|
{"hyphenated", "Lo-Fi Hip Hop", []string{"Lo-Fi Hip Hop"}},
|
|
|
|
// Casing repair runs on text from the file, including the
|
|
// parenthesised and refinement paths; punctuation around an acronym
|
|
// does not hide it.
|
|
{"casing repaired", "Edm", []string{"EDM"}},
|
|
{"casing repaired after a reference", "(17)Uk Garage", []string{"Rock", "UK Garage"}},
|
|
{"casing repaired inside parentheses", "(Live Edm)", []string{"(Live EDM)"}},
|
|
{"casing repaired after an escaped paren", "((Children'S Music", []string{"(Children's Music"}},
|
|
// It never touches a name from the ID3v1 table, which is kept
|
|
// canonical: entry 40's 1990s spelling stays though it reads like
|
|
// damage.
|
|
{"table name not recased", "40", []string{"AlternRock"}},
|
|
{"parenthesised table name not recased", "(40)", []string{"AlternRock"}},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := normaliseGenreValue(tc.in)
|
|
if !equalStrings(got, tc.want) {
|
|
t.Errorf("normaliseGenreValue(%q) = %q, want %q", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// normaliseGenres removes values that repeat within one file, keeping the
|
|
// first spelling; it imposes no canonical case beyond the acronym repair.
|
|
func TestNormaliseGenres(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
in []string
|
|
want []string
|
|
}{
|
|
{"case-insensitive repeats", []string{"Rock", "rock", "ROCK", "Pop"}, []string{"Rock", "Pop"}},
|
|
// "(40)AlternRock" declares one genre twice, numerically and in text.
|
|
{"a reference and its own name", []string{"(40)AlternRock"}, []string{"AlternRock"}},
|
|
{"acronym spellings", []string{"Edm", "EDM", "edm"}, []string{"EDM"}},
|
|
{"nothing usable", []string{"", " ", "9999"}, nil},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := normaliseGenres(tc.in); !equalStrings(got, tc.want) {
|
|
t.Errorf("normaliseGenres(%q) = %q, want %q", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// extractGenres is what the scanner calls, after dhowden/tag has read the
|
|
// file. In every format the values stay separate (dhowden welds ID3 values
|
|
// together and keeps only the last Vorbis GENRE field), numeric ID3
|
|
// references resolve to names, and a file with no genre yields nothing and is
|
|
// not reported as a fallback, which would log a warning for every untagged
|
|
// file in the library.
|
|
func TestExtractGenres(t *testing.T) {
|
|
oggComment := append([]byte("\x03vorbis"), vorbisComment(genreFields("Shoegaze", "Dream Pop")...)...)
|
|
oggComment = append(oggComment, 1) // framing bit
|
|
for _, tc := range []struct {
|
|
name string
|
|
data []byte
|
|
want []string
|
|
}{
|
|
{"id3 multi-value", buildID3v2(t, 4, rawFrame{"TIT2", utf8Frame("A Song")},
|
|
rawFrame{"TCON", utf8Frame("Alternative Rock", "Rock")}), []string{"Alternative Rock", "Rock"}},
|
|
{"id3 numeric references", buildID3v2(t, 4, rawFrame{"TCON", utf8Frame("40", "17")}), []string{"AlternRock", "Rock"}},
|
|
{"id3 with no genre", buildID3v2(t, 4, rawFrame{"TIT2", utf8Frame("A Song")}), nil},
|
|
{"flac repeated fields", buildFLAC(false, append([]string{"TITLE=Eons"}, genreFields(lifeForms...)...)...), lifeForms},
|
|
{"ogg vorbis repeated fields", buildOgg(7, vorbisIdent(), oggComment), []string{"Shoegaze", "Dream Pop"}},
|
|
{"mp4 several data atoms", buildMP4(true, mp4Box("\xa9gen", mp4Text("Hip Hop"), mp4Text("Lo-Fi"))), []string{"Hip Hop", "Lo-Fi"}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
rs := bytes.NewReader(tc.data)
|
|
meta, err := tag.ReadFrom(rs)
|
|
if err != nil {
|
|
t.Fatalf("tag.ReadFrom: %v", err)
|
|
}
|
|
genres, fellBack := extractGenres(meta, rs)
|
|
if fellBack {
|
|
t.Error("fellBack = true, want false")
|
|
}
|
|
if !equalStrings(genres, tc.want) {
|
|
t.Errorf("genres = %q, want %q", genres, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func concat(parts ...[]byte) []byte {
|
|
var out []byte
|
|
for _, p := range parts {
|
|
out = append(out, p...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func utf16LE(s string) []byte {
|
|
out := make([]byte, 0, len(s)*2)
|
|
for _, r := range s {
|
|
out = append(out, byte(r), byte(r>>8))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func utf16BE(s string) []byte {
|
|
out := make([]byte, 0, len(s)*2)
|
|
for _, r := range s {
|
|
out = append(out, byte(r>>8), byte(r))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func equalStrings(a, b []string) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestTrueUpCasing(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
// Casing damage tag editors leave.
|
|
{"edm acronym", "Edm", "EDM"},
|
|
{"idm acronym", "Idm", "IDM"},
|
|
{"aor acronym", "Aor", "AOR"},
|
|
{"uk prefix", "Uk Garage", "UK Garage"},
|
|
{"uk hardcore", "Uk Hardcore", "UK Hardcore"},
|
|
{"acronym mid-phrase", "Trap Edm", "Trap EDM"},
|
|
{"acronym at the end", "Glitch Hop Edm", "Glitch Hop EDM"},
|
|
{"possessive", "Children'S Music", "Children's Music"},
|
|
|
|
// Already correct input must be left exactly alone.
|
|
{"correct acronym", "EDM", "EDM"},
|
|
{"correct possessive", "Children's Music", "Children's Music"},
|
|
|
|
// Case-insensitive, so a lower-cased tag also lands on the canonical
|
|
// form rather than becoming a third variant.
|
|
{"lowercase acronym", "edm", "EDM"},
|
|
|
|
// Names with an apostrophe followed by a real word are NOT contractions
|
|
// and must keep their capital — this is why the suffix list is fixed
|
|
// rather than "lowercase anything after an apostrophe".
|
|
{"irish surname", "O'Brien Core", "O'Brien Core"},
|
|
{"french elision", "D'Arcy Soul", "D'Arcy Soul"},
|
|
|
|
// Ordinary genres pass through untouched. Case is otherwise exposed as
|
|
// the file says it — "Rock" vs "rock" stays a real distinction.
|
|
{"plain", "Alternative Rock", "Alternative Rock"},
|
|
{"lowercase plain", "rock", "rock"},
|
|
{"hyphenated", "Lo-Fi Hip Hop", "Lo-Fi Hip Hop"},
|
|
{"ampersand", "R&B", "R&B"},
|
|
{"empty", "", ""},
|
|
|
|
// Punctuation around a word must not hide the acronym inside it.
|
|
{"parenthesised acronym", "Hip Hop (Edm)", "Hip Hop (EDM)"},
|
|
{"acronym with comma", "Edm, Trap", "EDM, Trap"},
|
|
|
|
// Interior punctuation stays in the core, so these are compared whole
|
|
// and cannot match a fragment by accident.
|
|
{"hyphenated stays whole", "Lo-Fi", "Lo-Fi"},
|
|
{"ampersand stays whole", "Drum & Bass", "Drum & Bass"},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := trueUpCasing(tc.in); got != tc.want {
|
|
t.Errorf("trueUpCasing(%q) = %q, want %q", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|