quintodrome/server/e2e/subsonic_sonic_similarity_test.go
Deluan Quintão 259c1a9484
feat(subsonic): add sonicSimilarity extension as plugin capability (#5419)
* feat(plugins): add sonicSimilarity capability types

Defines the SonicSimilarity plugin capability interface with
GetSonicSimilarTracks and FindSonicPath methods, along with
their request/response types.

* feat(sonic): add core sonic similarity service

Implements the Sonic service with HasProvider, GetSonicSimilarTracks,
and FindSonicPath, delegating to the PluginLoader and using the
Matcher for index-preserving library resolution.

* test(sonic): add sonic service unit tests

Covers HasProvider, GetSonicSimilarTracks, and FindSonicPath with
mock plugin loader and provider, verifying error propagation and
successful match resolution via the library matcher.

* feat(matcher): add MatchSongsToLibraryMap for index-preserving matching

Adds a new method alongside MatchSongsToLibrary that returns a
map[int]MediaFile keyed by input song index rather than a flat slice,
enabling callers to correlate similarity scores back to the original
position in the results.

* fix(sonic): check provider availability before MediaFile lookup

Avoids unnecessary DB call when no plugin is available, and ensures
the correct error path is tested.

* feat(plugins): add sonic similarity adapter

Adds SonicSimilarityAdapter implementing sonic.Provider, bridging the
plugin system to the core sonic service via Extism plugin functions.
Reuses existing songRefsToAgentSongs helper for SongRef conversion.

* feat(plugins): add LoadSonicSimilarity to plugin manager

Adds Manager.LoadSonicSimilarity method following the pattern of
LoadLyricsProvider, enabling the core sonic service to load a
SonicSimilarityAdapter from a named plugin.

* feat(subsonic): add sonicMatch response type

Add SonicMatch struct with Entry and Similarity fields, and a SonicMatches slice to the Subsonic response struct. These types support the OpenSubsonic sonicSimilarity extension for returning similarity-scored track results.

* feat(subsonic): add getSonicSimilarTracks and findSonicPath handlers

Add two new Subsonic API handlers for the sonicSimilarity OpenSubsonic extension: GetSonicSimilarTracks returns similarity-scored tracks similar to a given song, and FindSonicPath returns a path of tracks connecting two songs. Both handlers delegate to the sonic core service and map results to SonicMatch response types.

* feat(subsonic): advertise sonicSimilarity extension when plugin available

Update GetOpenSubsonicExtensions to conditionally include the sonicSimilarity extension only when a sonic similarity plugin provider is available. The nil guard ensures backward compatibility with tests that pass nil for the sonic field. Also update the existing test to pass the new nil parameter.

* feat(subsonic): wire sonic similarity service into router

Add the sonic.Sonic service to the Router struct and New() constructor, register the getSonicSimilarTracks and findSonicPath routes, and wire sonic.New and its PluginLoader binding into the Wire dependency injection graph. Update all existing test call sites to pass the new nil parameter. Regenerate wire_gen.go.

* fix(e2e): add sonic parameter to subsonic.New call in e2e tests

* test(subsonic): add sonicSimilarity extension advertisement tests

Restructures the GetOpenSubsonicExtensions test into two contexts: one verifying the baseline 5 extensions are returned when no sonic similarity plugin is configured, and one verifying that the sonicSimilarity extension is advertised (making 6 total) when a plugin loader reports an available provider. Adds a mockSonicPluginLoader to satisfy the sonic.PluginLoader interface without requiring a real plugin.

* feat(subsonic): add nil guard and e2e tests for sonic similarity endpoints

Handlers return ErrorDataNotFound when no sonic service is configured,
preventing nil panics. E2e tests verify both endpoints return proper
error responses when no plugin is available.

* fix(subsonic): return HTTP 404 when no sonic similarity plugin available

Endpoints are always registered but return 404 when no provider is
available, rather than a subsonic error code 70.

* refactor: clean up sonic similarity code after review

Extract shared helpers to reduce duplication across the sonic similarity
implementation: loadAllMatches in matcher consolidates the 4-phase
matching pipeline, songRefToAgentSong eliminates per-iteration slice
allocation in the adapter, sonicMatchResponse deduplicates response
building in handlers, and a package-level constant replaces raw
capability name strings in core/sonic.

* fix empty response shapes

Signed-off-by: Deluan <deluan@navidrome.org>

* test(plugins): add testdata plugin and e2e tests for sonic similarity

Add a test-sonic-similarity WASM plugin that implements both
GetSonicSimilarTracks and FindSonicPath via the generated sonicsimilarity
PDK. The plugin returns deterministic test data with decreasing similarity
scores and supports error injection via config. Adapter tests verify the
full round-trip through the WASM plugin including error handling. Also
includes regenerated PDK code from make gen.

* docs: update README to include new capabilities and usage examples for plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* test(e2e): enhance sonic similarity tests with additional scenarios and mock provider

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: address PR review feedback for sonic similarity

Fix incorrect field names in README documentation ({from, to} →
{startSong, endSong}) and remove unnecessary XML serialization test
from e2e suite since OpenSubsonic endpoints only use JSON.

* refactor: rename Matcher methods for conciseness

Rename MatchSongsToLibrary to MatchSongs and MatchSongsToLibraryMap to
MatchSongsIndexed. The Matcher receiver already establishes the "to
library" context, making that suffix redundant, and "Indexed" better
describes the intent (preserving input ordering) than "Map" which
describes the data structure.

* refactor: standardize variable naming for media files in sonic path methods

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: simplify plugin loading by introducing adapter constructors

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2026-04-27 17:50:09 -04:00

272 lines
8.4 KiB
Go

package e2e
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/agents"
"github.com/navidrome/navidrome/core/lyrics"
"github.com/navidrome/navidrome/core/matcher"
"github.com/navidrome/navidrome/core/metrics"
"github.com/navidrome/navidrome/core/playback"
"github.com/navidrome/navidrome/core/playlists"
"github.com/navidrome/navidrome/core/sonic"
"github.com/navidrome/navidrome/core/stream"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/server/subsonic"
"github.com/navidrome/navidrome/server/subsonic/responses"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// buildSonicRouter creates a subsonic.Router with a real sonic.Sonic service
// backed by the given provider and the shared e2e DataStore.
func buildSonicRouter(provider sonic.Provider) *subsonic.Router {
loader := &mockSonicPluginLoader{provider: provider}
m := matcher.New(ds)
sonicSvc := sonic.New(ds, loader, m)
decider := stream.NewTranscodeDecider(ds, noopFFmpeg{})
return subsonic.New(
ds,
noopArtwork{},
&spyStreamer{},
noopArchiver{},
core.NewPlayers(ds),
noopProvider{},
nil, // scanner
events.NoopBroker(),
playlists.NewPlaylists(ds, core.NewImageUploadService()),
noopPlayTracker{},
core.NewShare(ds),
playback.PlaybackServer(nil),
metrics.NewNoopInstance(),
lyrics.NewLyrics(nil),
decider,
sonicSvc,
)
}
// doSonicReq makes a request through a sonic-enabled router and returns the parsed response.
func doSonicReq(sonicRouter *subsonic.Router, endpoint string, params ...string) *responses.Subsonic {
w := httptest.NewRecorder()
r := buildReq(adminUser, endpoint, params...)
sonicRouter.ServeHTTP(w, r)
return parseJSONResponse(w)
}
// doSonicRawReq makes a request through a sonic-enabled router and returns the raw recorder.
func doSonicRawReq(sonicRouter *subsonic.Router, endpoint string, params ...string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
r := buildReq(adminUser, endpoint, params...)
sonicRouter.ServeHTTP(w, r)
return w
}
var _ = Describe("Sonic Similarity Endpoints", func() {
BeforeEach(func() {
setupTestDB()
})
Context("without sonic similarity plugin", func() {
Describe("getSonicSimilarTracks", func() {
It("returns 404 when no sonic similarity plugin is available", func() {
w := doRawReq("getSonicSimilarTracks", "id", "any-song-id")
Expect(w.Code).To(Equal(http.StatusNotFound))
})
})
Describe("findSonicPath", func() {
It("returns 404 when no sonic similarity plugin is available", func() {
w := doRawReq("findSonicPath", "startSongId", "any-song-id", "endSongId", "another-song-id")
Expect(w.Code).To(Equal(http.StatusNotFound))
})
})
})
Context("with sonic similarity plugin", func() {
var (
sonicRouter *subsonic.Router
comeTogether model.MediaFile
something model.MediaFile
)
BeforeEach(func() {
songs, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{
Filters: squirrel.Eq{"title": "Come Together"},
})
Expect(err).ToNot(HaveOccurred())
Expect(songs).ToNot(BeEmpty())
comeTogether = songs[0]
songs, err = ds.MediaFile(ctx).GetAll(model.QueryOptions{
Filters: squirrel.Eq{"title": "Something"},
})
Expect(err).ToNot(HaveOccurred())
Expect(songs).ToNot(BeEmpty())
something = songs[0]
provider := &mockSonicProvider{
similarIDs: []string{something.ID, comeTogether.ID},
pathIDs: []string{comeTogether.ID, something.ID},
}
sonicRouter = buildSonicRouter(provider)
})
Describe("getSonicSimilarTracks", func() {
It("returns similar tracks with similarity scores", func() {
resp := doSonicReq(sonicRouter, "getSonicSimilarTracks", "id", comeTogether.ID)
Expect(resp.Status).To(Equal(responses.StatusOK))
matches := *resp.SonicMatches
Expect(matches).To(HaveLen(2))
Expect(matches[0].Entry.Title).To(Equal("Something"))
Expect(matches[0].Similarity).To(Equal(1.0))
Expect(matches[1].Entry.Title).To(Equal("Come Together"))
Expect(matches[1].Similarity).To(Equal(0.9))
})
It("respects the count parameter", func() {
resp := doSonicReq(sonicRouter, "getSonicSimilarTracks", "id", comeTogether.ID, "count", "1")
Expect(resp.Status).To(Equal(responses.StatusOK))
Expect(*resp.SonicMatches).To(HaveLen(1))
})
It("returns an error for a missing id parameter", func() {
resp := doSonicReq(sonicRouter, "getSonicSimilarTracks")
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
It("returns an error for a non-existent song ID", func() {
resp := doSonicReq(sonicRouter, "getSonicSimilarTracks", "id", "non-existent-id")
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
It("returns correct JSON structure", func() {
w := doSonicRawReq(sonicRouter, "getSonicSimilarTracks", "id", comeTogether.ID)
Expect(w.Code).To(Equal(http.StatusOK))
var wrapper responses.JsonWrapper
Expect(json.Unmarshal(w.Body.Bytes(), &wrapper)).To(Succeed())
matches := *wrapper.Subsonic.SonicMatches
Expect(matches).To(HaveLen(2))
Expect(matches[0].Similarity).To(BeNumerically(">", 0))
Expect(matches[0].Entry.Id).ToNot(BeEmpty())
})
})
Describe("findSonicPath", func() {
It("returns a path between two tracks with similarity scores", func() {
resp := doSonicReq(sonicRouter, "findSonicPath",
"startSongId", comeTogether.ID,
"endSongId", something.ID,
)
Expect(resp.Status).To(Equal(responses.StatusOK))
matches := *resp.SonicMatches
Expect(matches).To(HaveLen(2))
Expect(matches[0].Entry.Title).To(Equal("Come Together"))
Expect(matches[0].Similarity).To(Equal(1.0))
Expect(matches[1].Entry.Title).To(Equal("Something"))
Expect(matches[1].Similarity).To(Equal(0.95))
})
It("returns an error for a missing startSongId parameter", func() {
resp := doSonicReq(sonicRouter, "findSonicPath", "endSongId", something.ID)
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
It("returns an error for a missing endSongId parameter", func() {
resp := doSonicReq(sonicRouter, "findSonicPath", "startSongId", comeTogether.ID)
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
It("returns an error for a non-existent start song ID", func() {
resp := doSonicReq(sonicRouter, "findSonicPath",
"startSongId", "non-existent-id",
"endSongId", something.ID,
)
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
It("returns an error for a non-existent end song ID", func() {
resp := doSonicReq(sonicRouter, "findSonicPath",
"startSongId", comeTogether.ID,
"endSongId", "non-existent-id",
)
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
})
})
})
// mockSonicProvider returns results using IDs from the real test library,
// so that the matcher can resolve them back to actual MediaFiles.
type mockSonicProvider struct {
similarIDs []string
pathIDs []string
}
func (m *mockSonicProvider) GetSonicSimilarTracks(_ context.Context, mf *model.MediaFile, count int) ([]sonic.SimilarResult, error) {
var results []sonic.SimilarResult
for i, id := range m.similarIDs {
if i >= count {
break
}
results = append(results, sonic.SimilarResult{
Song: agents.Song{ID: id},
Similarity: 1.0 - float64(i)*0.1,
})
}
return results, nil
}
func (m *mockSonicProvider) FindSonicPath(_ context.Context, startMf, endMf *model.MediaFile, count int) ([]sonic.SimilarResult, error) {
var results []sonic.SimilarResult
for i, id := range m.pathIDs {
if i >= count {
break
}
results = append(results, sonic.SimilarResult{
Song: agents.Song{ID: id},
Similarity: 1.0 - float64(i)*0.05,
})
}
return results, nil
}
type mockSonicPluginLoader struct {
provider sonic.Provider
}
func (m *mockSonicPluginLoader) PluginNames(capability string) []string {
if capability == "SonicSimilarity" && m.provider != nil {
return []string{"mock-sonic"}
}
return nil
}
func (m *mockSonicPluginLoader) LoadSonicSimilarity(_ string) (sonic.Provider, bool) {
if m.provider != nil {
return m.provider, true
}
return nil, false
}