/* Copyright © 2023 Philipp Wolfer This file is part of Scotty. Scotty is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Scotty is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scotty. If not, see . */ package backends_test import ( "reflect" "testing" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "go.uploadedlobster.com/scotty/internal/backends" "go.uploadedlobster.com/scotty/internal/backends/dump" "go.uploadedlobster.com/scotty/internal/backends/funkwhale" "go.uploadedlobster.com/scotty/internal/backends/jspf" "go.uploadedlobster.com/scotty/internal/backends/listenbrainz" "go.uploadedlobster.com/scotty/internal/backends/maloja" "go.uploadedlobster.com/scotty/internal/backends/scrobblerlog" "go.uploadedlobster.com/scotty/internal/backends/subsonic" "go.uploadedlobster.com/scotty/internal/models" ) func TestResolveBackend(t *testing.T) { config := viper.New() config.Set("backend", "dump") backend, err := backends.ResolveBackend[models.ListensImport](config) assert.NoError(t, err) assert.IsType(t, &dump.DumpBackend{}, backend) } func TestResolveBackendUnknown(t *testing.T) { config := viper.New() config.Set("backend", "foo") _, err := backends.ResolveBackend[models.ListensImport](config) assert.EqualError(t, err, "Unknown backend foo") } func TestResolveBackendInvalidInterface(t *testing.T) { config := viper.New() config.Set("backend", "dump") _, err := backends.ResolveBackend[models.ListensExport](config) assert.EqualError(t, err, "Backend dump does not implement ListensExport") } func TestGetBackends(t *testing.T) { backends := backends.GetBackends() for _, info := range backends { if info.Name == "dump" { assert.Containsf(t, info.ImportCapabilities, "listens", "ImportCapabilities for \"dump\" expected to contain \"listens\"") assert.Emptyf(t, info.ExportCapabilities, "ExportCapabilities for \"dump\" expected to be empty") return // Finish the test } } // If we got here the "dump" backend was not included t.Errorf("GetBackends() did not return expected bacend \"dump\"") } func TestImplementsInterfaces(t *testing.T) { expectInterface[models.ListensImport](t, &dump.DumpBackend{}) expectInterface[models.LovesImport](t, &dump.DumpBackend{}) expectInterface[models.ListensExport](t, &funkwhale.FunkwhaleApiBackend{}) // expectInterface[models.ListensImport](t, &funkwhale.FunkwhaleApiBackend{}) expectInterface[models.LovesExport](t, &funkwhale.FunkwhaleApiBackend{}) // expectInterface[models.LovesImport](t, &funkwhale.FunkwhaleApiBackend{}) // expectInterface[models.ListensExport](t, &jspf.JSPFBackend{}) // expectInterface[models.ListensImport](t, &jspf.JSPFBackend{}) // expectInterface[models.LovesExport](t, &jspf.JSPFBackend{}) expectInterface[models.LovesImport](t, &jspf.JSPFBackend{}) expectInterface[models.ListensExport](t, &listenbrainz.ListenBrainzApiBackend{}) // expectInterface[models.ListensImport](t, &listenbrainz.ListenBrainzApiBackend{}) expectInterface[models.LovesExport](t, &listenbrainz.ListenBrainzApiBackend{}) expectInterface[models.LovesImport](t, &listenbrainz.ListenBrainzApiBackend{}) expectInterface[models.ListensExport](t, &maloja.MalojaApiBackend{}) expectInterface[models.ListensImport](t, &maloja.MalojaApiBackend{}) expectInterface[models.ListensExport](t, &scrobblerlog.ScrobblerLogBackend{}) expectInterface[models.ListensImport](t, &scrobblerlog.ScrobblerLogBackend{}) expectInterface[models.LovesExport](t, &subsonic.SubsonicApiBackend{}) // expectInterface[models.LovesImport](t, &subsonic.SubsonicApiBackend{}) } func expectInterface[T interface{}](t *testing.T, backend models.Backend) { ok, name := backends.ImplementsInterface[T](&backend) if !ok { t.Errorf("%v expected to implement %v", reflect.TypeOf(backend).Name(), name) } }