From 0f3b307f409ad11dbc681652112ca518564a3942 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Wed, 9 Dec 2015 10:21:15 +0100 Subject: [PATCH] port tests for score and randomDrop --- common/script/api-v3/scoreTask.js | 6 +- test/common/algos.mocha.js | 104 ++++++------------------ website/src/controllers/api-v3/tasks.js | 8 +- 3 files changed, 30 insertions(+), 88 deletions(-) diff --git a/common/script/api-v3/scoreTask.js b/common/script/api-v3/scoreTask.js index 2a84f00568..91e872ee4b 100644 --- a/common/script/api-v3/scoreTask.js +++ b/common/script/api-v3/scoreTask.js @@ -12,7 +12,7 @@ const CLOSE_ENOUGH = 0.00001; function _getTaskValue (taskValue) { if (taskValue < MIN_TASK_VALUE) { return MIN_TASK_VALUE; - } else if (taskValue < MAX_TASK_VALUE) { + } else if (taskValue > MAX_TASK_VALUE) { return MAX_TASK_VALUE; } else { return taskValue; @@ -167,7 +167,7 @@ function _changeTaskValue (user, task, direction, times, cron) { return addToDelta; } -export default function scoreTask (options = {}, req) { +export default function scoreTask (options = {}, req = {}) { let {user, task, direction, times = 1, cron = false} = options; let delta = 0; let stats = { @@ -200,7 +200,7 @@ export default function scoreTask (options = {}, req) { let thl = task.history.length; if (th[thl - 1] && moment(th[thl - 1].date).isSame(new Date(), 'day')) { - th[thl - 1].value = task.value; + th[thl - 1].value = task.value; // TODO mark modified? } else { th.push({ date: Number(new Date()), // TODO are we going to cast history entries? diff --git a/test/common/algos.mocha.js b/test/common/algos.mocha.js index 07d97be9ea..a8c23bb9ba 100644 --- a/test/common/algos.mocha.js +++ b/test/common/algos.mocha.js @@ -5,6 +5,7 @@ import { startOfDay, daysSince, } from '../../common/script/cron'; +import scoreTask from '../../common/script/api-v3/scoreTask'; let expect = require('expect.js'); let sinon = require('sinon'); @@ -720,12 +721,9 @@ describe('User', () => { for (let random = MIN_RANGE_FOR_POTION; random <= MAX_RANGE_FOR_POTION; random += 0.1) { sinon.stub(user.fns, 'predictableRandom').returns(random); - user.ops.score({ - params: { - id: this.task_id, - direction: 'up', - }, - }); + + let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'}); + user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {}); expect(user.items.eggs).to.be.empty; expect(user.items.hatchingPotions).to.not.be.empty; expect(user.items.food).to.be.empty; @@ -738,12 +736,8 @@ describe('User', () => { for (let random = MIN_RANGE_FOR_EGG; random <= MAX_RANGE_FOR_EGG; random += 0.1) { sinon.stub(user.fns, 'predictableRandom').returns(random); - user.ops.score({ - params: { - id: this.task_id, - direction: 'up', - }, - }); + let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'}); + user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {}); expect(user.items.eggs).to.not.be.empty; expect(user.items.hatchingPotions).to.be.empty; expect(user.items.food).to.be.empty; @@ -757,12 +751,8 @@ describe('User', () => { for (let random = MIN_RANGE_FOR_FOOD; random <= MAX_RANGE_FOR_FOOD; random += 0.1) { sinon.stub(user.fns, 'predictableRandom').returns(random); - user.ops.score({ - params: { - id: this.task_id, - direction: 'up', - }, - }); + let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'}); + user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {}); expect(user.items.eggs).to.be.empty; expect(user.items.hatchingPotions).to.be.empty; expect(user.items.food).to.not.be.empty; @@ -773,12 +763,8 @@ describe('User', () => { it('does not get a drop', function () { sinon.stub(user.fns, 'predictableRandom').returns(0.5); - user.ops.score({ - params: { - id: this.task_id, - direction: 'up', - }, - }); + let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'}); + user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {}); expect(user.items.eggs).to.eql({}); expect(user.items.hatchingPotions).to.eql({}); expect(user.items.food).to.eql({}); @@ -930,76 +916,38 @@ describe('Simple Scoring', () => { }); it('Habits : Up', function () { - this.after.ops.score({ - params: { - id: this.after.habits[0].id, - direction: 'down', - }, - query: { - times: 5, - }, - }); + let delta = scoreTask({task: this.after.habits[0], user: this.after, direction: 'down', times: 5}); + this.after.fns.randomDrop({task: this.after.habits[0], delta}, {}); expectLostPoints(this.before, this.after, 'habit'); }); it('Habits : Down', function () { - this.after.ops.score({ - params: { - id: this.after.habits[0].id, - direction: 'up', - }, - query: { - times: 5, - }, - }); + let delta = scoreTask({task: this.after.habits[0], user: this.after, direction: 'up', times: 5}); + this.after.fns.randomDrop({task: this.after.habits[0], delta}, {}); expectGainedPoints(this.before, this.after, 'habit'); }); it('Dailys : Up', function () { - this.after.ops.score({ - params: { - id: this.after.dailys[0].id, - direction: 'up', - }, - }); + let delta = scoreTask({task: this.after.dailys[0], user: this.after, direction: 'up'}); + this.after.fns.randomDrop({task: this.after.dailys[0], delta}, {}); expectGainedPoints(this.before, this.after, 'daily'); }); it('Dailys : Up, Down', function () { - this.after.ops.score({ - params: { - id: this.after.dailys[0].id, - direction: 'up', - }, - }); - this.after.ops.score({ - params: { - id: this.after.dailys[0].id, - direction: 'down', - }, - }); + let delta = scoreTask({task: this.after.dailys[0], user: this.after, direction: 'up'}); + this.after.fns.randomDrop({task: this.after.dailys[0], delta}, {}); + let delta2 = scoreTask({task: this.after.dailys[0], user: this.after, direction: 'down'}); + this.after.fns.randomDrop({task: this.after.dailys[0], delta2}, {}); expectClosePoints(this.before, this.after, 'daily'); }); it('Todos : Up', function () { - this.after.ops.score({ - params: { - id: this.after.todos[0].id, - direction: 'up', - }, - }); + let delta = scoreTask({task: this.after.todos[0], user: this.after, direction: 'up'}); + this.after.fns.randomDrop({task: this.after.todos[0], delta}, {}); expectGainedPoints(this.before, this.after, 'todo'); }); it('Todos : Up, Down', function () { - this.after.ops.score({ - params: { - id: this.after.todos[0].id, - direction: 'up', - }, - }); - this.after.ops.score({ - params: { - id: this.after.todos[0].id, - direction: 'down', - }, - }); + let delta = scoreTask({task: this.after.todos[0], user: this.after, direction: 'up'}); + this.after.fns.randomDrop({task: this.after.todos[0], delta}, {}); + let delta2 = scoreTask({task: this.after.todos[0], user: this.after, direction: 'down'}); + this.after.fns.randomDrop({task: this.after.todos[0], delta2}, {}); expectClosePoints(this.before, this.after, 'todo'); }); }); diff --git a/website/src/controllers/api-v3/tasks.js b/website/src/controllers/api-v3/tasks.js index e61bd79965..2f36b8881a 100644 --- a/website/src/controllers/api-v3/tasks.js +++ b/website/src/controllers/api-v3/tasks.js @@ -258,13 +258,7 @@ api.scoreTask = { task.completed = direction === 'up'; } - let delta; - try { - delta = scoreTask({task, user, direction}, req); - } catch (e) { - throw e; - } - + let delta = scoreTask({task, user, direction}, req); // Drop system (don't run on the client, as it would only be discarded since ops are sent to the API, not the results) if (direction === 'up') user.fns.randomDrop({task, delta}, req);