sonda/internal/api/http/library_test.go
tgiachi 6d004c4225 feat(library): count unsupported audio formats and skip NAS system folders
- feat: the scan summary has an unsupported counter for audio files in formats
  Sonda cannot read yet (m4a, ogg, opus, wav, wma, ...); they are counted and
  never opened, instead of disappearing without a trace. Video containers such
  as .mpg are not counted
- feat: folders named .@__thumb, @Recycle, #recycle, @eaDir, $RECYCLE.BIN and
  System Volume Information are skipped, matched by exact name ignoring case,
  so a NAS recycle bin does not end up in the library; a library whose own
  folder carries one of these names is still scanned, and files indexed before
  from such a folder are flagged missing like any vanished file
- docs: the README lists the new counter and the skipped folders
- test: system folders at any depth, near-miss names, a library inside a
  system-named folder, entries flagged missing, unsupported counting

Closes #14
2026-11-11 14:25:33 +01:00

98 lines
2.4 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, 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)
}
}