start adding mocha tests, based on the old user.mocha.coffee

This commit is contained in:
Tyler Renelle 2013-05-24 14:28:57 +01:00
parent 30f4f091a4
commit be670dcb04
3 changed files with 176 additions and 0 deletions

View file

@ -8,5 +8,9 @@
"lodash": "~1.2.1",
"moment": "~2.0.0",
"relative-date": "~1.1.1"
},
"devDependencies": {
"mocha": "*",
"expect.js": "*"
}
}

166
tests/algos.mocha.coffee Normal file
View file

@ -0,0 +1,166 @@
_ = require 'lodash'
expect = require 'expect.js'
moment = require 'moment'
# Custom modules
algos = require '../script/algos'
helpers = require '../script/helpers'
items = require '../script/items'
###### Specs ######
describe 'Cron', ->
user = undefined
beforeEach -> user = helpers.newUser()
it 'computes shouldCron', ->
expect(algos.shouldCron(user)).to.be true
it 'calculates day differences with dayStart properly', ->
dayStart = 4
yesterday = moment().subtract('d', 1).add('h', dayStart)
now = moment().startOf('day').add('h', dayStart-1) #today
console.log {yesterday: yesterday.format('MM/DD HH:00'), now: now.format('MM/DD HH:00')}
console.log {diff: Math.abs(moment(yesterday).diff(moment(now), 'days'))}
expect(helpers.daysBetween(yesterday, now, dayStart)).to.eql 0
now = moment().startOf('day').add('h', dayStart)
console.log {now: now.format('MM/DD HH:00')}
expect(helpers.daysBetween(yesterday, now, dayStart)).to.eql 1
describe 'User', ->
user = undefined
beforeEach -> user = helpers.newUser()
expectStrings = (obj, paths) ->
_.each paths, (path) -> expect(obj[path]).to.be.ok()
it 'sets correct user defaults', ->
expect(user.stats).to.eql { gp: 0, exp: 0, lvl: 1, hp: 50 }
expect(user.party).to.eql { invitation: null }
expect(user.items).to.eql { weapon: 0, armor: 0, head: 0, shield: 0 }
expect(user.preferences).to.eql { gender: 'm', skin: 'white', hair: 'blond', armorSet: 'v1', dayStart:0, showHelm: true }
expect(user.balance).to.eql 0
expect(user.lastCron).to.eql 'new'
expect(user.flags).to.eql {partyEnabled: false, itemsEnabled: false, ads: 'show'}
expectStrings(user, ['apiToken'])
expectStrings(user.habits[0], ['text','id'])
expectStrings(user.dailys[0], ['text','id'])
expectStrings(user.todos[0], ['text','id'])
expectStrings(user.rewards[0], ['text','id'])
expectStrings(user.tags[0], ['name','id'])
expectStrings(user.tags[1], ['name','id'])
expectStrings(user.tags[2], ['name','id'])
##### Habits #####
describe 'Tasks', ->
describe 'Habits', ->
#TODO test actual math
it 'test a few scoring numbers', ->
[before, after] = [helpers.newUser(), helpers.newUser()]
algos.score(after, after.habits[0], 'down', {times:5})
expect(after.stats.hp).to.be.lessThan before.stats.hp
expect(after.stats.exp).to.be before.stats.exp
expect(after.stats.gp).to.be before.stats.gp
expect(after.habits[0].value).to.be.lessThan before.habits[0].value
[before, after] = [helpers.newUser(), helpers.newUser()]
algos.score(after, after.habits[0], 'up', {times:5})
expect(after.stats.hp).to.be before.stats.hp
expect(after.stats.exp).to.be.greaterThan before.stats.exp
expect(after.stats.gp).to.be.greaterThan before.stats.gp
expect(after.habits[0].value).to.be.greaterThan before.habits[0].value
it.skip 'made proper modifications when down-scored', ->
## Trial 1
shouldBe = modificationsLookup('down')
scoring.score(uuid,'down')
[stats, task] = statsTask()
expect(stats.hp).to.be.eql shouldBe.user.stats.hp
expect(task.value).to.eql shouldBe.value
## Trial 2
freshTask {type: 'habit', text: 'Habit', completed: false}
shouldBe = modificationsLookup('down', {times:10})
scoring.score(uuid,'down', {times:10})
[stats, task] = statsTask()
expect(stats.hp).to.be.eql shouldBe.user.stats.hp
expect(task.value).to.eql shouldBe.value
it.skip 'made proper modifications when up-scored', ->
# Up-score the habit
[statsBefore, taskBefore] = statsTask()
scoring.score(uuid, 'up')
[statsAfter, taskAfter] = statsTask()
# User should have gained Exp, GP
expect(statsAfter.exp).to.be.greaterThan statsBefore.exp
expect(statsAfter.money).to.be.greaterThan statsBefore.money
# HP should not change
expect(statsAfter.hp).to.eql statsBefore.hp
# Task should have lost value
expect(taskBefore.value).to.eql 0
expect(taskAfter.value).to.be.greaterThan taskBefore.value
## Trial 2
taskBefore = pathSnapshots(taskPath)
scoring.score(uuid, 'up')
taskAfter = pathSnapshots(taskPath)
# Should have lost in value
expect(taskAfter.value).to.be > taskBefore.value
# And lost more than trial 1
diff = Math.abs(taskAfter.value) - Math.abs(taskBefore.value)
expect(diff).to.be.lessThan 1
describe.skip 'Lvl & Items', ->
beforeEach ->
freshTask {type: 'habit', text: 'Habit', up: true, down: true}
it 'modified damage based on lvl & armor'
it 'modified exp/gp based on lvl & weapon'
it 'always decreases hp with damage, regardless of stats/items'
it 'always increases exp/gp with gain, regardless of stats/items'
describe.skip 'Dailies', ->
beforeEach ->
freshTask {type: 'daily', text: 'Daily', completed: false}
it 'created the daily', ->
task = model.get(taskPath)
expect(task.text).to.eql 'Daily'
expect(task.value).to.eql 0
it 'does proper calculations when daily is complete'
it 'calculates dailys properly when they have repeat dates'
runCron = (times, pass=1) ->
# Set lastCron to days ago
today = new moment()
ago = new moment().subtract('days',times)
model.set '_user.lastCron', ago.toDate()
# Run run
scoring.cron()
[stats, task] = statsTask()
# Should have updated cron to today
lastCron = moment(model.get('_user.lastCron'))
expect(today.diff(lastCron, 'days')).to.eql 0
shouldBe = modificationsLookup('down', {times:times*pass})
# Should have updated points properly
expect(Math.round(stats.hp)).to.be.eql Math.round(shouldBe.user.stats.hp)
expect(Math.round(task.value)).to.eql Math.round(shouldBe.value)
it 'calculates user.stats & task.value properly on cron', ->
runCron(10)
it 'runs cron multiple times properly', ->
runCron(5)
runCron(5, 2)

6
tests/mocha.opts Normal file
View file

@ -0,0 +1,6 @@
--colors
--reporter spec
--growl
--debug
--compilers coffee:coffee-script
--globals io