clipper/backend/youtube/helpers_test.go

82 lines
1.8 KiB
Go

package youtube_test
import (
"testing"
"git.netflux.io/rob/clipper/youtube"
youtubev2 "github.com/kkdai/youtube/v2"
"github.com/stretchr/testify/assert"
)
func TestSortAudio(t *testing.T) {
formats := []youtubev2.Format{
{
MimeType: `audio/webm; codecs="opus"`,
Bitrate: 350_000,
AudioChannels: 2,
AudioSampleRate: "16000",
},
{
MimeType: `audio/webm; codecs="opus"`,
Bitrate: 350_000,
AudioChannels: 2,
AudioSampleRate: "44100",
},
{
MimeType: `audio/mp4; codecs="mp4a.40.2"`,
Bitrate: 250_000,
AudioChannels: 2,
AudioSampleRate: "44100",
},
{
MimeType: `audio/webm; codecs="opus"`,
Bitrate: 125_000,
AudioChannels: 2,
AudioSampleRate: "44100",
},
}
sortedFormats := youtube.SortAudio(formats)
assert.Equal(t, formats[1], sortedFormats[0])
assert.Equal(t, formats[3], sortedFormats[1])
assert.Equal(t, formats[0], sortedFormats[2])
assert.Equal(t, formats[2], sortedFormats[3])
}
func TestSortVideo(t *testing.T) {
formats := []youtubev2.Format{
{
MimeType: `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
QualityLabel: "240p",
FPS: 30,
AudioChannels: 2,
},
{
MimeType: `audio/webm; codecs="opus"`,
QualityLabel: "",
FPS: 0,
AudioChannels: 2,
},
{
MimeType: `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
QualityLabel: "720p",
FPS: 30,
AudioChannels: 2,
},
{
MimeType: `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
QualityLabel: "360p",
FPS: 30,
AudioChannels: 2,
},
}
sortedFormats := youtube.SortVideo(formats)
assert.Len(t, sortedFormats, 3)
assert.Equal(t, formats[3], sortedFormats[0])
assert.Equal(t, formats[0], sortedFormats[1])
assert.Equal(t, formats[2], sortedFormats[2])
}