diff --git a/CHANGELOG.md b/CHANGELOG.md index d5682fe..5829ba6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## [Unreleased] +### Fixed +- `sonos open`, `play-spotify`, and SMAPI open no longer report success when the speaker omits the enqueued track number. Playback is not started on an unknown queue position. + ## [0.3.5] - 2026-10-23 **Highlights:** Fix the reachable crypto/x509 vulnerability GO-2026-6809 with Go 1.27.3, explain SMAPI API-client identity rejections, and correctly label initial watch events. diff --git a/internal/sonos/queue.go b/internal/sonos/queue.go index 679dea7..9e85277 100644 --- a/internal/sonos/queue.go +++ b/internal/sonos/queue.go @@ -72,6 +72,14 @@ func (c *Client) PlayFromQueueTrack(ctx context.Context, oneBasedTrackNumber int return c.playFromQueueTrack(ctx, oneBasedTrackNumber) } +func (c *Client) startEnqueuedPlayback(ctx context.Context, firstTrack int) error { + if firstTrack <= 0 { + // The queue was not cleared, so track 1 can be an older item. + return errors.New("playback was not started because the speaker did not report the new queue position; the track is already on the queue. Run sonos queue list, then sonos queue play for this speaker. Do not enqueue the track again") + } + return c.playFromQueueTrack(ctx, firstTrack) +} + func (c *Client) playFromQueueTrack(ctx context.Context, oneBasedTrackNumber int) error { dd, err := c.GetDeviceDescription(ctx) if err != nil { diff --git a/internal/sonos/smapi_enqueue.go b/internal/sonos/smapi_enqueue.go index 351d5f4..94254f5 100644 --- a/internal/sonos/smapi_enqueue.go +++ b/internal/sonos/smapi_enqueue.go @@ -42,12 +42,10 @@ func (c *Client) EnqueueSMAPIItem(ctx context.Context, svc MusicServiceDescripto if err != nil { return 0, err } - if opts.PlayNow && first > 0 { - if err := c.playFromQueueTrack(ctx, first); err != nil { + if opts.PlayNow { + if err := c.startEnqueuedPlayback(ctx, first); err != nil { return first, err } - } else if opts.PlayNow { - _ = c.Play(ctx) } return first, nil } diff --git a/internal/sonos/smapi_enqueue_test.go b/internal/sonos/smapi_enqueue_test.go index 0d46825..8a8ff8b 100644 --- a/internal/sonos/smapi_enqueue_test.go +++ b/internal/sonos/smapi_enqueue_test.go @@ -68,6 +68,41 @@ func TestEnqueueSMAPIItem(t *testing.T) { } } +func TestEnqueueSMAPIItem_PlayNowMissingTrackNumber(t *testing.T) { + t.Parallel() + + var actions []string + c := &Client{ + IP: "192.0.2.1", + HTTP: &http.Client{Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) { + action := r.Header.Get("SOAPACTION") + actions = append(actions, action) + if strings.Contains(action, "AddURIToQueue") { + return httpResponse(200, ``), nil + } + if strings.Contains(action, "Play") { + return httpResponse(200, ``), nil + } + t.Fatalf("unexpected action %s", action) + return nil, nil + })}, + } + + _, err := c.EnqueueSMAPIItem(context.Background(), + MusicServiceDescriptor{ID: "2311", Name: "Spotify"}, + SMAPIItem{ID: "track-1", Title: "Song"}, + EnqueueOptions{PlayNow: true}, + ) + if err == nil || !enqueueMissingPositionMessage(err) { + t.Fatalf("error = %v", err) + } + for _, action := range actions { + if strings.Contains(action, "Play") { + t.Fatalf("started playback without a track number: %v", actions) + } + } +} + func TestEnqueueSMAPIItemValidation(t *testing.T) { t.Parallel() diff --git a/internal/sonos/spotify.go b/internal/sonos/spotify.go index 11dee37..bb3ab41 100644 --- a/internal/sonos/spotify.go +++ b/internal/sonos/spotify.go @@ -122,13 +122,10 @@ func (c *Client) EnqueueSpotify(ctx context.Context, input string, opts EnqueueO lastErr = err continue } - if opts.PlayNow && first > 0 { - if err := c.playFromQueueTrack(ctx, first); err != nil { + if opts.PlayNow { + if err := c.startEnqueuedPlayback(ctx, first); err != nil { return first, err } - } else if opts.PlayNow { - // If the speaker doesn't report a first track number, just call Play. - _ = c.Play(ctx) } return first, nil } diff --git a/internal/sonos/spotify_enqueue_test.go b/internal/sonos/spotify_enqueue_test.go index c060d9e..5e29356 100644 --- a/internal/sonos/spotify_enqueue_test.go +++ b/internal/sonos/spotify_enqueue_test.go @@ -195,3 +195,65 @@ func TestClientEnqueueSpotify_TrackFallbacksToLegacyURI(t *testing.T) { t.Fatalf("attempted URIs mismatch: got %v want %v", attemptedURIs, expected) } } + +func TestClientEnqueueSpotify_PlayNowMissingTrackNumber(t *testing.T) { + t.Parallel() + + var actions []string + c := &Client{ + IP: "192.0.2.1", + HTTP: &http.Client{Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) { + if r.Method == http.MethodGet { + return httpResponse(200, `uuid:RINCON_ABC1400`), nil + } + action := r.Header.Get("SOAPACTION") + actions = append(actions, action) + if strings.Contains(action, "#AddURIToQueue") { + return httpResponse(200, ``), nil + } + if strings.Contains(action, "#Play") { + return httpResponse(200, ``), nil + } + t.Fatalf("unexpected SOAPACTION %q", action) + return nil, nil + })}, + } + + _, err := c.EnqueueSpotify(context.Background(), "spotify:track:abc123", EnqueueOptions{PlayNow: true}) + if err == nil || !enqueueMissingPositionMessage(err) { + t.Fatalf("error = %v", err) + } + for _, action := range actions { + if strings.Contains(action, "#Play") || strings.Contains(action, "#Seek") { + t.Fatalf("started playback without a track number: %v", actions) + } + } +} + +func TestStartEnqueuedPlaybackMissingPosition(t *testing.T) { + t.Parallel() + + err := (&Client{}).startEnqueuedPlayback(context.Background(), 0) + if !enqueueMissingPositionMessage(err) { + t.Fatalf("error = %v", err) + } +} + +func enqueueMissingPositionMessage(err error) bool { + if err == nil { + return false + } + msg := err.Error() + for _, part := range []string{ + "playback was not started", + "already on the queue", + "sonos queue list", + "sonos queue play ", + "Do not enqueue the track again", + } { + if !strings.Contains(msg, part) { + return false + } + } + return true +}