discord-jukebox-bot/pkg/subsonic/client_test.go
2025-05-13 11:52:37 +02:00

145 lines
No EOL
4.2 KiB
Go

package subsonic
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestParseRandomSongsJSON(t *testing.T) {
// Open the test JSON file
file, err := os.Open("testdata/random_songs_response.json")
if err != nil {
t.Fatalf("Failed to open test file: %v", err)
}
defer file.Close()
// Read the file content
jsonData, err := io.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read test file: %v", err)
}
// Parse JSON into Response struct
var respWrapper struct {
SubsonicResponse Response `json:"subsonic-response"`
}
err = json.Unmarshal(jsonData, &respWrapper)
if err != nil {
t.Fatalf("Failed to parse JSON: %v", err)
}
resp := respWrapper.SubsonicResponse
// Verify the Response fields
if resp.Status != "ok" {
t.Errorf("Expected status 'ok', got '%s'", resp.Status)
}
if resp.Version != "1.16.1" {
t.Errorf("Expected version '1.16.1', got '%s'", resp.Version)
}
if resp.Type != "navidrome" {
t.Errorf("Expected type 'navidrome', got '%s'", resp.Type)
}
if resp.ServerVersion != "0.55.2 (a057a680)" {
t.Errorf("Expected serverVersion '0.55.2 (a057a680)', got '%s'", resp.ServerVersion)
}
if !resp.OpenSubsonic {
t.Error("Expected openSubsonic to be true")
}
// Verify RandomSong data
if resp.RandomSong == nil {
t.Fatal("RandomSong is nil, expected data")
}
// We should have 2 songs in our test data
if len(resp.RandomSong.Song) != 2 {
t.Fatalf("Expected 2 songs, got %d", len(resp.RandomSong.Song))
}
// Check the first song
song1 := resp.RandomSong.Song[0]
if song1.ID != "WxADUtZQmq1rvWMKRteTvh" {
t.Errorf("Expected song ID 'WxADUtZQmq1rvWMKRteTvh', got '%s'", song1.ID)
}
if song1.Title != "The First Book (Extended)" {
t.Errorf("Expected song title 'The First Book (Extended)', got '%s'", song1.Title)
}
if song1.Artist != "桜庭統" {
t.Errorf("Expected artist '桜庭統', got '%s'", song1.Artist)
}
if song1.Album != "Golden Sun" {
t.Errorf("Expected album 'Golden Sun', got '%s'", song1.Album)
}
if song1.Duration != 377 {
t.Errorf("Expected duration 377, got %d", song1.Duration)
}
if song1.Path != "桜庭統/Golden Sun/01-02 - The First Book (Extended).mp3" {
t.Errorf("Expected path '桜庭統/Golden Sun/01-02 - The First Book (Extended).mp3', got '%s'", song1.Path)
}
// Check nested structures
if len(song1.Genres) != 1 {
t.Errorf("Expected 1 genre, got %d", len(song1.Genres))
} else if song1.Genres[0].Name != "Game Soundtrack" {
t.Errorf("Expected genre 'Game Soundtrack', got '%s'", song1.Genres[0].Name)
}
// Check second song
song2 := resp.RandomSong.Song[1]
if song2.ID != "1LuCYVkmVmNfmJgc8orwCi" {
t.Errorf("Expected song ID '1LuCYVkmVmNfmJgc8orwCi', got '%s'", song2.ID)
}
if song2.Title != "Divine Beast Vah Ruta Battle" {
t.Errorf("Expected song title 'Divine Beast Vah Ruta Battle', got '%s'", song2.Title)
}
if song2.Artist != "Yasuaki Iwata" {
t.Errorf("Expected artist 'Yasuaki Iwata', got '%s'", song2.Artist)
}
}
func TestMakeRequest(t *testing.T) {
// Create a test server that returns our test JSON
testFile, err := os.ReadFile("testdata/random_songs_response.json")
if err != nil {
t.Fatalf("Failed to read test file: %v", err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(testFile)
}))
defer server.Close()
// Create a client using our test server URL
client := &Client{
baseURL: server.URL,
username: "testuser",
password: "testpass",
version: "1.16.1",
httpClient: &http.Client{},
}
// Test the GetRandomSongs method which uses makeRequest
songs, err := client.GetRandomSongs(10)
if err != nil {
t.Fatalf("GetRandomSongs failed: %v", err)
}
// Verify we got some songs back
if len(songs) != 2 {
t.Errorf("Expected 2 songs, got %d", len(songs))
}
// Check that the song data was parsed correctly
if songs[0].Title != "The First Book (Extended)" {
t.Errorf("Expected song title 'The First Book (Extended)', got '%s'", songs[0].Title)
}
if songs[1].Title != "Divine Beast Vah Ruta Battle" {
t.Errorf("Expected song title 'Divine Beast Vah Ruta Battle', got '%s'", songs[1].Title)
}
}