sonda/internal/storage/images_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

491 lines
18 KiB
Go

package storage
import (
"context"
"errors"
"testing"
"time"
"github.com/uptrace/bun"
"git.kmalloc.org/squid/sonda/internal/images"
)
func TestImageRepoSaveAndGet(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewImageRepo(db)
if _, ok, err := repo.Get(ctx, "album", 5); ok || err != nil {
t.Fatalf("empty: ok=%v err=%v", ok, err)
}
at := time.Unix(1_700_000_000, 0)
rec := images.Record{Status: images.StatusOK, Source: images.SourceFolder, ETag: "abc", Width: 800, Height: 600, CheckedAt: at}
if saved, err := repo.Save(ctx, "album", 5, rec); err != nil || !saved {
t.Fatalf("Save: saved=%v err=%v", saved, err)
}
got, ok, err := repo.Get(ctx, "album", 5)
if err != nil || !ok || got.Status != rec.Status || got.Source != rec.Source || got.ETag != "abc" || got.Width != 800 || got.Height != 600 || !got.CheckedAt.Equal(at) {
t.Errorf("got %+v ok=%v err=%v, want %+v", got, ok, err, rec)
}
// Saved again, it replaces.
none := images.Record{Status: images.StatusNone, CheckedAt: at.Add(time.Hour)}
if saved, err := repo.Save(ctx, "album", 5, none); err != nil || !saved {
t.Fatalf("Save again: saved=%v err=%v", saved, err)
}
if got, _, _ = repo.Get(ctx, "album", 5); got.Status != images.StatusNone || got.Source != "" || got.ETag != "" {
t.Errorf("after replacing: %+v", got)
}
// Another owner and another kind are separate.
if _, ok, _ := repo.Get(ctx, "album", 6); ok {
t.Error("another album has a record")
}
if _, ok, _ := repo.Get(ctx, "artist", 5); ok {
t.Error("an artist has the record of an album")
}
})
}
func TestImageRepoKeepsTheCreditOfAPicture(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewImageRepo(db)
at := time.Unix(1_700_000_000, 0)
credit := images.Credit{Author: "Raph é Ö", License: "CC BY 4.0", LicenseURL: "https://creativecommons.org/licenses/by/4.0", PageURL: "https://commons.wikimedia.org/wiki/File:Band.jpg"}
rec := images.Record{Status: images.StatusOK, Source: images.SourceCommons, Credit: credit, ETag: "e", Width: 5940, Height: 2160, CheckedAt: at}
if saved, err := repo.Save(ctx, "artist", 3, rec); err != nil || !saved {
t.Fatalf("Save: %v %v", saved, err)
}
got, ok, err := repo.Get(ctx, "artist", 3)
if err != nil || !ok || got.Credit != credit || got.Source != images.SourceCommons {
t.Errorf("got %+v ok=%v err=%v, want the credit kept", got, ok, err)
}
// A new answer without a picture forgets the old credit.
if saved, err := repo.Save(ctx, "artist", 3, images.Record{Status: images.StatusNone, CheckedAt: at}); err != nil || !saved {
t.Fatal(saved, err)
}
if got, _, _ = repo.Get(ctx, "artist", 3); got.Credit != (images.Credit{}) {
t.Errorf("credit = %+v after 'none'", got.Credit)
}
})
}
func TestPurgingTheArtistPicturesForgetsThemAndLeavesTheCovers(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewImageRepo(db)
at := time.Unix(1_700_000_000, 0)
for _, k := range []struct {
kind string
id int64
}{{"artist", 1}, {"artist", 2}, {"album", 1}} {
if _, err := repo.Save(ctx, k.kind, k.id, images.Record{Status: images.StatusNone, CheckedAt: at}); err != nil {
t.Fatal(err)
}
}
if out, err := NewDiscoveryRepo(db).Purge(ctx, "lastfm"); err != nil || out.Images != 0 {
t.Fatalf("purge of another provider: %+v %v", out, err)
}
if n := count(t, db, "SELECT COUNT(*) FROM images WHERE kind = 'artist'"); n != 2 {
t.Fatalf("a purge of Last.fm removed artist pictures: %d left", n)
}
out, err := NewDiscoveryRepo(db).Purge(ctx, "commons")
if err != nil || out.Images != 2 {
t.Fatalf("purge = %+v err = %v, want 2 pictures", out, err)
}
if n := count(t, db, "SELECT COUNT(*) FROM images WHERE kind = 'artist'"); n != 0 {
t.Errorf("%d artist pictures left", n)
}
if n := count(t, db, "SELECT COUNT(*) FROM images WHERE kind = 'album'"); n != 1 {
t.Errorf("a purge of Commons touched the covers: %d left", n)
}
})
}
func TestAChangeIsNotUndoneByABuildThatStartedBeforeIt(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewImageRepo(db)
at := time.Unix(1_700_000_000, 0)
clear := func(id int64) {
t.Helper()
err := db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { return clearAlbumCovers(ctx, tx, id) })
if err != nil {
t.Fatal(err)
}
}
// A change with no record yet leaves a stale one, with a generation the build never saw.
clear(7)
got, ok, _ := repo.Get(ctx, "album", 7)
if !ok || got.Status != images.StatusStale || got.Gen != 1 {
t.Fatalf("after a change: %+v ok=%v, want stale at generation 1", got, ok)
}
// A build that read generation 0 before the change is not saved.
if saved, err := repo.Save(ctx, "album", 7, images.Record{Status: images.StatusNone, Gen: 0, CheckedAt: at}); err != nil || saved {
t.Errorf("an out of date build: saved=%v err=%v, want it dropped", saved, err)
}
if got, _, _ = repo.Get(ctx, "album", 7); got.Status != images.StatusStale {
t.Errorf("record = %+v, want it still stale", got)
}
// One that read the current generation is, and the generation stays.
if saved, err := repo.Save(ctx, "album", 7, images.Record{Status: images.StatusOK, Source: images.SourceFolder, ETag: "e", Gen: 1, CheckedAt: at}); err != nil || !saved {
t.Errorf("a current build: saved=%v err=%v", saved, err)
}
if got, _, _ = repo.Get(ctx, "album", 7); got.Status != images.StatusOK || got.Gen != 1 || got.ETag != "e" {
t.Errorf("record = %+v", got)
}
// Another change moves it again and forgets the old picture's details.
clear(7)
if got, _, _ = repo.Get(ctx, "album", 7); got.Status != images.StatusStale || got.Gen != 2 || got.ETag != "" || got.Source != "" {
t.Errorf("record = %+v, want stale at generation 2 with no picture details", got)
}
// A build that finds nothing with no record at all is saved (generation 0).
if saved, err := repo.Save(ctx, "album", 8, images.Record{Status: images.StatusNone, CheckedAt: at}); err != nil || !saved {
t.Errorf("first save: saved=%v err=%v", saved, err)
}
})
}
func TestImageRepoAlbumFilesPutFilesWithoutNumbersFirstOnBothDatabases(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
lib := NewLibraryRepo(db)
l, _ := lib.CreateLibrary(ctx, "m", "/lib/m")
for _, f := range []struct {
rel string
track int
}{{"b.flac", 2}, {"x.flac", 0}, {"a.flac", 1}, {"y.flac", 0}} {
if _, err := lib.SaveFile(ctx, l.ID, pf(f.rel, f.rel, "Mixed", "Band", f.track, 0, "flac", "Band"), 0); err != nil {
t.Fatal(err)
}
}
var album int64
if err := db.QueryRow("SELECT id FROM albums WHERE title = 'Mixed'").Scan(&album); err != nil {
t.Fatal(err)
}
files, err := NewImageRepo(db).AlbumFiles(ctx, album)
if err != nil {
t.Fatal(err)
}
var got []string
for _, f := range files {
got = append(got, f.RelPath)
}
want := []string{"x.flac", "y.flac", "a.flac", "b.flac"} // no number counts as 0, then by id
if len(got) != 4 || got[0] != want[0] || got[1] != want[1] || got[2] != want[2] || got[3] != want[3] {
t.Errorf("order = %v, want %v", got, want)
}
})
}
func TestImageRepoAlbumFiles(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
seedCatalog(t, db)
ctx := context.Background()
repo := NewImageRepo(db)
var kind, ghost, hidden int64
if err := db.QueryRow("SELECT id FROM albums WHERE title = 'Kind of Blue'").Scan(&kind); err != nil {
t.Fatal(err)
}
if err := db.QueryRow("SELECT id FROM albums WHERE title = 'OK Computer'").Scan(&ghost); err != nil {
t.Fatal(err)
}
if err := db.QueryRow("SELECT id FROM albums WHERE title = 'Hidden Album'").Scan(&hidden); err != nil {
t.Fatal(err)
}
files, err := repo.AlbumFiles(ctx, kind)
if err != nil || len(files) != 3 {
t.Fatalf("files = %+v err=%v", files, err)
}
// In play order: track 1, 2, 3 (they were saved as 3, 1, 2).
want := []string{"m/1.mp3", "m/2.mp3", "m/3.mp3"}
for i, f := range files {
if f.RelPath != want[i] || f.Root != "/lib/jazz" {
t.Errorf("file %d = %+v, want %s in /lib/jazz", i, f, want[i])
}
}
// The file that is gone (Ghost) is not offered.
files, err = repo.AlbumFiles(ctx, ghost)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
if f.RelPath == "r/4.flac" {
t.Error("a missing file was offered")
}
}
// An album whose files are all gone, and one that does not exist.
if _, err := repo.AlbumFiles(ctx, hidden); !errors.Is(err, images.ErrAlbumNotFound) {
t.Errorf("album with nothing to play: err = %v", err)
}
if _, err := repo.AlbumFiles(ctx, 99999); !errors.Is(err, images.ErrAlbumNotFound) {
t.Errorf("unknown album: err = %v", err)
}
})
}
func TestSaveFileClearsTheCoversThatMayHaveChanged(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
lib := NewLibraryRepo(db)
l, err := lib.CreateLibrary(ctx, "m", "/lib/m")
if err != nil {
t.Fatal(err)
}
images := NewImageRepo(db)
save := func(rel, album string, existing int64) {
t.Helper()
if _, err := lib.SaveFile(ctx, l.ID, pf(rel, rel, album, "Band", 1, 1, "flac", "Band"), existing); err != nil {
t.Fatal(err)
}
}
albumID := func(title string) int64 {
var id int64
if err := db.QueryRow("SELECT id FROM albums WHERE title = ?", title).Scan(&id); err != nil {
t.Fatal(err)
}
return id
}
fileID := func(rel string) int64 {
var id int64
if err := db.QueryRow("SELECT id FROM audio_files WHERE rel_path = ?", rel).Scan(&id); err != nil {
t.Fatal(err)
}
return id
}
mark := func(ids ...int64) { // a built cover at the current generation
for _, id := range ids {
cur, _, err := images.Get(ctx, "album", id)
if err != nil {
t.Fatal(err)
}
rec := imagesRec()
rec.Gen = cur.Gen
if _, err := images.Save(ctx, "album", id, rec); err != nil {
t.Fatal(err)
}
}
}
has := func(id int64) bool { // a cover that is still believed
rec, ok, err := images.Get(ctx, "album", id)
if err != nil {
t.Fatal(err)
}
return ok && rec.Status != "stale"
}
save("a/1.flac", "First", 0)
save("b/1.flac", "Second", 0)
first, second := albumID("First"), albumID("Second")
// A new file in the first album clears only that album.
mark(first, second)
save("a/2.flac", "First", 0)
if has(first) || !has(second) {
t.Errorf("after a new file: first=%v second=%v, want the first cleared only", has(first), has(second))
}
// A changed file clears its album.
mark(first)
save("a/1.flac", "First", fileID("a/1.flac"))
if has(first) || !has(second) {
t.Errorf("after a changed file: first=%v second=%v", has(first), has(second))
}
// A file retagged into another album clears both: the old one lost a track.
mark(first, second)
save("a/1.flac", "Second", fileID("a/1.flac"))
if has(first) || has(second) {
t.Errorf("after a move: first=%v second=%v, want both cleared", has(first), has(second))
}
})
}
func imagesRec() images.Record {
return images.Record{Status: images.StatusOK, Source: images.SourceFolder, ETag: "x", CheckedAt: time.Unix(1_700_000_000, 0)}
}
func TestImageRepoAlbumMBID(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
lib := NewLibraryRepo(db)
l, _ := lib.CreateLibrary(ctx, "m", "/lib/m")
const rg = "b1392450-e666-3926-a536-22c65f834433"
if _, err := lib.SaveFile(ctx, l.ID, withAlbumID(pf("a/1.flac", "T", "With", "Band", 1, 1, "flac", "Band"), rg), 0); err != nil {
t.Fatal(err)
}
if _, err := lib.SaveFile(ctx, l.ID, pf("b/1.flac", "T", "Without", "Band", 1, 1, "flac", "Band"), 0); err != nil {
t.Fatal(err)
}
repo := NewImageRepo(db)
album := func(title string) int64 {
var id int64
if err := db.QueryRow("SELECT id FROM albums WHERE title = ?", title).Scan(&id); err != nil {
t.Fatal(err)
}
return id
}
if got, err := repo.AlbumMBID(ctx, album("With")); err != nil || got != rg {
t.Errorf("With = %q err = %v", got, err)
}
if got, err := repo.AlbumMBID(ctx, album("Without")); err != nil || got != "" {
t.Errorf("Without = %q err = %v", got, err)
}
if _, err := repo.AlbumMBID(ctx, 99999); !errors.Is(err, images.ErrAlbumNotFound) {
t.Errorf("unknown album: err = %v", err)
}
})
}
func TestImageRepoAlbumNames(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
lib := NewLibraryRepo(db)
l, _ := lib.CreateLibrary(ctx, "m", "/lib/m")
if _, err := lib.SaveFile(ctx, l.ID, pf("a/1.flac", "T", "OK Computer", "Radiohead", 1, 1, "flac", "Radiohead"), 0); err != nil {
t.Fatal(err)
}
if _, err := db.ExecContext(ctx, "INSERT INTO albums (title, title_key, search_key) VALUES ('Orphan', 'orphan', 'orphan')"); err != nil {
t.Fatal(err)
}
id := func(title string) int64 {
var id int64
if err := db.QueryRow("SELECT id FROM albums WHERE title = ?", title).Scan(&id); err != nil {
t.Fatal(err)
}
return id
}
repo := NewImageRepo(db)
if artist, title, err := repo.AlbumNames(ctx, id("OK Computer")); err != nil || artist != "Radiohead" || title != "OK Computer" {
t.Errorf("names = %q / %q err = %v", artist, title, err)
}
if artist, title, err := repo.AlbumNames(ctx, id("Orphan")); err != nil || artist != "" || title != "Orphan" {
t.Errorf("an album with no artist: %q / %q err = %v", artist, title, err)
}
if _, _, err := repo.AlbumNames(ctx, 99999); !errors.Is(err, images.ErrAlbumNotFound) {
t.Errorf("unknown album: err = %v", err)
}
})
}
func TestPurgingTheArchiveForgetsOnlyTheCoversThatCameFromIt(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewImageRepo(db)
at := time.Unix(1_700_000_000, 0)
for _, r := range []struct {
kind, source string
id int64
}{
{"album", images.SourceCoverArt, 1}, {"album", images.SourceCoverArt, 2},
{"album", images.SourceFolder, 3}, {"album", images.SourceEmbedded, 4}, {"artist", images.SourceCommons, 1},
} {
if _, err := repo.Save(ctx, r.kind, r.id, images.Record{Status: images.StatusOK, Source: r.source, ETag: "e", CheckedAt: at}); err != nil {
t.Fatal(err)
}
}
if _, err := repo.Save(ctx, "album", 5, images.Record{Status: images.StatusNone, CheckedAt: at}); err != nil {
t.Fatal(err)
}
out, err := NewDiscoveryRepo(db).Purge(ctx, "coverartarchive")
if err != nil || out.Images != 2 {
t.Fatalf("purge = %+v err = %v, want 2", out, err)
}
if n := count(t, db, "SELECT COUNT(*) FROM images WHERE source = 'coverartarchive'"); n != 0 {
t.Errorf("%d archive covers left", n)
}
// The covers from the user's own files, the "no cover" records and the artists' pictures stay.
if n := count(t, db, "SELECT COUNT(*) FROM images"); n != 4 {
t.Errorf("%d records left, want 4", n)
}
})
}
func TestImageRepoListsTheAlbumsThatNeedACover(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
lib := NewLibraryRepo(db)
l, _ := lib.CreateLibrary(ctx, "m", "/lib/m")
repo := NewImageRepo(db)
now := time.Unix(1_800_000_000, 0)
fresh, oldOK, oldNone := now.Add(-time.Hour), now.Add(-images.CoverTTL-time.Hour), now.Add(-images.NoCoverTTL-time.Hour)
var ids = map[string]int64{}
for _, title := range []string{"A none yet", "B ok fresh", "C ok old", "D none fresh", "E none old", "F stale", "G missing files"} {
if _, err := lib.SaveFile(ctx, l.ID, pf("x/"+title+".flac", title, title, "Band", 1, 1, "flac", "Band"), 0); err != nil {
t.Fatal(err)
}
var id int64
if err := db.QueryRow("SELECT id FROM albums WHERE title = ?", title).Scan(&id); err != nil {
t.Fatal(err)
}
ids[title] = id
}
save := func(title, status string, at time.Time) {
t.Helper()
// Saving a file marked the album stale at generation 1: a build saves at that one.
saved, err := repo.Save(ctx, "album", ids[title], images.Record{Status: status, Source: images.SourceFolder, Gen: 1, CheckedAt: at})
if err != nil || !saved {
t.Fatalf("save %s: %v %v", title, saved, err)
}
}
save("B ok fresh", images.StatusOK, fresh)
save("C ok old", images.StatusOK, oldOK)
save("D none fresh", images.StatusNone, fresh)
save("E none old", images.StatusNone, oldNone)
// "A none yet" and "F stale" keep the stale record that saving their file left.
if _, err := db.Exec("UPDATE audio_files SET missing_at = ? WHERE rel_path = ?", now.Unix(), "x/G missing files.flac"); err != nil {
t.Fatal(err)
}
// An artist's picture with the same number must not count for an album.
if _, err := repo.Save(ctx, "artist", ids["A none yet"], images.Record{Status: images.StatusOK, CheckedAt: fresh}); err != nil {
t.Fatal(err)
}
want := []int64{ids["A none yet"], ids["C ok old"], ids["E none old"], ids["F stale"]}
got, err := repo.AlbumsToBuild(ctx, 0, 100, false, now)
if err != nil || !sameIDs(got, want) {
t.Errorf("to build = %v err = %v, want %v", got, err, want)
}
if n, err := repo.CountAlbumsToBuild(ctx, false, now); err != nil || n != len(want) {
t.Errorf("count = %d err = %v, want %d", n, err, len(want))
}
// With refresh every album that has something to play, but not the one whose files are gone.
all := []int64{ids["A none yet"], ids["B ok fresh"], ids["C ok old"], ids["D none fresh"], ids["E none old"], ids["F stale"]}
got, err = repo.AlbumsToBuild(ctx, 0, 100, true, now)
if err != nil || !sameIDs(got, all) {
t.Errorf("refresh = %v err = %v, want %v", got, err, all)
}
if n, _ := repo.CountAlbumsToBuild(ctx, true, now); n != len(all) {
t.Errorf("refresh count = %d, want %d", n, len(all))
}
// Pages: after an id, with a limit, in order.
page, _ := repo.AlbumsToBuild(ctx, 0, 2, false, now)
if len(page) != 2 || page[0] != want[0] || page[1] != want[1] {
t.Errorf("first page = %v", page)
}
next, _ := repo.AlbumsToBuild(ctx, page[1], 2, false, now)
if len(next) != 2 || next[0] != want[2] || next[1] != want[3] {
t.Errorf("second page = %v", next)
}
if end, _ := repo.AlbumsToBuild(ctx, want[3], 2, false, now); len(end) != 0 {
t.Errorf("past the end = %v", end)
}
})
}
func sameIDs(a, b []int64) bool {
if len(a) != len(b) {
return false
}
seen := map[int64]int{}
for _, x := range a {
seen[x]++
}
for _, x := range b {
seen[x]--
}
for _, n := range seen {
if n != 0 {
return false
}
}
return true
}