package artwork
import (
"context"
"errors"
"io/fs"
"testing/fstest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type unreadableFS struct{ fstest.MapFS }
func (u unreadableFS) Open(string) (fs.File, error) { return nil, fs.ErrPermission }
var _ = Describe("findImageInFolder", func() {
var ctx context.Context
var files fstest.MapFS
BeforeEach(func() {
ctx = context.Background()
files = fstest.MapFS{"artist.jpg": &fstest.MapFile{Data: []byte("img")}}
})
It("returns the first matching image", func() {
r, hit, err := findImageInFolder(ctx, files, ".", "/lib", "artist.*")
Expect(err).ToNot(HaveOccurred())
defer r.Close()
Expect(hit).To(HaveSuffix("artist.jpg"))
})
It("reports a matched but unreadable image as unreadable, not as a miss", func() {
_, _, err := findImageInFolder(ctx, unreadableFS{files}, ".", "/lib", "artist.*")
Expect(err).To(MatchError(errSourceUnreadable))
})
It("reports a plain miss when nothing matches", func() {
_, _, err := findImageInFolder(ctx, files, ".", "/lib", "nothing.*")
Expect(err).To(HaveOccurred())
Expect(errors.Is(err, errSourceUnreadable)).To(BeFalse(), "no match is definitive, not transient")
})
})