misskey-awawa/test/streaming.ts

82 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-01-23 03:15:27 +00: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';
import * as assert from 'chai';
import { _signup, _request, _uploadFile, _post, _react, resetDb } from './utils';
assert.use(require('chai-http'));
const expect = assert.expect;
//#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 04:35:22 +00:00
const server = require('../built/server').startServer();
2019-01-23 03:15:27 +00:00
const db = require('../built/db/mongodb').default;
2019-01-23 04:35:22 +00:00
const apiServer = http.createServer(app.callback());
2019-01-23 03:15:27 +00:00
//#region Utilities
2019-01-23 04:35:22 +00:00
const request = _request(apiServer);
2019-01-23 03:15:27 +00:00
const signup = _signup(request);
//#endregion
describe('Streaming', () => {
// Reset database each test
beforeEach(resetDb(db));
2019-01-23 04:50:36 +00:00
after(() => {
2019-01-23 04:35:22 +00:00
server.close();
});
2019-01-23 03:15:27 +00:00
it('投稿がタイムラインに流れる', done => {
const post = {
text: 'foo'
};
signup().then(me => {
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') {
expect(msg.body.body.text).eql(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
}
}));
});
});
});
});