Implement /now_playing and /sources endpoints with real device integration

## New Endpoints

### GET /now_playing 
- Rich XML models with PlayStatus, ShuffleSetting, RepeatSetting enums
- Comprehensive playback information (track, artist, album, artwork, position)
- Device capabilities (skip, seek, favorite functionality)
- Smart display methods for different content types (music vs radio)
- Duration formatting with position/total time display

### GET /sources 
- Complete audio source management with SourceStatus enum
- Source categorization (Local/Remote, Streaming, Multiroom support)
- Multiple account support (multiple Spotify accounts per device)
- Availability filtering (Ready vs Unavailable sources)
- Helper methods for quick capability checks

## Real Device Integration

- Fetched actual XML responses from SoundTouch devices (192.168.178.28 & 192.168.178.35)
- Updated all test fixtures with real device data (anonymized)
- Enhanced XML models to handle all real-world fields and edge cases
- Verified compatibility across different device types and configurations

## Enhanced CLI Tool

- Added -nowplaying command with rich formatted output
- Added -sources command with categorized source listing
- Display enhancements: duration info, capabilities, source attributes
- Improved build process to use ./build/ directory consistently

## Comprehensive Testing

- 15+ unit tests for XML models with enum validation
- Client integration tests with mock HTTP responses
- Real device response validation
- Edge case handling (empty states, network errors, invalid data)

## Documentation & Guidelines

- Updated CLAUDE.md with build directory and real device testing guidelines
- Enhanced README with comprehensive usage examples
- Updated PLAN.md to reflect implementation progress
- All examples use real device data patterns

## Quality Improvements

- Type-safe XML unmarshaling with custom validation
- Consistent error handling across all endpoints
- Privacy protection (anonymized account information)
- Production-ready code structure and patterns

Features:
 GET /info - Device information
 GET /now_playing - Current playback status with full metadata
 GET /sources - Available audio sources with smart categorization
 UPnP device discovery
 Cross-platform CLI tool with rich output formatting
 Comprehensive test coverage with real device data
 Build automation with proper directory structure
