2021-05-16 02:12:18 -07:00
|
|
|
import WS from 'jest-websocket-mock';
|
|
|
|
import Stream from '../src/streaming';
|
|
|
|
|
|
|
|
describe('Streaming', () => {
|
2021-05-22 21:34:36 -07:00
|
|
|
test('useChannel', async () => {
|
2021-05-16 02:12:18 -07:00
|
|
|
const server = new WS('wss://misskey.test/streaming');
|
|
|
|
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
|
|
|
const mainChannelReceived: any[] = [];
|
2021-05-22 21:34:36 -07:00
|
|
|
const main = stream.useChannel('main');
|
2021-05-17 03:50:31 -07:00
|
|
|
main.on('meUpdated', payload => {
|
2021-05-16 02:12:18 -07:00
|
|
|
mainChannelReceived.push(payload);
|
|
|
|
});
|
|
|
|
await server.connected;
|
|
|
|
const msg = JSON.parse(await server.nextMessage as string);
|
|
|
|
const mainChannelId = msg.body.id;
|
|
|
|
expect(msg.type).toEqual('connect');
|
|
|
|
expect(msg.body.channel).toEqual('main');
|
|
|
|
expect(mainChannelId != null).toEqual(true);
|
|
|
|
|
|
|
|
server.send(JSON.stringify({
|
|
|
|
type: 'channel',
|
|
|
|
body: {
|
|
|
|
id: mainChannelId,
|
2021-05-17 03:50:31 -07:00
|
|
|
type: 'meUpdated',
|
2021-05-16 02:12:18 -07:00
|
|
|
body: {
|
2021-05-17 03:50:31 -07:00
|
|
|
id: 'foo'
|
2021-05-16 02:12:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(mainChannelReceived[0]).toEqual({
|
2021-05-17 03:50:31 -07:00
|
|
|
id: 'foo'
|
2021-05-16 02:12:18 -07:00
|
|
|
});
|
2021-05-17 03:57:04 -07:00
|
|
|
|
2021-06-12 07:01:48 -07:00
|
|
|
stream.close();
|
2021-05-17 03:57:04 -07:00
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2021-05-22 21:34:36 -07:00
|
|
|
test('useChannel with parameters', async () => {
|
2021-06-27 05:17:42 -07:00
|
|
|
const server = new WS('wss://misskey.test/streaming');
|
|
|
|
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
|
|
|
const messagingChannelReceived: any[] = [];
|
|
|
|
const messaging = stream.useChannel('messaging', { otherparty: 'aaa' });
|
|
|
|
messaging.on('message', payload => {
|
|
|
|
messagingChannelReceived.push(payload);
|
|
|
|
});
|
|
|
|
await server.connected;
|
|
|
|
const msg = JSON.parse(await server.nextMessage as string);
|
|
|
|
const messagingChannelId = msg.body.id;
|
|
|
|
expect(msg.type).toEqual('connect');
|
|
|
|
expect(msg.body.channel).toEqual('messaging');
|
|
|
|
expect(msg.body.params).toEqual({ otherparty: 'aaa' });
|
|
|
|
expect(messagingChannelId != null).toEqual(true);
|
|
|
|
|
|
|
|
server.send(JSON.stringify({
|
|
|
|
type: 'channel',
|
|
|
|
body: {
|
|
|
|
id: messagingChannelId,
|
|
|
|
type: 'message',
|
|
|
|
body: {
|
|
|
|
id: 'foo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(messagingChannelReceived[0]).toEqual({
|
|
|
|
id: 'foo'
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
2021-05-22 21:34:36 -07:00
|
|
|
});
|
|
|
|
|
2021-06-28 01:57:15 -07:00
|
|
|
test('ちゃんとチャンネルごとにidが異なる', async () => {
|
|
|
|
const server = new WS('wss://misskey.test/streaming');
|
|
|
|
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
|
|
|
|
|
|
|
stream.useChannel('messaging', { otherparty: 'aaa' });
|
|
|
|
stream.useChannel('messaging', { otherparty: 'bbb' });
|
|
|
|
|
|
|
|
await server.connected;
|
|
|
|
|
|
|
|
const msg = JSON.parse(await server.nextMessage as string);
|
|
|
|
const messagingChannelId = msg.body.id;
|
|
|
|
const msg2 = JSON.parse(await server.nextMessage as string);
|
|
|
|
const messagingChannelId2 = msg2.body.id;
|
|
|
|
|
|
|
|
expect(messagingChannelId != null).toEqual(true);
|
|
|
|
expect(messagingChannelId2 != null).toEqual(true);
|
|
|
|
expect(messagingChannelId).not.toEqual(messagingChannelId2);
|
|
|
|
|
|
|
|
stream.close();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
|
2021-05-22 21:34:36 -07:00
|
|
|
test('Connection#dispose', async () => {
|
2021-05-17 03:57:04 -07:00
|
|
|
const server = new WS('wss://misskey.test/streaming');
|
|
|
|
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
|
|
|
const mainChannelReceived: any[] = [];
|
2021-05-22 21:34:36 -07:00
|
|
|
const main = stream.useChannel('main');
|
2021-05-17 03:57:04 -07:00
|
|
|
main.on('meUpdated', payload => {
|
|
|
|
mainChannelReceived.push(payload);
|
|
|
|
});
|
|
|
|
await server.connected;
|
|
|
|
const msg = JSON.parse(await server.nextMessage as string);
|
|
|
|
const mainChannelId = msg.body.id;
|
|
|
|
expect(msg.type).toEqual('connect');
|
|
|
|
expect(msg.body.channel).toEqual('main');
|
|
|
|
expect(mainChannelId != null).toEqual(true);
|
|
|
|
main.dispose();
|
|
|
|
|
|
|
|
server.send(JSON.stringify({
|
|
|
|
type: 'channel',
|
|
|
|
body: {
|
|
|
|
id: mainChannelId,
|
|
|
|
type: 'meUpdated',
|
|
|
|
body: {
|
|
|
|
id: 'foo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
expect(mainChannelReceived.length).toEqual(0);
|
|
|
|
|
2021-06-12 07:01:48 -07:00
|
|
|
stream.close();
|
2021-05-17 03:57:04 -07:00
|
|
|
server.close();
|
2021-05-16 02:12:18 -07:00
|
|
|
});
|
2021-05-17 03:57:04 -07:00
|
|
|
|
|
|
|
// TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト
|
2021-05-22 21:34:36 -07:00
|
|
|
|
|
|
|
// TODO: チャンネル接続が使いまわされるかのテスト
|
2021-05-16 02:12:18 -07:00
|
|
|
});
|