package subsonic import "testing" func TestSearch3(t *testing.T) { s, srv := newTestRecorderServer(t) createTestUser(t, s, "alice", "password123", false) seedLibrary(t, s) q := baseParams("alice", "password123") q.Set("query", "Second") q.Set("f", "json") resp := getPath(t, srv.URL, "/rest/search3.view", q) j := decodeJSON(t, resp) result := j["searchResult3"].(map[string]any) songs, ok := result["song"].([]any) if !ok || len(songs) != 1 { t.Fatalf("search3 songs = %v, want 1 match for 'Second'", result["song"]) } } func TestSearch2(t *testing.T) { s, srv := newTestRecorderServer(t) createTestUser(t, s, "alice", "password123", false) seedLibrary(t, s) q := baseParams("alice", "password123") q.Set("query", "Test Artist") q.Set("f", "json") resp := getPath(t, srv.URL, "/rest/search2.view", q) j := decodeJSON(t, resp) result := j["searchResult2"].(map[string]any) artists, ok := result["artist"].([]any) if !ok || len(artists) != 1 { t.Fatalf("search2 artists = %v, want 1 match", result["artist"]) } } func TestSearchDeprecatedFlat(t *testing.T) { s, srv := newTestRecorderServer(t) createTestUser(t, s, "alice", "password123", false) seedLibrary(t, s) q := baseParams("alice", "password123") q.Set("any", "Test Album") q.Set("f", "json") resp := getPath(t, srv.URL, "/rest/search.view", q) j := decodeJSON(t, resp) result := j["searchResult"].(map[string]any) if result["totalHits"] == nil { t.Fatalf("search: missing totalHits: %v", result) } matches, ok := result["match"].([]any) if !ok || len(matches) == 0 { t.Fatalf("search: matches = %v, want at least 1", result["match"]) } } func TestSearch3MissingQuery(t *testing.T) { s, srv := newTestRecorderServer(t) createTestUser(t, s, "alice", "password123", false) resp := getPath(t, srv.URL, "/rest/search3.view", baseParams("alice", "password123")) x := decodeXML(t, resp) if x.Status != "failed" || x.Error == nil || x.Error.Code != ErrMissingParameter { t.Fatalf("expected error 10, got %+v", x) } } func TestSearch3NoResults(t *testing.T) { s, srv := newTestRecorderServer(t) createTestUser(t, s, "alice", "password123", false) seedLibrary(t, s) q := baseParams("alice", "password123") q.Set("query", "ThisWillNeverMatchAnything") q.Set("f", "json") resp := getPath(t, srv.URL, "/rest/search3.view", q) j := decodeJSON(t, resp) if j["status"] != "ok" { t.Fatalf("status = %v, want ok even with no results", j["status"]) } }