Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

blitz

node v8.17.0
version: 3.0.0
endpointsharetweet
solution.js
const express = require('express'); const { BEEP_CODES } = require('@yandex-blitz/phone'); const createApp = ({ phone }) => { const app = express(); // звонит по номеру записанному в "быстром наборе" под цифрой digit app.get("/speeddial/:digit", async (req, res) => { phone.getData().then(value => { const speeddialDict = JSON.parse(value); phone.connect().then(() => { phone.dial(speeddialDict[req.params.digit]); res.sendStatus(200); }); }).catch(() => { phone.beep(BEEP_CODES.ERROR); res.sendStatus(500); }); }); // записывает в "быстрый набор" под цифру digit номер phonenumber app.post("/speeddial/:digit/:phonenumber", (req, res) => { phone.getData() .then(value => { const speeddialDict = JSON.parse(value); speeddialDict[req.params.digit] = Number(req.params.phonenumber); return phone.setData(JSON.stringify(speeddialDict)) .then(() => { phone.beep(BEEP_CODES.SUCCESS); res.sendStatus(200); }); }) .catch(() => { phone.beep(BEEP_CODES.ERROR); res.sendStatus(500); }); }); return app; }; exports.createApp = createApp;
solution.test.js
const assert = require('assert'); const { tests } = require('mocha-runkit'); const request = require('supertest'); const sinon = require('sinon'); const { createPhone } = require('@yandex-blitz/phone'); await tests({ 'после записи в "быстрый набор" можно позвонить по записанному номеру': async () => { const phone = createPhone({ data: '{}' }); sinon.spy(phone, 'dial'); const app = createApp({ phone }); await request(app).post('/speeddial/1/635889'); await request(app).get('/speeddial/1'); const number = phone.dial.args[0][0]; assert.equal(number, 635889, 'номер должен совпадать с желаемым'); }, 'телефон должен бибикнуть ошибкой при быстром наборе номера, которого нет в словаре': async () => { const phone = createPhone({ data: '{}' }); sinon.spy(phone, 'beep'); const app = createApp({ phone }); await request(app).get('/speeddial/1'); const code = phone.beep.args[0][0]; assert.equal(code, BEEP_CODES.ERROR); }, 'при невозможности подключиться к линии телефон должен бибикнуть фатальной ошибкой': async () => { const phone = createPhone({ data: '{}' }); sinon.spy(phone, 'beep'); sinon.stub(phone, 'connect').rejects(new Error('unable to connect')); const app = createApp({ phone }); await request(app).get('/speeddial/1'); const code = phone.beep.args[0][0]; assert.equal(code, BEEP_CODES.FATAL); }, 'при записи двух подряд номеров в быстрый набор должны сохраниться оба номера': async () => { const phone = createPhone({ data: '{}' }); sinon.spy(phone, 'dial'); const app = createApp({ phone }); await Promise.all([ request(app).post('/speeddial/1/635889'), request(app).post('/speeddial/2/241983') ]); await request(app).get('/speeddial/1'); assert.equal(phone.dial.args[0][0], 635889); await request(app).get('/speeddial/2'); assert.equal(phone.dial.args[1][0], 241983); } });
Loading…

no comments

    sign in to comment