1
1
mirror of https://github.com/kokonect-link/cherrypick synced 2024-12-15 23:28:20 +09:00
cherrypick/test/streaming.ts

81 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-05-16 18:12:18 +09:00
import WS from 'jest-websocket-mock';
import Stream from '../src/streaming';
describe('Streaming', () => {
2021-05-23 13:34:36 +09:00
test('useChannel', async () => {
2021-05-16 18:12:18 +09:00
const server = new WS('wss://misskey.test/streaming');
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannelReceived: any[] = [];
2021-05-23 13:34:36 +09:00
const main = stream.useChannel('main');
main.on('meUpdated', payload => {
2021-05-16 18:12:18 +09: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,
type: 'meUpdated',
2021-05-16 18:12:18 +09:00
body: {
id: 'foo'
2021-05-16 18:12:18 +09:00
}
}
}));
expect(mainChannelReceived[0]).toEqual({
id: 'foo'
2021-05-16 18:12:18 +09:00
});
2021-05-17 19:57:04 +09:00
2021-06-12 23:01:48 +09:00
stream.close();
2021-05-17 19:57:04 +09:00
server.close();
});
2021-06-12 23:01:48 +09:00
/* TODO
2021-05-23 13:34:36 +09:00
test('useChannel with parameters', async () => {
});
2021-06-12 23:01:48 +09:00
*/
2021-05-23 13:34:36 +09:00
test('Connection#dispose', async () => {
2021-05-17 19:57:04 +09:00
const server = new WS('wss://misskey.test/streaming');
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannelReceived: any[] = [];
2021-05-23 13:34:36 +09:00
const main = stream.useChannel('main');
2021-05-17 19:57:04 +09: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 23:01:48 +09:00
stream.close();
2021-05-17 19:57:04 +09:00
server.close();
2021-05-16 18:12:18 +09:00
});
2021-05-17 19:57:04 +09:00
// TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト
2021-05-23 13:34:36 +09:00
// TODO: チャンネル接続が使いまわされるかのテスト
2021-05-16 18:12:18 +09:00
});