navidrome/core/artwork_internal_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
5.3 KiB
Go
Raw Normal View History

2022-12-19 21:34:21 +01:00
package core
import (
"context"
2022-12-19 23:07:29 +01:00
"image"
2022-12-19 21:34:21 +01:00
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
2022-12-19 23:07:29 +01:00
var _ = Describe("Artwork", func() {
2022-12-19 21:34:21 +01:00
var aw *artwork
var ds model.DataStore
ctx := log.NewContext(context.TODO())
2022-12-19 23:07:29 +01:00
var alOnlyEmbed, alEmbedNotFound, alOnlyExternal, alExternalNotFound, alAllOptions model.Album
2022-12-20 16:53:52 +01:00
var mfWithEmbed, mfWithoutEmbed, mfCorruptedCover model.MediaFile
2022-12-19 21:34:21 +01:00
BeforeEach(func() {
ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepo{}}
alOnlyEmbed = model.Album{ID: "222", Name: "Only embed", EmbedArtPath: "tests/fixtures/test.mp3"}
alEmbedNotFound = model.Album{ID: "333", Name: "Embed not found", EmbedArtPath: "tests/fixtures/NON_EXISTENT.mp3"}
2022-12-19 23:07:29 +01:00
alOnlyExternal = model.Album{ID: "444", Name: "Only external", ImageFiles: "tests/fixtures/front.png"}
alExternalNotFound = model.Album{ID: "555", Name: "External not found", ImageFiles: "tests/fixtures/NON_EXISTENT.png"}
alAllOptions = model.Album{ID: "666", Name: "All options", EmbedArtPath: "tests/fixtures/test.mp3",
ImageFiles: "tests/fixtures/cover.jpg:tests/fixtures/front.png",
}
2022-12-20 16:53:52 +01:00
mfWithEmbed = model.MediaFile{ID: "22", Path: "tests/fixtures/test.mp3", HasCoverArt: true, AlbumID: "222"}
mfWithoutEmbed = model.MediaFile{ID: "44", Path: "tests/fixtures/test.ogg", AlbumID: "444"}
mfCorruptedCover = model.MediaFile{ID: "45", Path: "tests/fixtures/test.ogg", HasCoverArt: true, AlbumID: "444"}
2022-12-19 21:34:21 +01:00
aw = NewArtwork(ds).(*artwork)
})
2022-12-19 23:07:29 +01:00
Context("Albums", func() {
Context("ID not found", func() {
It("returns placeholder if album is not in the DB", func() {
2022-12-20 16:53:52 +01:00
_, path, err := aw.get(context.Background(), "al-NOT_FOUND-0", 0)
2022-12-19 23:07:29 +01:00
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal(consts.PlaceholderAlbumArt))
2022-12-19 21:34:21 +01:00
})
})
2022-12-19 23:07:29 +01:00
Context("Embed images", func() {
BeforeEach(func() {
ds.Album(ctx).(*tests.MockAlbumRepo).SetData(model.Albums{
alOnlyEmbed,
alEmbedNotFound,
})
})
It("returns embed cover", func() {
_, path, err := aw.get(context.Background(), alOnlyEmbed.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal("tests/fixtures/test.mp3"))
})
It("returns placeholder if embed path is not available", func() {
_, path, err := aw.get(context.Background(), alEmbedNotFound.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal(consts.PlaceholderAlbumArt))
})
})
Context("External images", func() {
BeforeEach(func() {
ds.Album(ctx).(*tests.MockAlbumRepo).SetData(model.Albums{
alOnlyExternal,
alAllOptions,
})
})
It("returns external cover", func() {
_, path, err := aw.get(context.Background(), alOnlyExternal.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal("tests/fixtures/front.png"))
})
It("returns the first image if more than one is available", func() {
_, path, err := aw.get(context.Background(), alAllOptions.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal("tests/fixtures/cover.jpg"))
})
It("returns placeholder if external file is not available", func() {
_, path, err := aw.get(context.Background(), alExternalNotFound.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal(consts.PlaceholderAlbumArt))
})
2022-12-19 21:34:21 +01:00
})
})
2022-12-20 16:53:52 +01:00
Context("MediaFiles", func() {
Context("ID not found", func() {
It("returns placeholder if album is not in the DB", func() {
_, path, err := aw.get(context.Background(), "mf-NOT_FOUND-0", 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal(consts.PlaceholderAlbumArt))
})
})
Context("Embed images", func() {
BeforeEach(func() {
ds.Album(ctx).(*tests.MockAlbumRepo).SetData(model.Albums{
alOnlyEmbed,
alOnlyExternal,
})
ds.MediaFile(ctx).(*tests.MockMediaFileRepo).SetData(model.MediaFiles{
mfWithEmbed,
mfWithoutEmbed,
mfCorruptedCover,
})
})
It("returns embed cover", func() {
_, path, err := aw.get(context.Background(), mfWithEmbed.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal("tests/fixtures/test.mp3"))
})
It("returns album cover if media file has no cover art", func() {
_, path, err := aw.get(context.Background(), mfWithoutEmbed.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal("tests/fixtures/front.png"))
})
It("returns album cover if cannot read embed artwork", func() {
_, path, err := aw.get(context.Background(), mfCorruptedCover.CoverArtID().String(), 0)
Expect(err).ToNot(HaveOccurred())
Expect(path).To(Equal("tests/fixtures/front.png"))
})
})
})
2022-12-19 23:07:29 +01:00
Context("Resize", func() {
2022-12-19 21:34:21 +01:00
BeforeEach(func() {
ds.Album(ctx).(*tests.MockAlbumRepo).SetData(model.Albums{
2022-12-19 23:07:29 +01:00
alOnlyExternal,
2022-12-19 21:34:21 +01:00
})
})
2022-12-19 23:07:29 +01:00
It("returns external cover resized", func() {
r, path, err := aw.get(context.Background(), alOnlyExternal.CoverArtID().String(), 300)
2022-12-19 21:34:21 +01:00
Expect(err).ToNot(HaveOccurred())
2022-12-19 23:07:29 +01:00
Expect(path).To(Equal("tests/fixtures/front.png@300"))
img, _, err := image.Decode(r)
Expect(err).To(BeNil())
Expect(img.Bounds().Size().X).To(Equal(300))
Expect(img.Bounds().Size().Y).To(Equal(300))
2022-12-19 21:34:21 +01:00
})
})
})