Files
decision-maker/backend/nodejs/test/random-decision.js
2021-04-05 20:49:54 +02:00

48 lines
2.1 KiB
JavaScript

const assert = require('assert');
const should = require('should');
const randomDecision = require('../random-decision');
describe('randomDecision', function () {
describe('#getRandomChoice()', function () {
it(`should return one of available choices`, function () {
randomDecision.getRandomChoice().should.equalOneOf(randomDecision.choices);
});
it('should return the first choice sometimes', function () {
const expectedChoice = randomDecision.choices[0];
const selectedChoices = new Set();
for (let i = 0; i <= 1000; i++) {
const decision = randomDecision.getRandomChoice();
selectedChoices.add(decision);
if (decision === expectedChoice) {
should.ok(decision, 'First choice was found once');
return;
}
}
should.fail(selectedChoices, expectedChoice, 'First choice was never selected');
})
it('should return the last choice sometimes', function () {
const expectedChoice = randomDecision.choices[randomDecision.choices.length - 1];
const selectedChoices = new Set();
for (let i = 0; i <= 1000; i++) {
const decision = randomDecision.getRandomChoice();
selectedChoices.add(decision);
if (decision === expectedChoice) {
should.ok(decision, 'Last choice was found once');
return;
}
}
should.fail(selectedChoices, expectedChoice, 'Last choice was never selected');
})
})
describe('#getRandomChoice(seed)', function () {
it('should always select the same value when the seed is the same', function () {
const seed = new Date();
const initialDecision = randomDecision.getRandomChoice(seed);
for (let i = 0; i <= 1000; i++) {
let randomChoice = randomDecision.getRandomChoice(seed);
assert.strictEqual(randomChoice, initialDecision);
}
})
})
})