-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.js
212 lines (139 loc) · 4.59 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const assert = require('chai').assert;
const fork = require('child_process').fork;
const child = fork('./socket.io-server');
const async = require('async');
process.on('exit', function () {
child.kill();
});
const rltm = require('./src/index');
const testMessageData = {
rand: Math.random()
};
const testStateData = {
rand: Math.random()
};
const testNewStateData = {
rand: Math.random()
};
const connectionInput = process.env.CLIENT || 'pubnub';
const connections = {
pubnub: rltm({
service: 'pubnub',
config: {
publishKey: 'demo',
subscribeKey: 'demo'
}
}),
socketio: rltm({
service: 'socketio',
config: {
endpoint: 'http://localhost:9000',
uuid: new Date().getTime()
}
})
};
const connection = connections[connectionInput];
describe(connection.service, function() {
describe('init', function() {
it('should create connection object', function() {
assert.isObject(connection, 'was successfully created');
});
});
describe('ready', function() {
it('should get called when ready', function(done) {
room = connection.join(new Date().getTime(), testStateData);
room.ready(() => {
done();
});
});
it('should get itself as a join event', function(done) {
room = connection.join(new Date().getTime(), testStateData);
room.on('join', function(uuid, state) {
assert.isOk(uuid, 'uuid is set');
done();
});
});
});
describe('publish subscribe', function() {
it('should send and receive message', function(done) {
room.on('message', function(uuid, message){
assert.deepEqual(message, testMessageData);
done();
});
room.message(testMessageData);
});
});
describe('here now', function() {
it('at least one user online', function(done) {
room.here().then(function(users) {
assert.isOk(users, 'At least one user online now');
done();
}, function(err) {
assert.fail();
});
});
});
describe('state', function() {
it('should set state', function(done) {
room.on('state', function(uuid, state) {
assert.isOk(uuid, 'uuid supplied');
assert.isObject(state, 'state is object');
done();
});
room.state(testNewStateData).then(function() {
// it worked
}, function(err) {
assert.fail();
});
});
});
describe('leave', function() {
it('should disconnect', function(done) {
room.leave().then(function(){
done();
});
});
});
describe('history', function() {
it('should recall history', function(done) {
this.timeout(8000);
setTimeout(function() {
room.history().then(function(history) {
assert.isOk(history[0]);
assert.deepEqual(history[0].data, testMessageData, 'latest message is correct');
assert.isAbove(history.length, 0, 'at least one messages received');
done();
}, function() {
assert.fail();
});
}, 1000);
});
});
describe('many rooms', function() {
it('should keep rooms separate', function(done) {
this.timeout(6000);
async.parallel({
one: function(callback) {
let input = {room: 1};
let room1 = connection.join('room-1');
room1.on('message', function(uuid, output) {
assert.deepEqual(input, output);
callback();
});
room1.message(input);
},
two: function(callback) {
let input = {room: 2};
let room2 = connection.join('room-2');
room2.on('message', function(uuid, output) {
assert.deepEqual(input, output);
callback();
});
room2.message(input);
}
}, function(err, results) {
done();
});
});
});
});