sonda/internal/storage/storage_test.go
tgiachi aafb2dd906 feat(discovery): similar artists from Last.fm
- feat: GET /api/v1/artists/{id}/similar lists the artists Last.fm finds similar
  to one in the library, best first, each with the provider's own score and
  rank and a flag saying whether it is in the library. It answers 200 when the
  provider is off or failing, with provider_status, stale and retrieved_at
- feat: a thin Last.fm client on top of the outbound client. It always sends
  autocorrect=0 so a misspelled local name cannot turn into another artist,
  prefers the MusicBrainz id when known, always sets a limit, and reads the
  error field even when the status is 200. Errors never carry the URL or the
  provider's own message, because the key travels in the query string
- feat: results are stored in artist_similar with a freshness ledger in
  provider_fetches: 7 days for an answer, 1 day for "unknown artist". Old data
  is served when the provider fails, and concurrent requests for the same
  artist share one call that does not depend on the first caller staying
- feat: artists named by a provider but without files become artists without
  tracks. GET /artists/{id} can show them (track_count 0), the lists of the
  library never include them, and a later scan adopts them by name. A provider
  id is never written onto an existing artist; names match only when at least
  one side has no id, and different ids always mean different artists
- feat: POST /providers/{name}/test (admin) checks a stored key with one cheap
  request, whether the provider is on or off, and reports ok, no_api_key,
  key_refused, rate_limited, unreachable or error
- test: the whole request is checked from the receiving end, with a user, a
  listen, a title and a library path in the database: only the artist and the
  key leave, with a User-Agent that names the project
- docs: README and OpenAPI updated; the contract test calls the new operations
2026-11-11 16:42:52 +01:00

281 lines
7.8 KiB
Go

