From 3bd81abda39854142a946324968bcdb90adb1449 Mon Sep 17 00:00:00 2001 From: Cole Gleason Date: Wed, 8 Jan 2014 22:26:43 -0600 Subject: [PATCH 1/2] startOfDay: if between midnight and custom day start, jump back to previous day --- script/index.coffee | 6 ++++-- tests/algos.mocha.coffee | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/script/index.coffee b/script/index.coffee index 50a9f98b14..d37f2d342a 100644 --- a/script/index.coffee +++ b/script/index.coffee @@ -31,7 +31,10 @@ api.startOfWeek = api.startOfWeek = (options={}) -> api.startOfDay = (options={}) -> o = sanitizeOptions(options) - moment(o.now).startOf('day').add('h', o.dayStart) + dayStart = moment(o.now).startOf('day').add('h', o.dayStart) + # if between midnight and Custom Day Start, jump back to previous day + dayStart.subtract('day', 1) if moment(o.now).isBefore(dayStart) + return dayStart dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s'} @@ -1239,4 +1242,3 @@ api.wrap = (user, main=true) -> get: -> tasks = user.habits.concat(user.dailys).concat(user.todos).concat(user.rewards) _.object(_.pluck(tasks, "id"), tasks) - diff --git a/tests/algos.mocha.coffee b/tests/algos.mocha.coffee index a86a5f6301..94178e890b 100644 --- a/tests/algos.mocha.coffee +++ b/tests/algos.mocha.coffee @@ -487,6 +487,8 @@ describe 'Helper', -> expect(shared.startOfDay({now: new Date(2013, 0, 1, 0)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00' expect(shared.startOfDay({now: new Date(2013, 0, 1, 5)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00' expect(shared.startOfDay({now: new Date(2013, 0, 1, 23, 59, 59)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00' + # between midnight and custom day start + expect(shared.startOfDay({now: new Date(2013, 0, 2, 2), dayStart: 4}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 04:00' it 'counts pets', -> pets = {} From 393bbb18855f5f1c3ce896e01a1e9362d209b8c6 Mon Sep 17 00:00:00 2001 From: Cole Gleason Date: Wed, 8 Jan 2014 23:42:31 -0600 Subject: [PATCH 2/2] Modify runCron tests to use hoursAgo instead of daysAgo. This commit also sets hoursAgo for many of the cases and restructures how cronMatrix is parsed so that more specific settings may override defaults higher up on the tree. --- tests/algos.mocha.coffee | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/algos.mocha.coffee b/tests/algos.mocha.coffee index 94178e890b..be2b7a831d 100644 --- a/tests/algos.mocha.coffee +++ b/tests/algos.mocha.coffee @@ -46,7 +46,7 @@ rewrapUser = (user)-> expectStrings = (obj, paths) -> _.each paths, (path) -> expect(obj[path]).to.be.ok() -# options.daysAgo: days ago when the last cron was executed +# options.hoursAgo: hours since the last cron was executed beforeAfter = (options={}) -> user = newUser() [before, after] = [user, _.cloneDeep(user)] @@ -57,9 +57,9 @@ beforeAfter = (options={}) -> if options.limitOne before["#{options.limitOne}s"] = [before["#{options.limitOne}s"][0]] after["#{options.limitOne}s"] = [after["#{options.limitOne}s"][0]] - lastCron = +(moment(options.now || +new Date).subtract('days', options.daysAgo)) if options.daysAgo + lastCron = +(moment(options.now || +new Date).subtract('hours', options.hoursAgo)) unless options.hoursAgo == undefined _.each [before,after], (obj) -> - obj.lastCron = lastCron if options.daysAgo + obj.lastCron = lastCron unless options.hoursAgo == undefined {before:before, after:after} #TODO calculate actual poins @@ -287,7 +287,7 @@ describe 'Cron', -> # expect(paths.lastCron).to.be true # busted cron (was set to after today's date) it 'only dailies & todos are effected', -> - {before,after} = beforeAfter({daysAgo:1}) + {before,after} = beforeAfter({hoursAgo:24}) before.dailys = before.todos = after.dailys = after.todos = [] after.fns.cron() expect(after.lastCron).to.not.be before.lastCron # make sure cron was run @@ -304,7 +304,7 @@ describe 'Cron', -> @clock.restore() it 'should preen user history', -> - {before,after} = beforeAfter({daysAgo:1}) + {before,after} = beforeAfter({hoursAgo:24}) history = [ # Last year should be condensed to one entry, avg: 1 {date:'09/01/2012', value: 0} @@ -362,7 +362,7 @@ describe 'Cron', -> describe 'Todos', -> it '1 day missed', -> - {before,after} = beforeAfter({daysAgo:1}) + {before,after} = beforeAfter({hoursAgo:24}) before.dailys = after.dailys = [] after.fns.cron() @@ -388,7 +388,7 @@ describe 'Cron', -> runCron = (options) -> _.each [480, 240, 0, -120], (timezoneOffset) -> # test different timezones now = shared.startOfWeek({timezoneOffset}).add('hours', options.currentHour||0) - {before,after} = beforeAfter({now, timezoneOffset, daysAgo:1, dayStart:options.dayStart||0, limitOne:'daily'}) + {before,after} = beforeAfter({now, timezoneOffset, hoursAgo:options.hoursAgo||0, dayStart:options.dayStart||0, limitOne:'daily'}) before.dailys[0].repeat = after.dailys[0].repeat = options.repeat if options.repeat before.dailys[0].streak = after.dailys[0].streak = 10 before.dailys[0].completed = after.dailys[0].completed = true if options.checked @@ -405,7 +405,7 @@ describe 'Cron', -> steps: 'due yesterday': - defaults: {daysAgo:1, limitOne: 'daily'} + defaults: {hoursAgo:24, limitOne: 'daily'} steps: '(simple)': {expect:'losePoints'} @@ -415,7 +415,7 @@ describe 'Cron', -> defaults: {repeat:{su:1,m:true,t:1,w:1,th:1,f:1,s:1}} steps: 'pre-dayStart': - defaults: {currentHour:3, dayStart:4, shouldDo:true} + defaults: {hoursAgo:2,currentHour:3, dayStart:4, shouldDo:true} steps: 'checked': {checked: true, expect:'noChange'} 'un-checked': {checked: false, expect:'noChange'} @@ -429,7 +429,7 @@ describe 'Cron', -> defaults: {repeat:{su:1,m:false,t:1,w:1,th:1,f:1,s:1}} steps: 'pre-dayStart': - defaults: {currentHour:3, dayStart:4, shouldDo:true} + defaults: {hoursAgo:23, currentHour:3, dayStart:4, shouldDo:true} steps: 'checked': {checked: true, expect:'noChange'} 'un-checked': {checked: false, expect:'noChange'} @@ -442,18 +442,18 @@ describe 'Cron', -> 'not due yesterday': defaults: repeatWithoutLastWeekday() steps: - '(simple)': {expect:'noDamage'} - 'post-dayStart': {currentHour:5,dayStart:4, expect:'noDamage'} - 'pre-dayStart': {currentHour:3, dayStart:4, expect:'noChange'} + '(simple)': {hoursAgo:24, expect:'noDamage'} + 'post-dayStart': {hoursAgo:23,currentHour:5,dayStart:4, expect:'noDamage'} + 'pre-dayStart': {hoursAgo:21, currentHour:3, dayStart:4, expect:'noChange'} recurseCronMatrix = (obj, options={}) -> if obj.steps _.each obj.steps, (step, text) -> o = _.cloneDeep options o.text ?= ''; o.text += " #{text} " - recurseCronMatrix step, _.defaults(o,obj.defaults) + recurseCronMatrix step, _.assign(o, obj.defaults) else - it "#{options.text}", -> runCron(_.defaults(obj,options)) + it "#{options.text}", -> runCron(_.assign(options, obj)) recurseCronMatrix(cronMatrix) it 'calculates day differences with dayStart properly', -> @@ -501,4 +501,4 @@ describe 'Helper', -> pets = { "Wolf-Base": 2, "Wolf-Veteran": 1, "Wolf-Cerberus": 1, "Dragon-Hydra": 1} expect(shared.countPets(null, pets)).to.eql 1 - expect(shared.countPets(_.size(pets), pets)).to.eql 1 \ No newline at end of file + expect(shared.countPets(_.size(pets), pets)).to.eql 1