test(library): merge repeated cases into tables, drop tests of other code (#8296)
release / web (push) Successful in 1m30s
release / go (push) Successful in 1m57s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m26s
release / android (push) Successful in 4m40s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 28s
release / Verify release artifacts (tag releases only) (push) Skipped

- Moved-file adoption: 14 tests become one table. uniqueMatch, the row
  converters and pgtype's zero UUID are dropped: the adoption table covers
  the first two, and the third tests pgx.
- Reconcile: marking and its change rows, and restoring and its change rows,
  are one test each; the five "a volume looks absent" refusals are one table.
  Dropped: an error-propagation test.
- Genres: the ID3 reader cases, the normalisers and extractGenres across
  ID3, FLAC, Ogg and MP4 are one table each.
- MusicBrainz tag ids: eleven per-format tests become one table.
- Scan trigger: start / skip / reap is one table. Dropped: a closed-pool test.
- Stage publisher: six tests become three.
- Duplicate resolver: the cross-release cases (rip, renamed, kept as fine,
  two titles) asserted the same outcome once the resolver stopped removing
  files. They become one table that also checks the group's note, which is
  where rip and clean still differ. The auto-resolve-off song-link check
  moves into the existing auto-resolve-off test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 12:32:36 -05:00
co-authored by Claude Opus 5.5
parent 4811ec2ec9
commit ac8792323c
9 changed files with 456 additions and 1238 deletions
+141 -287
View File
@@ -67,151 +67,10 @@ func utf8Frame(values ...string) []byte {
return append([]byte{0x03}, []byte(strings.Join(values, "\x00"))...)
}
// TestReadID3v2GenreValues_MultiValue: dhowden/tag
// rejoins these values with the empty string, producing "Alternative RockRock";
// the whole point of our own reader is that they stay separate.
func TestReadID3v2GenreValues_MultiValue(t *testing.T) {
for _, major := range []byte{2, 3, 4} {
id := "TCON"
if major == 2 {
id = "TCO"
}
data := buildID3v2(t, major, rawFrame{id, utf8Frame("Alternative Rock", "Rock")})
got, err := readID3v2GenreValues(bytes.NewReader(data))
if err != nil {
t.Fatalf("v2.%d: %v", major, err)
}
want := []string{"Alternative Rock", "Rock"}
if !equalStrings(got, want) {
t.Errorf("v2.%d genres = %q, want %q", major, got, want)
}
}
}
// Eight values that a joining reader would weld into one 70-character token.
func TestReadID3v2GenreValues_ManyValues(t *testing.T) {
values := []string{
"Boom Bap", "Downtempo", "Hip Hop", "Instrumental",
"Lo-Fi", "Lo-Fi Hip Hop", "Chillwave", "Instrumental Hip Hop",
}
data := buildID3v2(t, 4, rawFrame{"TCON", utf8Frame(values...)})
got, err := readID3v2GenreValues(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if !equalStrings(got, values) {
t.Errorf("genres = %q, want %q", got, values)
}
}
// A trailing null terminator is legal and must not produce an empty value.
func TestReadID3v2GenreValues_TrailingTerminator(t *testing.T) {
data := buildID3v2(t, 4, rawFrame{"TCON", append(utf8Frame("Jazz"), 0x00)})
got, err := readID3v2GenreValues(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if !equalStrings(got, []string{"Jazz"}) {
t.Errorf("genres = %q, want [Jazz]", got)
}
}
// UTF-16 uses a TWO-byte separator. Splitting it on single nulls would cut
// every ASCII character in half, so this guards the width handling.
func TestReadID3v2GenreValues_UTF16(t *testing.T) {
tests := []struct {
name string
payload []byte
}{
{
// Spec-correct: encoding 1 with a BOM on every value.
name: "utf16le, BOM on each value",
payload: concat([]byte{0x01},
[]byte{0xFF, 0xFE}, utf16LE("Rock"),
[]byte{0x00, 0x00},
[]byte{0xFF, 0xFE}, utf16LE("Pop")),
},
{
// Sloppy but common: BOM only on the first value. Without carrying
// the byte order forward, "Pop" decodes byte-swapped to CJK.
name: "utf16le, BOM only on the first value",
payload: concat([]byte{0x01},
[]byte{0xFF, 0xFE}, utf16LE("Rock"),
[]byte{0x00, 0x00}, utf16LE("Pop")),
},
{
// Encoding 2: big-endian, no BOM anywhere.
name: "utf16be no BOM",
payload: concat([]byte{0x02},
utf16BE("Rock"), []byte{0x00, 0x00}, utf16BE("Pop")),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
data := buildID3v2(t, 4, rawFrame{"TCON", tc.payload})
got, err := readID3v2GenreValues(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if !equalStrings(got, []string{"Rock", "Pop"}) {
t.Errorf("genres = %q, want [Rock Pop]", got)
}
})
}
}
// ISO-8859-1 must be widened, not reinterpreted as UTF-8 — "Bj\xf6rk" would
// otherwise come back as invalid bytes.
func TestReadID3v2GenreValues_Latin1(t *testing.T) {
payload := append([]byte{0x00}, []byte("Chanson Fran\xe7aise")...)
data := buildID3v2(t, 3, rawFrame{"TCON", payload})
got, err := readID3v2GenreValues(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if !equalStrings(got, []string{"Chanson Française"}) {
t.Errorf("genres = %q, want [Chanson Française]", got)
}
}
// Frames before TCON must be walked over correctly. If the size field were
// decoded with the wrong scheme the walk lands mid-frame and TCON is missed.
func TestReadID3v2GenreValues_SkipsPrecedingFrames(t *testing.T) {
for _, major := range []byte{3, 4} {
data := buildID3v2(t, major,
rawFrame{"TIT2", utf8Frame("Some Title")},
rawFrame{"TPE1", utf8Frame("Some Artist")},
rawFrame{"TCON", utf8Frame("Shoegaze", "Dream Pop")},
)
got, err := readID3v2GenreValues(bytes.NewReader(data))
if err != nil {
t.Fatalf("v2.%d: %v", major, err)
}
if !equalStrings(got, []string{"Shoegaze", "Dream Pop"}) {
t.Errorf("v2.%d genres = %q, want [Shoegaze Dream Pop]", major, got)
}
}
}
func TestReadID3v2GenreValues_NoGenreFrame(t *testing.T) {
data := buildID3v2(t, 4, rawFrame{"TIT2", utf8Frame("Only A Title")})
if _, err := readID3v2GenreValues(bytes.NewReader(data)); err == nil {
t.Fatal("expected an error when no genre frame is present")
}
}
func TestReadID3v2GenreValues_NotAnID3File(t *testing.T) {
if _, err := readID3v2GenreValues(bytes.NewReader([]byte("not a tag at all"))); err == nil {
t.Fatal("expected an error for a file with no ID3v2 tag")
}
}
// Padding after the last frame is zero bytes; the walk must stop rather than
// read a frame id of "\x00\x00\x00\x00".
func TestReadID3v2GenreValues_StopsAtPadding(t *testing.T) {
// 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")})
// Splice 32 padding bytes in before the MPEG sync trailer, growing the
// declared tag size to match.
body := tagged[10 : len(tagged)-4]
padded := append(append([]byte{}, body...), make([]byte, 32)...)
var out bytes.Buffer
@@ -219,41 +78,90 @@ func TestReadID3v2GenreValues_StopsAtPadding(t *testing.T) {
out.Write([]byte{4, 0x00, 0x00})
out.Write(synchsafeBytes(len(padded)))
out.Write(padded)
got, err := readID3v2GenreValues(bytes.NewReader(out.Bytes()))
if err != nil {
t.Fatal(err)
}
if !equalStrings(got, []string{"Rock"}) {
t.Errorf("genres = %q, want [Rock]", got)
}
return out.Bytes()
}
// Unsynchronisation inserts 0xFF 0x00 pairs that must be collapsed before the
// frame list is walked, or every offset past the first pair is wrong.
func TestReadID3v2GenreValues_TagUnsynchronisation(t *testing.T) {
// Latin-1 so a genre can legitimately contain the byte 0xFF ("ÿ"). Once
// unsynchronised that becomes 0xFF 0x00 — which is indistinguishable from a
// value separator until the collapse runs, so this fails loudly if
// undoUnsynchronisation is skipped.
// 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("test is vacuous: nothing was unsynchronised")
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()
}
got, err := readID3v2GenreValues(bytes.NewReader(out.Bytes()))
if err != nil {
t.Fatal(err)
}
if !equalStrings(got, []string{"Roÿck", "Pop"}) {
t.Errorf("genres = %q, want [Roÿck Pop]", got)
// 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)
}
})
}
}
@@ -288,6 +196,19 @@ func TestNormaliseGenreValue(t *testing.T) {
// 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) {
@@ -299,88 +220,64 @@ func TestNormaliseGenreValue(t *testing.T) {
}
}
func TestNormaliseGenres_DedupesCaseInsensitively(t *testing.T) {
got := normaliseGenres([]string{"Rock", "rock", "ROCK", "Pop"})
// First spelling wins — we are not imposing a canonical case here, only
// removing values that repeat within a single file.
if !equalStrings(got, []string{"Rock", "Pop"}) {
t.Errorf("genres = %q, want [Rock Pop]", got)
// 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)
}
})
}
}
// "(40)AlternRock" declares the same genre twice — numerically and in text.
func TestNormaliseGenres_DedupesResolvedNumeric(t *testing.T) {
got := normaliseGenres([]string{"(40)AlternRock"})
if !equalStrings(got, []string{"AlternRock"}) {
t.Errorf("genres = %q, want [AlternRock]", got)
}
}
func TestNormaliseGenres_AllJunkYieldsNil(t *testing.T) {
if got := normaliseGenres([]string{"", " ", "9999"}); got != nil {
t.Errorf("genres = %q, want nil", got)
}
}
// End-to-end through dhowden/tag, which is what the scanner actually calls.
// Proves the welded value never reaches the caller.
func TestExtractGenres_EndToEnd(t *testing.T) {
data := buildID3v2(t, 4,
rawFrame{"TIT2", utf8Frame("A Song")},
rawFrame{"TCON", utf8Frame("Alternative Rock", "Rock")},
)
rs := bytes.NewReader(data)
meta, err := tag.ReadFrom(rs)
if err != nil {
t.Fatalf("tag.ReadFrom: %v", err)
}
// Confirm the upstream behaviour this reader works around is still present —
// if dhowden ever fixes it, this test tells us the workaround can go.
if welded := meta.Genre(); welded != "Alternative RockRock" {
t.Logf("note: dhowden/tag no longer welds multi-values (got %q)", welded)
}
genres, fellBack := extractGenres(meta, rs)
if fellBack {
t.Error("fellBack = true, want false — the TCON frame is parseable")
}
if !equalStrings(genres, []string{"Alternative Rock", "Rock"}) {
t.Errorf("genres = %q, want [Alternative Rock Rock]", genres)
}
if joined := strings.Join(genres, genreDelimiter); joined != "Alternative Rock;Rock" {
t.Errorf("stored value = %q, want %q", joined, "Alternative Rock;Rock")
}
}
// The digit-soup case, end to end: numeric references resolve to names.
func TestExtractGenres_ResolvesNumericReferences(t *testing.T) {
data := buildID3v2(t, 4, rawFrame{"TCON", utf8Frame("40", "17")})
rs := bytes.NewReader(data)
meta, err := tag.ReadFrom(rs)
if err != nil {
t.Fatalf("tag.ReadFrom: %v", err)
}
genres, _ := extractGenres(meta, rs)
if !equalStrings(genres, []string{"AlternRock", "Rock"}) {
t.Errorf("genres = %q, want [AlternRock Rock]", genres)
}
}
// A file with no genre at all must yield nothing and must NOT be reported as a
// fallback — that would log a warning for every untagged file in the library.
func TestExtractGenres_NoGenreIsNotAFallback(t *testing.T) {
data := buildID3v2(t, 4, rawFrame{"TIT2", utf8Frame("A Song")})
rs := bytes.NewReader(data)
meta, err := tag.ReadFrom(rs)
if err != nil {
t.Fatalf("tag.ReadFrom: %v", err)
}
genres, fellBack := extractGenres(meta, rs)
if len(genres) != 0 {
t.Errorf("genres = %q, want none", genres)
}
if fellBack {
t.Error("fellBack = true for an untagged file; would log on every such file")
// 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)
}
})
}
}
@@ -475,46 +372,3 @@ func TestTrueUpCasing(t *testing.T) {
})
}
}
// The casing repair must never touch a name resolved from the ID3v1 table.
// That table is kept canonical, so entry 40's 1990s spelling "AlternRock"
// stays as-is even though it reads like damage.
func TestNormaliseGenreValue_CanonicalTableNamesNotRecased(t *testing.T) {
if got := normaliseGenreValue("40"); !equalStrings(got, []string{"AlternRock"}) {
t.Errorf("bare 40 = %q, want [AlternRock]", got)
}
if got := normaliseGenreValue("(40)"); !equalStrings(got, []string{"AlternRock"}) {
t.Errorf("(40) = %q, want [AlternRock]", got)
}
}
// Casing runs on values that came from the file, including the parenthesised
// and refinement paths.
func TestNormaliseGenreValue_CasingAppliedToFileText(t *testing.T) {
tests := []struct {
in string
want []string
}{
{"Edm", []string{"EDM"}},
{"(17)Uk Garage", []string{"Rock", "UK Garage"}},
// Punctuation around the acronym must not hide it — the letter core is
// what gets matched, and the trimmed edges are re-attached.
{"(Live Edm)", []string{"(Live EDM)"}},
{"((Children'S Music", []string{"(Children's Music"}},
}
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
if got := normaliseGenreValue(tc.in); !equalStrings(got, tc.want) {
t.Errorf("normaliseGenreValue(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
// Two spellings of one acronym in the same file collapse to a single tag rather
// than surviving as near-duplicates.
func TestNormaliseGenres_AcronymVariantsDedupe(t *testing.T) {
if got := normaliseGenres([]string{"Edm", "EDM", "edm"}); !equalStrings(got, []string{"EDM"}) {
t.Errorf("genres = %q, want [EDM]", got)
}
}