package storage
import (
"context"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"testing/fstest"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/migrations"
)
// postgresDSNEnv points the tests at a throwaway PostgreSQL server, e.g.
// postgres://postgres:sonda@127.0.0.1:55432/sonda?sslmode=disable
// Without it only the SQLite tests run, so the default suite needs no network.
const postgresDSNEnv = "SONDA_TEST_POSTGRES_DSN"
var schemaCounter atomic.Int64
// openBackend opens a fresh, empty database for driver. Postgres tests get
// their own schema, dropped when the test ends.
func openBackend(t *testing.T, driver string) *bun.DB {
t.Helper()
switch driver {
case DriverSQLite:
db, err := Open(DriverSQLite, filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Close() })
return db
case DriverPostgres:
admin, err := Open(DriverPostgres, os.Getenv(postgresDSNEnv))
if err != nil {
t.Fatal(err)
}
schema := fmt.Sprintf("t_%d_%d", os.Getpid(), schemaCounter.Add(1))
if _, err := admin.Exec("CREATE SCHEMA " + schema); err != nil {
t.Fatal(err)
}
u, err := url.Parse(os.Getenv(postgresDSNEnv))
if err != nil {
t.Fatal(err)
}
q := u.Query()
q.Set("search_path", schema)
u.RawQuery = q.Encode()
db, err := Open(DriverPostgres, u.String())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = db.Close()
_, _ = admin.Exec("DROP SCHEMA " + schema + " CASCADE")
_ = admin.Close()
})
return db
}
t.Fatalf("unknown driver %q", driver)
return nil
}
// forEachBackend runs fn once per available backend with a migrated database.
func forEachBackend(t *testing.T, fn func(t *testing.T, db *bun.DB, driver string)) {
t.Helper()
drivers := []string{DriverSQLite}
if os.Getenv(postgresDSNEnv) != "" {
drivers = append(drivers, DriverPostgres)
}
for _, driver := range drivers {
t.Run(driver, func(t *testing.T) {
db := openBackend(t, driver)
fsys, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatal(err)
}
fn(t, db, driver)
})
}
}
func tableExists(t *testing.T, db *bun.DB, driver, table string) bool {
t.Helper()
var n int
var err error
if driver == DriverPostgres {
err = db.QueryRow(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = ?",
table).Scan(&n)
} else {
err = db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?", table).Scan(&n)
}
if err != nil {
t.Fatal(err)
}
return n == 1
}
func TestOpenRejectsUnknownDriver(t *testing.T) {
if _, err := Open("mysql", "x"); err == nil {
t.Fatal("expected error for unknown driver")
}
}
func TestSQLiteOpenSetsPragmas(t *testing.T) {
db := openBackend(t, DriverSQLite)
var fk int
if err := db.QueryRow("PRAGMA foreign_keys").Scan(&fk); err != nil {
t.Fatal(err)
}
if fk != 1 {
t.Errorf("foreign_keys = %d, want 1", fk)
}
var mode string
if err := db.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil {
t.Fatal(err)
}
if mode != "wal" {
t.Errorf("journal_mode = %q, want wal", mode)
}
var busy int
if err := db.QueryRow("PRAGMA busy_timeout").Scan(&busy); err != nil {
t.Fatal(err)
}
if busy <= 0 {
t.Errorf("busy_timeout = %d, want > 0", busy)
}
}
func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, driver string) {
fsys, _ := migrations.For(driver)
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatalf("second Migrate: %v", err)
}
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches"} {
if !tableExists(t, db, driver, table) {
t.Errorf("table %s missing", table)
}
}
var applied int
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
t.Fatal(err)
}
if applied != 8 {
t.Errorf("applied migrations = %d, want 8", applied)
}
})
}
func TestSchemaEnforcesForeignKeys(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
if _, err := db.Exec("INSERT INTO tracks (title, album_id) VALUES ('x', 999)"); err == nil {
t.Fatal("expected foreign key violation")
}
})
}
func TestSchemaAllowsUnknownMBIDsButRejectsDuplicates(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
// Several artists without an MBID must be allowed.
for _, name := range []string{"A", "B"} {
if _, err := db.Exec("INSERT INTO artists (name) VALUES (?)", name); err != nil {
t.Fatalf("insert %s: %v", name, err)
}
}
// The same MBID twice must not.
if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('C', 'm1')"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('D', 'm1')"); err == nil {
t.Fatal("expected unique violation on mbid")
}
})
}
func TestAudioFilePathIsUniquePerLibrary(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('t')"); err != nil {
t.Fatal(err)
}
var a, b int64
for name, dst := range map[string]*int64{"a": &a, "b": &b} {
err := db.QueryRow("INSERT INTO libraries (name, path, created_at) VALUES (?, ?, 1) RETURNING id",
name, "/music/"+name).Scan(dst)
if err != nil {
t.Fatal(err)
}
}
insert := func(lib int64, rel string) error {
_, err := db.Exec(
`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix)
VALUES (1, ?, ?, 'mp3', 1, 1)`, lib, rel)
return err
}
if err := insert(a, "x/y.mp3"); err != nil {
t.Fatal(err)
}
// Same relative path in another library is a different file.
if err := insert(b, "x/y.mp3"); err != nil {
t.Fatalf("same rel_path in another library should be allowed: %v", err)
}
// Same library and same relative path is a duplicate.
if err := insert(a, "x/y.mp3"); err == nil {
t.Fatal("expected unique violation on (library_id, rel_path)")
}
// A file must belong to a real library.
if err := insert(999, "z.mp3"); err == nil {
t.Fatal("expected foreign key violation for an unknown library")
}
})
}
func TestMigrateRollsBackFailedMigration(t *testing.T) {
for _, driver := range availableDrivers() {
t.Run(driver, func(t *testing.T) {
db := openBackend(t, driver)
fsys := fstest.MapFS{
"0001_ok.sql": {Data: []byte("CREATE TABLE a (id INTEGER);")},
"0002_bad.sql": {Data: []byte("CREATE TABLE b (id INTEGER); THIS IS NOT SQL;")},
}
if err := Migrate(context.Background(), db, fsys); err == nil {
t.Fatal("expected error from bad migration")
}
if tableExists(t, db, driver, "b") {
t.Error("table b exists: failed migration was not rolled back")
}
var n int
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&n); err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("applied = %d, want 1 (only 0001)", n)
}
})
}
}
func availableDrivers() []string {
drivers := []string{DriverSQLite}
if os.Getenv(postgresDSNEnv) != "" {
drivers = append(drivers, DriverPostgres)
}
return drivers
}
func TestMigrationFoldersMatch(t *testing.T) {
names := func(driver string) string {
fsys, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
entries, err := fs.ReadDir(fsys, ".")
if err != nil {
t.Fatal(err)
}
var out []string
for _, e := range entries {
out = append(out, e.Name())
}
return strings.Join(out, ",")
}
if a, b := names(DriverSQLite), names(DriverPostgres); a != b {
t.Errorf("migration files differ: sqlite=%s postgres=%s", a, b)
}
}