modaq
Version:
Quiz Bowl Reader using TypeScript, React, and MobX
392 lines • 20.6 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const GameFormats = __importStar(require("src/state/GameFormats"));
const Cycle_1 = require("src/state/Cycle");
require("src/state/IBuzzMarker");
const TeamState_1 = require("src/state/TeamState");
require("src/state/Events");
function addDefaultCorrectBuzz(cycle) {
const marker = {
player: new TeamState_1.Player("Alice", "Alpha", /* isStarter */ true),
position: 10,
points: 10,
};
cycle.addCorrectBuzz(marker, 2, GameFormats.UndefinedGameFormat, 0, 3);
}
function verifyDefaultBonusAnswer(bonusAnswer, bonusIndex) {
// Verify that the bonus answer is set up correctly, and that none of the parts share the same reference
chai_1.expect(bonusAnswer.bonusIndex).to.equal(bonusIndex);
chai_1.expect(bonusAnswer.parts.length).to.equal(3);
chai_1.expect(bonusAnswer.parts.map((part) => part.points)).to.deep.equal([0, 0, 0]);
chai_1.expect(bonusAnswer.parts.map((part) => part.teamName)).to.deep.equal(["", "", ""]);
chai_1.expect(bonusAnswer.parts[0]).to.not.equal(bonusAnswer.parts[1]);
chai_1.expect(bonusAnswer.parts[0]).to.not.equal(bonusAnswer.parts[2]);
chai_1.expect(bonusAnswer.parts[1]).to.not.equal(bonusAnswer.parts[2]);
}
describe("CycleTests", () => {
describe("addCorrectBuzz", () => {
it("Adds buzz", () => {
var _a, _b;
const cycle = new Cycle_1.Cycle();
const marker = {
player: new TeamState_1.Player("Alice", "Alpha", /* isStarter */ true),
position: 10,
points: 10,
};
cycle.addCorrectBuzz(marker, 2, GameFormats.UndefinedGameFormat, 1, 3);
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect((_a = cycle.correctBuzz) === null || _a === void 0 ? void 0 : _a.tossupIndex).to.equal(2);
chai_1.expect((_b = cycle.correctBuzz) === null || _b === void 0 ? void 0 : _b.marker).to.deep.equal(marker);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
verifyDefaultBonusAnswer(cycle.bonusAnswer, 1);
});
it("Correct buzz removes wrong buzz", () => {
var _a, _b, _c;
const cycle = new Cycle_1.Cycle();
const player = new TeamState_1.Player("Alice", "Alpha", /* isStarter */ true);
const negMarker = {
player,
position: 15,
points: -5,
};
cycle.addWrongBuzz(negMarker, 2, GameFormats.UndefinedGameFormat);
const marker = {
player,
position: 10,
points: 10,
};
cycle.addCorrectBuzz(marker, 2, GameFormats.UndefinedGameFormat, 1, 3);
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect((_a = cycle.correctBuzz) === null || _a === void 0 ? void 0 : _a.tossupIndex).to.equal(2);
chai_1.expect((_b = cycle.correctBuzz) === null || _b === void 0 ? void 0 : _b.marker).to.deep.equal(marker);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
verifyDefaultBonusAnswer(cycle.bonusAnswer, 1);
chai_1.expect(cycle.wrongBuzzes).to.exist;
chai_1.expect((_c = cycle.wrongBuzzes) === null || _c === void 0 ? void 0 : _c.length).to.equal(0);
});
it("Correct buzz change for same team keeps bonus", () => {
var _a, _b;
const cycle = new Cycle_1.Cycle();
const player = new TeamState_1.Player("Alice", "Alpha", /* isStarter */ true);
const otherPlayer = new TeamState_1.Player("Anna", "Alpha", /* isStarter */ true);
const marker = {
player,
position: 10,
points: 10,
};
cycle.addCorrectBuzz(marker, 0, GameFormats.UndefinedGameFormat, 0, 3);
cycle.setBonusPartAnswer(1, "Alpha", 10);
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect((_a = cycle.correctBuzz) === null || _a === void 0 ? void 0 : _a.tossupIndex).to.equal(0);
chai_1.expect((_b = cycle.correctBuzz) === null || _b === void 0 ? void 0 : _b.marker).to.deep.equal(marker);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
chai_1.expect(cycle.bonusAnswer.parts.map((part) => part.points)).to.deep.equal([0, 10, 0]);
const newMarker = {
player: otherPlayer,
position: 11,
points: 10,
};
cycle.addCorrectBuzz(newMarker, 0, GameFormats.UndefinedGameFormat, 0, 3);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was nul after fixing the buzz");
}
chai_1.expect(cycle.bonusAnswer.parts.map((part) => part.points)).to.deep.equal([0, 10, 0]);
});
it("Correct buzz changes wrong buzz and other buzz to neg", () => {
var _a, _b;
const cycle = new Cycle_1.Cycle();
const player = new TeamState_1.Player("Alice", "Alpha", /* isStarter */ true);
const otherPlayer = new TeamState_1.Player("Bob", "Beta", /* isStarter */ true);
const negMarker = {
player,
position: 5,
points: -5,
isLastWord: false,
};
cycle.addWrongBuzz(negMarker, 1, GameFormats.UndefinedGameFormat);
const otherNegMarker = {
player: otherPlayer,
position: 10,
points: 0,
isLastWord: false,
};
cycle.addWrongBuzz(otherNegMarker, 1, GameFormats.UndefinedGameFormat);
const marker = {
player,
position: 15,
points: 10,
isLastWord: false,
};
cycle.addCorrectBuzz(marker, 1, GameFormats.UndefinedGameFormat, 0, 3);
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect((_a = cycle.correctBuzz) === null || _a === void 0 ? void 0 : _a.tossupIndex).to.equal(1);
chai_1.expect((_b = cycle.correctBuzz) === null || _b === void 0 ? void 0 : _b.marker).to.deep.equal(marker);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
verifyDefaultBonusAnswer(cycle.bonusAnswer, 0);
if (cycle.wrongBuzzes == undefined) {
chai_1.assert.fail("wrongBuzzes shouldn't be undefined");
}
chai_1.expect(cycle.wrongBuzzes.length).to.equal(1);
const wrongBuzz = cycle.wrongBuzzes[0];
chai_1.expect(wrongBuzz.marker.player).to.equal(otherPlayer);
chai_1.expect(wrongBuzz.marker.position).to.equal(otherNegMarker.position);
chai_1.expect(wrongBuzz.marker.points).to.equal(GameFormats.UndefinedGameFormat.negValue);
});
it("Adds buzz with non-3-part bonus", () => {
var _a, _b;
const cycle = new Cycle_1.Cycle();
const marker = {
player: new TeamState_1.Player("Alice", "Alpha", /* isStarter */ true),
position: 10,
points: 10,
};
cycle.addCorrectBuzz(marker, 2, GameFormats.UndefinedGameFormat, 1, 2);
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect((_a = cycle.correctBuzz) === null || _a === void 0 ? void 0 : _a.tossupIndex).to.equal(2);
chai_1.expect((_b = cycle.correctBuzz) === null || _b === void 0 ? void 0 : _b.marker).to.deep.equal(marker);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
chai_1.expect(cycle.bonusAnswer.parts.length).to.equal(2);
chai_1.expect(cycle.bonusAnswer.parts.map((part) => part.points)).to.deep.equal([0, 0]);
chai_1.expect(cycle.bonusAnswer.parts.map((part) => part.teamName)).to.deep.equal(["", ""]);
chai_1.expect(cycle.bonusAnswer.parts[0]).to.not.equal(cycle.bonusAnswer.parts[1]);
});
});
describe("setBonusPartAnswer", () => {
it("Scoring bonus with no correct buzz does nothing", () => {
const cycle = new Cycle_1.Cycle();
cycle.setBonusPartAnswer(0, "A", 10);
chai_1.expect(cycle.bonusAnswer).to.not.exist;
});
it("Set first part", () => {
const cycle = new Cycle_1.Cycle();
addDefaultCorrectBuzz(cycle);
cycle.setBonusPartAnswer(0, "Alpha", 10);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
chai_1.expect(cycle.bonusAnswer.parts[0]).to.deep.equal({ teamName: "Alpha", points: 10 });
chai_1.expect(cycle.bonusAnswer.parts.slice(1).map((part) => part.teamName)).to.deep.equal(["", ""]);
chai_1.expect(cycle.bonusAnswer.parts.slice(1).map((part) => part.points)).to.deep.equal([0, 0]);
});
it("Bonus with bouncebacks has different teams", () => {
const cycle = new Cycle_1.Cycle();
addDefaultCorrectBuzz(cycle);
cycle.setBonusPartAnswer(0, "Alpha", 10);
cycle.setBonusPartAnswer(2, "Beta", 10);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
chai_1.expect(cycle.bonusAnswer.parts).to.deep.equal([
{ teamName: "Alpha", points: 10 },
{ teamName: "", points: 0 },
{ teamName: "Beta", points: 10 },
]);
});
it("Bonus with different point values", () => {
const cycle = new Cycle_1.Cycle();
addDefaultCorrectBuzz(cycle);
cycle.setBonusPartAnswer(1, "Alpha", 10);
cycle.setBonusPartAnswer(2, "Alpha", 15);
if (cycle.bonusAnswer == undefined) {
chai_1.assert.fail("bonus answer was null");
}
chai_1.expect(cycle.bonusAnswer.parts).to.deep.equal([
{ teamName: "", points: 0 },
{ teamName: "Alpha", points: 10 },
{ teamName: "Alpha", points: 15 },
]);
});
});
describe("Update handler", () => {
it("Update handler called on updates", () => {
const cycle = new Cycle_1.Cycle();
let updated = false;
cycle.setUpdateHandler(() => {
updated = true;
});
cycle.addThrownOutTossup(0);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.addWrongBuzz({ player: new TeamState_1.Player("Bob", "Beta", /* isStarter */ true), position: 5, points: -5 }, 1, GameFormats.UndefinedGameFormat);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.addTossupProtest("Beta", 1, 5, "Wrong thing", "Reason");
chai_1.expect(updated).to.be.true;
updated = false;
addDefaultCorrectBuzz(cycle);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.addThrownOutBonus(0);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.setBonusPartAnswer(0, "Alpha", 10);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.addBonusProtest(1, 1, "Wrong", "Reason", "Alpha");
chai_1.expect(updated).to.be.true;
updated = false;
cycle.removeBonusProtest(1);
chai_1.expect(updated).to.be.true;
updated = false;
const newPlayer = { name: "Alvaro", teamName: "Alpha", isStarter: false };
cycle.addPlayerJoins(newPlayer);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.addSwapSubstitution(newPlayer, { name: "Alice", teamName: "Alpha", isStarter: true });
chai_1.expect(updated).to.be.true;
updated = false;
cycle.addPlayerLeaves({ name: "Anna", teamName: "Alpha", isStarter: false });
chai_1.expect(updated).to.be.true;
updated = false;
if (cycle.playerLeaves == undefined) {
chai_1.assert.fail("There should be a player leaves event");
}
cycle.removePlayerLeaves(cycle.playerLeaves[0]);
chai_1.expect(updated).to.be.true;
updated = false;
if (cycle.subs == undefined) {
chai_1.assert.fail("There should be a substitution event");
}
cycle.removeSubstitution(cycle.subs[0]);
chai_1.expect(updated).to.be.true;
updated = false;
if (cycle.playerJoins == undefined) {
chai_1.assert.fail("There should be a player joins event");
}
cycle.removePlayerJoins(cycle.playerJoins[0]);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.removeThrownOutBonus(0);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.removeCorrectBuzz();
chai_1.expect(updated).to.be.true;
updated = false;
cycle.removeTossupProtest("Beta");
chai_1.expect(updated).to.be.true;
updated = false;
cycle.removeWrongBuzz(new TeamState_1.Player("Bob", "Beta", /* isStarter */ true), GameFormats.UndefinedGameFormat);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.removeThrownOutTossup(0);
chai_1.expect(updated).to.be.true;
updated = false;
});
it("Update handler can be cleared", () => {
const cycle = new Cycle_1.Cycle();
let updated = false;
cycle.setUpdateHandler(() => {
updated = true;
});
const newPlayer = { name: "Alvaro", teamName: "Alpha", isStarter: false };
cycle.addPlayerJoins(newPlayer);
chai_1.expect(updated).to.be.true;
updated = false;
cycle.setUpdateHandler(() => {
return;
});
cycle.addPlayerJoins(newPlayer);
chai_1.expect(updated).to.be.false;
});
});
describe("removeNewPlayerEvents", () => {
it("Remove new player's correct buzz and bonus events", () => {
var _a, _b, _c, _d, _e;
const newPlayer = new TeamState_1.Player("Newton", "Alpha", false);
const existingPlayer = new TeamState_1.Player("Zach", "Zeta", true);
const cycle = new Cycle_1.Cycle();
cycle.addPlayerJoins(newPlayer);
cycle.addWrongBuzz({ player: existingPlayer, points: -5, position: 10 }, 1, GameFormats.UndefinedGameFormat);
cycle.addCorrectBuzz({ player: newPlayer, points: 10, position: 20 }, 1, GameFormats.UndefinedGameFormat, 0, 3);
cycle.addBonusProtest(0, 0, "Answer", "Reason", newPlayer.teamName);
cycle.setBonusPartAnswer(1, newPlayer.teamName, 10);
cycle.setBonusPartAnswer(2, newPlayer.teamName, 10);
// Verify we have the parts and protests and buzzes
if (cycle.playerJoins == undefined) {
chai_1.assert.fail("There should be a player joins event");
}
chai_1.expect(cycle.playerJoins[0].inPlayer).to.equal(newPlayer);
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect((_a = cycle.correctBuzz) === null || _a === void 0 ? void 0 : _a.marker.player).to.equal(newPlayer);
if (cycle.bonusProtests == undefined) {
chai_1.assert.fail("bonus protest should exist before we remove the player");
}
chai_1.expect(cycle.bonusProtests[0].teamName).to.equal(newPlayer.teamName);
chai_1.expect(cycle.bonusAnswer).to.exist;
chai_1.expect((_b = cycle.bonusAnswer) === null || _b === void 0 ? void 0 : _b.receivingTeamName).to.equal(newPlayer.teamName);
chai_1.expect((_c = cycle.bonusAnswer) === null || _c === void 0 ? void 0 : _c.parts[1]).to.exist;
chai_1.expect((_d = cycle.bonusAnswer) === null || _d === void 0 ? void 0 : _d.parts[2]).to.exist;
cycle.removeNewPlayerEvents(newPlayer);
// Verify that the parts are gone
chai_1.expect(cycle.playerJoins.length).to.equal(0);
chai_1.expect(cycle.correctBuzz).to.not.exist;
chai_1.expect(cycle.bonusAnswer).to.not.exist;
chai_1.expect(cycle.bonusProtests).to.not.exist;
chai_1.expect((_e = cycle.wrongBuzzes) === null || _e === void 0 ? void 0 : _e.length).to.equal(1);
});
it("Remove new player's wrong buzz and protest", () => {
var _a;
const newPlayer = new TeamState_1.Player("Newton", "Alpha", false);
const existingPlayer = new TeamState_1.Player("Zach", "Zeta", true);
const cycle = new Cycle_1.Cycle();
cycle.addPlayerJoins(newPlayer);
cycle.addWrongBuzz({ player: newPlayer, points: -5, position: 10 }, 1, GameFormats.UndefinedGameFormat);
cycle.addTossupProtest(newPlayer.teamName, 1, 10, "Answer", "Reason");
cycle.addCorrectBuzz({ player: existingPlayer, points: 10, position: 20 }, 1, GameFormats.UndefinedGameFormat, 0, 3);
cycle.addBonusProtest(0, 0, "Answer", "Reason", existingPlayer.teamName);
cycle.setBonusPartAnswer(1, existingPlayer.teamName, 10);
// Verify we have the parts and protests and buzzes
if (cycle.playerJoins == undefined) {
chai_1.assert.fail("There should be a player joins event");
}
chai_1.expect(cycle.playerJoins[0].inPlayer).to.equal(newPlayer);
if (cycle.wrongBuzzes == undefined) {
chai_1.assert.fail("There should be at least one wrong buzz");
}
chai_1.expect(cycle.wrongBuzzes[0].marker.player).to.equal(newPlayer);
if (cycle.tossupProtests == undefined) {
chai_1.assert.fail("bonus protest should exist before we remove the player");
}
chai_1.expect(cycle.tossupProtests[0].teamName).to.equal(newPlayer.teamName);
cycle.removeNewPlayerEvents(newPlayer);
// Verify that the parts are gone
chai_1.expect(cycle.playerJoins.length).to.equal(0);
chai_1.expect((_a = cycle.wrongBuzzes) === null || _a === void 0 ? void 0 : _a.length).to.equal(0);
chai_1.expect(cycle.tossupProtests.length).to.equal(0);
// Other player events shouldn't be changed
chai_1.expect(cycle.correctBuzz).to.exist;
chai_1.expect(cycle.bonusAnswer).to.exist;
});
});
});
//# sourceMappingURL=CycleTests.js.map