- fix: on PostgreSQL a purge could delete a discovered artist while a scan was adopting it. The scan then failed on the missing artist for the whole file, and the two could also deadlock. Purge now takes the same artist lock as the scanner and discovery before it removes anything. Saving a list or tags does too, and an artist that is gone by then is reported as not found instead of a foreign key error, so a user who opened an artist that was removed meanwhile gets a 404 - fix: a discovered artist that was only the source of a list stayed forever and kept the artists of its own list alive, so chains built up. Cleanup now removes such an artist and then follows the chain until nothing more goes - fix: in a full rescan a file that came back but could not be read stayed marked missing. It is restored first, as in a normal scan, and read as well - fix: ?full given twice is refused, so a typo cannot start a scan of another kind - fix: genre cleaning lives in one place, used by the reader and the store - docs: the files genres count the tracks the artist is credited on, and the spec says every files tag is a genre - test: a purge colliding with a scan on both databases (it fails without the lock), a chain of discovered artists, saving for an artist that is gone, a file that returns unreadable during a full scan
135 lines
3.9 KiB
Go
135 lines
3.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"codeberg.org/squidd/sonda/internal/library"
|
|
)
|
|
|
|
type fakeScan struct {
|
|
running bool
|
|
noLibs bool
|
|
starts int
|
|
lastOnly int64 // library of the last StartLibrary call
|
|
lastFull bool // whether the last start asked for a full rescan
|
|
status library.Status
|
|
}
|
|
|
|
func (f *fakeScan) Start(full bool) error {
|
|
if f.noLibs {
|
|
return library.ErrNoLibraries
|
|
}
|
|
if f.running {
|
|
return library.ErrScanRunning
|
|
}
|
|
f.running = true
|
|
f.starts++
|
|
f.lastFull = full
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeScan) StartLibrary(id int64, full bool) error {
|
|
if id == 999 {
|
|
return library.ErrLibraryNotFound
|
|
}
|
|
if f.running {
|
|
return library.ErrScanRunning
|
|
}
|
|
f.running = true
|
|
f.starts++
|
|
f.lastOnly = id
|
|
f.lastFull = full
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeScan) Status() library.Status {
|
|
s := f.status
|
|
s.Running = f.running
|
|
return s
|
|
}
|
|
|
|
func TestScanEndpointsRequireAuth(t *testing.T) {
|
|
h := newTestAPIWithScan(t, &fakeScan{})
|
|
for _, method := range []string{"POST", "GET"} {
|
|
if r := do(t, h, method, "/api/v1/library/scan", "", nil); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("%s without token: %d", method, r.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScanStartAndConflict(t *testing.T) {
|
|
scan := &fakeScan{}
|
|
h := newTestAPIWithScan(t, scan)
|
|
tok := registerAndLogin(t, h)
|
|
|
|
r := do(t, h, "POST", "/api/v1/library/scan", tok, nil)
|
|
if r.Code != http.StatusAccepted || r.Body["status"] != "started" {
|
|
t.Fatalf("start: %d %v", r.Code, r.Body)
|
|
}
|
|
r = do(t, h, "POST", "/api/v1/library/scan", tok, nil)
|
|
if r.Code != http.StatusConflict || errCode(r) != "scan_running" {
|
|
t.Fatalf("second start: %d %v", r.Code, r.Body)
|
|
}
|
|
if scan.starts != 1 {
|
|
t.Errorf("starts = %d, want 1", scan.starts)
|
|
}
|
|
}
|
|
|
|
func TestScanStatus(t *testing.T) {
|
|
scan := &fakeScan{status: library.Status{
|
|
LastFinishedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC),
|
|
Last: &library.Summary{Libraries: 1, Found: 10, Added: 7, Failed: 1, Unsupported: 4},
|
|
LastError: "boom",
|
|
}}
|
|
h := newTestAPIWithScan(t, scan)
|
|
tok := registerAndLogin(t, h)
|
|
|
|
r := do(t, h, "GET", "/api/v1/library/scan", tok, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("status: %d %v", r.Code, r.Body)
|
|
}
|
|
if r.Body["running"] != false || r.Body["last_error"] != "boom" ||
|
|
r.Body["last_finished_at"] != "2026-01-02T03:04:05Z" {
|
|
t.Errorf("body = %v", r.Body)
|
|
}
|
|
last, _ := r.Body["last"].(map[string]any)
|
|
if last["found"] != float64(10) || last["added"] != float64(7) || last["failed"] != float64(1) || last["unsupported"] != float64(4) {
|
|
t.Errorf("last = %v", last)
|
|
}
|
|
}
|
|
|
|
// A full rescan is asked for explicitly; anything else in the parameter is a mistake.
|
|
func TestScanCanBeAskedToReadEveryFileAgain(t *testing.T) {
|
|
scan := &fakeScan{}
|
|
h := newTestAPIWithScan(t, scan)
|
|
tok := registerAndLogin(t, h)
|
|
|
|
for _, tt := range []struct {
|
|
query string
|
|
full bool
|
|
}{{"", false}, {"?full=false", false}, {"?full=true", true}} {
|
|
scan.running = false
|
|
if r := do(t, h, "POST", "/api/v1/library/scan"+tt.query, tok, nil); r.Code != http.StatusAccepted || scan.lastFull != tt.full {
|
|
t.Errorf("scan%s: %d full=%v, want %v", tt.query, r.Code, scan.lastFull, tt.full)
|
|
}
|
|
scan.running = false
|
|
if r := do(t, h, "POST", "/api/v1/libraries/7/scan"+tt.query, tok, nil); r.Code != http.StatusAccepted || scan.lastFull != tt.full || scan.lastOnly != 7 {
|
|
t.Errorf("library scan%s: %d full=%v only=%d", tt.query, r.Code, scan.lastFull, scan.lastOnly)
|
|
}
|
|
}
|
|
|
|
starts := scan.starts
|
|
scan.running = false
|
|
for _, q := range []string{"?full=maybe", "?full=1", "?full=TRUE", "?full=", "?full=true&full=bogus", "?full=false&full=true"} {
|
|
for _, path := range []string{"/api/v1/library/scan", "/api/v1/libraries/7/scan"} {
|
|
if r := do(t, h, "POST", path+q, tok, nil); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
|
t.Errorf("%s%s: %d %v, want 400 invalid_input", path, q, r.Code, r.Body)
|
|
}
|
|
}
|
|
}
|
|
if scan.starts != starts {
|
|
t.Error("a scan started with a bad parameter")
|
|
}
|
|
}
|