ensl_gathers/spec/gather.js

424 lines
13 KiB
JavaScript
Raw Normal View History

2015-07-23 13:36:51 +00:00
"use strict";
var helper = require("./helpers/index.js");
var User = helper.User;
var Gather = helper.Gather;
var Gatherer = helper.Gatherer;
var assert = require("chai").assert;
2015-07-24 13:34:02 +00:00
describe("Gather Model:", function () {
var user, gather, gatherers;
2015-07-23 13:36:51 +00:00
beforeEach(function () {
user = helper.createUser();
2015-07-24 13:34:02 +00:00
gatherers = [];
2016-01-02 21:32:26 +00:00
for (var i = 0; i < 12; i++) {
gatherers.push(helper.createUser());
}
2015-07-23 13:36:51 +00:00
gather = Gather();
});
2015-10-03 15:55:42 +00:00
2015-07-23 13:36:51 +00:00
describe("addUser", function () {
it ("adds gatherer to lobby", function () {
gather.addUser(user);
assert.equal(gather.gatherers.length, 1);
assert.equal(gather.gatherers[0].id, user.id);
});
it ("does not add duplicate users", function () {
gather.addUser(user);
gather.addUser(user);
assert.equal(gather.gatherers.length, 1);
});
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("removeUser", function () {
it ("removes gatherer altogether", function () {
gather.addUser(user);
assert.equal(gather.gatherers.length, 1);
assert.equal(gather.gatherers[0].id, user.id);
gather.removeUser(user);
assert.equal(gather.gatherers.length, 0);
});
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("moveToMarine", function () {
it ("moves a player to marine", function () {
gather.addUser(user);
gather.moveToMarine(user);
assert.equal(gather.marines().length, 1);
assert.equal(gather.marines()[0].id, user.id);
});
2015-07-24 13:34:02 +00:00
it ("will not move player if team is full", function () {
gatherers.forEach(function (gatherer, index) {
gather.addUser(gatherer);
gather.moveToMarine(gatherer);
assert.isTrue(gather.marines().length <= gather.TEAM_SIZE);
});
});
describe("with mover argument", function () {
var marineCount, gathererCursor, alienLeader, marineLeader;
beforeEach(function () {
helper.populateGatherAndVotes(gather, gatherers);
alienLeader = gather.gatherers[0];
assert.equal(alienLeader.team, "alien");
marineLeader = gather.gatherers[1];
assert.equal(marineLeader.team, "marine");
gathererCursor = 2;
marineCount = gather.marines().length;
});
it ("moves player if mover is captain & is marine & current turn", function () {
gather.moveToMarine(gather.gatherers[gathererCursor], marineLeader);
assert.equal(gather.marines().length, marineCount + 1);
assert.equal(gather.gatherers[gathererCursor].team, "marine");
});
it ("does not move player if mover is not in team", function () {
gather.moveToMarine(gather.gatherers[gathererCursor], alienLeader);
assert.equal(gather.marines().length, marineCount);
assert.equal(gather.gatherers[gathererCursor].team, "lobby");
});
it ("does not move player if not captain", function () {
gather.moveToMarine(gather.gatherers[gathererCursor], marineLeader);
assert.equal(gather.gatherers[gathererCursor].team, "marine");
gather.moveToAlien(gather.gatherers[gathererCursor + 1], alienLeader);
gather.moveToAlien(gather.gatherers[gathererCursor + 2], alienLeader);
assert.equal(gather.marines().length, 2);
assert.equal(gather.aliens().length, 3);
gather.moveToMarine(gather.gatherers[gathererCursor + 3],
gather.gatherers[gathererCursor]);
assert.equal(gather.marines().length, marineCount + 1);
assert.equal(gather.gatherers[gathererCursor + 3].team, "lobby");
});
it ("does not move player if already assigned to team", function () {
gather.moveToMarine(alienLeader, marineLeader);
assert.equal(gather.gatherers[0].team, "alien");
});
});
2015-07-23 13:36:51 +00:00
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("moveToAlien", function () {
it ("moves a player to alien", function () {
gather.addUser(user);
gather.moveToAlien(user);
assert.equal(gather.aliens().length, 1);
assert.equal(gather.aliens()[0].id, user.id);
});
2015-07-24 13:34:02 +00:00
it ("will not move player if team is full", function () {
gatherers.forEach(function (gatherer, index) {
gather.addUser(gatherer);
gather.moveToAlien(gatherer);
assert.isTrue(gather.aliens().length <= gather.TEAM_SIZE);
});
});
describe("with mover argument", function () {
var alienCount, gathererCursor, alienLeader, marineLeader;
beforeEach(function () {
helper.populateGatherAndVotes(gather, gatherers);
gather.moveToMarine(gather.gatherers[2]);
alienLeader = gather.gatherers[0];
assert.equal(alienLeader.team, "alien");
marineLeader = gather.gatherers[1];
assert.equal(marineLeader.team, "marine");
gathererCursor = 3;
alienCount = gather.aliens().length;
});
it ("moves player if mover is captain & is alien & current turn", function () {
gather.moveToAlien(gather.gatherers[gathererCursor], alienLeader);
assert.equal(gather.aliens().length, alienCount + 1);
assert.equal(gather.gatherers[gathererCursor].team, "alien");
});
it ("does not move player if mover is not in team", function () {
gather.moveToAlien(gather.gatherers[gathererCursor], marineLeader);
assert.equal(gather.aliens().length, alienCount);
assert.equal(gather.gatherers[gathererCursor].team, "lobby");
});
it ("does not move player if not captain", function () {
gather.moveToAlien(gather.gatherers[gathererCursor], alienLeader);
assert.equal(gather.gatherers[gathererCursor].team, "alien");
alienCount++;
gather.moveToAlien(gather.gatherers[gathererCursor + 1],
gather.gatherers[gathererCursor]);
assert.equal(gather.aliens().length, alienCount);
assert.equal(gather.gatherers[gathererCursor + 1].team, "lobby");
});
it ("does not move player if already assigned to team", function () {
gather.moveToMarine(alienLeader, marineLeader);
assert.equal(gather.gatherers[0].team, "alien");
});
});
2015-07-23 13:36:51 +00:00
});
2015-07-28 15:54:29 +00:00
2015-09-16 11:44:34 +00:00
describe("pickingTurn", function () {
var marineLeader, alienLeader;
beforeEach(function () {
helper.populateGatherAndVotes(gather, gatherers);
2015-09-16 11:44:34 +00:00
marineLeader = gather.marineLeader();
alienLeader = gather.alienLeader();
assert.isNotNull(marineLeader);
assert.isNotNull(alienLeader);
assert.equal(gather.current, "selection");
});
it ("returns null if current state is not selection", function () {
gather = Gather();
assert.isNull(gather.pickingTurn());
});
it ("gives first pick to aliens, then 2 for each side after", function () {
var gathererCursor = 2; // Picking i=2 gatherer next
var assertMarineNextMove = function () {
assert.equal(gather.pickingTurn(), "marine");
gather.moveToMarine(gather.gatherers[gathererCursor], marineLeader);
gathererCursor++;
};
var assertAlienNextMove = function () {
assert.equal(gather.pickingTurn(), "alien");
gather.moveToAlien(gather.gatherers[gathererCursor], alienLeader);
gathererCursor++;
};
assertMarineNextMove();
assertAlienNextMove();
assertAlienNextMove();
assertMarineNextMove();
assertMarineNextMove();
assertAlienNextMove();
assertAlienNextMove();
assertMarineNextMove();
assertMarineNextMove();
assertAlienNextMove();
});
});
2015-07-23 13:36:51 +00:00
describe("moveToLobby", function () {
it ("moves a player to lobby", function () {
gather.addUser(user);
gather.moveToAlien(user);
assert.equal(gather.aliens().length, 1);
gather.moveToLobby(user);
assert.equal(gather.lobby().length, 1);
assert.equal(gather.lobby()[0].id, user.id);
});
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("aliens", function () {
it ("returns all gatherers in aliens", function () {
gather.addUser(user);
gather.moveToAlien(user);
assert.equal(gather.aliens().length, 1);
});
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("marines", function () {
it ("returns all gatherers in marines", function () {
gather.addUser(user);
gather.moveToMarine(user);
assert.equal(gather.marines().length, 1);
});
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("lobby", function () {
it ("returns all gatherers in lobby", function () {
gather.addUser(user);
assert.equal(gather.lobby().length, 1);
});
});
2015-07-28 15:54:29 +00:00
2015-07-23 13:36:51 +00:00
describe("toJson", function () {
2015-07-24 13:34:02 +00:00
it ("returns a json representation of the gather instance", function () {
var output = gather.toJson();
2015-07-27 11:55:36 +00:00
assert.isArray(output.gatherers);
assert.isString(output.state);
2015-07-31 15:03:09 +00:00
assert.isNull(output.election.startTime);
2015-09-27 15:37:34 +00:00
assert.isObject(output.cooldown);
2015-09-20 00:17:37 +00:00
assert.equal(output.election.interval, gather.election.INTERVAL);
2015-07-24 13:34:02 +00:00
});
});
2015-07-28 15:54:29 +00:00
2015-07-24 13:34:02 +00:00
describe("leaderVotes", function () {
beforeEach(function () {
gatherers.forEach(function (user) {
gather.addUser(user);
});
});
it ("initialises with an empty array", function () {
assert.isArray(gather.leaderVotes());
assert.equal(gather.leaderVotes(), 0);
});
it ("returns an array of user ids", function () {
var candidate = gatherers[0];
var voter = gatherers[1];
gather.voteForLeader(voter, candidate);
assert.isArray(gather.leaderVotes());
assert.equal(gather.leaderVotes().length, 1);
assert.equal(gather.leaderVotes()[0], candidate.id);
});
it ("ignores candidates who have left the gather", function () {
var candidate = gatherers[0];
var voter = gatherers[1];
gather.voteForLeader(voter, candidate);
gather.removeUser(candidate);
assert.equal(gather.leaderVotes().length, 0);
});
});
2015-07-28 15:54:29 +00:00
2015-07-24 13:34:02 +00:00
describe("voteForLeader", function () {
beforeEach(function () {
gatherers.forEach(function (user) {
gather.addUser(user);
});
});
it ("assigns vote for a leader", function () {
var candidate = gatherers[0];
var voter = gatherers[1];
gather.voteForLeader(voter, candidate);
var votes = gather.leaderVotes();
assert.equal(votes.length, 1);
assert.equal(votes[0], candidate.id);
});
it ("reassigns vote if already voted", function () {
var candidate = gatherers[0];
var secondCandidate = gatherers[2];
var voter = gatherers[1];
gather.voteForLeader(voter, candidate);
var votes = gather.leaderVotes();
assert.equal(votes.length, 1);
assert.equal(votes[0], candidate.id);
gather.voteForLeader(voter, secondCandidate);
votes = gather.leaderVotes();
assert.equal(votes.length, 1);
assert.equal(votes[0], secondCandidate.id);
});
});
2015-07-28 15:54:29 +00:00
2015-07-24 13:34:02 +00:00
describe("alienLeader", function () {
beforeEach(function () {
gatherers.forEach(function (gatherer) {
gather.addGatherer(gatherer);
});
});
it ("returns alien leader", function () {
gather.gatherers[0].team = "alien";
gather.gatherers[0].leader = true;
assert.equal(gather.alienLeader().id, gather.gatherers[0].id);
});
it ("returns undefined if no alien leader", function () {
assert.isUndefined(gather.alienLeader());
});
});
2015-07-28 15:54:29 +00:00
2015-07-24 13:34:02 +00:00
describe("marineLeader", function () {
beforeEach(function () {
gatherers.forEach(function (gatherer) {
gather.addGatherer(gatherer);
});
});
it ("returns marine leader", function () {
gather.gatherers[0].team = "marine";
gather.gatherers[0].leader = true;
assert.equal(gather.marineLeader().id, gather.gatherers[0].id);
});
it ("returns undefined if no marine leader", function () {
assert.isUndefined(gather.marineLeader());
});
});
2015-07-28 15:54:29 +00:00
2015-07-24 13:34:02 +00:00
describe("assignMarineLeader", function () {
it ("assigns a marine leader", function () {
gather.addUser(user);
gather.assignMarineLeader(user.id);
var leader = gather.marineLeader();
assert.equal(leader.id, user.id);
});
});
2015-07-28 15:54:29 +00:00
2015-07-24 13:34:02 +00:00
describe("assignAlienLeader", function () {
it ("assigns an alien leader", function () {
gather.addUser(user);
gather.assignAlienLeader(user.id);
var leader = gather.alienLeader();
assert.equal(leader.id, user.id);
});
});
2015-07-28 15:54:29 +00:00
2015-09-25 21:23:30 +00:00
describe("modifyGatherer", function () {
beforeEach(function () {
gather.addGatherer(user);
});
it ("modifies a gatherer", function () {
assert.isFalse(gather.gatherers[0].confirm);
gather.modifyGatherer(user, gatherer => {
gatherer.confirm = true;
});
let g = gather.getGatherer(user);
assert.isTrue(g.confirm);
});
});
2015-07-28 15:54:29 +00:00
describe("getGatherer", function () {
beforeEach(function () {
gather.addGatherer(user);
});
it ("returns a gatherer given a user", function () {
var gatherer = gather.getGatherer(user);
assert.equal(gatherer.id, user.id);
});
it ("returns null if user is not a gatherer", function () {
var gatherer = gather.getGatherer(gatherers[0]);
assert.isNull(gatherer);
});
});
describe("toggleMapVote", function () {
2015-07-29 13:50:39 +00:00
beforeEach(function() {
gather.addGatherer(user);
});
it ("assigns map vote to gatherer", function () {
var mapId = 1;
gather.toggleMapVote(user, mapId);
2015-07-29 13:50:39 +00:00
var gatherer = gather.getGatherer(user);
assert.equal(gatherer.mapVote, mapId);
});
});
describe("toggleServerVote", function () {
2015-07-29 13:50:39 +00:00
beforeEach(function() {
gather.addGatherer(user);
});
it ("assigns map vote to gatherer", function () {
var serverId = 1;
gather.toggleServerVote(user, serverId);
2015-07-29 13:50:39 +00:00
var gatherer = gather.getGatherer(user);
assert.equal(gatherer.serverVote, serverId);
});
});
2015-09-17 13:56:44 +00:00
describe("applyCooldown", function () {
it ("applies a cooldown to the gatherer", function () {
gather.applyCooldown(user);
assert.isDefined(gather.cooldown[user.id]);
});
});
describe("needsToCoolOff", function () {
it ("returns false if gatherer is new", function () {
assert.isFalse(gather.needsToCoolOff(user));
});
it ("returns false if gatherer has cooled off", function () {
gather.applyCooldown(user);
gather.cooldown[user.id] = new Date(0);
assert.isFalse(gather.needsToCoolOff(user));
});
it ("returns true if gatherer needs to cool off", function () {
gather.applyCooldown(user);
assert.isTrue(gather.needsToCoolOff(user));
});
});
2015-09-17 13:56:44 +00:00
describe("regatherVotes", function () {
beforeEach(function() {
gather.addGatherer(user);
});
it ("counts the number of regather votes", function () {
assert.equal(gather.regatherVotes(), 0);
gather.gatherers[0].regatherVote = true;
assert.equal(gather.regatherVotes(), 1);
});
});
2015-07-23 13:36:51 +00:00
});