-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
123 lines (101 loc) · 3.83 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import test from 'ava';
import { platform } from 'os';
import Discord from 'discord.js';
const middlewares = require('./middleware');
// These are standard properties that exist in all types
const checkStandardProperties = (t, message) => {
t.is(message.text, 'this is an ambient message');
t.is(message.channel.name, 'foobar');
}
test('Normalize: Process Text Channel', t => {
const mockNormalize = require('./mocks/normalize-ambient');
const normalizedAmbient = middlewares.normalize.exec({}, mockNormalize);
t.is(normalizedAmbient.user.id, '237437571875995648');
t.is(normalizedAmbient.user.username, 'codingbyhim');
t.is(normalizedAmbient.type, 'text');
t.is(normalizedAmbient.guild.name, 'foobar');
t.is(normalizedAmbient.guild.id, "1234");
checkStandardProperties(t, normalizedAmbient);
});
test('Normalize: Process DM Channel', t => {
const mockNormalize = require('./mocks/normalize-dm');
const normalizedDM = middlewares.normalize.exec({}, mockNormalize);
t.is(normalizedDM.type, 'dm');
t.is(normalizedDM.lastMessage, 'sample');
t.is(normalizedDM.user.username, 'codingbyhim');
checkStandardProperties(t, normalizedDM);
});
test('Categorize: Check Accurate Types', t => {
const mockNormalize = require('./mocks/normalize-ambient');
const normalizedAmbient = middlewares.normalize.exec({}, mockNormalize);
const botStub = {
botkit: {
config: {
client: {
user: {
id: 123456
}
}
}
}
};
const ambientMessage = Object.assign({}, normalizedAmbient);
ambientMessage.guildId = '123456';
const ambientMessageCategorize = middlewares.categorize.exec(botStub, ambientMessage);
t.is(ambientMessageCategorize.type, 'ambient');
// Format the normalized ambient message to meet direct_message criteria
const insertMessage = normalizedAmbient;
insertMessage.type = 'dm';
delete insertMessage.guildId;
const messageReceived = middlewares.categorize.exec(botStub, insertMessage);
t.is(messageReceived.type, 'direct_message');
insertMessage.text = '<@123456> hello!';
const directMention = middlewares.categorize.exec(botStub, insertMessage);
t.is(directMention.type, 'direct_mention');
insertMessage.text = 'hello! <@123456>';
const mentionMessage = middlewares.categorize.exec(botStub, insertMessage);
t.is(mentionMessage.type, 'mention');
insertMessage.text = undefined;
const undefMessage = middlewares.categorize.exec(botStub, insertMessage);
t.is(mentionMessage.type, 'mention');
});
test('Format: Correct Attachments, Embeds, and Responses', t => {
const mockNormalize = require('./mocks/normalize-ambient');
const normalizedAmbient = middlewares.normalize.exec({}, mockNormalize);
normalizedAmbient.response = {
text: "hello back"
};
const platformMessage = middlewares.format.exec({}, normalizedAmbient, {});
t.is(platformMessage.channel.id, '518588622123827211');
t.is(platformMessage.text, 'hello back');
const embedPlatformMessage = middlewares.format.exec({}, {
response: new Discord.RichEmbed({
author: "test author"
})
}, {});
t.is(embedPlatformMessage.options.author, 'test author')
const attachPlatformMessage = middlewares.format.exec({}, {
response: new Discord.Attachment('test.js', 'test123')
}, {});
t.deepEqual(attachPlatformMessage.options.file.attachment, 'test.js')
});
test('Receive: Attach relevant API methods', t => {
const stubBot = {};
const stubNext = () => {};
middlewares.receive.handler(stubBot, {
member: {
voiceChannel: {
join: () => {},
leave: () => {}
}
}
}, stubNext);
t.is(typeof stubBot.api.joinVoiceChannel, 'function');
t.is(typeof stubBot.api.leaveVoiceChannel, 'function');
// Test to make sure that direct_messages don't have voice helpers
middlewares.receive.handler(stubBot, {
type: 'direct_message'
}, stubNext);
t.is(typeof stubBot.api.joinVoiceChannel, 'undefined');
t.is(typeof stubBot.api.leaveVoiceChannel, 'undefined');
});