Skip to content

Commit

Permalink
Merge pull request #20 from telegram-ru/testing
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
sergeysova authored Nov 30, 2017
2 parents 6008c3a + 3c3e820 commit ac3295f
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 5 deletions.
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Telegram bot to manage spam and rules for @_ru community",
"main": "src/index.js",
"scripts": {
"test": "npm run test:lint",
"test": "npm run test:lint && npm run test:code",
"test:code": "ava",
"test:lint": "eslint .",
"dev": "cross-env DEBUG=rubot:* nodemon -e yaml,js,json -w locales -w src ./src",
"start": "pm2 startOrRestart ./process.config.js",
Expand All @@ -31,13 +32,17 @@
},
"homepage": "https://github.com/LestaD/ru_bot#readme",
"devDependencies": {
"ava": "^0.24.0",
"cross-env": "^5.1.1",
"eslint": "^4.12.0",
"eslint-config-atomix-base": "^5.0.0",
"faker": "^4.1.0",
"husky": "^0.14.3",
"nodemon": "^1.12.1",
"nyc": "^11.3.0",
"pm2": "^2.8.0",
"sequelize-cli": "^3.1.0"
"sequelize-cli": "^3.1.0",
"sinon": "^4.1.2"
},
"dependencies": {
"botanio": "0.0.6",
Expand All @@ -47,5 +52,10 @@
"pg-hstore": "^2.3.2",
"sequelize": "^4.23.2",
"telegraf": "^3.16.4"
},
"ava": {
"files": [
"src/**/*.test.js"
]
}
}
25 changes: 25 additions & 0 deletions src/features/get-id/get-id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import test from 'ava'
import { Extra } from 'telegraf'

import { Context } from '../../tests/telegraf'
import installFeature from './index'

/* eslint-disable no-magic-numbers */

test('!id command reply with chat id', async (t) => {
t.plan(3)

let testFn = null
const bot = {
hears(re, fn) {
t.is(re.toString(), /^!id$/.toString())
t.true(typeof fn === 'function')
testFn = fn
},
}
const context = Context.create()

installFeature(bot)
testFn(context)
t.true(context.reply.calledWith(context.chat.id, Extra.inReplyTo(context.message.message_id)))
})
85 changes: 85 additions & 0 deletions src/features/private-greetings/private-greetings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import test from 'ava'

import { Context } from '../../tests/telegraf'
import installFeature from './index'

/* eslint-disable no-magic-numbers */

test('/start reply with greet in private', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()

installFeature(bot)

testFn(context)
t.regex(context.reply.getCall(0).args[0], new RegExp(`${context.message.from.first_name}`))
})

test('/start not reply in supergroup', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()
context.chat.$type('supergroup')

installFeature(bot)

testFn(context)
t.true(context.reply.notCalled)
})

test('/start not reply in group', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()
context.chat.$type('group')

installFeature(bot)

testFn(context)
t.true(context.reply.notCalled)
})

test('/start not reply in channel', async (t) => {
let testFn = null
const bot = {
command(cmd, fn) {
t.is(cmd, 'start', 'command is start')
t.true(typeof fn === 'function', 'callback is function')
testFn = fn
},
}
const context = Context.create()

context.message.$from()
context.chat.$type('channel')

installFeature(bot)

testFn(context)
t.true(context.reply.notCalled)
})
4 changes: 1 addition & 3 deletions src/lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ function fullName({ first_name: first, last_name: last, username }) {
* @param {Chat} param0
* @return {string}
*/
function chatTitle({
title, username, id, type,
}) {
function chatTitle({ title, username, id, type }) {
const parts = [
title,
username && `(@${username})`,
Expand Down
55 changes: 55 additions & 0 deletions src/lib/text.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import test from 'ava'
import sinon from 'sinon'
import text from './text'

/* eslint-disable no-magic-numbers */


test('random(variants) should return from variant', (t) => {
const variants = [() => 1, () => 2]
const result = text.random(variants)()

t.true(result === 1 || result === 2)
})

test('random(variants)() should pass arguments', (t) => {
const variants = [sinon.spy(), sinon.spy()]

text.random(variants)(1)
t.true(variants[0].calledWith(1) || variants[1].calledWith(1))
})


test('fullName(user) should get correct result', (t) => {
t.is(text.fullName({ first_name: 'Foo' }), 'Foo')
t.is(text.fullName({ last_name: 'Bar' }), 'Bar')
t.is(text.fullName({ username: 'foobar' }), '(@foobar)')

t.is(text.fullName({ first_name: 'Foo', last_name: 'Bar' }), 'Foo Bar')
t.is(text.fullName({ first_name: 'Foo', username: 'foobar' }), 'Foo (@foobar)')
t.is(text.fullName({ last_name: 'Bar', username: 'foobar' }), 'Bar (@foobar)')
t.is(text.fullName({ first_name: 'Foo', last_name: 'Bar', username: 'foobar' }), 'Foo Bar (@foobar)')
})

test('chatTitle(chat) should return correct name', (t) => {
t.is(text.chatTitle({ type: 'private', id: 1 }), 'private:1')
t.is(text.chatTitle({ type: 'private', id: 1, title: 'Chat' }), 'Chat')
t.is(text.chatTitle({ type: 'private', id: 1, username: 'chat' }), '(@chat)')
t.is(text.chatTitle({ type: 'private', id: 1, title: 'Chat', username: 'chat' }), 'Chat (@chat)')
})

test('select(value, cases, defaultCase) should select from map', (t) => {
const cases = { foo: 1, bar: 2, baz: 3, 555: 4 }

t.is(text.select('foo', cases, 5), 1)
t.is(text.select('bar', cases, 5), 2)
t.is(text.select('baz', cases, 5), 3)
t.is(text.select(555, cases, 5), 4)
t.is(text.select('NOT', cases, 5), 5)
})

test('column(1,2) should join with \n', (t) => {
t.is(text.column('a', 'b', 'c'), 'a\nb\nc')
t.is(text.column('a', 'b'), 'a\nb')
t.is(text.column('a'), 'a')
})
5 changes: 5 additions & 0 deletions src/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"import/no-extraneous-dependencies": "off"
}
}
84 changes: 84 additions & 0 deletions src/tests/telegraf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const sinon = require('sinon')
const faker = require('faker')


const ID_USER_START = 10000
const ID_MESSAGE_START = 1000000
const ID_CHAT_START = 1000

let ID_USER = ID_USER_START
let ID_MESSAGE = ID_MESSAGE_START
let ID_CHAT = ID_CHAT_START

class User {
constructor() {
this.id = ++ID_USER
this.is_bot = false
this.first_name = faker.name.firstName()
this.last_name = faker.name.lastName()
this.username = faker.internet.userName(this.first_name, this.last_name)
}
}

class Chat {
constructor() {
this.id = ++ID_CHAT
this.type = 'private'
}

/**
*
* @param {'private'|'group'|'supergroup'|'channel'} type
*/
$type(type) {
this.type = type
return this
}

/**
*
* @param {string} title
*/
$title(title) {
this.title = title
return this
}
}

class Message {
constructor() {
this.message_id = ++ID_MESSAGE
this.date = Date.now()
this.chat = new Chat()
}

$from() {
this.from = new User()
}
}

class Context {
static create() {
return new this()
}

constructor() {
this.message = new Message()

this.reply = sinon.stub().resolves()
}

get from() {
return this.message.from
}

get chat() {
return this.message.chat
}
}

module.exports = {
Context,
Message,
Chat,
}

0 comments on commit ac3295f

Please sign in to comment.