sonda/internal/api/http/library_test.go
tgiachi 4c7e99bd6a feat(library): add concurrent read-only scanner with progress events
- feat(library): walk each root, plan the changes, read tags with N parallel
  workers and write from a single goroutine
- feat(library): detect renames by size and mtime only when the match is
  unambiguous; mark vanished files missing instead of deleting them
- fix-safe: an unreadable root, an unreadable subdirectory or an interrupted
  scan never marks files as missing; symlinks are skipped
- feat(library): event hub that fans scan progress out to subscribers without
  ever blocking the scan (ready for a websocket client)
- feat(library): TagLib (WASM, no CGO) reader for MP3 and FLAC tags and
  audio properties, with title and artist fallbacks
- feat(storage): library repository for SQLite and PostgreSQL
- feat(api): POST/GET /api/v1/library/scan to start a scan and read its status
- feat(config): library.scan_workers
2026-11-11 12:16:45 +01:00

80 lines
2 KiB
Go

package httpapi
import (
"net/http"
"testing"
"time"
"codeberg.org/squidd/sonda/internal/library"
)
type fakeScan struct {
running bool
starts int
status library.Status
}
func (f *fakeScan) Start() error {
if f.running {
return library.ErrScanRunning
}
f.running = true
f.starts++
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{Roots: 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)
}
}