mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
port tests for score and randomDrop
This commit is contained in:
@@ -12,7 +12,7 @@ const CLOSE_ENOUGH = 0.00001;
|
|||||||
function _getTaskValue (taskValue) {
|
function _getTaskValue (taskValue) {
|
||||||
if (taskValue < MIN_TASK_VALUE) {
|
if (taskValue < MIN_TASK_VALUE) {
|
||||||
return MIN_TASK_VALUE;
|
return MIN_TASK_VALUE;
|
||||||
} else if (taskValue < MAX_TASK_VALUE) {
|
} else if (taskValue > MAX_TASK_VALUE) {
|
||||||
return MAX_TASK_VALUE;
|
return MAX_TASK_VALUE;
|
||||||
} else {
|
} else {
|
||||||
return taskValue;
|
return taskValue;
|
||||||
@@ -167,7 +167,7 @@ function _changeTaskValue (user, task, direction, times, cron) {
|
|||||||
return addToDelta;
|
return addToDelta;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function scoreTask (options = {}, req) {
|
export default function scoreTask (options = {}, req = {}) {
|
||||||
let {user, task, direction, times = 1, cron = false} = options;
|
let {user, task, direction, times = 1, cron = false} = options;
|
||||||
let delta = 0;
|
let delta = 0;
|
||||||
let stats = {
|
let stats = {
|
||||||
@@ -200,7 +200,7 @@ export default function scoreTask (options = {}, req) {
|
|||||||
let thl = task.history.length;
|
let thl = task.history.length;
|
||||||
|
|
||||||
if (th[thl - 1] && moment(th[thl - 1].date).isSame(new Date(), 'day')) {
|
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 {
|
} else {
|
||||||
th.push({
|
th.push({
|
||||||
date: Number(new Date()), // TODO are we going to cast history entries?
|
date: Number(new Date()), // TODO are we going to cast history entries?
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
startOfDay,
|
startOfDay,
|
||||||
daysSince,
|
daysSince,
|
||||||
} from '../../common/script/cron';
|
} from '../../common/script/cron';
|
||||||
|
import scoreTask from '../../common/script/api-v3/scoreTask';
|
||||||
|
|
||||||
let expect = require('expect.js');
|
let expect = require('expect.js');
|
||||||
let sinon = require('sinon');
|
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) {
|
for (let random = MIN_RANGE_FOR_POTION; random <= MAX_RANGE_FOR_POTION; random += 0.1) {
|
||||||
sinon.stub(user.fns, 'predictableRandom').returns(random);
|
sinon.stub(user.fns, 'predictableRandom').returns(random);
|
||||||
user.ops.score({
|
|
||||||
params: {
|
let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'});
|
||||||
id: this.task_id,
|
user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {});
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(user.items.eggs).to.be.empty;
|
expect(user.items.eggs).to.be.empty;
|
||||||
expect(user.items.hatchingPotions).to.not.be.empty;
|
expect(user.items.hatchingPotions).to.not.be.empty;
|
||||||
expect(user.items.food).to.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) {
|
for (let random = MIN_RANGE_FOR_EGG; random <= MAX_RANGE_FOR_EGG; random += 0.1) {
|
||||||
sinon.stub(user.fns, 'predictableRandom').returns(random);
|
sinon.stub(user.fns, 'predictableRandom').returns(random);
|
||||||
user.ops.score({
|
let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'});
|
||||||
params: {
|
user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {});
|
||||||
id: this.task_id,
|
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(user.items.eggs).to.not.be.empty;
|
expect(user.items.eggs).to.not.be.empty;
|
||||||
expect(user.items.hatchingPotions).to.be.empty;
|
expect(user.items.hatchingPotions).to.be.empty;
|
||||||
expect(user.items.food).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) {
|
for (let random = MIN_RANGE_FOR_FOOD; random <= MAX_RANGE_FOR_FOOD; random += 0.1) {
|
||||||
sinon.stub(user.fns, 'predictableRandom').returns(random);
|
sinon.stub(user.fns, 'predictableRandom').returns(random);
|
||||||
user.ops.score({
|
let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'});
|
||||||
params: {
|
user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {});
|
||||||
id: this.task_id,
|
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(user.items.eggs).to.be.empty;
|
expect(user.items.eggs).to.be.empty;
|
||||||
expect(user.items.hatchingPotions).to.be.empty;
|
expect(user.items.hatchingPotions).to.be.empty;
|
||||||
expect(user.items.food).to.not.be.empty;
|
expect(user.items.food).to.not.be.empty;
|
||||||
@@ -773,12 +763,8 @@ describe('User', () => {
|
|||||||
|
|
||||||
it('does not get a drop', function () {
|
it('does not get a drop', function () {
|
||||||
sinon.stub(user.fns, 'predictableRandom').returns(0.5);
|
sinon.stub(user.fns, 'predictableRandom').returns(0.5);
|
||||||
user.ops.score({
|
let delta = scoreTask({task: user.dailys[user.dailys.length - 1], user, direction: 'up'});
|
||||||
params: {
|
user.fns.randomDrop({task: user.dailys[user.dailys.length - 1], delta}, {});
|
||||||
id: this.task_id,
|
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expect(user.items.eggs).to.eql({});
|
expect(user.items.eggs).to.eql({});
|
||||||
expect(user.items.hatchingPotions).to.eql({});
|
expect(user.items.hatchingPotions).to.eql({});
|
||||||
expect(user.items.food).to.eql({});
|
expect(user.items.food).to.eql({});
|
||||||
@@ -930,76 +916,38 @@ describe('Simple Scoring', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Habits : Up', function () {
|
it('Habits : Up', function () {
|
||||||
this.after.ops.score({
|
let delta = scoreTask({task: this.after.habits[0], user: this.after, direction: 'down', times: 5});
|
||||||
params: {
|
this.after.fns.randomDrop({task: this.after.habits[0], delta}, {});
|
||||||
id: this.after.habits[0].id,
|
|
||||||
direction: 'down',
|
|
||||||
},
|
|
||||||
query: {
|
|
||||||
times: 5,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expectLostPoints(this.before, this.after, 'habit');
|
expectLostPoints(this.before, this.after, 'habit');
|
||||||
});
|
});
|
||||||
it('Habits : Down', function () {
|
it('Habits : Down', function () {
|
||||||
this.after.ops.score({
|
let delta = scoreTask({task: this.after.habits[0], user: this.after, direction: 'up', times: 5});
|
||||||
params: {
|
this.after.fns.randomDrop({task: this.after.habits[0], delta}, {});
|
||||||
id: this.after.habits[0].id,
|
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
query: {
|
|
||||||
times: 5,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expectGainedPoints(this.before, this.after, 'habit');
|
expectGainedPoints(this.before, this.after, 'habit');
|
||||||
});
|
});
|
||||||
it('Dailys : Up', function () {
|
it('Dailys : Up', function () {
|
||||||
this.after.ops.score({
|
let delta = scoreTask({task: this.after.dailys[0], user: this.after, direction: 'up'});
|
||||||
params: {
|
this.after.fns.randomDrop({task: this.after.dailys[0], delta}, {});
|
||||||
id: this.after.dailys[0].id,
|
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expectGainedPoints(this.before, this.after, 'daily');
|
expectGainedPoints(this.before, this.after, 'daily');
|
||||||
});
|
});
|
||||||
it('Dailys : Up, Down', function () {
|
it('Dailys : Up, Down', function () {
|
||||||
this.after.ops.score({
|
let delta = scoreTask({task: this.after.dailys[0], user: this.after, direction: 'up'});
|
||||||
params: {
|
this.after.fns.randomDrop({task: this.after.dailys[0], delta}, {});
|
||||||
id: this.after.dailys[0].id,
|
let delta2 = scoreTask({task: this.after.dailys[0], user: this.after, direction: 'down'});
|
||||||
direction: 'up',
|
this.after.fns.randomDrop({task: this.after.dailys[0], delta2}, {});
|
||||||
},
|
|
||||||
});
|
|
||||||
this.after.ops.score({
|
|
||||||
params: {
|
|
||||||
id: this.after.dailys[0].id,
|
|
||||||
direction: 'down',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expectClosePoints(this.before, this.after, 'daily');
|
expectClosePoints(this.before, this.after, 'daily');
|
||||||
});
|
});
|
||||||
it('Todos : Up', function () {
|
it('Todos : Up', function () {
|
||||||
this.after.ops.score({
|
let delta = scoreTask({task: this.after.todos[0], user: this.after, direction: 'up'});
|
||||||
params: {
|
this.after.fns.randomDrop({task: this.after.todos[0], delta}, {});
|
||||||
id: this.after.todos[0].id,
|
|
||||||
direction: 'up',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expectGainedPoints(this.before, this.after, 'todo');
|
expectGainedPoints(this.before, this.after, 'todo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Todos : Up, Down', function () {
|
it('Todos : Up, Down', function () {
|
||||||
this.after.ops.score({
|
let delta = scoreTask({task: this.after.todos[0], user: this.after, direction: 'up'});
|
||||||
params: {
|
this.after.fns.randomDrop({task: this.after.todos[0], delta}, {});
|
||||||
id: this.after.todos[0].id,
|
let delta2 = scoreTask({task: this.after.todos[0], user: this.after, direction: 'down'});
|
||||||
direction: 'up',
|
this.after.fns.randomDrop({task: this.after.todos[0], delta2}, {});
|
||||||
},
|
|
||||||
});
|
|
||||||
this.after.ops.score({
|
|
||||||
params: {
|
|
||||||
id: this.after.todos[0].id,
|
|
||||||
direction: 'down',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
expectClosePoints(this.before, this.after, 'todo');
|
expectClosePoints(this.before, this.after, 'todo');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -258,13 +258,7 @@ api.scoreTask = {
|
|||||||
task.completed = direction === 'up';
|
task.completed = direction === 'up';
|
||||||
}
|
}
|
||||||
|
|
||||||
let delta;
|
let delta = scoreTask({task, user, direction}, req);
|
||||||
try {
|
|
||||||
delta = scoreTask({task, user, direction}, req);
|
|
||||||
} catch (e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Drop system (don't run on the client, as it would only be discarded since ops are sent to the API, not the results)
|
// 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);
|
if (direction === 'up') user.fns.randomDrop({task, delta}, req);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user