This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
146 lines (125 loc) · 3.33 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
var fetch = require('node-fetch')
var assert = require('assert')
var http = require('http')
var WebSocket = require('ws')
var login = require('plug-login')
var socket = require('./')
var room = 'plug-socket-test'
var token
var user
describe('plug.dj', function () {
this.timeout(30000)
it('is reachable', function () {
return fetch('https://plug.dj/').then(function (response) {
return response.text()
}).then(function (body) {
if (body.indexOf('<title>maintenance') !== -1)
throw new Error('plug.dj is currently in maintenance mode.')
})
})
it('can connect as guest and gets a valid auth token', function () {
return login.guest({ authToken: true }).then(function (result) {
assert.ok(result)
assert.ok(result.token)
token = result.token
})
})
})
describe('plug-socket', function () {
this.timeout(30000)
var s
beforeEach(function () {
assert.ok(token, 'has an auth token')
})
afterEach(function () {
if (s) s.close()
})
it('can connect and authenticate with an auth token', function (done) {
s = socket(token)
s.once('ack', function (param) {
assert.strictEqual(param, '1')
done()
})
})
it('can connect without an initial auth token', function (done) {
s = socket()
s.on('ack', assert.fail)
setTimeout(function () {
s.removeListener('ack', assert.fail)
s.on('ack', function (param) {
assert.strictEqual(param, '1')
done()
}).auth(token)
}, 1000)
})
it('emits events for chat', function (done) {
var user = { id: 342546, username: 'test' }
var testMsg = 'This is plug-socket speaking!'
s = socket()
s.on('chat', function (msg) {
if (msg.uid === user.id) {
assert.strictEqual(msg.message, testMsg)
done()
}
})
s.once('open', function () {
s.onmessage({
data: JSON.stringify([ {
a: 'chat',
p: {
cid: user.id + '-' + Date.now(),
message: testMsg,
sub: 0,
uid: user.id,
un: user.username
},
s: room
} ])
})
})
})
it('emits "action" events for each individual message', function (done) {
s = socket()
var calledEarn = false
var calledGift = false
var called = {}
s.on('earn', function () {
calledEarn = true
})
s.on('gift', function () {
calledGift = true
})
s.on('action', function (action, param, slug) {
called[action] = true
assert.ok(param)
assert.ok(slug)
})
s.once('open', function () {
s.onmessage({
data: JSON.stringify([
{ a: 'earn', p: { xp: 1000 }, s: 'dashboard' },
{ a: 'gift', p: { uid: -1 }, s: 'tastycat' }
])
})
assert.ok(calledEarn)
assert.ok(calledGift)
assert.ok(called.earn)
assert.ok(called.gift)
done()
})
})
it('disconnects if a message has not been received for some time', function (done) {
this.timeout(2000)
var server = http.createServer().listen()
var wsserver = new WebSocket.Server({ server: server })
s = socket(null, {
timeout: 300,
url: 'ws://localhost:' + server.address().port
})
s.onclose = function (event) {
assert.equal(event.code, 3001)
server.close()
done()
}
})
})