sonda/internal/storage/storage_test.go
tgiachi bfa3e6e458 feat: the seek bar shows the waveform of the track
The seek bar of the player is the shape of the track, with the part already
heard in colour, as in the approved mockups.

- GET /tracks/{id}/peaks gives 120 levels from 0 to 255 (the loudest part is
  255 whatever the volume). The server finds them by decoding the file, MP3 with
  go-mp3 and FLAC with eaburns/flac, both in pure Go. It takes a few seconds, so
  the first request starts the work in the background and answers 202 with
  Retry-After. It is done once for each file (migration 0025), worked out again
  if the size or the date of the file change, one track at a time so that a NAS
  is not kept busy, and a file that cannot be decoded is remembered as such. A
  track deleted while its file is being decoded is not an error.
- The seek bar of the player bar and of the full-screen player draws the shape
  and asks again every few seconds until it is there. Until then, and for a
  friend's track, which has none, the bar is flat. The heard part is teal, or
  the friend colour. It is still a native range input, 44 pixels high in the
  bar.
- OpenAPI path and schema, contract test, tests with real MP3 and FLAC files,
  and a real server checked: a 4-minute MP3 is ready in about a second and a
  half, a 12 MB FLAC in three, and asking again takes a millisecond.
2026-11-13 17:35:00 +01:00

281 lines
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", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions", "federation_settings", "federation_invites", "federation_friends", "federation_requests", "track_peaks"} {
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 != 25 {
t.Errorf("applied migrations = %d, want 25", 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)
}
}