Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Nov 6, 2017
1 parent 54fed45 commit d3fb149
Show file tree
Hide file tree
Showing 8 changed files with 505 additions and 32 deletions.
29 changes: 18 additions & 11 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const request = require('../../../js/lib/request')
const ledgerUtil = require('../../common/lib/ledgerUtil')
const tabState = require('../../common/state/tabState')
const pageDataUtil = require('../../common/lib/pageDataUtil')
const ledgerVideoCahce = require('../../common/cache/ledgerVideoCache')
const ledgerVideoCache = require('../../common/cache/ledgerVideoCache')

// Caching
let locationDefault = 'NOOP'
Expand Down Expand Up @@ -567,7 +567,7 @@ const getPublisherData = (result, scorekeeper) => {
weight: result.pinPercentage
}

data.publisherURL = result.publisherURL || (result.protocol || 'http:') + '//' + result.publisherKey
data.publisherURL = result.publisherURL || ((result.protocol || 'http:') + '//' + result.publisherKey)

// media publisher
if (result.faviconName) {
Expand Down Expand Up @@ -940,7 +940,7 @@ const saveVisit = (state, publisherKey, duration, revisited) => {
synopsis.addPublisher(publisherKey, {duration: duration, revisitP: revisited})
state = ledgerState.setPublisher(state, publisherKey, synopsis.publishers[publisherKey])
state = updatePublisherInfo(state)
state = verifiedP(state, publisherKey, (error, result) => {
state = module.exports.verifiedP(state, publisherKey, (error, result) => {
if (!error) {
appActions.onPublisherOptionUpdate(publisherKey, 'verified', result)
savePublisherOption(publisherKey, 'verified', result)
Expand Down Expand Up @@ -2345,7 +2345,7 @@ const saveOptionSynopsis = (prop, value) => {
}

const savePublisherOption = (publisherKey, prop, value) => {
if (synopsis.publishers && synopsis.publishers[publisherKey]) {
if (synopsis.publishers && synopsis.publishers[publisherKey] && synopsis.publishers[publisherKey].options) {
synopsis.publishers[publisherKey].options[prop] = value
}
}
Expand Down Expand Up @@ -2474,7 +2474,7 @@ const transitionWalletToBat = () => {

let currentMediaKey = null
const onMediaRequest = (state, xhr, type) => {
if (!xhr) {
if (!xhr || type == null) {
return state
}

Expand All @@ -2497,10 +2497,10 @@ const onMediaRequest = (state, xhr, type) => {
currentMediaKey = mediaKey
}

const cache = ledgerVideoCahce.getDataByVideoId(state, mediaKey)
const cache = ledgerVideoCache.getDataByVideoId(state, mediaKey)

if (!cache.isEmpty()) {
return saveVisit(state, cache.get('publisher'), duration, revisited)
return module.exports.saveVisit(state, cache.get('publisher'), duration, revisited)
}

const options = underscore.extend({roundtrip: roundtrip}, clientOptions)
Expand All @@ -2527,7 +2527,7 @@ const onMediaRequest = (state, xhr, type) => {
}

const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
const publisherKey = response.get('publisher')
const publisherKey = response ? response.get('publisher') : null
if (publisherKey == null) {
return state
}
Expand Down Expand Up @@ -2567,14 +2567,16 @@ const onMediaPublisher = (state, mediaKey, response, duration, revisited) => {
} else {
synopsis.publishers[publisherKey].faviconName = faviconName
synopsis.publishers[publisherKey].faviconURL = faviconURL
synopsis.publishers[publisherKey].publisherURL = publisherURL
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconName', faviconName)
state = ledgerState.setPublishersProp(state, publisherKey, 'faviconURL', faviconURL)
state = ledgerState.setPublishersProp(state, publisherKey, 'publisherURL', publisherURL)
}

// Add to cache
state = ledgerVideoCahce.setCacheByVideoId(state, mediaKey, response)
state = ledgerVideoCache.setCacheByVideoId(state, mediaKey, response)

state = saveVisit(state, publisherKey, duration, revisited)
state = module.exports.saveVisit(state, publisherKey, duration, revisited)

return state
}
Expand Down Expand Up @@ -2614,7 +2616,8 @@ const getMethods = () => {
getNewClient,
savePublisherData,
onMediaRequest,
onMediaPublisher
onMediaPublisher,
saveVisit
}

let privateMethods = {}
Expand All @@ -2633,6 +2636,10 @@ const getMethods = () => {
setSynopsis: (data) => {
synopsis = data
},
setCurrentMediaKey: (key) => {
currentMediaKey = key
},
getCurrentMediaKey: (key) => currentMediaKey,
synopsisNormalizer,
pruneSynopsis
}
Expand Down
14 changes: 14 additions & 0 deletions app/common/cache/ledgerVideoCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const Immutable = require('immutable')
const assert = require('assert')

const { makeImmutable, isMap } = require('../state/immutableUtil')

const validateState = function (state) {
state = makeImmutable(state)
assert.ok(isMap(state), 'state must be an Immutable.Map')
assert.ok(isMap(state.getIn(['cache', 'ledgerVideos'])), 'state must contain ledgerVideos as Immutable.Map')
return state
}

const getDataByVideoId = (state, key) => {
state = validateState(state)
if (key == null) {
return Immutable.Map()
}
Expand All @@ -12,10 +23,13 @@ const getDataByVideoId = (state, key) => {
}

const setCacheByVideoId = (state, key, data) => {
state = validateState(state)
if (key == null) {
return state
}

data = makeImmutable(data)

return state.setIn(['cache', 'ledgerVideos', key], data)
}

Expand Down
68 changes: 48 additions & 20 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ const stickyP = (state, publisherKey) => {

const getMediaId = (data, type) => {
let id = null

if (type == null || data == null) {
return id
}

switch (type) {
case ledgerMediaProviders.YOUTUBE:
{
Expand All @@ -204,11 +209,16 @@ const getMediaKey = (id, type) => {
return null
}

return `${type.toLowerCase()}_${id.toLowerCase()}`
return `${type.toLowerCase()}_${id}`
}

const getMediaData = (xhr, type) => {
let result = null

if (xhr == null || type == null) {
return result
}

switch (type) {
case ledgerMediaProviders.YOUTUBE:
{
Expand Down Expand Up @@ -253,18 +263,22 @@ const getYoutubeDuration = (data) => {
}

for (let i = 0; i < startTime.length; i++) {
time += parseInt(endTime[i]) - parseInt(startTime[i])
time += parseFloat(endTime[i]) - parseFloat(startTime[i])
}

// we get seconds back, so we need to convert it into ms
time = time * 1000

return time
return parseInt(time)
}

const isMediaProvider = (url) => {
let provider = null

if (url == null) {
return provider
}

// Youtube
if (url.startsWith('https://www.youtube.com/api/stats/watchtime?')) {
provider = ledgerMediaProviders.YOUTUBE
Expand All @@ -273,21 +287,35 @@ const isMediaProvider = (url) => {
return provider
}

module.exports = {
shouldTrackView,
batToCurrencyString,
formattedTimeFromNow,
formattedDateFromTimestamp,
walletStatus,
blockedP,
contributeP,
visibleP,
eligibleP,
stickyP,
formatCurrentBalance,
getMediaId,
getMediaDuration,
isMediaProvider,
getMediaData,
getMediaKey
const getMethods = () => {
const publicMethods = {
shouldTrackView,
batToCurrencyString,
formattedTimeFromNow,
formattedDateFromTimestamp,
walletStatus,
blockedP,
contributeP,
visibleP,
eligibleP,
stickyP,
formatCurrentBalance,
getMediaId,
getMediaDuration,
isMediaProvider,
getMediaData,
getMediaKey
}

let privateMethods = {}

if (process.env.NODE_ENV === 'test') {
privateMethods = {
getYoutubeDuration
}
}

return Object.assign({}, publicMethods, privateMethods)
}

module.exports = getMethods()
3 changes: 2 additions & 1 deletion app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,8 @@ module.exports.defaultAppState = () => {
},
cache: {
bookmarkLocation: undefined,
bookmarkOrder: {}
bookmarkOrder: {},
ledgerVideos: {}
},
pinnedSites: {},
bookmarks: {},
Expand Down
3 changes: 3 additions & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ AppStore
order: number,
type: string // siteTags.BOOKMARK or siteTags.BOOKMARK_FOLDER
}]
},
ledgerVideos: {
[mediaKey]: string // publisher key
}
}
clearBrowsingDataDefaults: {
Expand Down
Loading

0 comments on commit d3fb149

Please sign in to comment.