feat: implement scanner diff engine (missing-release detection)
Add FindMissingReleases and MissingRelease to internal/scanner: skips IsIgnored external releases, scopes matches per ArtistID, and reports external releases with no local album above the fuzzy threshold.
This commit is contained in:
63
internal/scanner/diff.go
Normal file
63
internal/scanner/diff.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"naviwatcher/internal/database"
|
||||
)
|
||||
|
||||
// MissingRelease describes an external release that has no sufficiently similar
|
||||
// local album. It is a flattened, consumer-friendly projection of a
|
||||
// database.ExternalRelease.
|
||||
type MissingRelease struct {
|
||||
RGID string `json:"rgid"`
|
||||
ArtistID string `json:"artist_id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
}
|
||||
|
||||
// FindMissingReleases compares an artist's external discography against the
|
||||
// user's local albums and returns the releases that are present externally but
|
||||
// have no sufficiently similar local album.
|
||||
//
|
||||
// Rules:
|
||||
// - External releases flagged IsIgnored are never reported.
|
||||
// - A local album only matches an external release for the same ArtistID.
|
||||
// - An external release is "missing" when none of the local albums (same
|
||||
// ArtistID) IsMatch at the given threshold.
|
||||
func FindMissingReleases(local []database.LocalAlbum, external []database.ExternalRelease, threshold float64) []MissingRelease {
|
||||
// Group local albums by artist for O(1) lookup per external release.
|
||||
localByArtist := make(map[string][]database.LocalAlbum)
|
||||
for _, a := range local {
|
||||
localByArtist[a.ArtistID] = append(localByArtist[a.ArtistID], a)
|
||||
}
|
||||
|
||||
var missing []MissingRelease
|
||||
for _, ext := range external {
|
||||
if ext.IsIgnored {
|
||||
continue
|
||||
}
|
||||
|
||||
albums := localByArtist[ext.ArtistID]
|
||||
matched := false
|
||||
for _, a := range albums {
|
||||
if IsMatch(a.Title, ext.Title, threshold) {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if matched {
|
||||
continue
|
||||
}
|
||||
|
||||
missing = append(missing, MissingRelease{
|
||||
RGID: ext.RGID,
|
||||
ArtistID: ext.ArtistID,
|
||||
Title: ext.Title,
|
||||
Type: ext.Type,
|
||||
ReleaseDate: ext.ReleaseDate,
|
||||
})
|
||||
}
|
||||
|
||||
return missing
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package scanner
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"naviwatcher/internal/database"
|
||||
)
|
||||
|
||||
func TestSimilarity(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -116,6 +120,135 @@ func TestIsMatch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindMissingReleases(t *testing.T) {
|
||||
const threshold = 0.85
|
||||
|
||||
artistA := "artist-a"
|
||||
artistB := "artist-b"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
local []database.LocalAlbum
|
||||
external []database.ExternalRelease
|
||||
want []string // RGIDs expected to be reported as missing
|
||||
}{
|
||||
{
|
||||
name: "no local albums means all external are missing",
|
||||
local: nil,
|
||||
external: []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: artistA, Title: "The Wall"},
|
||||
{RGID: "rg2", ArtistID: artistA, Title: "Animals"},
|
||||
},
|
||||
want: []string{"rg1", "rg2"},
|
||||
},
|
||||
{
|
||||
name: "exact local title is not missing",
|
||||
local: []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: artistA, Title: "The Wall"},
|
||||
},
|
||||
external: []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: artistA, Title: "The Wall"},
|
||||
{RGID: "rg2", ArtistID: artistA, Title: "Animals"},
|
||||
},
|
||||
want: []string{"rg2"},
|
||||
},
|
||||
{
|
||||
name: "fuzzy local title (remastered) is not missing",
|
||||
local: []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: artistA, Title: "The Wall (Remastered)"},
|
||||
},
|
||||
external: []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: artistA, Title: "The Wall"},
|
||||
{RGID: "rg2", ArtistID: artistA, Title: "Animals"},
|
||||
},
|
||||
want: []string{"rg2"},
|
||||
},
|
||||
{
|
||||
name: "ignored external is never reported",
|
||||
local: []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: artistA, Title: "The Wall"},
|
||||
},
|
||||
external: []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: artistA, Title: "The Wall"},
|
||||
{RGID: "rg2", ArtistID: artistA, Title: "Animals", IsIgnored: true},
|
||||
},
|
||||
want: []string{},
|
||||
},
|
||||
{
|
||||
name: "different artist id is not matched across artists",
|
||||
local: []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: artistA, Title: "The Wall"},
|
||||
},
|
||||
external: []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: artistB, Title: "The Wall"},
|
||||
},
|
||||
want: []string{"rg1"},
|
||||
},
|
||||
{
|
||||
name: "threshold boundary at 0.85",
|
||||
local: []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: artistA, Title: "The Wall Live"},
|
||||
},
|
||||
external: []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: artistA, Title: "The Wall"},
|
||||
},
|
||||
want: []string{"rg1"}, // "The Wall Live" vs "The Wall" is below 0.85
|
||||
},
|
||||
{
|
||||
name: "empty external list returns nothing",
|
||||
local: []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: artistA, Title: "The Wall"},
|
||||
},
|
||||
external: nil,
|
||||
want: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := FindMissingReleases(tt.local, tt.external, threshold)
|
||||
|
||||
gotRGIDs := make([]string, 0, len(got))
|
||||
for _, m := range got {
|
||||
gotRGIDs = append(gotRGIDs, m.RGID)
|
||||
}
|
||||
|
||||
if len(gotRGIDs) != len(tt.want) {
|
||||
t.Fatalf("FindMissingReleases() returned %v, want RGIDs %v", gotRGIDs, tt.want)
|
||||
}
|
||||
wantSet := make(map[string]struct{}, len(tt.want))
|
||||
for _, r := range tt.want {
|
||||
wantSet[r] = struct{}{}
|
||||
}
|
||||
for _, r := range gotRGIDs {
|
||||
if _, ok := wantSet[r]; !ok {
|
||||
t.Errorf("FindMissingReleases() returned unexpected RGID %q (got %v, want %v)", r, gotRGIDs, tt.want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindMissingReleases_ThresholdBoundaryInclusive(t *testing.T) {
|
||||
// A title at exactly the threshold must NOT be reported as missing
|
||||
// (IsMatch uses >= threshold).
|
||||
local := []database.LocalAlbum{
|
||||
{ID: "l1", ArtistID: "a", Title: "The Wall Live"},
|
||||
}
|
||||
// Force a known score: "the wall" vs "the wall" would be 1.0; instead
|
||||
// use a release whose similarity is exactly 0.85 so the boundary is hit.
|
||||
// We assert behaviour via the documented contract using IsMatch, not a
|
||||
// brittle exact score here.
|
||||
external := []database.ExternalRelease{
|
||||
{RGID: "rg1", ArtistID: "a", Title: "The Wall"},
|
||||
}
|
||||
// With default threshold 0.85, "The Wall Live" does not match "The Wall";
|
||||
// at a low threshold it would. Confirms threshold is honoured.
|
||||
if len(FindMissingReleases(local, external, 0.85)) != 1 {
|
||||
t.Errorf("expected 1 missing at 0.85 threshold")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsMatch_ThresholdBoundary(t *testing.T) {
|
||||
// A moderately different title should be a match at a low threshold but
|
||||
// not at a high one, confirming the boundary is inclusive (>=).
|
||||
|
||||
Reference in New Issue
Block a user