sonda/internal/api/http/library_test.go
tgiachi 46463846b3 feat(library): named media libraries managed through the API
- feat(storage): libraries table (name, path); audio_files points at a library
  by id instead of repeating the root path; migration 0005 for both drivers
  turns every existing root into a library and keeps file ids, tracks and the
  missing flag
- feat(library): Libraries service validates names and paths: absolute, an
  existing directory, never the filesystem root, never inside or around
  another library (checked on real paths, so symlinks cannot hide an overlap)
- feat(api): admin-only GET/POST /libraries, PATCH/DELETE /libraries/{id} and
  POST /libraries/{id}/scan; deleting a library drops its index entries and
  keeps tracks and listening history
- feat(library): the scanner and the scan manager work on libraries; scanning
  with no libraries answers 409 no_libraries
- feat(streaming): the library directory comes from the database instead of
  a configured list of roots
- breaking(config): library.roots is removed; an old config fails with a hint
  to use POST /api/v1/libraries
- test: CRUD and duplicates on both databases, path change keeps files,
  upgrade of a database written before libraries existed
2026-11-11 13:30:25 +01:00

98 lines
2.3 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
status library.Status
}
func (f *fakeScan) Start() error {
if f.noLibs {
return library.ErrNoLibraries
}
if f.running {
return library.ErrScanRunning
}
f.running = true
f.starts++
return nil
}
func (f *fakeScan) StartLibrary(id int64) error {
if id == 999 {
return library.ErrLibraryNotFound
}
if f.running {
return library.ErrScanRunning
}
f.running = true
f.starts++
f.lastOnly = id
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},
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) {
t.Errorf("last = %v", last)
}
}