Skip to content

Commit

Permalink
feat: recover alice tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Aug 12, 2018
1 parent 42568eb commit f71db67
Showing 1 changed file with 38 additions and 76 deletions.
114 changes: 38 additions & 76 deletions tests/alice.spec.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,49 @@
import { Alice } from '../src/alice';
import { generateRequest } from './testUtils';

// Test for matching all command types

test('matching with string', async done => {
const alice = new Alice();

alice.command('Привет, как дела', ctx => done());
alice.handleRequest(generateRequest('Привет, как дела?'));
});

test('matching with array', async done => {
const alice = new Alice();

alice.command(['привет', 'как дела'], ctx => done());
alice.handleRequest(generateRequest('Привет, как дела?'));
});

test('matching with regexp', async done => {
const alice = new Alice();

alice.command(/[а-яё]+/i, ctx => done());
alice.any(ctx => ctx);
alice.handleRequest(generateRequest('Привет как дела'));
});

test('priority check, strings over regexps', async done => {
const alice = new Alice();
import { Alice } from '../src/';
import { request, getRandomText } from './testUtils';

describe('alice module', () => {
let alice = null;
let randomText = '';
beforeEach(() => {
alice = new Alice();
randomText = getRandomText();
});

alice.command(/[а-яё]+/i, ctx => new Error('Error has occured'));
alice.command('привет', ctx => done());
alice.any(ctx => ctx);
alice.handleRequest(generateRequest('Привет как дела'));
});
test('listening on port & stop listening', async done => {
const server = alice.listen(8123, '/');
setTimeout(() => {
server.stop();
done();
}, 0);
});

test('listenining on port with callback', async done => {
const alice = new Alice();
alice.listen('/', 3000, () => {
alice.stopListening();
done();
test('any command', async () => {
alice.any(ctx => ({ text: randomText }));
const data = await alice.handleRequest(request('ping'));
expect(data.response.text).toBe(randomText);
});
});

test('listening on port with promise', async done => {
const alice = new Alice();
alice.listen('/', 3000).then(() => {
alice.stopListening();
done();
test('text command', async () => {
alice.command('foo bar', ctx => ({ text: randomText }));
const data = await alice.handleRequest(request('foo bar'));
expect(data.response.text).toBe(randomText);
});
});

test('ctx body', async done => {
const alice = new Alice();
alice.command('забронируй встречу в ${where} на ${when}', ctx => {
/*
* Context body parses message and extract phrases
* in brackets!
*/
expect(ctx.body).toEqual({
where: '7-холмов',
when: '18:00',
});
done();
test('regex command', async () => {
alice.command(/foo/, ctx => ({ text: randomText }));
const data = await alice.handleRequest(request('foo bar'));
expect(data.response.text).toBe(randomText);
});
alice.handleRequest(
generateRequest('забронируй встречу в 7-холмов на 18:00'),
);
});

test('handling command resolve with callback', async done => {
const alice = new Alice();
const MOCK_MSG = 'Hello world';
alice.any(ctx => ctx.reply(MOCK_MSG));
alice.handleRequest(generateRequest('hi!'), response => {
expect(response.response.text).toBe(MOCK_MSG);
done();
test('text array command', async () => {
alice.command(['foo', 'foo bar'], ctx => ({ text: randomText }));
const data = await alice.handleRequest(request('foo bar'));
expect(data.response.text).toBe(randomText);
});
});

test('handling command resolve with promise', async () => {
const alice = new Alice();
const MOCK_MSG = 'Hello world';
alice.any(ctx => ctx.reply(MOCK_MSG));
const response = await alice.handleRequest(generateRequest('hi!'));
expect(response.response.text).toBe(MOCK_MSG);
test('matcher command', async () => {
alice.command(ctx => true, ctx => ({ text: randomText }));
const data = await alice.handleRequest(request('foo bar'));
expect(data.response.text).toBe(randomText);
});
});

0 comments on commit f71db67

Please sign in to comment.