From 26c327145f84b30c96a4ca128a2296ab8b9a6f7e Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Tue, 22 Jan 2019 18:30:49 +0900
Subject: [PATCH] [Test] Add streaming test

#3955
---
 test/api.ts | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/test/api.ts b/test/api.ts
index d82014e75..d87380a1d 100644
--- a/test/api.ts
+++ b/test/api.ts
@@ -1,10 +1,17 @@
 /*
  * Tests of API
+ *
+ * How to run the tests:
+ * > mocha test/api.ts --require ts-node/register
+ *
+ * To specify test:
+ * > mocha test/api.ts --require ts-node/register -g 'test name'
  */
 
 import * as http from 'http';
 import * as fs from 'fs';
 import * as assert from 'chai';
+import * as WebSocket from 'ws';
 
 assert.use(require('chai-http'));
 const expect = assert.expect;
@@ -20,6 +27,7 @@ process.on('unhandledRejection', console.dir);
 //#endregion
 
 const app = require('../built/server/api').default;
+require('../built/server').default();
 const db = require('../built/db/mongodb').default;
 
 const server = http.createServer(app.callback());
@@ -1231,4 +1239,39 @@ describe('API', () => {
 			expect(res).have.status(400);
 		}));
 	});
+
+	describe('streaming', () => {
+		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);
+								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
+						}
+					}));
+				});
+			});
+		});
+	});
 });