Skip to content

Commit 80fe989

Browse files
committed
update
1 parent 6a2ae77 commit 80fe989

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

test/mocks/parseCommand.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = (client, content) => {
2+
let args = content.slice(client.config.discord.prefix.length).trim().split(/ +/g)
3+
args = args.map((arg) => client.translator.reverse(arg.toLowerCase().replace(/_/g, ' '), true).toLowerCase())
4+
args.shift().toLowerCase()
5+
6+
let initialArgs
7+
if (args.includes('|')) {
8+
initialArgs = args.join(' ').split('|').map((com) => com.split(' ').filter((a) => a))
9+
} else {
10+
initialArgs = [args]
11+
}
12+
return initialArgs
13+
}
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const { assert } = require('chai')
2+
const config = require('config')
3+
const { log } = require('../../../../src/lib/logger')
4+
const Discord = require('../../../mocks/FakeDiscord')
5+
const discordMessaveValidator = require('../../../mocks/discordMessageValidator')
6+
const mustache = require('../../../../src/lib/handlebars')()
7+
const { getKnex } = require('../../../../src/lib/configFetcher')
8+
const Controller = require('../../../../src/controllers/controller')
9+
const Translator = require('../../../../src/util/translate')
10+
const messageContentParser = require('../../../mocks/parseCommand')
11+
12+
const translator = new Translator(config.general.locale)
13+
const knex = getKnex(
14+
{
15+
database: {
16+
client: 'sqlite',
17+
},
18+
},
19+
)
20+
const controller = new Controller(knex, config)
21+
const location = require('../../../../src/lib/discord/commando/commands/location')
22+
23+
const client = new Discord(controller, config, log, mustache, translator)
24+
25+
const testMessages = require('../../../testData/commandMessages')(client)
26+
27+
describe('!location command tests', () => {
28+
29+
beforeEach(async () => {
30+
await knex('humans').truncate()
31+
client.setDefaults()
32+
testMessages.location.humans.map(async (h) => {
33+
await controller.insertQuery('humans', h)
34+
})
35+
})
36+
37+
it('Should update the location of a registered user', async () => {
38+
await location.run(client, testMessages.location.happy, messageContentParser(client, testMessages.location.happy.content))
39+
const result = await controller.selectOneQuery('humans', { id: '222' })
40+
assert.equal(result.latitude, 59.4372155)
41+
assert.equal(result.longitude, 24.7453688)
42+
assert.equal(client.lastReact, '✅')
43+
assert.equal(client.lastMessage, ':wave:, I set registeredUserInTallinns location to : \nhttps://www.google.com/maps/search/?api=1&query=59.4372155,24.7453688')
44+
assert.equal(discordMessaveValidator(client.lastMessage, 'bot'), true)
45+
})
46+
47+
it('Should ignore !location of an unregistered user', async () => {
48+
await location.run(client, testMessages.location.sad, messageContentParser(client, testMessages.location.sad.content))
49+
const result = await controller.selectOneQuery('humans', { id: '15867' })
50+
assert.notExists(result)
51+
assert.equal(client.lastReact, '🙅')
52+
assert.equal(client.lastMessage, '')
53+
assert.equal(discordMessaveValidator(client.lastMessage, 'bot'), true)
54+
})
55+
56+
it('Should update location of a channel when admin asks', async () => {
57+
await location.run(client, testMessages.location.happyAdmin, messageContentParser(client, testMessages.location.happyAdmin.content))
58+
const result = await controller.selectOneQuery('humans', { id: '333' })
59+
assert.equal(result.latitude, 59.4372155)
60+
assert.equal(result.longitude, 24.7453688)
61+
assert.equal(client.lastReact, '✅')
62+
assert.equal(client.lastMessage, ':wave:, I set adminSetLocationInThisChannels location to : \nhttps://www.google.com/maps/search/?api=1&query=59.4372155,24.7453688')
63+
assert.equal(discordMessaveValidator(client.lastMessage, 'bot'), true)
64+
})
65+
66+
it('Should update location of a webhook when admin asks', async () => {
67+
await location.run(client, testMessages.location.happyAdminWebhook, messageContentParser(client, testMessages.location.happyAdminWebhook.content))
68+
const result = await controller.selectOneQuery('humans', { name: 'registeredwebhook' })
69+
assert.equal(result.latitude, 59.4372155)
70+
assert.equal(result.longitude, 24.7453688)
71+
assert.equal(client.lastReact, '✅')
72+
assert.equal(client.lastMessage, ':wave:, I set registeredwebhooks location to : \nhttps://www.google.com/maps/search/?api=1&query=59.4372155,24.7453688')
73+
assert.equal(discordMessaveValidator(client.lastMessage, 'bot'), true)
74+
})
75+
76+
it('Should advise accordingly if webhook is not registered and admin asks', async () => {
77+
await location.run(client, testMessages.location.sadAdminWebhook, messageContentParser(client, testMessages.location.sadAdminWebhook.content))
78+
assert.equal(client.lastReact, '')
79+
assert.equal(client.lastMessage, `Webhook registeredwebhookthatdoesntexist does not seem to be registered. add it with ${config.discord.prefix}webhook add <Your-Webhook-url>`)
80+
assert.equal(discordMessaveValidator(client.lastMessage, 'bot'), true)
81+
})
82+
83+
it('Should advise accordingly if channel is not registered and admin asks', async () => {
84+
await location.run(client, testMessages.location.sadAdmin, messageContentParser(client, testMessages.location.sadAdmin.content))
85+
assert.equal(client.lastReact, '')
86+
assert.equal(client.lastMessage, `channelUnregistered does not seem to be registered. add it with ${config.discord.prefix}channel add`)
87+
assert.equal(discordMessaveValidator(client.lastMessage, 'bot'), true)
88+
})
89+
})

0 commit comments

Comments
 (0)