navidrome/utils/req/req_test.go
Deluan Quintão 94eb6c522b
Some checks are pending
Pipeline: Test, Lint, Build / Get version info (push) Waiting to run
Pipeline: Test, Lint, Build / Lint Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Waiting to run
Pipeline: Test, Lint, Build / Test JS code (push) Waiting to run
Pipeline: Test, Lint, Build / Lint i18n files (push) Waiting to run
Pipeline: Test, Lint, Build / Check Docker configuration (push) Waiting to run
Pipeline: Test, Lint, Build / Build (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-1 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Package/Release (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-2 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-3 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-4 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-5 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-6 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-7 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-8 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-9 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-10 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to GHCR (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build Windows installers (push) Blocked by required conditions
feat(subsonic): implement playbackReport OpenSubsonic extension (#5442)
* feat(req): add Float64Or helper for parsing float query params

* feat(scrobbler): extend NowPlayingInfo with state/position/rate fields

* feat(scrobbler): implement ReportPlayback with state machine and auto-scrobble

* feat(responses): add state/positionMs/playbackRate to NowPlayingEntry

* feat(subsonic): add reportPlayback endpoint handler

* feat(subsonic): include state/positionMs/playbackRate in getNowPlaying response

* feat(subsonic): register playbackReport OpenSubsonic extension

* test(e2e): add reportPlayback endpoint e2e tests

* refactor(scrobbler): simplify ReportPlayback — extract helpers, remove duplication

- Add state constants and exported ValidStates map
- Extract remainingTTL() helper (was duplicated 3x)
- Merge playing/paused switch cases into single branch
- Use Get instead of GetWithParticipants for non-stopped states
- Guard NowPlayingCount broadcast with count-change detection
- Use cache entry for NowPlaying dispatch instead of extra DB query
- Remove redundant Position field from NowPlayingInfo

* refactor(scrobbler): skip DB query in playing/paused when playMap has entry

* fix(play_tracker): handle errors when adding/updating NowPlayingInfo in cache

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(play_tracker): replace sort with slices.SortFunc for NowPlayingInfo

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(play_tracker): check all ReportPlayback errors in tests

Replace _ = with explicit error assertions to avoid masking
failures in intermediate calls.

Signed-off-by: Deluan <deluan@navidrome.org>

* test(e2e): use real PlayTracker and assert getNowPlaying after reportPlayback

Replace noopPlayTracker with a real PlayTracker backed by the E2E
database. E2E tests now verify the full round-trip: reportPlayback
creates/updates/removes entries visible via getNowPlaying, including
state, positionMs, and playbackRate fields.

Export NewPlayTracker constructor for use outside the scrobbler package.

* fix(play_tracker): account for playback rate in TTL and detect track switches

The remainingTTL function now divides remaining time by the playback rate,
so cache entries expire correctly at non-1x speeds (e.g., 2x playback halves
the TTL). Zero/negative rates default to 1.0. The playing/paused case now
checks if the cached MediaFile ID matches the reported mediaId, falling back
to a DB fetch when the client switches tracks without sending stopped/starting.
Adds parameterized tests for remainingTTL covering rate variations and edge cases.

* fix(subsonic): validate positionMs and playbackRate in reportPlayback

Reject negative positionMs values and invalid playbackRate values (NaN,
Inf, zero, negative) at the API boundary before they reach TTL and
position estimation math. Returns clear error messages for each case.

* feat(play_tracker): add ClientId and ClientName to ReportPlayback parameters

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(play_tracker): replace NowPlaying method with ReportPlayback calls

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(play_tracker_test): remove redundant TTL behavior tests and clean up mockPluginLoader

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
2026-04-30 23:04:05 -04:00

294 lines
7.6 KiB
Go

package req_test
import (
"fmt"
"net/http/httptest"
"testing"
"time"
"github.com/navidrome/navidrome/utils/req"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestUtils(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Request Helpers Suite")
}
var _ = Describe("Request Helpers", func() {
var r *req.Values
Describe("ParamString", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?a=123", nil))
})
It("returns param as string", func() {
Expect(r.String("a")).To(Equal("123"))
})
It("returns empty string if param does not exist", func() {
v, err := r.String("NON_EXISTENT_PARAM")
Expect(err).To(MatchError(req.ErrMissingParam))
Expect(err.Error()).To(ContainSubstring("NON_EXISTENT_PARAM"))
Expect(v).To(BeEmpty())
})
})
Describe("ParamStringDefault", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?a=123", nil))
})
It("returns param as string", func() {
Expect(r.StringOr("a", "default_value")).To(Equal("123"))
})
It("returns default string if param does not exist", func() {
Expect(r.StringOr("xx", "default_value")).To(Equal("default_value"))
})
})
Describe("ParamStrings", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?a=123&a=456", nil))
})
It("returns all param occurrences as []string", func() {
Expect(r.Strings("a")).To(Equal([]string{"123", "456"}))
})
It("returns empty array if param does not exist", func() {
v, err := r.Strings("xx")
Expect(err).To(MatchError(req.ErrMissingParam))
Expect(v).To(BeEmpty())
})
})
Describe("ParamTime", func() {
d := time.Date(2002, 8, 9, 12, 11, 13, 1000000, time.Local)
t := d.UnixMilli()
now := time.Now()
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", fmt.Sprintf("/ping?t=%d&inv=abc", t), nil))
})
It("returns parsed time", func() {
Expect(r.TimeOr("t", now)).To(Equal(d))
})
It("returns default time if param does not exist", func() {
Expect(r.TimeOr("xx", now)).To(Equal(now))
})
It("returns default time if param is an invalid timestamp", func() {
Expect(r.TimeOr("inv", now)).To(Equal(now))
})
})
Describe("ParamTimes", func() {
d1 := time.Date(2002, 8, 9, 12, 11, 13, 1000000, time.Local)
d2 := time.Date(2002, 8, 9, 12, 13, 56, 0000000, time.Local)
t1 := d1.UnixMilli()
t2 := d2.UnixMilli()
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", fmt.Sprintf("/ping?t=%d&t=%d", t1, t2), nil))
})
It("returns all param occurrences as []time.Time", func() {
Expect(r.Times("t")).To(Equal([]time.Time{d1, d2}))
})
It("returns empty string if param does not exist", func() {
v, err := r.Times("xx")
Expect(err).To(MatchError(req.ErrMissingParam))
Expect(v).To(BeEmpty())
})
It("returns current time as default if param is invalid", func() {
now := time.Now()
r = req.Params(httptest.NewRequest("GET", "/ping?t=null", nil))
times, err := r.Times("t")
Expect(err).ToNot(HaveOccurred())
Expect(times).To(HaveLen(1))
Expect(times[0]).To(BeTemporally(">=", now))
})
})
Describe("ParamInt", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?i=123&inv=123.45", nil))
})
Context("int", func() {
It("returns parsed int", func() {
Expect(r.IntOr("i", 999)).To(Equal(123))
})
It("returns default value if param does not exist", func() {
Expect(r.IntOr("xx", 999)).To(Equal(999))
})
It("returns default value if param is an invalid int", func() {
Expect(r.IntOr("inv", 999)).To(Equal(999))
})
It("returns error if param is an invalid int", func() {
_, err := r.Int("inv")
Expect(err).To(MatchError(req.ErrInvalidParam))
})
})
Context("int64", func() {
It("returns parsed int64", func() {
Expect(r.Int64Or("i", 999)).To(Equal(int64(123)))
})
It("returns default value if param does not exist", func() {
Expect(r.Int64Or("xx", 999)).To(Equal(int64(999)))
})
It("returns default value if param is an invalid int", func() {
Expect(r.Int64Or("inv", 999)).To(Equal(int64(999)))
})
It("returns error if param is an invalid int", func() {
_, err := r.Int64("inv")
Expect(err).To(MatchError(req.ErrInvalidParam))
})
})
})
Describe("ParamInts", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?i=123&i=456", nil))
})
It("returns array of occurrences found", func() {
Expect(r.Ints("i")).To(Equal([]int{123, 456}))
})
It("returns empty array if param does not exist", func() {
v, err := r.Ints("xx")
Expect(err).To(MatchError(req.ErrMissingParam))
Expect(v).To(BeEmpty())
})
})
Describe("ParamBool", func() {
Context("value is true", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?b=true&c=on&d=1&e=True", nil))
})
It("parses 'true'", func() {
Expect(r.BoolOr("b", false)).To(BeTrue())
})
It("parses 'on'", func() {
Expect(r.BoolOr("c", false)).To(BeTrue())
})
It("parses '1'", func() {
Expect(r.BoolOr("d", false)).To(BeTrue())
})
It("parses 'True'", func() {
Expect(r.BoolOr("e", false)).To(BeTrue())
})
})
Context("value is false", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?b=false&c=off&d=0", nil))
})
It("parses 'false'", func() {
Expect(r.BoolOr("b", true)).To(BeFalse())
})
It("parses 'off'", func() {
Expect(r.BoolOr("c", true)).To(BeFalse())
})
It("parses '0'", func() {
Expect(r.BoolOr("d", true)).To(BeFalse())
})
It("returns default value if param does not exist", func() {
Expect(r.BoolOr("xx", true)).To(BeTrue())
})
})
})
Describe("ParamStringPtr", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?a=123", nil))
})
It("returns pointer to string if param exists", func() {
ptr := r.StringPtr("a")
Expect(ptr).ToNot(BeNil())
Expect(*ptr).To(Equal("123"))
})
It("returns nil if param does not exist", func() {
ptr := r.StringPtr("xx")
Expect(ptr).To(BeNil())
})
It("returns pointer to empty string if param exists but is empty", func() {
r = req.Params(httptest.NewRequest("GET", "/ping?a=", nil))
ptr := r.StringPtr("a")
Expect(ptr).ToNot(BeNil())
Expect(*ptr).To(Equal(""))
})
})
Describe("Float64Or", func() {
It("returns parsed float value", func() {
r := req.Params(httptest.NewRequest("GET", "/test?rate=1.5", nil))
Expect(r.Float64Or("rate", 1.0)).To(Equal(1.5))
})
It("returns default when param is missing", func() {
r := req.Params(httptest.NewRequest("GET", "/test", nil))
Expect(r.Float64Or("rate", 1.0)).To(Equal(1.0))
})
It("returns default when param is not a valid float", func() {
r := req.Params(httptest.NewRequest("GET", "/test?rate=abc", nil))
Expect(r.Float64Or("rate", 1.0)).To(Equal(1.0))
})
})
Describe("ParamBoolPtr", func() {
Context("value is true", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?b=true", nil))
})
It("returns pointer to true if param is 'true'", func() {
ptr := r.BoolPtr("b")
Expect(ptr).ToNot(BeNil())
Expect(*ptr).To(BeTrue())
})
})
Context("value is false", func() {
BeforeEach(func() {
r = req.Params(httptest.NewRequest("GET", "/ping?b=false", nil))
})
It("returns pointer to false if param is 'false'", func() {
ptr := r.BoolPtr("b")
Expect(ptr).ToNot(BeNil())
Expect(*ptr).To(BeFalse())
})
})
It("returns nil if param does not exist", func() {
ptr := r.BoolPtr("xx")
Expect(ptr).To(BeNil())
})
})
})