@bigfishtv/cockpit
Version:
592 lines (522 loc) • 18.8 kB
JavaScript
import {
detectVideo,
extractYoutubeId,
extractYoutubePlaylistId,
extractVimeoId,
extractWistiaId,
buildEmbedUrl,
buildVideoUrl,
extractWistiaStartTime,
extractYouTubeStartTime,
extractVimeoStartTime,
extractVimeoPasscode,
} from './videoUtils.js'
describe('extracting youtube playlist ids', () => {
it('should detect a valid id', () => {
const input = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(extractYoutubePlaylistId(input)).toEqual(expected)
})
it('should extract from a playlist URL', () => {
const input = 'https://www.youtube.com/playlist?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(extractYoutubePlaylistId(input)).toEqual(expected)
})
it('should extract from a playlist + video URL', () => {
const input = 'https://www.youtube.com/watch?v=0dFpxsQS2K4&list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo&index=1'
const expected = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(extractYoutubePlaylistId(input)).toEqual(expected)
})
it('should extract from a shared video + playlist URL', () => {
const input = 'https://youtu.be/0dFpxsQS2K4?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(extractYoutubePlaylistId(input)).toEqual(expected)
})
it('should extract from a shared embed code', () => {
const input =
'<iframe width="560" height="315" src="https://www.youtube.com/embed/0dFpxsQS2K4?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo" frameborder="0" allowfullscreen></iframe>'
const expected = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(extractYoutubePlaylistId(input)).toEqual(expected)
})
it('should return false if playlist id not found', () => {
const input = 'https://www.youtube.com/watch?v=0dFpxsQS2K4'
const expected = false
expect(extractYoutubePlaylistId(input)).toEqual(expected)
})
})
describe('extracting youtube video ids', () => {
it('should detect a valid id', () => {
const input = 'RK2b7bWIhcQ'
const expected = 'RK2b7bWIhcQ'
expect(extractYoutubeId(input)).toEqual(expected)
})
it('should extract from a video URL', () => {
const input = 'https://www.youtube.com/watch?v=RK2b7bWIhcQ'
const expected = 'RK2b7bWIhcQ'
expect(extractYoutubeId(input)).toEqual(expected)
})
it('should extract from a playlist + video URL', () => {
const input = 'https://www.youtube.com/watch?v=0dFpxsQS2K4&list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo&index=1'
const expected = '0dFpxsQS2K4'
expect(extractYoutubeId(input)).toEqual(expected)
})
it('should extract from a shared video + playlist URL', () => {
const input = 'https://youtu.be/0dFpxsQS2K4?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = '0dFpxsQS2K4'
expect(extractYoutubeId(input)).toEqual(expected)
})
it('should extract from a shared embed code', () => {
const input =
'<iframe width="560" height="315" src="https://www.youtube.com/embed/0dFpxsQS2K4?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo" frameborder="0" allowfullscreen></iframe>'
const expected = '0dFpxsQS2K4'
expect(extractYoutubeId(input)).toEqual(expected)
})
it('should return false if video id not found', () => {
const input = 'https://www.youtube.com/playlist?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = false
expect(extractYoutubeId(input)).toEqual(expected)
})
})
describe('extracting vimeo video ids', () => {
it('should detect a valid id', () => {
const input = '146865820'
const expected = '146865820'
expect(extractVimeoId(input)).toEqual(expected)
})
it('should extract from a video URL', () => {
const input = 'https://vimeo.com/146865820'
const expected = '146865820'
expect(extractVimeoId(input)).toEqual(expected)
})
it('should extract from a video URL with start time', () => {
const input = 'https://vimeo.com/94957294#t=300s'
const expected = '94957294'
expect(extractVimeoId(input)).toEqual(expected)
})
it('should extract from a shared embed code', () => {
const input =
'<iframe src="https://player.vimeo.com/video/94957294" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <p><a href="https://vimeo.com/94957294">Domestic Appliances</a> from <a href="https://vimeo.com/user14632340">Lewis Firth Bolton</a> on <a href="https://vimeo.com">Vimeo</a>.</p>'
const expected = '94957294'
expect(extractVimeoId(input)).toEqual(expected)
})
it('should extract from a embed url', () => {
const input = 'https://player.vimeo.com/video/94957294'
const expected = '94957294'
expect(extractVimeoId(input)).toEqual(expected)
})
it('should return false if video id not found', () => {
const input = 'https://www.youtube.com/playlist?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = false
expect(extractVimeoId(input)).toEqual(expected)
})
})
describe('extracting vimeo video passcodes', () => {
it('should detect passcode from link', () => {
const input = 'https://vimeo.com/624996930/e6dae8e87d'
const expected = 'e6dae8e87d'
expect(extractVimeoPasscode(input)).toEqual(expected)
})
it('should detect passcode from embed', () => {
const input =
'<div style="padding:66.67% 0 0 0;position:relative;"><iframe src="https://player.vimeo.com/video/624996930?h=e6dae8e87d&badge=0&autopause=0&player_id=0&app_id=58479" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen style="position:absolute;top:0;left:0;width:100%;height:100%;" title="login-720p"></iframe></div><script src="https://player.vimeo.com/api/player.js"></script>'
const expected = 'e6dae8e87d'
expect(extractVimeoPasscode(input)).toEqual(expected)
})
it("should return false if it can't detect a passcode", () => {
const input = 'https://vimeo.com/624996930'
const expected = false
expect(extractVimeoPasscode(input)).toEqual(expected)
})
})
describe('extracting wistia video ids', () => {
it('should detect a valid id from div', () => {
const input =
'<div class="wistia_embed wistia_async_vud7ff4i6w controlsVisibleOnLoad=false playButton=true playerColor=40aaef" style="height:411px;width:730px"> </div>'
const expected = 'vud7ff4i6w'
expect(extractWistiaId(input)).toEqual(expected)
})
it('should detect a valid id from url', () => {
const input = 'https://hutchinsonbuilders.wistia.com/medias/mkiw1zkr0x'
const expected = 'mkiw1zkr0x'
expect(extractWistiaId(input)).toEqual(expected)
})
it('should detect a valid id from iframe embed', () => {
const input =
'<iframe src="//fast.wistia.net/embed/iframe/sztcrbdhr2?controlsVisibleOnLoad=false&playButton=true&playerColor=40aaef" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" allowfullscreen="allowfullscreen" mozallowfullscreen="mozallowfullscreen" webkitallowfullscreen="webkitallowfullscreen" oallowfullscreen="oallowfullscreen" msallowfullscreen="msallowfullscreen" width="730" height="411"></iframe>'
const expected = 'sztcrbdhr2'
expect(extractWistiaId(input)).toEqual(expected)
})
it('should return false if video id not found', () => {
const input = 'https://www.youtube.com/playlist?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = false
expect(extractWistiaId(input)).toEqual(expected)
})
})
describe('detect vimeo, youtube or playlist ids', () => {
it('should detect youtube video id', () => {
const input = 'RK2b7bWIhcQ'
const expected = {
type: 'youtube',
playlist_id: null,
video_id: 'RK2b7bWIhcQ',
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should detect youtube playlist id', () => {
const input = 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
const expected = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
video_id: false,
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should detect youtube video id from url', () => {
const input = 'https://www.youtube.com/watch?v=RK2b7bWIhcQ'
const expected = {
type: 'youtube',
playlist_id: null,
video_id: 'RK2b7bWIhcQ',
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should detect youtube video + playlist id from url', () => {
const input = 'https://www.youtube.com/watch?v=0dFpxsQS2K4&list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo&index=1'
const expected = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
video_id: '0dFpxsQS2K4',
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should detect a valid vimeo id', () => {
const input = '146865820'
const expected = {
type: 'vimeo',
playlist_id: null,
video_id: '146865820',
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should extract from a vimeo video URL', () => {
const input = 'https://vimeo.com/146865820'
const expected = {
type: 'vimeo',
playlist_id: null,
video_id: '146865820',
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should extract from a wistia video URL', () => {
const input = 'https://hutchinsonbuilders.wistia.com/medias/mkiw1zkr0x'
const expected = {
type: 'wistia',
playlist_id: null,
video_id: 'mkiw1zkr0x',
start_time: 0,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should extract from a wistia video URL with start time', () => {
const input = 'https://fast.wistia.net/embed/iframe/2wt9l0b9it?wtime=4m14s'
const expected = {
type: 'wistia',
playlist_id: null,
video_id: '2wt9l0b9it',
start_time: 254,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should extract from a wistia video URL with start time (time param)', () => {
const input = 'https://fast.wistia.net/embed/iframe/2wt9l0b9it?time=4m14s'
const expected = {
type: 'wistia',
playlist_id: null,
video_id: '2wt9l0b9it',
start_time: 254,
passcode: null,
}
expect(detectVideo(input)).toEqual(expected)
})
it('should return false if invalid', () => {
const input = 'https://google.com'
const expected = false
expect(detectVideo(input)).toEqual(expected)
})
it('should extract from a vimeo video URL with passcode', () => {
const input = 'https://vimeo.com/624996930/e6dae8e87d'
const expected = {
type: 'vimeo',
playlist_id: null,
video_id: '624996930',
start_time: 0,
passcode: 'e6dae8e87d',
}
expect(detectVideo(input)).toEqual(expected)
})
})
describe('build embed url', () => {
it('should build from youtube video id', () => {
const input = {
type: 'youtube',
video_id: 'RK2b7bWIhcQ',
start_time: 0,
}
const expected = 'https://www.youtube.com/embed/RK2b7bWIhcQ'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from youtube video id with start time', () => {
const input = {
type: 'youtube',
video_id: 'RK2b7bWIhcQ',
start_time: 200,
}
const expected = 'https://www.youtube.com/embed/RK2b7bWIhcQ?t=200'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from youtube playlist id', () => {
const input = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
}
const expected = 'https://www.youtube.com/embed?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from youtube video + playlist id', () => {
const input = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
video_id: 'oVxH_NGzqi0',
}
const expected = 'https://www.youtube.com/embed/oVxH_NGzqi0?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from vimeo id', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
}
const expected = 'https://player.vimeo.com/video/94957294'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from vimeo id with start time', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
start_time: 240,
}
const expected = 'https://player.vimeo.com/video/94957294#t=240'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from vimeo id and passcode', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
passcode: 'aef123c23',
}
const expected = 'https://player.vimeo.com/video/94957294?h=aef123c23'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from vimeo id and passcode and start time', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
start_time: 240,
passcode: 'aef123c23',
}
const expected = 'https://player.vimeo.com/video/94957294?h=aef123c23#t=240'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from wistia id', () => {
const input = {
type: 'wistia',
video_id: 'mkiw1zkr0x',
}
const expected = 'https://fast.wistia.net/embed/iframe/mkiw1zkr0x'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should build from wistia id with start time', () => {
const input = {
type: 'wistia',
video_id: 'mkiw1zkr0x',
start_time: 300,
}
const expected = 'https://fast.wistia.net/embed/iframe/mkiw1zkr0x?time=300'
expect(buildEmbedUrl(input)).toEqual(expected)
})
it('should return false if invalid args', () => {
const input = {
type: 'blablah',
video_id: '94957294',
}
const expected = false
expect(buildEmbedUrl(input)).toEqual(expected)
})
})
describe('build video url', () => {
it('should build from youtube video id', () => {
const input = {
type: 'youtube',
video_id: 'Rt7Mmef7nQI',
}
const expected = 'https://www.youtube.com/watch?v=Rt7Mmef7nQI'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from youtube video id with start time', () => {
const input = {
type: 'youtube',
video_id: 'Rt7Mmef7nQI',
start_time: 200,
}
const expected = 'https://www.youtube.com/watch?v=Rt7Mmef7nQI&t=200'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from youtube playlist id', () => {
const input = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
}
const expected = 'https://www.youtube.com/playlist?list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from youtube video + playlist id', () => {
const input = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
video_id: 'tlJ6TT0iW88',
}
const expected = 'https://www.youtube.com/watch?v=tlJ6TT0iW88&list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from youtube video + playlist id with start time', () => {
const input = {
type: 'youtube',
playlist_id: 'PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo',
video_id: 'tlJ6TT0iW88',
start_time: 300,
}
const expected = 'https://www.youtube.com/watch?v=tlJ6TT0iW88&list=PLCC436JpVnK1xiwpa1yT8NLI7VSQB6Hdo&t=300'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from vimeo id', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
}
const expected = 'https://vimeo.com/94957294'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from vimeo id with start time', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
start_time: 300,
}
const expected = 'https://vimeo.com/94957294#t=300'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from vimeo id and passcode', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
passcode: 'ad67f2a1sd',
}
const expected = 'https://vimeo.com/94957294/ad67f2a1sd'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from vimeo id and passcode with start time', () => {
const input = {
type: 'vimeo',
video_id: '94957294',
start_time: 240,
passcode: 'ad67f2a1sd',
}
const expected = 'https://vimeo.com/94957294/ad67f2a1sd#t=240'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from wistia id (iframe embed code)', () => {
const input = {
type: 'wistia',
video_id: 'mkiw1zkr0x',
}
const expected = 'https://fast.wistia.net/embed/iframe/mkiw1zkr0x'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should build from wistia id (iframe embed code) with start time', () => {
const input = {
type: 'wistia',
video_id: 'mkiw1zkr0x',
start_time: 140,
}
const expected = 'https://fast.wistia.net/embed/iframe/mkiw1zkr0x?time=140'
expect(buildVideoUrl(input)).toEqual(expected)
})
it('should return false if invalid args', () => {
const input = {
type: 'blablah',
video_id: '94957294',
}
const expected = false
expect(buildVideoUrl(input)).toEqual(expected)
})
})
describe('extractWistiaStartTime', () => {
it('should parse int from mins/seconds', () => {
const time = extractWistiaStartTime('https://fast.wistia.net/embed/iframe/2wt9l0b9it?wtime=4m14s')
expect(time).toEqual(254)
})
it('should parse int from seconds', () => {
const time = extractWistiaStartTime('https://fast.wistia.net/embed/iframe/2wt9l0b9it?wtime=254')
expect(time).toEqual(254)
})
it('should parse int from mins/seconds (time param)', () => {
const time = extractWistiaStartTime('https://fast.wistia.net/embed/iframe/2wt9l0b9it?time=4m14s')
expect(time).toEqual(254)
})
it('should parse int from seconds (time param)', () => {
const time = extractWistiaStartTime('https://fast.wistia.net/embed/iframe/2wt9l0b9it?time=254')
expect(time).toEqual(254)
})
it('should return 0 if no time', () => {
const time = extractWistiaStartTime('https://fast.wistia.net/embed/iframe/2wt9l0b9it')
expect(time).toEqual(0)
})
})
describe('extractYouTubeStartTime', () => {
it('should parse int from seconds', () => {
const time = extractYouTubeStartTime('https://youtu.be/yXF8Lhvjqa8?t=260')
expect(time).toEqual(260)
})
it('should return 0 if no time', () => {
const time = extractYouTubeStartTime('https://youtu.be/yXF8Lhvjqa8')
expect(time).toEqual(0)
})
})
describe('extractVimeoStartTime', () => {
it('should parse int from mins/seconds', () => {
const time = extractVimeoStartTime('https://vimeo.com/81400335#t=1m5s')
expect(time).toEqual(65)
})
it('should parse int from seconds', () => {
const time = extractVimeoStartTime('https://vimeo.com/81400335#t=65')
expect(time).toEqual(65)
})
it('should return 0 if no time', () => {
const time = extractVimeoStartTime('https://vimeo.com/81400335')
expect(time).toEqual(0)
})
})