diff --git a/app/common/lib/ledgerUtil.js b/app/common/lib/ledgerUtil.js index 8ab59e1e795..3ae25ef25f9 100644 --- a/app/common/lib/ledgerUtil.js +++ b/app/common/lib/ledgerUtil.js @@ -224,7 +224,7 @@ const getMediaId = (data, type) => { } case ledgerMediaProviders.TWITCH: { - if (data.event === 'minute-watched') { + if (data.event === 'minute-watched' && data.properties) { id = data.properties.channel let vod = data.properties.vod @@ -271,15 +271,22 @@ const getMediaData = (xhr, type) => { { result = queryString.parse(parsedUrl.query) if (!result.data) { + result = null break } let obj = Buffer.from(result.data, 'base64').toString('utf8') if (obj == null) { + result = null break } - result = JSON.parse(obj) + try { + result = JSON.parse(obj) + } catch (error) { + result = null + console.error(error.toString()) + } break } } @@ -308,6 +315,12 @@ const getMediaDuration = (data, type) => { const getMediaFavicon = (providerName) => { let image = null + if (!providerName) { + return image + } + + providerName = providerName.toLowerCase() + switch (providerName) { case ledgerMediaProviders.YOUTUBE: { @@ -331,7 +344,7 @@ const getTwitchDuration = (data) => { return time } - if (data.properties.minutes_logged) { + if (data.properties && data.properties.minutes_logged) { time = 60 * 1000 // 1 min } @@ -426,7 +439,8 @@ const getMethods = () => { if (process.env.NODE_ENV === 'test') { privateMethods = { - getYouTubeDuration + getYouTubeDuration, + getTwitchDuration } } diff --git a/test/unit/app/common/lib/ledgerUtilTest.js b/test/unit/app/common/lib/ledgerUtilTest.js index a0d8d2ace1a..d2e058f5397 100644 --- a/test/unit/app/common/lib/ledgerUtilTest.js +++ b/test/unit/app/common/lib/ledgerUtilTest.js @@ -28,6 +28,8 @@ describe('ledgerUtil unit test', function () { mockery.registerMock('electron', fakeElectron) mockery.registerMock('ad-block', fakeAdBlock) mockery.registerMock('level', fakeLevel) + mockery.registerMock('../../../img/mediaProviders/youtube.png', 'youtube.png') + mockery.registerMock('../../../img/mediaProviders/twitch.svg', 'twitch.svg') mockery.registerMock('../../../js/settings', { getSetting: (settingKey) => { @@ -395,6 +397,48 @@ describe('ledgerUtil unit test', function () { assert.equal(result, 'kLiLOkzLetE') }) }) + + describe('Twitch', function () { + it('null case', function () { + const result = ledgerUtil.getMediaId(null, ledgerMediaProviders.TWITCH) + assert.equal(result, null) + }) + + it('event is not correct', function () { + const result = ledgerUtil.getMediaId({ + event: 'wrong' + }, ledgerMediaProviders.TWITCH) + assert.equal(result, null) + }) + + it('properties are missing', function () { + const result = ledgerUtil.getMediaId({ + event: 'minute-watched' + }, ledgerMediaProviders.TWITCH) + assert.equal(result, null) + }) + + it('content is a live stream', function () { + const result = ledgerUtil.getMediaId({ + event: 'minute-watched', + properties: { + channel: 'tchannel' + } + }, ledgerMediaProviders.TWITCH) + assert.equal(result, 'tchannel') + }) + + it('content is a vod', function () { + const result = ledgerUtil.getMediaId({ + event: 'minute-watched', + properties: { + channel: 'tchannel', + vod: 'v12343234' + } + }, ledgerMediaProviders.TWITCH) + assert.equal(result, 'tchannel_vod_12343234') + }) + }) }) describe('getMediaKey', function () { @@ -408,14 +452,16 @@ describe('ledgerUtil unit test', function () { assert.equal(result, null) }) - it('id is null', function () { - const result = ledgerUtil.getMediaKey(null, ledgerMediaProviders.YOUTUBE) - assert.equal(result, null) - }) + describe('YouTube', function () { + it('id is null', function () { + const result = ledgerUtil.getMediaKey(null, ledgerMediaProviders.YOUTUBE) + assert.equal(result, null) + }) - it('data is ok', function () { - const result = ledgerUtil.getMediaKey('kLiLOkzLetE', ledgerMediaProviders.YOUTUBE) - assert.equal(result, 'youtube_kLiLOkzLetE') + it('data is ok', function () { + const result = ledgerUtil.getMediaKey('kLiLOkzLetE', ledgerMediaProviders.YOUTUBE) + assert.equal(result, 'youtube_kLiLOkzLetE') + }) }) }) @@ -430,17 +476,17 @@ describe('ledgerUtil unit test', function () { assert.equal(result, null) }) + it('query is not present', function () { + const result = ledgerUtil.getMediaData('https://youtube.com', ledgerMediaProviders.YOUTUBE) + assert.equal(result, null) + }) + describe('Youtube', function () { it('null case', function () { const result = ledgerUtil.getMediaData(null, ledgerMediaProviders.YOUTUBE) assert.equal(result, null) }) - it('query is not present', function () { - const result = ledgerUtil.getMediaData('https://youtube.com', ledgerMediaProviders.YOUTUBE) - assert.equal(result, null) - }) - it('query is present', function () { const result = ledgerUtil.getMediaData('https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=11.338&et=21.339', ledgerMediaProviders.YOUTUBE) assert.deepEqual(result, { @@ -450,6 +496,33 @@ describe('ledgerUtil unit test', function () { }) }) }) + + describe('Twitch', function () { + it('null case', function () { + const result = ledgerUtil.getMediaData(null, ledgerMediaProviders.TWITCH) + assert.equal(result, null) + }) + + it('data is missing', function () { + const result = ledgerUtil.getMediaData('https://api.mixpanel.com', ledgerMediaProviders.TWITCH) + assert.equal(result, null) + }) + + it('data is empty string', function () { + const result = ledgerUtil.getMediaData('https://api.mixpanel.com?data=', ledgerMediaProviders.TWITCH) + assert.equal(result, null) + }) + + it('obj is parsed correctly', function () { + const result = ledgerUtil.getMediaData('https://api.mixpanel.com?data=eyJldmVudCI6Im1pbnV0ZS13YXRjaGVkIiwicHJvcGVydGllcyI6eyJjaGFubmVsIjoidHcifX0=', ledgerMediaProviders.TWITCH) + assert.deepEqual(result, { + event: 'minute-watched', + properties: { + channel: 'tw' + } + }) + }) + }) }) describe('getYouTubeDuration', function () { @@ -475,6 +548,27 @@ describe('ledgerUtil unit test', function () { }) }) + describe('getTwitchDuration', function () { + it('null case', function () { + const result = ledgerUtil.getTwitchDuration() + assert.equal(result, 0) + }) + + it('properties is missing', function () { + const result = ledgerUtil.getTwitchDuration({}) + assert.equal(result, 0) + }) + + it('minutes_logged is present', function () { + const result = ledgerUtil.getTwitchDuration({ + properties: { + minutes_logged: 1 + } + }) + assert.equal(result, 60000) + }) + }) + describe('getMediaProvider', function () { it('null case', function () { const result = ledgerUtil.getMediaProvider() @@ -490,6 +584,30 @@ describe('ledgerUtil unit test', function () { const result = ledgerUtil.getMediaProvider('https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=11.338&et=21.339') assert.equal(result, ledgerMediaProviders.YOUTUBE) }) + + describe('twitch', function () { + it('we only have url', function () { + const result = ledgerUtil.getMediaProvider('https://api.mixpanel.com?data=lll') + assert.equal(result, null) + }) + + it('video is on twitch.tv', function () { + const result = ledgerUtil.getMediaProvider( + 'https://api.mixpanel.com?data=lll', + 'https://www.twitch.tv' + ) + assert.equal(result, ledgerMediaProviders.TWITCH) + }) + + it('video is embeded', function () { + const result = ledgerUtil.getMediaProvider( + 'https://api.mixpanel.com?data=lll', + 'https://www.site.tv', + 'https://player.twitch.tv' + ) + assert.equal(result, ledgerMediaProviders.TWITCH) + }) + }) }) describe('milliseconds', function () { @@ -529,4 +647,21 @@ describe('ledgerUtil unit test', function () { assert.deepEqual(ledgerUtil.defaultMonthlyAmounts.toJS(), [5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0]) }) }) + + describe('getMediaFavicon', function () { + it('null case', function () { + const result = ledgerUtil.getMediaFavicon() + assert.equal(result, null) + }) + + it('youtube', function () { + const result = ledgerUtil.getMediaFavicon('YouTube') + assert.equal(result, 'youtube.png') + }) + + it('twitch', function () { + const result = ledgerUtil.getMediaFavicon('Twitch') + assert.equal(result, 'twitch.svg') + }) + }) })