package library import ( "context" "io" "log/slog" "os" "path/filepath" "testing" "github.com/fsnotify/fsnotify" ) // libraryTree writes empty files at each path below a fresh root. func libraryTree(t *testing.T, files ...string) string { t.Helper() root := t.TempDir() for _, f := range files { p := filepath.Join(root, filepath.FromSlash(f)) if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(p, nil, 0o644); err != nil { t.Fatal(err) } } return root } // A full scan lists the library's audio files and none inside a hidden // folder or hidden itself: a download manager's set-aside copies and macOS // "._" files are not tracks. func TestEnumerate_LeavesHiddenFilesAndFoldersOut(t *testing.T) { root := libraryTree(t, "Artist/Album (2001)/01 Song.flac", ".sluice-held/Artist/Album (2001)/01 Song.flac", "Artist/.cache/02 Other.mp3", "Artist/Album (2001)/._01 Song.flac", ) s := &Scanner{paths: []string{root}, logger: slog.New(slog.NewTextHandler(io.Discard, nil))} var stats Stats got, errs := s.enumerate(context.Background(), nil, &stats) want := filepath.Join(root, "Artist", "Album (2001)", "01 Song.flac") if errs != 0 || len(got) != 1 || got[0] != want { t.Fatalf("enumerate = %v (errors %d), want only %s", got, errs, want) } } func TestHiddenInLibrary(t *testing.T) { root := filepath.Join(string(filepath.Separator), "srv", ".music") for _, tc := range []struct { path string want bool }{ {"Artist/Album/01.flac", false}, {".sluice-held/Artist/Album/01.flac", true}, {".sluice-held", true}, {"Artist/._01.flac", true}, {"Artist/Album..Live/01.flac", false}, {"", false}, } { path := filepath.Join(root, filepath.FromSlash(tc.path)) if got := hiddenInLibrary([]string{root}, path); got != tc.want { t.Errorf("hiddenInLibrary(%q) = %v, want %v (a hidden root itself does not count)", path, got, tc.want) } } outside := filepath.Join(string(filepath.Separator), "srv", ".other", "01.flac") if hiddenInLibrary([]string{root}, outside) { t.Errorf("a path outside every root counted as hidden in the library") } } // The watcher ignores a hidden folder appearing in the library, so nothing // set aside in it is watched or queued for a scan. func TestWatcher_IgnoresAHiddenFolderAppearing(t *testing.T) { root := libraryTree(t, ".sluice-held/Artist/Album/01 Song.flac") w := &Watcher{ scanner: &Scanner{paths: []string{root}}, logger: slog.New(slog.NewTextHandler(io.Discard, nil)), } pending := map[string]struct{}{} held := filepath.Join(root, ".sluice-held") if w.handleEvent(nil, fsnotify.Event{Name: held, Op: fsnotify.Create}, pending) || len(pending) != 0 { t.Fatalf("a hidden folder appearing queued %v", pending) } }