This commit is contained in:
Tobias Gesellchen
2026-01-08 23:21:01 +01:00
parent 7de6b8246b
commit 5caad90d51
14 changed files with 2325 additions and 22 deletions
+20
View File
@@ -76,6 +76,26 @@ func (c *Client) GetDeviceInfo() (*models.DeviceInfo, error) {
return &deviceInfo, nil
}
// GetNowPlaying retrieves current playback information from the /now_playing endpoint
func (c *Client) GetNowPlaying() (*models.NowPlaying, error) {
var nowPlaying models.NowPlaying
err := c.get("/now_playing", &nowPlaying)
if err != nil {
return nil, fmt.Errorf("failed to get now playing: %w", err)
}
return &nowPlaying, nil
}
// GetSources retrieves available audio sources from the /sources endpoint
func (c *Client) GetSources() (*models.Sources, error) {
var sources models.Sources
err := c.get("/sources", &sources)
if err != nil {
return nil, fmt.Errorf("failed to get sources: %w", err)
}
return &sources, nil
}
// Ping checks if the device is reachable by calling /info
func (c *Client) Ping() error {
_, err := c.GetDeviceInfo()
+395
View File
@@ -3,8 +3,10 @@ package client
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
@@ -287,6 +289,399 @@ func TestClientTimeout(t *testing.T) {
}
}
func TestClient_GetNowPlaying(t *testing.T) {
tests := []struct {
name string
responseFile string
expectedError bool
expectedTrack string
expectedArtist string
expectedSource string
expectedStatus string
}{
{
name: "spotify track playing",
responseFile: "nowplaying_response.xml",
expectedError: false,
expectedTrack: "In Between Breaths - Paris Unplugged",
expectedArtist: "SYML",
expectedSource: "SPOTIFY",
expectedStatus: "Playing",
},
{
name: "radio station playing",
responseFile: "nowplaying_radio.xml",
expectedError: false,
expectedTrack: "",
expectedArtist: "",
expectedSource: "TUNEIN",
expectedStatus: "Playing",
},
{
name: "standby state",
responseFile: "nowplaying_empty.xml",
expectedError: false,
expectedTrack: "",
expectedArtist: "",
expectedSource: "STANDBY",
expectedStatus: "Unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Expected GET request, got %s", r.Method)
}
if r.URL.Path != "/now_playing" {
t.Errorf("Expected path /now_playing, got %s", r.URL.Path)
}
// Check headers
if userAgent := r.Header.Get("User-Agent"); userAgent == "" {
t.Error("Expected User-Agent header to be set")
}
if accept := r.Header.Get("Accept"); accept != "application/xml" {
t.Errorf("Expected Accept header 'application/xml', got '%s'", accept)
}
// Read test data
data, err := os.ReadFile(filepath.Join("testdata", tt.responseFile))
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(http.StatusOK)
w.Write(data)
}))
defer server.Close()
// Parse server URL to get host and port
serverURL, err := url.Parse(server.URL)
if err != nil {
t.Fatalf("Failed to parse server URL: %v", err)
}
host := serverURL.Hostname()
port, _ := strconv.Atoi(serverURL.Port())
client := NewClient(ClientConfig{
Host: host,
Port: port,
Timeout: 5 * time.Second,
UserAgent: "test-client",
})
nowPlaying, err := client.GetNowPlaying()
if tt.expectedError {
if err == nil {
t.Error("Expected error but got none")
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if nowPlaying == nil {
t.Fatal("Expected NowPlaying response but got nil")
}
// Verify basic fields
if nowPlaying.Source != tt.expectedSource {
t.Errorf("Expected Source '%s', got '%s'", tt.expectedSource, nowPlaying.Source)
}
if nowPlaying.Track != tt.expectedTrack {
t.Errorf("Expected Track '%s', got '%s'", tt.expectedTrack, nowPlaying.Track)
}
if nowPlaying.Artist != tt.expectedArtist {
t.Errorf("Expected Artist '%s', got '%s'", tt.expectedArtist, nowPlaying.Artist)
}
if nowPlaying.PlayStatus.String() != tt.expectedStatus {
t.Errorf("Expected PlayStatus '%s', got '%s'", tt.expectedStatus, nowPlaying.PlayStatus.String())
}
})
}
}
func TestClient_GetNowPlaying_ServerError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal Server Error"))
}))
defer server.Close()
serverURL, _ := url.Parse(server.URL)
host := serverURL.Hostname()
port, _ := strconv.Atoi(serverURL.Port())
client := NewClient(ClientConfig{
Host: host,
Port: port,
Timeout: 5 * time.Second,
})
_, err := client.GetNowPlaying()
if err == nil {
t.Error("Expected error for server error response")
}
expectedErrorMsg := "failed to get now playing"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", expectedErrorMsg, err.Error())
}
}
func TestClient_GetNowPlaying_NetworkError(t *testing.T) {
client := NewClient(ClientConfig{
Host: "non-existent-host.invalid",
Port: 8090,
Timeout: 1 * time.Second,
})
_, err := client.GetNowPlaying()
if err == nil {
t.Error("Expected error for network error")
}
expectedErrorMsg := "failed to get now playing"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", expectedErrorMsg, err.Error())
}
}
func TestClient_GetNowPlaying_InvalidXML(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(http.StatusOK)
w.Write([]byte("<invalid-xml>"))
}))
defer server.Close()
serverURL, _ := url.Parse(server.URL)
host := serverURL.Hostname()
port, _ := strconv.Atoi(serverURL.Port())
client := NewClient(ClientConfig{
Host: host,
Port: port,
Timeout: 5 * time.Second,
})
_, err := client.GetNowPlaying()
if err == nil {
t.Error("Expected error for invalid XML response")
}
expectedErrorMsg := "failed to get now playing"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", expectedErrorMsg, err.Error())
}
}
func TestClient_GetSources(t *testing.T) {
tests := []struct {
name string
responseFile string
expectedError bool
expectedCount int
expectedReady int
hasSpotify bool
hasAux bool
hasBluetooth bool
}{
{
name: "sources with mixed availability",
responseFile: "sources_response.xml",
expectedError: false,
expectedCount: 14,
expectedReady: 5,
hasSpotify: true,
hasAux: true,
hasBluetooth: false, // Bluetooth is unavailable in test data
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Expected GET request, got %s", r.Method)
}
if r.URL.Path != "/sources" {
t.Errorf("Expected path /sources, got %s", r.URL.Path)
}
// Check headers
if userAgent := r.Header.Get("User-Agent"); userAgent == "" {
t.Error("Expected User-Agent header to be set")
}
if accept := r.Header.Get("Accept"); accept != "application/xml" {
t.Errorf("Expected Accept header 'application/xml', got '%s'", accept)
}
// Read test data
data, err := os.ReadFile(filepath.Join("testdata", tt.responseFile))
if err != nil {
t.Fatalf("Failed to read test data: %v", err)
}
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(http.StatusOK)
w.Write(data)
}))
defer server.Close()
// Parse server URL to get host and port
serverURL, err := url.Parse(server.URL)
if err != nil {
t.Fatalf("Failed to parse server URL: %v", err)
}
host := serverURL.Hostname()
port, _ := strconv.Atoi(serverURL.Port())
client := NewClient(ClientConfig{
Host: host,
Port: port,
Timeout: 5 * time.Second,
UserAgent: "test-client",
})
sources, err := client.GetSources()
if tt.expectedError {
if err == nil {
t.Error("Expected error but got none")
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if sources == nil {
t.Fatal("Expected Sources response but got nil")
}
// Verify counts
if sources.GetSourceCount() != tt.expectedCount {
t.Errorf("Expected source count %d, got %d", tt.expectedCount, sources.GetSourceCount())
}
if sources.GetReadySourceCount() != tt.expectedReady {
t.Errorf("Expected ready source count %d, got %d", tt.expectedReady, sources.GetReadySourceCount())
}
// Verify specific sources
if sources.HasSpotify() != tt.hasSpotify {
t.Errorf("Expected HasSpotify() %v, got %v", tt.hasSpotify, sources.HasSpotify())
}
if sources.HasAux() != tt.hasAux {
t.Errorf("Expected HasAux() %v, got %v", tt.hasAux, sources.HasAux())
}
if sources.HasBluetooth() != tt.hasBluetooth {
t.Errorf("Expected HasBluetooth() %v, got %v", tt.hasBluetooth, sources.HasBluetooth())
}
})
}
}
func TestClient_GetSources_ServerError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal Server Error"))
}))
defer server.Close()
serverURL, _ := url.Parse(server.URL)
host := serverURL.Hostname()
port, _ := strconv.Atoi(serverURL.Port())
client := NewClient(ClientConfig{
Host: host,
Port: port,
Timeout: 5 * time.Second,
})
_, err := client.GetSources()
if err == nil {
t.Error("Expected error for server error response")
}
expectedErrorMsg := "failed to get sources"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", expectedErrorMsg, err.Error())
}
}
func TestClient_GetSources_NetworkError(t *testing.T) {
client := NewClient(ClientConfig{
Host: "non-existent-host.invalid",
Port: 8090,
Timeout: 1 * time.Second,
})
_, err := client.GetSources()
if err == nil {
t.Error("Expected error for network error")
}
expectedErrorMsg := "failed to get sources"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", expectedErrorMsg, err.Error())
}
}
func TestClient_GetSources_InvalidXML(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(http.StatusOK)
w.Write([]byte("<invalid-xml>"))
}))
defer server.Close()
serverURL, _ := url.Parse(server.URL)
host := serverURL.Hostname()
port, _ := strconv.Atoi(serverURL.Port())
client := NewClient(ClientConfig{
Host: host,
Port: port,
Timeout: 5 * time.Second,
})
_, err := client.GetSources()
if err == nil {
t.Error("Expected error for invalid XML response")
}
expectedErrorMsg := "failed to get sources"
if !strings.Contains(err.Error(), expectedErrorMsg) {
t.Errorf("Expected error message to contain '%s', got '%s'", expectedErrorMsg, err.Error())
}
}
// Helper functions
func loadTestData(t *testing.T, filename string) string {
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<nowPlaying deviceID="1234567890AB" source="STANDBY">
<ContentItem source="STANDBY" isPresetable="false" />
</nowPlaying>
+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<nowPlaying deviceID="A81B6A536A98" source="TUNEIN">
<ContentItem source="TUNEIN" type="stationurl" location="http://stream.example.com/radio" sourceAccount="" isPresetable="true">
<itemName>Classic Rock 101.5</itemName>
<containerArt>https://cdn-radiotime-logos.tunein.com/s123456q.png</containerArt>
</ContentItem>
<stationName>Classic Rock 101.5</stationName>
<description>The Best Classic Rock Hits</description>
<stationLocation>New York, NY</stationLocation>
<art artImageStatus="IMAGE_PRESENT">https://cdn-radiotime-logos.tunein.com/s123456q.png</art>
<playStatus>PLAY_STATE</playStatus>
<shuffleSetting>SHUFFLE_OFF</shuffleSetting>
<repeatSetting>REPEAT_OFF</repeatSetting>
<streamType>RADIO_STREAMING</streamType>
</nowPlaying>
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<nowPlaying deviceID="A81B6A536A98" source="SPOTIFY" sourceAccount="user@example.com">
<ContentItem source="SPOTIFY" type="tracklisturl" location="/playback/container/c3BvdGlmeTphcnRpc3Q6NkF5QVRHZzdtRGdCbFo0TjV1Tm9nMA==" sourceAccount="user@example.com" isPresetable="true">
<itemName>SYML</itemName>
<containerArt>https://i.scdn.co/image/ab67616d0000b273ca6f00df62ef197fdc8af79c</containerArt>
</ContentItem>
<track>In Between Breaths - Paris Unplugged</track>
<artist>SYML</artist>
<album>Paris Unplugged</album>
<stationName></stationName>
<art artImageStatus="IMAGE_PRESENT">https://i.scdn.co/image/ab67616d0000b273ca6f00df62ef197fdc8af79c</art>
<time total="210">36</time>
<skipEnabled />
<favoriteEnabled />
<playStatus>PLAY_STATE</playStatus>
<shuffleSetting>SHUFFLE_OFF</shuffleSetting>
<repeatSetting>REPEAT_OFF</repeatSetting>
<skipPreviousEnabled />
<seekSupported value="true" />
<streamType>TRACK_ONDEMAND</streamType>
<trackID>spotify:track:3LX0dk3YT8cUgp7XxUJgTB</trackID>
</nowPlaying>
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<sources deviceID="A81B6A536A98">
<sourceItem source="AUX" sourceAccount="AUX" status="READY" isLocal="true" multiroomallowed="true">AUX IN</sourceItem>
<sourceItem source="AIRPLAY" sourceAccount="AirPlayUserName" status="UNAVAILABLE" isLocal="false" multiroomallowed="false">AirPlayUserName</sourceItem>
<sourceItem source="QPLAY" sourceAccount="QPlay1UserName" status="UNAVAILABLE" isLocal="true" multiroomallowed="true">QPlay1UserName</sourceItem>
<sourceItem source="QPLAY" sourceAccount="QPlay2UserName" status="UNAVAILABLE" isLocal="true" multiroomallowed="true">QPlay2UserName</sourceItem>
<sourceItem source="STORED_MUSIC_MEDIA_RENDERER" sourceAccount="StoredMusicUserName" status="UNAVAILABLE" isLocal="false" multiroomallowed="true">StoredMusicUserName</sourceItem>
<sourceItem source="UPNP" sourceAccount="UPnPUserName" status="UNAVAILABLE" isLocal="false" multiroomallowed="true">UPnPUserName</sourceItem>
<sourceItem source="NOTIFICATION" status="UNAVAILABLE" isLocal="false" multiroomallowed="true" />
<sourceItem source="SPOTIFY" sourceAccount="user@example.com" status="READY" isLocal="false" multiroomallowed="true">user+spotify@example.com</sourceItem>
<sourceItem source="BLUETOOTH" status="UNAVAILABLE" isLocal="true" multiroomallowed="true" />
<sourceItem source="SPOTIFY" sourceAccount="SpotifyConnectUserName" status="UNAVAILABLE" isLocal="false" multiroomallowed="true">SpotifyConnectUserName</sourceItem>
<sourceItem source="SPOTIFY" sourceAccount="SpotifyAlexaUserName" status="UNAVAILABLE" isLocal="false" multiroomallowed="true">SpotifyAlexaUserName</sourceItem>
<sourceItem source="ALEXA" status="READY" isLocal="false" multiroomallowed="true" />
<sourceItem source="TUNEIN" status="READY" isLocal="false" multiroomallowed="true" />
<sourceItem source="LOCAL_INTERNET_RADIO" status="READY" isLocal="false" multiroomallowed="true" />
</sources>