test: improve serve_index_test code

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan
2025-06-28 20:01:47 -04:00
parent b4aaa7f3a6
commit 411b32ebb8
3 changed files with 66 additions and 300 deletions
+3
View File
@@ -26,6 +26,9 @@ docker-compose.yml
binaries binaries
navidrome-* navidrome-*
AGENTS.md AGENTS.md
.github/prompts
.github/instructions
.github/git-commit-instructions.md
*.exe *.exe
*.test *.test
*.wasm *.wasm
+1 -1
View File
@@ -64,7 +64,7 @@ lintall: lint ##@Development Lint Go and JS code
format: ##@Development Format code format: ##@Development Format code
@(cd ./ui && npm run prettier) @(cd ./ui && npm run prettier)
@go tool goimports -w `find . -name '*.go' | grep -v _gen.go$$` @go tool goimports -w `find . -name '*.go' | grep -v _gen.go$$ | grep -v .pb.go$$`
@go mod tidy @go mod tidy
.PHONY: format .PHONY: format
+62 -299
View File
@@ -39,7 +39,7 @@ var _ = Describe("serveIndex", func() {
Expect(w.Code).To(Equal(200)) Expect(w.Code).To(Equal(200))
config := extractAppConfig(w.Body.String()) config := extractAppConfig(w.Body.String())
Expect(config).To(BeAssignableToTypeOf(map[string]interface{}{})) Expect(config).To(BeAssignableToTypeOf(map[string]any{}))
}) })
It("sets firstTime = true when User table is empty", func() { It("sets firstTime = true when User table is empty", func() {
@@ -53,17 +53,6 @@ var _ = Describe("serveIndex", func() {
Expect(config).To(HaveKeyWithValue("firstTime", true)) Expect(config).To(HaveKeyWithValue("firstTime", true))
}) })
It("includes the VariousArtistsID", func() {
mockUser.empty = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("variousArtistsId", consts.VariousArtistsID))
})
It("sets firstTime = false when User table is not empty", func() { It("sets firstTime = false when User table is not empty", func() {
mockUser.empty = false mockUser.empty = false
r := httptest.NewRequest("GET", "/index.html", nil) r := httptest.NewRequest("GET", "/index.html", nil)
@@ -75,289 +64,63 @@ var _ = Describe("serveIndex", func() {
Expect(config).To(HaveKeyWithValue("firstTime", false)) Expect(config).To(HaveKeyWithValue("firstTime", false))
}) })
It("sets baseURL", func() { DescribeTable("sets configuration values",
conf.Server.BasePath = "base_url_test" func(configSetter func(), configKey string, expectedValue any) {
r := httptest.NewRequest("GET", "/index.html", nil) configSetter()
w := httptest.NewRecorder() r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("baseURL", "base_url_test")) config := extractAppConfig(w.Body.String())
}) Expect(config).To(HaveKeyWithValue(configKey, expectedValue))
},
It("sets the welcomeMessage", func() { Entry("baseURL", func() { conf.Server.BasePath = "base_url_test" }, "baseURL", "base_url_test"),
conf.Server.UIWelcomeMessage = "Hello" Entry("welcomeMessage", func() { conf.Server.UIWelcomeMessage = "Hello" }, "welcomeMessage", "Hello"),
r := httptest.NewRequest("GET", "/index.html", nil) Entry("maxSidebarPlaylists", func() { conf.Server.MaxSidebarPlaylists = 42 }, "maxSidebarPlaylists", float64(42)),
w := httptest.NewRecorder() Entry("enableTranscodingConfig", func() { conf.Server.EnableTranscodingConfig = true }, "enableTranscodingConfig", true),
Entry("enableDownloads", func() { conf.Server.EnableDownloads = true }, "enableDownloads", true),
serveIndex(ds, fs, nil)(w, r) Entry("enableFavourites", func() { conf.Server.EnableFavourites = true }, "enableFavourites", true),
Entry("enableStarRating", func() { conf.Server.EnableStarRating = true }, "enableStarRating", true),
config := extractAppConfig(w.Body.String()) Entry("defaultTheme", func() { conf.Server.DefaultTheme = "Light" }, "defaultTheme", "Light"),
Expect(config).To(HaveKeyWithValue("welcomeMessage", "Hello")) Entry("defaultLanguage", func() { conf.Server.DefaultLanguage = "pt" }, "defaultLanguage", "pt"),
}) Entry("defaultUIVolume", func() { conf.Server.DefaultUIVolume = 45 }, "defaultUIVolume", float64(45)),
Entry("enableCoverAnimation", func() { conf.Server.EnableCoverAnimation = true }, "enableCoverAnimation", true),
It("sets the maxSidebarPlaylists", func() { Entry("enableNowPlaying", func() { conf.Server.EnableNowPlaying = true }, "enableNowPlaying", true),
conf.Server.MaxSidebarPlaylists = 42 Entry("gaTrackingId", func() { conf.Server.GATrackingID = "UA-12345" }, "gaTrackingId", "UA-12345"),
r := httptest.NewRequest("GET", "/index.html", nil) Entry("defaultDownloadableShare", func() { conf.Server.DefaultDownloadableShare = true }, "defaultDownloadableShare", true),
w := httptest.NewRecorder() Entry("devSidebarPlaylists", func() { conf.Server.DevSidebarPlaylists = true }, "devSidebarPlaylists", true),
Entry("lastFMEnabled", func() { conf.Server.LastFM.Enabled = true }, "lastFMEnabled", true),
serveIndex(ds, fs, nil)(w, r) Entry("devShowArtistPage", func() { conf.Server.DevShowArtistPage = true }, "devShowArtistPage", true),
Entry("devUIShowConfig", func() { conf.Server.DevUIShowConfig = true }, "devUIShowConfig", true),
config := extractAppConfig(w.Body.String()) Entry("listenBrainzEnabled", func() { conf.Server.ListenBrainz.Enabled = true }, "listenBrainzEnabled", true),
Expect(config).To(HaveKeyWithValue("maxSidebarPlaylists", float64(42))) Entry("enableReplayGain", func() { conf.Server.EnableReplayGain = true }, "enableReplayGain", true),
}) Entry("enableExternalServices", func() { conf.Server.EnableExternalServices = true }, "enableExternalServices", true),
Entry("devActivityPanel", func() { conf.Server.DevActivityPanel = true }, "devActivityPanel", true),
It("sets the enableTranscodingConfig", func() { Entry("shareURL", func() { conf.Server.ShareURL = "https://share.example.com" }, "shareURL", "https://share.example.com"),
conf.Server.EnableTranscodingConfig = true Entry("enableInspect", func() { conf.Server.Inspect.Enabled = true }, "enableInspect", true),
r := httptest.NewRequest("GET", "/index.html", nil) Entry("defaultDownsamplingFormat", func() { conf.Server.DefaultDownsamplingFormat = "mp3" }, "defaultDownsamplingFormat", "mp3"),
w := httptest.NewRecorder() Entry("enableUserEditing", func() { conf.Server.EnableUserEditing = false }, "enableUserEditing", false),
Entry("enableSharing", func() { conf.Server.EnableSharing = true }, "enableSharing", true),
serveIndex(ds, fs, nil)(w, r) )
config := extractAppConfig(w.Body.String()) DescribeTable("sets other UI configuration values",
Expect(config).To(HaveKeyWithValue("enableTranscodingConfig", true)) func(configKey string, expectedValueFunc func() any) {
}) r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
It("sets the enableDownloads", func() {
conf.Server.EnableDownloads = true serveIndex(ds, fs, nil)(w, r)
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder() config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue(configKey, expectedValueFunc()))
serveIndex(ds, fs, nil)(w, r) },
Entry("version", "version", func() any { return consts.Version }),
config := extractAppConfig(w.Body.String()) Entry("variousArtistsId", "variousArtistsId", func() any { return consts.VariousArtistsID }),
Expect(config).To(HaveKeyWithValue("enableDownloads", true)) Entry("losslessFormats", "losslessFormats", func() any {
}) return strings.ToUpper(strings.Join(mime.LosslessFormats, ","))
}),
It("sets the enableLoved", func() { Entry("separator", "separator", func() any { return string(os.PathSeparator) }),
conf.Server.EnableFavourites = true )
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableFavourites", true))
})
It("sets the enableStarRating", func() {
conf.Server.EnableStarRating = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableStarRating", true))
})
It("sets the defaultTheme", func() {
conf.Server.DefaultTheme = "Light"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("defaultTheme", "Light"))
})
It("sets the defaultLanguage", func() {
conf.Server.DefaultLanguage = "pt"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("defaultLanguage", "pt"))
})
It("sets the defaultUIVolume", func() {
conf.Server.DefaultUIVolume = 45
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("defaultUIVolume", float64(45)))
})
It("sets the enableCoverAnimation", func() {
conf.Server.EnableCoverAnimation = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableCoverAnimation", true))
})
It("sets the enableNowPlaying", func() {
conf.Server.EnableNowPlaying = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableNowPlaying", true))
})
It("sets the gaTrackingId", func() {
conf.Server.GATrackingID = "UA-12345"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("gaTrackingId", "UA-12345"))
})
It("sets the version", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("version", consts.Version))
})
It("sets the losslessFormats", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
expected := strings.ToUpper(strings.Join(mime.LosslessFormats, ","))
Expect(config).To(HaveKeyWithValue("losslessFormats", expected))
})
It("sets the enableUserEditing", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableUserEditing", true))
})
It("sets the enableSharing", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableSharing", false))
})
It("sets the defaultDownloadableShare", func() {
conf.Server.DefaultDownloadableShare = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("defaultDownloadableShare", true))
})
It("sets the defaultDownsamplingFormat", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("defaultDownsamplingFormat", conf.Server.DefaultDownsamplingFormat))
})
It("sets the devSidebarPlaylists", func() {
conf.Server.DevSidebarPlaylists = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("devSidebarPlaylists", true))
})
It("sets the lastFMEnabled", func() {
conf.Server.LastFM.Enabled = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("lastFMEnabled", true))
})
It("sets the devShowArtistPage", func() {
conf.Server.DevShowArtistPage = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("devShowArtistPage", true))
})
It("sets the devUIShowConfig", func() {
conf.Server.DevUIShowConfig = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("devUIShowConfig", true))
})
It("sets the listenBrainzEnabled", func() {
conf.Server.ListenBrainz.Enabled = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("listenBrainzEnabled", true))
})
It("sets the enableReplayGain", func() {
conf.Server.EnableReplayGain = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableReplayGain", true))
})
It("sets the enableExternalServices", func() {
conf.Server.EnableExternalServices = true
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
serveIndex(ds, fs, nil)(w, r)
config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("enableExternalServices", true))
})
Describe("loginBackgroundURL", func() { Describe("loginBackgroundURL", func() {
Context("empty BaseURL", func() { Context("empty BaseURL", func() {
@@ -448,12 +211,12 @@ var _ = Describe("serveIndex", func() {
var _ = Describe("addShareData", func() { var _ = Describe("addShareData", func() {
var ( var (
r *http.Request r *http.Request
data map[string]interface{} data map[string]any
shareInfo *model.Share shareInfo *model.Share
) )
BeforeEach(func() { BeforeEach(func() {
data = make(map[string]interface{}) data = make(map[string]any)
r = httptest.NewRequest("GET", "/", nil) r = httptest.NewRequest("GET", "/", nil)
}) })
@@ -538,8 +301,8 @@ var _ = Describe("addShareData", func() {
var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__=(.*);</script>`) var appConfigRegex = regexp.MustCompile(`(?m)window.__APP_CONFIG__=(.*);</script>`)
func extractAppConfig(body string) map[string]interface{} { func extractAppConfig(body string) map[string]any {
config := make(map[string]interface{}) config := make(map[string]any)
match := appConfigRegex.FindStringSubmatch(body) match := appConfigRegex.FindStringSubmatch(body)
if match == nil { if match == nil {
return config return config