Compare commits

..

2 Commits

Author SHA1 Message Date
koeberlue
45b39fea22 [backend][nodejs] Return random choice from server 2021-04-05 20:35:03 +02:00
koeberlue
49f1166bb3 [backend][nodejs] Add decision maker with hardcoded decisions A,B,C 2021-04-05 20:33:18 +02:00
6 changed files with 128 additions and 11 deletions

View File

@@ -1,11 +1,11 @@
const http = require('http'); const http = require('http');
const randomDecision = require("./random-decision")
const port = 3000; const port = 3000;
const server = http.createServer((request, response) => { const server = http.createServer((request, response) => {
response.statusCode = 200; response.statusCode = 200;
response.setHeader('Content-Type', 'text/plain'); response.setHeader('Content-Type', 'text/plain');
response.end('Hello World'); response.end(randomDecision.getRandomChoice().toString());
}); });
server.listen(port, () => { server.listen(port, () => {

View File

@@ -521,6 +521,14 @@
"integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==", "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==",
"dev": true "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": { "randombytes": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
@@ -551,6 +559,11 @@
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"dev": true "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": { "serialize-javascript": {
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-5.0.1.tgz", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-5.0.1.tgz",
@@ -560,6 +573,54 @@
"randombytes": "^2.1.0" "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": { "string-width": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",

View File

@@ -11,5 +11,9 @@
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"mocha": "^8.3.2" "mocha": "^8.3.2"
},
"dependencies": {
"random": "^3.0.3",
"should": "^13.2.3"
} }
} }

View 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;

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');
})
})
})

View File

@@ -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);
});
})
})