๐Ÿก index : quine-global/quintodrome.git

package core

import (
	"context"

	"github.com/deluan/rest"
	"github.com/navidrome/navidrome/model"
	"github.com/navidrome/navidrome/model/request"
	"github.com/navidrome/navidrome/tests"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

var _ = Describe("Share", func() {
	var ds model.DataStore
	var share Share
	var mockedRepo rest.Persistable[model.Share]
	ctx := context.Background()

	BeforeEach(func() {
		ds = &tests.MockDataStore{}
		mockedRepo = ds.Share().(rest.Persistable[model.Share])
		share = NewShare(ds)
	})

	Describe("NewRepository", func() {
		var repo rest.Persistable[model.Share]

		BeforeEach(func() {
			repo = share.Repository().(rest.Persistable[model.Share])
			_ = ds.Album().Put(ctx, &model.Album{ID: "123", Name: "Album"})
		})

		Describe("Save", func() {
			It("it sets a random ID", func() {
				entity := &model.Share{Description: "test", ResourceIDs: "123"}
				id, err := repo.Save(ctx, entity)
				Expect(err).ToNot(HaveOccurred())
				Expect(id).ToNot(BeEmpty())
				Expect(entity.ID).To(Equal(id))
			})

			It("assigns the logged-in user as owner, ignoring a client-supplied UserID", func() {
				loggedInCtx := request.WithUser(context.Background(), model.User{ID: "logged-in-user"})
				repo := share.Repository().(rest.Persistable[model.Share])
				entity := &model.Share{Description: "test", ResourceIDs: "123", UserID: "victim-user"}
				_, err := repo.Save(loggedInCtx, entity)
				Expect(err).ToNot(HaveOccurred())
				Expect(entity.UserID).To(Equal("logged-in-user"))
			})

			It("does not truncate ASCII labels shorter than 30 characters", func() {
				_ = ds.MediaFile().Put(ctx, &model.MediaFile{ID: "456", Title: "Example Media File"})
				entity := &model.Share{Description: "test", ResourceIDs: "456"}
				_, err := repo.Save(ctx, entity)
				Expect(err).ToNot(HaveOccurred())
				Expect(entity.Contents).To(Equal("Example Media File"))
			})

			It("truncates ASCII labels longer than 30 characters", func() {
				_ = ds.MediaFile().Put(ctx, &model.MediaFile{ID: "789", Title: "Example Media File But The Title Is Really Long For Testing Purposes"})
				entity := &model.Share{Description: "test", ResourceIDs: "789"}
				_, err := repo.Save(ctx, entity)
				Expect(err).ToNot(HaveOccurred())
				Expect(entity.Contents).To(Equal("Example Media File But The ..."))
			})

			It("does not truncate CJK labels shorter than 30 runes", func() {
				_ = ds.MediaFile().Put(ctx, &model.MediaFile{ID: "456", Title: "้’ๆ˜ฅใ‚ณใƒณใƒ—ใƒฌใƒƒใ‚ฏใ‚น"})
				entity := &model.Share{Description: "test", ResourceIDs: "456"}
				_, err := repo.Save(ctx, entity)
				Expect(err).ToNot(HaveOccurred())
				Expect(entity.Contents).To(Equal("้’ๆ˜ฅใ‚ณใƒณใƒ—ใƒฌใƒƒใ‚ฏใ‚น"))
			})

			It("truncates CJK labels longer than 30 runes", func() {
				_ = ds.MediaFile().Put(ctx, &model.MediaFile{ID: "789", Title: "็งใฎไธญใฎๅนปๆƒณ็š„ไธ–็•Œ่ฆณๅŠใณใใฎ้ก•็พใ‚’ๆƒณ่ตทใ•ใ›ใŸใ‚ใ‚‹็พๅฎŸใงใฎๅ‡บๆฅไบ‹ใซ้–ขใ™ใ‚‹ไธ€่€ƒๅฏŸ"})
				entity := &model.Share{Description: "test", ResourceIDs: "789"}
				_, err := repo.Save(ctx, entity)
				Expect(err).ToNot(HaveOccurred())
				Expect(entity.Contents).To(Equal("็งใฎไธญใฎๅนปๆƒณ็š„ไธ–็•Œ่ฆณๅŠใณใใฎ้ก•็พใ‚’ๆƒณ่ตทใ•ใ›ใŸใ‚ใ‚‹็พๅฎŸใง..."))
			})

			It("fails when any of the resource IDs does not exist", func() {
				entity := &model.Share{Description: "test", ResourceIDs: "123,missing"}
				_, err := repo.Save(ctx, entity)
				Expect(err).To(MatchError(model.ErrNotFound))
			})

			It("fails when the resource IDs are of mixed types", func() {
				_ = ds.MediaFile().Put(ctx, &model.MediaFile{ID: "456", Title: "Example Media File"})
				entity := &model.Share{Description: "test", ResourceIDs: "123,456"}
				_, err := repo.Save(ctx, entity)
				Expect(err).To(HaveOccurred())
			})
		})

		Describe("Update", func() {
			It("filters out read-only fields", func() {
				err := repo.Update(ctx, "id", model.Share{})
				Expect(err).ToNot(HaveOccurred())
				Expect(mockedRepo.(*tests.MockShareRepo).Cols).To(ConsistOf("description", "downloadable"))
			})
		})
	})
})