mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-01 07:21:15 +00:00
93 lines
2.6 KiB
CoffeeScript
93 lines
2.6 KiB
CoffeeScript
|
|
assert = require 'assert'
|
||
|
|
{BrowserModel: Model} = require 'racer/test/util/model'
|
||
|
|
derby = require 'derby'
|
||
|
|
_ = require 'underscore'
|
||
|
|
moment = require 'moment'
|
||
|
|
request = require 'request'
|
||
|
|
|
||
|
|
# Custom modules
|
||
|
|
scoring = require '../src/app/scoring'
|
||
|
|
character = require '../src/app/character'
|
||
|
|
|
||
|
|
###### Helpers & Variables ######
|
||
|
|
|
||
|
|
model = null
|
||
|
|
uuid = null
|
||
|
|
taskPath = null
|
||
|
|
baseURL = 'http://localhost:3000'
|
||
|
|
|
||
|
|
## Helper which clones the content at a path so tests can compare before/after values
|
||
|
|
# Otherwise, using model.get(path) will give the same object before as after
|
||
|
|
pathSnapshots = (paths) ->
|
||
|
|
if _.isString(paths)
|
||
|
|
return clone(model.get(paths))
|
||
|
|
_.map paths, (path) -> clone(model.get(path))
|
||
|
|
statsTask = -> pathSnapshots(['_user.stats', taskPath]) # quick snapshot of user.stats & task
|
||
|
|
|
||
|
|
cleanUserObj = ->
|
||
|
|
userObj = character.newUserObject()
|
||
|
|
userObj.tasks = {}
|
||
|
|
userObj.habitIds = []
|
||
|
|
userObj.dailyIds = []
|
||
|
|
userObj.todoIds = []
|
||
|
|
userObj.rewardIds = []
|
||
|
|
return userObj
|
||
|
|
resetUser = -> model.set '_user', cleanUserObj()
|
||
|
|
|
||
|
|
freshTask = (taskObj) ->
|
||
|
|
resetUser()
|
||
|
|
# create a test task
|
||
|
|
uuid = derby.uuid()
|
||
|
|
taskPath = "_user.tasks.#{uuid}"
|
||
|
|
{type} = taskObj
|
||
|
|
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
|
||
|
|
[taskObj.id, taskObj.value] = [uuid, 0]
|
||
|
|
model.at("_#{type}List").push taskObj
|
||
|
|
|
||
|
|
###
|
||
|
|
Helper function to determine if stats updates are numerically correct based on scoring
|
||
|
|
@direction: 'up' or 'down'
|
||
|
|
@options: The user stats modifiers and times to run, defaults to {times:1, modifiers:{lvl:1, weapon:0, armor:0}}
|
||
|
|
###
|
||
|
|
modificationsLookup = (direction, options = {}) ->
|
||
|
|
merged = _.defaults options, {times:1, lvl:1, weapon:0, armor:0}
|
||
|
|
{times, lvl, armor, weapon} = merged
|
||
|
|
userObj = cleanUserObj()
|
||
|
|
value = 0
|
||
|
|
_.times times, (n) ->
|
||
|
|
delta = scoring.taskDeltaFormula(value, direction)
|
||
|
|
value += delta
|
||
|
|
if direction=='up'
|
||
|
|
gain = scoring.expModifier(delta, options)
|
||
|
|
userObj.stats.exp += gain
|
||
|
|
userObj.stats.money += gain
|
||
|
|
else
|
||
|
|
loss = scoring.hpModifier(delta, options)
|
||
|
|
userObj.stats.hp += loss
|
||
|
|
return {user:userObj, value:value}
|
||
|
|
|
||
|
|
###### Specs ######
|
||
|
|
|
||
|
|
describe 'API', ->
|
||
|
|
model = null
|
||
|
|
|
||
|
|
before ->
|
||
|
|
model = new Model
|
||
|
|
model.set '_user', character.newUserObject()
|
||
|
|
scoring.setModel model
|
||
|
|
|
||
|
|
it '/v1/:uid/tasks returns correct user defaults', (done) ->
|
||
|
|
user = model.get '_user'
|
||
|
|
|
||
|
|
request "#{baseURL}/#{user.id}/tasks", (err, res, body) ->
|
||
|
|
assert.ok !err
|
||
|
|
tasks = []
|
||
|
|
|
||
|
|
['habit','daily'].map (type) ->
|
||
|
|
model.refList "_#{type}List", "_user.tasks", "_user.#{type}Ids"
|
||
|
|
tasks.concat model.get "_#{type}List"
|
||
|
|
|
||
|
|
console.log 'hi', tasks
|
||
|
|
assert.ok _.isEqual tasks, body
|
||
|
|
done()
|