Add shared audio source discovery
This commit is contained in:
106
internal/audio/discovery_test.go
Normal file
106
internal/audio/discovery_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
)
|
||||
|
||||
type discoveryLister struct {
|
||||
objects []storage.ObjectInfo
|
||||
prefix string
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *discoveryLister) List(_ context.Context, prefix string) ([]storage.ObjectInfo, error) {
|
||||
s.prefix = prefix
|
||||
return s.objects, s.err
|
||||
}
|
||||
|
||||
func TestDiscoverLocalSelection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for _, name := range []string{"one.MP3", "two.flac", "note.txt", "unfamiliar.xyz", "extensionless"} {
|
||||
if err := os.WriteFile(filepath.Join(root, name), []byte("sample"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(root, "folder.wav"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := DiscoverLocal(root, nil, ".")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{filepath.Join(root, "one.MP3"), filepath.Join(root, "two.flac")}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
got, err = DiscoverLocal(root, []string{" extensionless ", "./unfamiliar.xyz"}, "missing")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want = []string{filepath.Join(root, "extensionless"), filepath.Join(root, "unfamiliar.xyz")}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
for _, files := range [][]string{{"one.MP3", "./one.MP3"}, {"missing"}, {"folder.wav"}, {" "}} {
|
||||
if _, err := DiscoverLocal(root, files, ""); err == nil {
|
||||
t.Errorf("expected error for %v", files)
|
||||
}
|
||||
}
|
||||
if _, err := DiscoverLocal(root, nil, "folder.wav"); err == nil {
|
||||
t.Fatal("expected empty directory error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverLocalRequiresRegularCandidates(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Symlink("missing", filepath.Join(root, "broken.wav")); err != nil {
|
||||
t.Skip(err)
|
||||
}
|
||||
if _, err := DiscoverLocal(root, nil, "."); err == nil {
|
||||
t.Fatal("expected missing target error")
|
||||
}
|
||||
if err := os.Remove(filepath.Join(root, "broken.wav")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(root, "target"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("target", filepath.Join(root, "link.wav")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := DiscoverLocal(root, nil, "."); err == nil {
|
||||
t.Fatal("expected non-regular target error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverS3Selection(t *testing.T) {
|
||||
store := &discoveryLister{objects: []storage.ObjectInfo{{Key: " prefix/z.MP3 ", Size: 42, ETag: "etag"}, {Key: "prefix/dir/"}, {Key: " "}, {Key: "prefix/notes.txt"}, {Key: "prefix/a.wav"}}}
|
||||
got, err := DiscoverS3(context.Background(), store, "prefix/")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if store.prefix != "prefix/" || len(got) != 2 || got[0].Key != "prefix/a.wav" || got[1].Key != "prefix/z.MP3" || got[1].Size != 42 || got[1].ETag != "etag" {
|
||||
t.Fatalf("unexpected selection: %#v", got)
|
||||
}
|
||||
store.objects = []storage.ObjectInfo{{Key: "a/same.flac"}, {Key: "b/same.flac"}}
|
||||
if _, err := DiscoverS3(context.Background(), store, "prefix/"); err == nil || !strings.Contains(err.Error(), "a/same.flac") || !strings.Contains(err.Error(), "b/same.flac") {
|
||||
t.Fatalf("duplicate error: %v", err)
|
||||
}
|
||||
store.objects = nil
|
||||
if _, err := DiscoverS3(context.Background(), store, "prefix/"); err == nil {
|
||||
t.Fatal("expected empty selection error")
|
||||
}
|
||||
sentinel := errors.New("list failed")
|
||||
store.err = sentinel
|
||||
if _, err := DiscoverS3(context.Background(), store, "prefix/"); !errors.Is(err, sentinel) {
|
||||
t.Fatalf("listing error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user