2016-01-20 02:11:20 +00:00
|
|
|
import moment from 'moment-timezone';
|
2016-02-09 17:10:18 +00:00
|
|
|
import sinon from 'sinon';
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-04-03 03:52:52 +00:00
|
|
|
import {
|
2016-02-09 17:10:18 +00:00
|
|
|
prepUniqueDays,
|
|
|
|
calcCurrentStreak,
|
|
|
|
calcLongestStreak
|
|
|
|
} from '../../../server/utils/user-stats';
|
2015-12-10 22:52:09 +00:00
|
|
|
|
|
|
|
let test = require('tape');
|
2016-02-09 17:10:18 +00:00
|
|
|
let clock = sinon.useFakeTimers(1454526000000); // setting now to 2016-02-03T11:00:00 (PST)
|
|
|
|
const PST = 'America/Los_Angeles';
|
|
|
|
|
|
|
|
test('Prepare calendar items', function (t) {
|
|
|
|
|
|
|
|
t.plan(5);
|
|
|
|
|
|
|
|
t.deepEqual(prepUniqueDays([
|
|
|
|
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc("8/3/2015 14:00", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc("8/3/2015 20:00", "M/D/YYYY H:mm").valueOf()
|
|
|
|
]), [1438560000000], "should return correct epoch when all entries fall into one day in UTC");
|
|
|
|
|
|
|
|
t.deepEqual(prepUniqueDays([
|
|
|
|
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf()
|
|
|
|
]), [1438560000000], "should return correct epoch when given two identical dates");
|
|
|
|
|
|
|
|
|
|
|
|
t.deepEqual(prepUniqueDays([
|
|
|
|
moment.utc("8/3/2015 2:00", "M/D/YYYY H:mm").valueOf(), // 8/2/2015 in America/Los_Angeles
|
|
|
|
moment.utc("8/3/2015 14:00", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc("8/3/2015 20:00", "M/D/YYYY H:mm").valueOf()
|
|
|
|
], PST), [1438498800000, 1438585200000], "should return 2 epochs when dates fall into two days in PST");
|
|
|
|
|
|
|
|
t.deepEqual(prepUniqueDays([
|
|
|
|
moment.utc("8/1/2015 2:00", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc("3/3/2015 14:00", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc("9/30/2014 20:00", "M/D/YYYY H:mm").valueOf()
|
|
|
|
]), [1412035200000, 1425340800000, 1438387200000], "should return 3 epochs when dates fall into three days");
|
|
|
|
|
|
|
|
t.deepEqual(prepUniqueDays([
|
|
|
|
1438387200000, 1425340800000, 1412035200000
|
|
|
|
]), [1412035200000, 1425340800000, 1438387200000], "should return same but sorted array if all input dates are start of day");
|
|
|
|
|
|
|
|
});
|
2015-12-10 22:52:09 +00:00
|
|
|
|
|
|
|
test('Current streak calculation', function (t) {
|
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.plan(11);
|
|
|
|
|
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 1, "should return 1 day when today one challenge was completed");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 1, "should return 1 day when today more than one challenge was completed");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc("9/11/2015 4:00", "M/D/YYYY H:mm").valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 0, "should return 0 day when today 0 challenges were completed");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 2, "should return 2 days when today and yesterday challenges were completed");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 3, "should return 3 when today and for two days before user was activity");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
|
|
|
moment.utc(moment.utc().subtract(47, 'hours')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(11, 'hours')).valueOf()
|
|
|
|
])), 1, "should return 1 when there is 1.5 days long break and dates fall into two days separated by third");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
|
|
|
moment.utc(moment.utc().subtract(40, 'hours')).valueOf(),
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 2, "should return 2 when the break is more than 1.5 days but dates fall into two consecutive days");
|
2016-01-20 02:11:20 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
]), undefined), 1, "should return correct count in default timezone UTC given 'undefined' timezone");
|
|
|
|
|
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
], PST), PST), 2, "should return 2 days when today and yesterday challenges were completed given PST");
|
|
|
|
|
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
|
|
|
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453383782844,
|
|
|
|
1453431903117, 1453471373080, 1453594733026, 1453645014058, 1453746762747,
|
|
|
|
1453747659197, 1453748029416, 1453818029213, 1453951796007, 1453988570615,
|
|
|
|
1454069704441, 1454203673979, 1454294055498, 1454333545125, 1454415163903,
|
|
|
|
1454519128123, moment.tz(PST).valueOf()
|
|
|
|
], PST), PST), 17, "should return 17 when there is no break in given timezone (but would be the break if in UTC)");
|
|
|
|
|
|
|
|
t.equal(calcCurrentStreak(
|
|
|
|
prepUniqueDays([
|
|
|
|
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453383782844,
|
|
|
|
1453431903117, 1453471373080, 1453594733026, 1453645014058, 1453746762747,
|
|
|
|
1453747659197, 1453748029416, 1453818029213, 1453951796007, 1453988570615,
|
|
|
|
1454069704441, 1454203673979, 1454294055498, 1454333545125, 1454415163903,
|
|
|
|
1454519128123, moment.utc().valueOf()
|
|
|
|
])), 4, "should return 4 when there is a break in UTC (but would be no break in PST)");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Longest streak calculation', function (t) {
|
2016-02-09 17:10:18 +00:00
|
|
|
t.plan(14);
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc("9/12/2015 4:00", "M/D/YYYY H:mm").valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 1, "should return 1 when there is the only one one-day-long streak available");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 4, "should return 4 when there is the only one more-than-one-days-long streak available");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 1, "should return 1 when there is only one one-day-long streak and it is today");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 2, "should return 2 when yesterday and today makes longest streak");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 4, "should return 4 when there is a month long break");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
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(),
|
2016-02-09 17:10:18 +00:00
|
|
|
moment.utc("9/12/2015 15:30", "M/D/YYYY H:mm").valueOf(),
|
|
|
|
moment.utc(moment.utc("9/12/2015 15:30", "M/D/YYYY H:mm").add(37, 'hours')).valueOf(),
|
2016-01-20 02:11:20 +00:00
|
|
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 2, "should return 2 when there is a more than 1.5 days long break of (36 hours)");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 4, "should return 4 when the longest streak consist of several same day timestamps");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
])), 4, "should return 4 when there are several longest streaks (same length)");
|
2015-12-10 22:52:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(prepUniqueDays(cals)), n, "should return correct longest streak when there is a very long period");
|
2016-01-20 02:11:20 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
|
|
|
|
moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
|
2016-02-09 17:10:18 +00:00
|
|
|
]), undefined), 2, "should return correct longest streak in default timezone UTC given 'undefined' timezone");
|
2016-01-20 02:11:20 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
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()
|
2016-02-09 17:10:18 +00:00
|
|
|
]), PST), 4, "should return 4 when there is the only one more-than-one-days-long streak available given PST");
|
2016-01-20 02:11:20 +00:00
|
|
|
|
2016-02-09 17:10:18 +00:00
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
2016-01-20 02:11:20 +00:00
|
|
|
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(),
|
2016-02-09 17:10:18 +00:00
|
|
|
moment.utc("9/14/2015 6:00", "M/D/YYYY H:mm").valueOf()
|
|
|
|
], PST), PST), 3, "should return 3 when longest streak is 3 in PST (but would be different in default timezone UTC)");
|
|
|
|
|
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
|
|
|
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453383782844,
|
|
|
|
1453431903117, 1453471373080, 1453594733026, 1453645014058, 1453746762747,
|
|
|
|
1453747659197, 1453748029416, 1453818029213, 1453951796007, 1453988570615,
|
|
|
|
1454069704441, 1454203673979, 1454294055498, 1454333545125, 1454415163903,
|
|
|
|
1454519128123, moment.tz(PST).valueOf()
|
|
|
|
], PST), PST), 17, "should return 17 when there is no break in PST (but would be break in UTC) and it is current");
|
|
|
|
|
|
|
|
t.equal(calcLongestStreak(
|
|
|
|
prepUniqueDays([
|
|
|
|
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453383782844,
|
|
|
|
1453431903117, 1453471373080, 1453594733026, 1453645014058, 1453746762747,
|
|
|
|
1453747659197, 1453748029416, 1453818029213, 1453951796007, 1453988570615,
|
|
|
|
1454069704441, 1454203673979, 1454294055498, 1454333545125, 1454415163903,
|
|
|
|
1454519128123, moment.utc().valueOf()
|
|
|
|
])), 4, "should return 4 when there is a break in UTC (but no break in PST)");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
test.onFinish(() => {
|
|
|
|
clock.restore();
|
2015-12-10 22:52:09 +00:00
|
|
|
});
|