-
Notifications
You must be signed in to change notification settings - Fork 15
/
bot.test.js
189 lines (160 loc) · 5.32 KB
/
bot.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* eslint-env mocha */
const assert = require('chai').assert;
const util = require('util');
const sinon = require('sinon');
const bot = require('./bot.js');
function mockClient() {
return {
say: sinon.spy(),
maxLineLength: 500,
opt: {
messageSplit: 400,
},
};
}
describe('bot.getReply', () => {
it('should work for regular looking stuff', () => {
const tests = [
[['foo'], 'foo'],
[[true], 'true'],
[[1], '1'],
[['x'.repeat(500)], 'x'.repeat(500)], // line splitting happens a layer below, we gucci
[['x\ny'], 'x\ny'], // line splitting happens a layer below, we gucci
];
for (let i = 0; i < tests.length; i += 1) {
bot.client = mockClient();
const reply = bot.getReply('#foo', false, 'jim');
reply.apply(bot, tests[i][0]);
const s = bot.client.say;
assert.isTrue(s.called, 'say not called');
assert.isTrue(s.calledOnce, `say called too many times: ${JSON.stringify(s.getCalls())}`);
assert.isTrue(s.calledWith('#foo', tests[i][1]), `wrong args. Expected #foo ${tests[i][1]}, got ${util.inspect(s.getCall(0).args)}`);
}
});
it('should handle overflow', () => {
const tests = [
[['x\ny\nz'], 'x\ny ...'],
[['x'.repeat(2000)], `${'x'.repeat((bot.client.opt.messageSplit * 2) - 4)} ...`],
];
for (let i = 0; i < tests.length; i += 1) {
bot.client = mockClient();
const reply = bot.getReply('#foo', false, 'jim');
reply.apply(bot, tests[i][0]);
const s = bot.client.say;
assert.isTrue(s.called, 'say not called');
assert.isTrue(s.calledOnce, `say called too many times: ${JSON.stringify(s.getCalls())}`);
assert.isTrue(s.calledWith('#foo', tests[i][1]), `wrong args. Expected #foo ${tests[i][1]}, got ${util.inspect(s.getCall(0).args)}`);
}
});
it('should spam with reply.spam', () => {
const tests = [
[['x\ny\nz\nalpha'], 'x\ny\nz\nalpha'],
[['x'.repeat(2000)], 'x'.repeat(2000)],
];
for (let i = 0; i < tests.length; i += 1) {
bot.client = mockClient();
const reply = bot.getReply('#foo', false, 'holden');
reply.spam.apply(bot, tests[i][0]);
const s = bot.client.say;
assert.isTrue(s.called, 'say not called');
assert.isTrue(s.calledOnce, `say called too many times: ${JSON.stringify(s.getCalls())}`);
assert.isTrue(s.calledWith('#foo', tests[i][1]), `wrong args. Expected #foo ${tests[i][1]}, got ${util.inspect(s.getCall(0).args)}`);
}
});
it('should spam by default in pm', () => {
const tests = [
[['x\ny\nz\nalpha'], 'x\ny\nz\nalpha'],
[['x'.repeat(2000)], 'x'.repeat(2000)],
];
for (let i = 0; i < tests.length; i += 1) {
bot.client = mockClient();
const reply = bot.getReply('timmy', true, 'timmy');
reply.apply(bot, tests[i][0]);
const s = bot.client.say;
assert.isTrue(s.called, 'say not called');
assert.isTrue(s.calledOnce, `say called too many times: ${JSON.stringify(s.getCalls())}`);
assert.isTrue(s.calledWith('timmy', tests[i][1]), `wrong args. Expected #foo ${tests[i][1]}, got ${util.inspect(s.getCall(0).args)}`);
}
});
it('should work with reply.custom', () => {
const tests = [
{
// defaulting
opts: {},
input: [' foo bar baz '],
output: 'foo bar baz',
pmOutput: null,
},
{
opts: {
trim: false,
},
input: [' foo bar baz '],
output: ' foo bar baz ',
pmOutput: null,
},
{
opts: {
lines: 5,
},
input: ['SAY\nWHAT\nONE\nMORE\nTIME'],
output: 'SAY\nWHAT\nONE\nMORE\nTIME',
pmOutput: null,
},
{
opts: {
lines: 5,
},
input: ['SAY\nWHAT\nONE\nMORE\nTIME\nI\nDARE\nYOU'],
output: 'SAY\nWHAT\nONE\nMORE\nTIME ...',
pmOutput: null,
},
{
opts: {
replaceNewlines: true,
},
input: ['SAY\nWHAT\nONE\nMORE\nTIME\nI\nDARE\nYOU'],
output: 'SAY | WHAT | ONE | MORE | TIME | I | DARE | YOU',
pmOutput: null,
},
{
opts: {
lines: 1,
pmExtra: true,
},
input: ['SAY\nWHAT'],
output: 'SAY ...',
pmOutput: 'SAY\nWHAT',
},
{
opts: {
lines: 1,
replaceNewlines: true,
pmExtra: true,
},
input: [`${'x'.repeat('350')}\n${'x'.repeat('350')}`],
output: `${'x'.repeat('350')} | ${'x'.repeat(400 - 350 - 3 - 4)} ...`,
pmOutput: `${'x'.repeat('350')}\n${'x'.repeat('350')}`,
},
];
tests.forEach((test) => {
bot.client = mockClient();
const reply = bot.getReply('#beep', false, 'timmy');
reply.custom(test.opts, ...test.input);
const s = bot.client.say;
assert.isTrue(s.called, 'say not called');
const calledTimes = test.pmOutput ? 2 : 1;
assert.equal(s.getCalls().length, calledTimes);
assert.isTrue(
s.calledWith('#beep', test.output),
`wrong args. Expected #beep ${test.output}, got ${util.inspect(s.getCall(0).args)}`,
);
if (test.pmOutput) {
assert.isTrue(
s.calledWith('timmy', test.pmOutput),
`wrong args. Expected #beep ${test.output}, got ${util.inspect(s.getCall(1).args)}`,
);
}
});
});
});