2016-01-20 02:11:20 +00:00
import moment from 'moment-timezone' ;
2015-12-10 22:52:09 +00:00
import { calcCurrentStreak , calcLongestStreak } from '../../../server/utils/user-stats' ;
let test = require ( 'tape' ) ;
test ( 'Current streak calculation' , function ( t ) {
2016-01-20 02:11:20 +00:00
t . plan ( 9 ) ;
2015-12-10 22:52:09 +00:00
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 1 , "should return 1 day when today one challenge was completed" ) ;
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
] ) , 1 , "should return 1 day when today more than one challenge was completed" ) ;
2015-12-10 22:52:09 +00:00
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 0 , "should return 0 day when today 0 challenges were completed" ) ;
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 1 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 2 , "should return 2 days when today and yesterday challenges were completed" ) ;
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "8/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 3:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/13/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/14/2015 5:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 2 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 3 , "should return 3 when today and for two days before user was activity" ) ;
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 37 , 'hours' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 1 , "should return 1 when between todays challenge completion and yesterdays there is a 1.5 day (36 hours) long break" ) ;
2016-01-20 02:11:20 +00:00
t . ok ( calcCurrentStreak ( [
moment . utc ( moment . utc ( ) . subtract ( 35 , 'hours' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
] ) >= 2 , "should return not less than 2 days when between todays challenge completion and yesterdays there is less than 1.5 day (36 hours) long break" ) ;
t . equal ( calcCurrentStreak ( [
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
] , undefined ) , 1 , "should return correct count in default timezone UTC given 'undefined' timezone" ) ;
2015-12-10 22:52:09 +00:00
t . equal ( calcCurrentStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 1 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
] , 'America/Los_Angeles' ) , 2 , "should return 2 days when today and yesterday challenges were completed given 'America/Los_Angeles' timezone" ) ;
2015-12-10 22:52:09 +00:00
} ) ;
test ( 'Longest streak calculation' , function ( t ) {
2016-01-20 02:11:20 +00:00
t . plan ( 12 ) ;
2015-12-10 22:52:09 +00:00
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "9/12/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 1 , "should return 1 when there is the only one one-day-long streak available" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/13/2015 3:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/14/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 4 , "should return 4 when there is the only one more-than-one-days-long streak available" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 1 , "should return 1 when there is only one one-day-long streak and it is today" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( moment . utc ( ) . subtract ( 1 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 2 , "should return 2 when yesterday and today makes longest streak" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "8/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/4/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/5/2015 5:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/6/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/7/2015 5:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "11/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 4 , "should return 4 when there is a month long break" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "8/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . add ( 37 , 'hours' ) ) . valueOf ( ) ,
moment . utc ( "9/14/2015 22:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/15/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 3 , "should return 3 when there is a more than 1.5 days long break of (36 hours)" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "8/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 3:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/13/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/14/2015 5:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 2 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'days' ) ) . valueOf ( ) ,
moment . utc ( ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 4 , "should return 4 when the longest streak consist of several same day timestamps" ) ;
t . equal ( calcLongestStreak ( [
2016-01-20 02:11:20 +00:00
moment . utc ( "8/3/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/13/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/14/2015 5:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/12/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/13/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "10/14/2015 5:00" , "M/D/YYYY H:mm" ) . valueOf ( )
2015-12-10 22:52:09 +00:00
] ) , 4 , "should return 4 when there are several longest streaks (same length)" ) ;
let cals = [ ] ;
const n = 100 ;
for ( var i = 0 ; i < n ; i ++ ) {
2016-01-20 02:11:20 +00:00
cals . push ( moment . utc ( moment . utc ( ) . subtract ( i , 'days' ) ) . valueOf ( ) ) ;
2015-12-10 22:52:09 +00:00
}
cals . sort ( ) ;
t . equal ( calcLongestStreak ( cals ) , n , "should return correct longest streak when there is a very long period" ) ;
2016-01-20 02:11:20 +00:00
t . equal ( calcLongestStreak ( [
moment . utc ( moment . utc ( ) . subtract ( 1 , 'days' ) ) . valueOf ( ) ,
moment . utc ( moment . utc ( ) . subtract ( 1 , 'hours' ) ) . valueOf ( )
] , undefined ) , 2 , "should return correct longest streak in default timezone UTC given 'undefined' timezone" ) ;
t . equal ( calcLongestStreak ( [
moment . utc ( "9/11/2015 4:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/13/2015 3:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/14/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( )
] , 'America/Los_Angeles' ) , 4 , "should return 4 when there is the only one more-than-one-days-long streak available given 'America/Los_Angeles' timezone" ) ;
t . equal ( calcLongestStreak ( [
moment . utc ( "9/11/2015 23:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/12/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/13/2015 2:00" , "M/D/YYYY H:mm" ) . valueOf ( ) ,
moment . utc ( "9/14/2015 1:00" , "M/D/YYYY H:mm" ) . valueOf ( )
] , 'America/Los_Angeles' ) , 3 , "should return 3 when longest streak is 3 in given 'America/Los_Angeles' timezone (but would be different in default timezone UTC)" ) ;
2015-12-10 22:52:09 +00:00
} ) ;