Files

115 lines
4.8 KiB
Go

package arr
import (
"encoding/json"
"reflect"
"testing"
)
func attrs(t *testing.T, raw string) FileAttrs {
t.Helper()
var f MediaFile
if err := json.Unmarshal([]byte(raw), &f); err != nil {
t.Fatal(err)
}
return f.Attrs()
}
func TestAttrsFromMediaInfo(t *testing.T) {
a := attrs(t, `{"quality":{"quality":{"name":"Bluray-2160p","resolution":2160}},
"mediaInfo":{"videoCodec":"x265","videoDynamicRange":"HDR","videoDynamicRangeType":"DV HDR10"},
"languages":[{"name":"English"},{"name":"German"}]}`)
want := FileAttrs{Resolution: 2160, Codec: CodecX265, HDR: true, DV: true, Languages: []string{"English", "German"}}
if !reflect.DeepEqual(a, want) {
t.Errorf("got %+v, want %+v", a, want)
}
}
func TestAttrsVariants(t *testing.T) {
for _, tc := range []struct {
name, raw string
check func(FileAttrs) bool
}{
{"hevc codec", `{"mediaInfo":{"videoCodec":"HEVC"}}`, func(a FileAttrs) bool { return a.Codec == CodecX265 }},
{"h264 codec", `{"mediaInfo":{"videoCodec":"h264"}}`, func(a FileAttrs) bool { return a.Codec == CodecX264 }},
{"avc codec", `{"mediaInfo":{"videoCodec":"AVC"}}`, func(a FileAttrs) bool { return a.Codec == CodecX264 }},
{"av1 codec", `{"mediaInfo":{"videoCodec":"AV1"}}`, func(a FileAttrs) bool { return a.Codec == CodecAV1 }},
{"unoffered codec is not guessed from the name", `{"sceneName":"x264.Movie","mediaInfo":{"videoCodec":"VC-1"}}`, func(a FileAttrs) bool { return a.Codec == "" }},
{"codec from scene name", `{"sceneName":"Movie.2020.1080p.BluRay.x265-GRP"}`, func(a FileAttrs) bool { return a.Codec == CodecX265 && a.Resolution == 1080 }},
{"dv from path", `{"relativePath":"Movie.2160p.DV.HDR10.mkv"}`, func(a FileAttrs) bool { return a.DV && a.HDR && a.Resolution == 2160 }},
{"dv only", `{"mediaInfo":{"videoDynamicRangeType":"DV"}}`, func(a FileAttrs) bool { return a.DV && !a.HDR }},
{"hlg is hdr", `{"mediaInfo":{"videoDynamicRangeType":"HLG"}}`, func(a FileAttrs) bool { return a.HDR && !a.DV }},
{"sdr", `{"mediaInfo":{"videoCodec":"x264"},"sceneName":"Movie.HDRip.x264"}`, func(a FileAttrs) bool { return !a.HDR && !a.DV }},
{"hdr word in a title is not enough with media info", `{"relativePath":"Hdr.Story.mkv","mediaInfo":{"videoDynamicRange":"SDR"}}`, func(a FileAttrs) bool { return !a.HDR }},
} {
if !tc.check(attrs(t, tc.raw)) {
t.Errorf("%s: got %+v", tc.name, attrs(t, tc.raw))
}
}
}
func TestReleaseAttrs(t *testing.T) {
var r Release
raw := `{"title":"Dune.2021.2160p.UHD.BluRay.DV.HDR10.x265-GRP","quality":{"quality":{"name":"Bluray-2160p","resolution":2160}},
"languages":[{"name":"English"}],"customFormats":[{"name":"HDR"}]}`
if err := json.Unmarshal([]byte(raw), &r); err != nil {
t.Fatal(err)
}
want := FileAttrs{Resolution: 2160, Codec: CodecX265, HDR: true, DV: true, Languages: []string{"English"}}
if got := r.Attrs(); !reflect.DeepEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
// Resolution from the title when the quality carries none; codec spelled H.264.
r = Release{Title: "Show.S01E01.720p.WEB.H.264-GRP"}
if got := r.Attrs(); got.Resolution != 720 || got.Codec != CodecX264 || got.HDR || got.DV {
t.Errorf("got %+v", got)
}
// "HDTV" and "HDRip" are not HDR.
r = Release{Title: "Show.S01E01.HDTV.x264-GRP"}
if got := r.Attrs(); got.HDR || got.DV {
t.Errorf("HDTV detected as HDR: %+v", got)
}
}
func TestAudioTags(t *testing.T) {
for _, tc := range []struct {
in string
want []string
}{
{"Movie.2160p.UHD.BluRay.TrueHD.7.1.Atmos.x265-G", []string{"Atmos", "TrueHD"}},
{"Movie.1080p.DDP5.1.Atmos.x265", []string{"Atmos", "DD+"}},
{"Movie.1080p.DTS-HD.MA.5.1.x264", []string{"DTS-HD", "DTS"}},
{"Movie.2160p.DTS-X.7.1", []string{"DTS:X", "DTS"}},
{"Movie.1080p.DTS.5.1", []string{"DTS"}},
{"Movie.1080p.EAC3.5.1", []string{"DD+"}},
{"Movie.1080p.E-AC-3.5.1", []string{"DD+"}},
{"Movie.1080p.AC3.5.1", []string{"DD"}},
{"Movie.720p.DD5.1.x264", []string{"DD"}},
{"Show.S01E01.AAC.2.0", []string{"AAC"}},
{"Album.FLAC", []string{"FLAC"}},
{"Show.Opus", []string{"Opus"}},
{"Movie.1080p.BluRay.x264-DTSAG", nil}, // a group name is not an audio format
{"Movie.1080p.x264", nil},
} {
if got := audioTags(tc.in); !reflect.DeepEqual(got, tc.want) {
t.Errorf("audioTags(%q) = %v, want %v", tc.in, got, tc.want)
}
}
}
func TestMediaFileAudio(t *testing.T) {
a := attrs(t, `{"sceneName":"Movie.x264.AC3","mediaInfo":{"audioCodec":"TrueHD Atmos"}}`)
if !reflect.DeepEqual(a.Audio, []string{"Atmos", "TrueHD"}) {
t.Errorf("media info wins over the name: %v", a.Audio)
}
a = attrs(t, `{"mediaInfo":{"audioCodec":"EAC3","audioAdditionalFeatures":"Atmos"}}`)
if !reflect.DeepEqual(a.Audio, []string{"Atmos", "DD+"}) {
t.Errorf("additional features: %v", a.Audio)
}
a = attrs(t, `{"relativePath":"Movie.1080p.DTS-HD.MA.mkv"}`)
if !reflect.DeepEqual(a.Audio, []string{"DTS-HD", "DTS"}) {
t.Errorf("fallback to the name: %v", a.Audio)
}
}