package api import ( "encoding/json" "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "git.fabledsword.com/bvandeusen/minstrel/internal/library" ) func TestGetFingerprintSettings_ServesDefaultsWithoutAService(t *testing.T) { h := &handlers{logger: slog.New(slog.NewTextHandler(io.Discard, nil))} rec := httptest.NewRecorder() h.handleGetFingerprintSettings(rec, httptest.NewRequest(http.MethodGet, "/api/admin/library/fingerprint-settings", nil)) if rec.Code != http.StatusOK { t.Fatalf("status = %d, want 200", rec.Code) } var got fingerprintSettingsBody if err := json.NewDecoder(rec.Body).Decode(&got); err != nil { t.Fatalf("decode: %v", err) } if want := fingerprintSettingsBodyOf(library.DefaultFingerprintSettings); got != want { t.Fatalf("body = %+v, want the defaults %+v", got, want) } } func TestUpdateFingerprintSettings_Rejects(t *testing.T) { h := &handlers{logger: slog.New(slog.NewTextHandler(io.Discard, nil))} for name, tc := range map[string]struct { body string code string mentions string }{ "a value out of range, naming the field": { body: `{"enabled":true,"chromaprint_length_sec":5,"acoustic_max_bit_error_rate":0.15,"backfill_concurrency":2,"sweep_interval_hours":1}`, code: "invalid_setting", mentions: "chromaprint_length_sec", }, // A partial body would otherwise zero every field it left out. "a body missing fields": {body: `{"enabled":false}`, code: "invalid_setting"}, "malformed JSON": {body: `{"enabled":`, code: "invalid_body"}, } { rec := httptest.NewRecorder() h.handleUpdateFingerprintSettings(rec, httptest.NewRequest( http.MethodPut, "/api/admin/library/fingerprint-settings", strings.NewReader(tc.body))) body := rec.Body.String() if rec.Code != http.StatusBadRequest || !strings.Contains(body, `"`+tc.code+`"`) || !strings.Contains(body, tc.mentions) { t.Errorf("%s: status %d body %s; want 400 %s mentioning %q", name, rec.Code, body, tc.code, tc.mentions) } } }