-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
181 lines (159 loc) · 5.47 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
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
var assert = require('assert')
var ircMessage = require('./')
var parse = ircMessage.parse
describe('#parse()', function() {
it('parses valid IRC messages', function() {
var tests = {
'FOO': {
tags: {},
prefix: null,
command: 'FOO',
params: []
},
':test FOO': {
tags: {},
prefix: 'test',
command: 'FOO',
params: []
},
':test FOO ': {
tags: {},
prefix: 'test',
command: 'FOO',
params: []
},
':[email protected] PRIVMSG #Test :This is a test': {
tags: {},
prefix: '[email protected]',
command: 'PRIVMSG',
params: ['#Test', 'This is a test']
},
'PRIVMSG #Test :This is a test': {
tags: {},
prefix: null,
command: 'PRIVMSG',
params: ['#Test', 'This is a test']
},
':test PRIVMSG foo :A string with spaces ': {
tags: {},
prefix: 'test',
command: 'PRIVMSG',
params: ['foo', 'A string with spaces ']
},
':test PRIVMSG foo :bar': {
tags: {},
prefix: 'test',
command: 'PRIVMSG',
params: ['foo', 'bar']
},
':test FOO bar baz quux': {
tags: {},
prefix: 'test',
command: 'FOO',
params: ['bar', 'baz', 'quux']
},
'FOO bar baz quux': {
tags: {},
prefix: null,
command: 'FOO',
params: ['bar', 'baz', 'quux']
},
'FOO bar baz quux': {
tags: {},
prefix: null,
command: 'FOO',
params: ['bar', 'baz', 'quux']
},
'FOO bar baz quux :This is a test': {
tags: {},
prefix: null,
command: 'FOO',
params: ['bar', 'baz', 'quux', 'This is a test']
},
':test PRIVMSG #fo:oo :This is a test': {
tags: {},
prefix: 'test',
command: 'PRIVMSG',
params: ['#fo:oo', 'This is a test']
},
'@test=super;single :[email protected] FOO bar baz quux :This is a test': {
tags: {
test: 'super',
single: true
},
prefix: '[email protected]',
command: 'FOO',
params: ['bar', 'baz', 'quux', 'This is a test']
}
}
Object.keys(tests).forEach(function(test) {
var expected = tests[test]
expected.raw = test
var parsed = parse(test)
assert.deepEqual(parsed, expected)
})
})
})
describe('#parserStream()', function() {
var assertMsg = function(msg, tags, prefix, command, params) {
assert.deepEqual(msg.tags, tags)
assert.deepEqual(msg.prefix, prefix)
assert.deepEqual(msg.command, command)
assert.deepEqual(msg.params, params)
}
it('parses a single IRC message', function(done) {
var stream = ircMessage.createStream()
stream.once('data', function(m) {
assertMsg(m, {}, 'user', 'HOW', ['are', 'you doing?'])
done()
})
stream.write(':user HOW are :you doing?\r\n')
})
it('correctly buffers messages before parsing', function(done) {
var stream = ircMessage.createStream()
var gotFirst = false
stream.on('data', function(m) {
if (!gotFirst) {
gotFirst = true
return assertMsg(m, {}, 'user', 'HOW', ['are', 'you doing?'])
}
assertMsg(m, {}, null, 'ERROR', ['Link closed (bad!)'])
done()
})
stream.write(':user H')
stream.write('OW are :you doi')
stream.write('ng?\r\nERROR :Link closed (bad!)')
stream.write('\r\n')
})
it('converts valid time tags into Date objects', function(done) {
var stream = ircMessage.createStream({ convertTimestamps: true })
stream.once('data', function(m) {
assert.ok(m.tags.time instanceof Date)
done()
})
stream.write('@time=2012-05-21T23:32:12.419Z :some@user PRIVMSG #t :hi\r\n')
})
it('returns invalid date with bad time tag', function(done) {
var stream = ircMessage.createStream({ convertTimestamps: true })
stream.once('data', function(m) {
assert.equal(m.tags.time.toString(), 'Invalid Date')
done()
})
stream.write('@time=notvalid :some@user PRIVMSG #t :hi\r\n')
})
it('converts the prefix to an object', function(done) {
var stream = ircMessage.createStream({ parsePrefix: true })
stream.once('data', function(m) {
assert.equal(m.prefix.isServer, false)
done()
})
stream.write(':some!one@host PRIVMSG #t :hi\r\n')
})
it('handles null values correctly', function(done) {
var stream = ircMessage.createStream()
stream.on('error', function() {
done()
})
stream.write(':::\r\n')
})
})