diff --git a/cmd/serve/dlna/cds_test.go b/cmd/serve/dlna/cds_test.go new file mode 100644 index 000000000..6ab793f0c --- /dev/null +++ b/cmd/serve/dlna/cds_test.go @@ -0,0 +1,102 @@ +package dlna + +import ( + "context" + "sort" + "testing" + + localBackend "github.com/rclone/rclone/backend/local" + "github.com/rclone/rclone/fs/config/configmap" + "github.com/rclone/rclone/vfs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMediaWithResources(t *testing.T) { + fs, err := localBackend.NewFs(context.Background(), "testdatafiles", "testdata/files", configmap.New()) + require.NoError(t, err) + + myvfs := vfs.New(fs, nil) + { + rootNode, err := myvfs.Stat("") + require.NoError(t, err) + + rootDir := rootNode.(*vfs.Dir) + dirEntries, err := rootDir.ReadDirAll() + require.NoError(t, err) + + mediaItems, assocResources := mediaWithResources(dirEntries) + + // ensure mediaItems contains some items we care about. + // We specifically check that the .mp4 file and a child directory is kept. + var videoMp4 *vfs.Node + foundSubdir := false + for _, mediaItem := range mediaItems { + if mediaItem.Name() == "video.mp4" { + videoMp4 = &mediaItem + } else if mediaItem.Name() == "subdir" { + foundSubdir = true + } + } + + assert.True(t, videoMp4 != nil, "expected mp4 to be found") + assert.True(t, foundSubdir, "expected subdir to be found") + + assocVideoResource, ok := assocResources[*videoMp4] + require.True(t, ok, "expected video.mp4 to have assoc video resource") + + // ensure both video.en.srt and video.srt are in assocVideoResource. + assocVideoResourceNames := make([]string, 0) + for _, e := range assocVideoResource { + assocVideoResourceNames = append(assocVideoResourceNames, e.Name()) + } + sort.Strings(assocVideoResourceNames) + assert.Equal(t, []string{"video.en.srt", "video.srt"}, assocVideoResourceNames) + } + // Now test inside subdir2. + // This directory only contains a video.mp4 file, but as it also contains a + // "Subs" subdir, `mediaWithResources` is called with its children appended, + // causing the media items are appropriately populated. + { + rootNode, err := myvfs.Stat("subdir2") + require.NoError(t, err) + + subtitleNode, err := myvfs.Stat("subdir2/Subs") + require.NoError(t, err) + + rootDir := rootNode.(*vfs.Dir) + subtitleDir := subtitleNode.(*vfs.Dir) + + dirEntries, err := rootDir.ReadDirAll() + require.NoError(t, err) + + subtitleEntries, err := subtitleDir.ReadDirAll() + require.NoError(t, err) + + dirEntries = append(dirEntries, subtitleEntries...) + + mediaItems, assocResources := mediaWithResources(dirEntries) + + // ensure mediaItems contains some items we care about. + // We specifically check that the .mp4 file is kept. + var videoMp4 *vfs.Node + for _, mediaItem := range mediaItems { + if mediaItem.Name() == "video.mp4" { + videoMp4 = &mediaItem + } + } + + assert.True(t, videoMp4 != nil, "expected mp4 to be found") + + assocVideoResource, ok := assocResources[*videoMp4] + require.True(t, ok, "expected video.mp4 to have assoc video resource") + + // ensure both video.en.srt and video.srt are in assocVideoResource. + assocVideoResourceNames := make([]string, 0) + for _, e := range assocVideoResource { + assocVideoResourceNames = append(assocVideoResourceNames, e.Name()) + } + sort.Strings(assocVideoResourceNames) + assert.Equal(t, []string{"video.en.srt", "video.srt"}, assocVideoResourceNames) + } +} diff --git a/cmd/serve/dlna/testdata/files/subdir2/Subs/video.en.srt b/cmd/serve/dlna/testdata/files/subdir2/Subs/video.en.srt new file mode 100644 index 000000000..1663efe84 --- /dev/null +++ b/cmd/serve/dlna/testdata/files/subdir2/Subs/video.en.srt @@ -0,0 +1,3 @@ +1 +00:00:00,000 --> 00:02:00,000 +Test diff --git a/cmd/serve/dlna/testdata/files/subdir2/Subs/video.srt b/cmd/serve/dlna/testdata/files/subdir2/Subs/video.srt new file mode 100644 index 000000000..1663efe84 --- /dev/null +++ b/cmd/serve/dlna/testdata/files/subdir2/Subs/video.srt @@ -0,0 +1,3 @@ +1 +00:00:00,000 --> 00:02:00,000 +Test diff --git a/cmd/serve/dlna/testdata/files/subdir2/video.mp4 b/cmd/serve/dlna/testdata/files/subdir2/video.mp4 new file mode 100644 index 000000000..7f6eeace9 Binary files /dev/null and b/cmd/serve/dlna/testdata/files/subdir2/video.mp4 differ