cherrypick/test/streaming.ts

149 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-01-23 12:15:27 +09:00
/*
* 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';
2019-01-25 19:37:45 +09:00
import * as assert from 'assert';
2019-01-23 12:15:27 +09:00
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;
2019-01-23 13:35:22 +09:00
const server = require('../built/server').startServer();
2019-01-23 12:15:27 +09:00
const db = require('../built/db/mongodb').default;
2019-01-23 13:35:22 +09:00
const apiServer = http.createServer(app.callback());
2019-01-23 12:15:27 +09:00
//#region Utilities
2019-01-23 13:35:22 +09:00
const request = _request(apiServer);
2019-01-23 12:15:27 +09:00
const signup = _signup(request);
2019-01-25 16:14:09 +09:00
const post = _post(request);
2019-01-23 12:15:27 +09:00
//#endregion
describe('Streaming', () => {
// Reset database each test
beforeEach(resetDb(db));
2019-01-23 13:50:36 +09:00
after(() => {
2019-01-23 13:35:22 +09:00
server.close();
});
2019-01-25 15:56:49 +09:00
it('投稿がタイムラインに流れる', () => new Promise(async done => {
2019-01-23 12:15:27 +09:00
const post = {
text: 'foo'
};
2019-01-25 15:56:49 +09:00
const me = await signup();
const ws = new WebSocket(`ws://localhost/streaming?i=${me.token}`);
2019-01-23 12:15:27 +09:00
2019-01-25 15:56:49 +09:00
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') {
2019-01-25 19:37:45 +09:00
assert.deepStrictEqual(msg.body.body.text, post.text);
2019-01-25 15:56:49 +09:00
ws.close();
done();
2019-01-23 12:15:27 +09:00
}
2019-01-25 15:56:49 +09:00
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', post, me);
}
2019-01-23 12:15:27 +09:00
});
2019-01-25 15:56:49 +09:00
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'homeTimeline',
id: 'a',
pong: true
}
}));
2019-01-23 12:15:27 +09:00
});
2019-01-25 15:56:49 +09:00
}));
2019-01-25 16:14:09 +09:00
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') {
2019-01-25 19:37:45 +09:00
assert.deepStrictEqual(msg.body.body.text, aliceNote.text);
2019-01-25 16:14:09 +09:00
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') {
2019-01-25 19:37:45 +09:00
assert.deepStrictEqual(msg.body.body.renoteId, bobNote.id);
2019-01-25 16:14:09 +09:00
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
}
}));
});
}));
2019-01-23 12:15:27 +09:00
});