package httpapi import ( "net/http" "testing" "time" "git.kmalloc.org/squid/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") } }