[backend][nodejs] Add decision maker with hardcoded decisions A,B,C

This commit is contained in:
koeberlue
2021-04-05 20:33:18 +02:00
parent 38ad5fadd0
commit 49f1166bb3
6 changed files with 127 additions and 10 deletions

View File

@@ -0,0 +1,38 @@
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');
})
})
})