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
|
/*
* Tests of streaming API
*
* How to run the tests:
* > mocha test/streaming.ts --require ts-node/register
*
* To specify test:
* > mocha test/streaming.ts --require ts-node/register -g 'test name'
*/
import * as http from 'http';
import * as WebSocket from 'ws';
import * as assert from 'assert';
import { _signup, _request, _uploadFile, _post, _react, resetDb } from './utils';
//#region process
Error.stackTraceLimit = Infinity;
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
// Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir);
//#endregion
const app = require('../built/server/api').default;
const server = require('../built/server').startServer();
const db = require('../built/db/mongodb').default;
const apiServer = http.createServer(app.callback());
//#region Utilities
const request = _request(apiServer);
const signup = _signup(request);
const post = _post(request);
//#endregion
describe('Streaming', () => {
// Reset database each test
beforeEach(resetDb(db));
after(() => {
server.close();
});
it('投稿がタイムラインに流れる', () => new Promise(async done => {
const post = {
text: 'foo'
};
const me = await signup();
const ws = new WebSocket(`ws://localhost/streaming?i=${me.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'note') {
assert.deepStrictEqual(msg.body.body.text, post.text);
ws.close();
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', post, me);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'homeTimeline',
id: 'a',
pong: true
}
}));
});
}));
it('mention event', () => new Promise(async done => {
const alice = await signup({ username: 'alice' });
const bob = await signup({ username: 'bob' });
const aliceNote = {
text: 'foo @bob bar'
};
const ws = new WebSocket(`ws://localhost/streaming?i=${bob.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'mention') {
assert.deepStrictEqual(msg.body.body.text, aliceNote.text);
ws.close();
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', aliceNote, alice);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'main',
id: 'a',
pong: true
}
}));
});
}));
it('renote event', () => new Promise(async done => {
const alice = await signup({ username: 'alice' });
const bob = await signup({ username: 'bob' });
const bobNote = await post(bob, {
text: 'foo'
});
const ws = new WebSocket(`ws://localhost/streaming?i=${bob.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'renote') {
assert.deepStrictEqual(msg.body.body.renoteId, bobNote.id);
ws.close();
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', {
renoteId: bobNote.id
}, alice);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'main',
id: 'a',
pong: true
}
}));
});
}));
});
|