[backend][nodejs] Add decision maker with hardcoded decisions A,B,C
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const http = require('http');
|
||||
|
||||
const randomDecision = require("./random-decision")
|
||||
const port = 3000;
|
||||
|
||||
const server = http.createServer((request, response) => {
|
||||
|
||||
61
backend/nodejs/package-lock.json
generated
61
backend/nodejs/package-lock.json
generated
@@ -521,6 +521,14 @@
|
||||
"integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==",
|
||||
"dev": true
|
||||
},
|
||||
"random": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/random/-/random-3.0.3.tgz",
|
||||
"integrity": "sha512-Ppz8eI5oYbzC0dQKW3oSFFtyKnh7B1kN1kD7s6J545/aq+R1+YcqQEDJ5AZ0di3wYWbFV9otf5MaHJAhYH56Fw==",
|
||||
"requires": {
|
||||
"seedrandom": "^3.0.5"
|
||||
}
|
||||
},
|
||||
"randombytes": {
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
|
||||
@@ -551,6 +559,11 @@
|
||||
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
|
||||
"dev": true
|
||||
},
|
||||
"seedrandom": {
|
||||
"version": "3.0.5",
|
||||
"resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz",
|
||||
"integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg=="
|
||||
},
|
||||
"serialize-javascript": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-5.0.1.tgz",
|
||||
@@ -560,6 +573,54 @@
|
||||
"randombytes": "^2.1.0"
|
||||
}
|
||||
},
|
||||
"should": {
|
||||
"version": "13.2.3",
|
||||
"resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz",
|
||||
"integrity": "sha512-ggLesLtu2xp+ZxI+ysJTmNjh2U0TsC+rQ/pfED9bUZZ4DKefP27D+7YJVVTvKsmjLpIi9jAa7itwDGkDDmt1GQ==",
|
||||
"requires": {
|
||||
"should-equal": "^2.0.0",
|
||||
"should-format": "^3.0.3",
|
||||
"should-type": "^1.4.0",
|
||||
"should-type-adaptors": "^1.0.1",
|
||||
"should-util": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"should-equal": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/should-equal/-/should-equal-2.0.0.tgz",
|
||||
"integrity": "sha512-ZP36TMrK9euEuWQYBig9W55WPC7uo37qzAEmbjHz4gfyuXrEUgF8cUvQVO+w+d3OMfPvSRQJ22lSm8MQJ43LTA==",
|
||||
"requires": {
|
||||
"should-type": "^1.4.0"
|
||||
}
|
||||
},
|
||||
"should-format": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://registry.npmjs.org/should-format/-/should-format-3.0.3.tgz",
|
||||
"integrity": "sha1-m/yPdPo5IFxT04w01xcwPidxJPE=",
|
||||
"requires": {
|
||||
"should-type": "^1.3.0",
|
||||
"should-type-adaptors": "^1.0.1"
|
||||
}
|
||||
},
|
||||
"should-type": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/should-type/-/should-type-1.4.0.tgz",
|
||||
"integrity": "sha1-B1bYzoRt/QmEOmlHcZ36DUz/XPM="
|
||||
},
|
||||
"should-type-adaptors": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/should-type-adaptors/-/should-type-adaptors-1.1.0.tgz",
|
||||
"integrity": "sha512-JA4hdoLnN+kebEp2Vs8eBe9g7uy0zbRo+RMcU0EsNy+R+k049Ki+N5tT5Jagst2g7EAja+euFuoXFCa8vIklfA==",
|
||||
"requires": {
|
||||
"should-type": "^1.3.0",
|
||||
"should-util": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"should-util": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/should-util/-/should-util-1.0.1.tgz",
|
||||
"integrity": "sha512-oXF8tfxx5cDk8r2kYqlkUJzZpDBqVY/II2WhvU0n9Y3XYvAYRmeaf1PvvIvTgPnv4KJ+ES5M0PyDq5Jp+Ygy2g=="
|
||||
},
|
||||
"string-width": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
|
||||
|
||||
@@ -11,5 +11,9 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"mocha": "^8.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"random": "^3.0.3",
|
||||
"should": "^13.2.3"
|
||||
}
|
||||
}
|
||||
|
||||
23
backend/nodejs/random-decision.js
Normal file
23
backend/nodejs/random-decision.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const random = require('random');
|
||||
|
||||
class Choice {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
const choices = [
|
||||
new Choice('A'),
|
||||
new Choice('B'),
|
||||
new Choice('C')
|
||||
];
|
||||
|
||||
exports.getRandomChoice = () => {
|
||||
return choices[random.int(0, choices.length - 1)];
|
||||
}
|
||||
|
||||
exports.choices = choices;
|
||||
38
backend/nodejs/test/random-decision.js
Normal file
38
backend/nodejs/test/random-decision.js
Normal 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');
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,9 +0,0 @@
|
||||
var assert = require('assert');
|
||||
|
||||
describe('Array', function () {
|
||||
describe('#indexOf', function () {
|
||||
it('should return -1 when the value is not present', function () {
|
||||
assert.strictEqual([1, 2, 3].indexOf(4), -1);
|
||||
});
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user