package subsonic

import (
	"encoding/json"
	"net/http/httptest"
	"testing"
)

// TestGetMusicFoldersShape pins the wire shape — a single folder with id=1,
// name "Music". Clients cache this response; breaking the shape invalidates
// their library state.
func TestGetMusicFoldersShape(t *testing.T) {
	b := &browseHandlers{}
	rec := httptest.NewRecorder()
	req := httptest.NewRequest("GET", "/rest/getMusicFolders.view", nil)
	b.getMusicFolders(rec, req)

	var payload map[string]map[string]any
	if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
		t.Fatalf("unmarshal: %v — body=%s", err, rec.Body)
	}
	inner := payload["subsonic-response"]
	if inner["status"] != "ok" {
		t.Fatalf("status = %v", inner["status"])
	}
	folders, ok := inner["musicFolders"].(map[string]any)
	if !ok {
		t.Fatalf("musicFolders missing: %+v", inner)
	}
	list, ok := folders["musicFolder"].([]any)
	if !ok || len(list) != 1 {
		t.Fatalf("musicFolder list = %+v", folders["musicFolder"])
	}
	first := list[0].(map[string]any)
	if first["id"].(float64) != 1 {
		t.Errorf("folder id = %v, want 1", first["id"])
	}
	if first["name"] != "Music" {
		t.Errorf("folder name = %v, want Music", first["name"])
	}
}
