강동현

더러운 서버 테스트코드 작성, yarn test

...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
3 "@types/chai": "^4.2.18", 3 "@types/chai": "^4.2.18",
4 "@types/express": "^4.17.11", 4 "@types/express": "^4.17.11",
5 "@types/mocha": "^8.2.2", 5 "@types/mocha": "^8.2.2",
6 + "@types/mocha-steps": "^1.3.0",
6 "@types/node": "^15.3.1", 7 "@types/node": "^15.3.1",
7 "@types/socket.io": "^3.0.2", 8 "@types/socket.io": "^3.0.2",
8 "@types/socket.io-client": "^3.0.0", 9 "@types/socket.io-client": "^3.0.0",
...@@ -10,6 +11,7 @@ ...@@ -10,6 +11,7 @@
10 "chai": "^4.3.4", 11 "chai": "^4.3.4",
11 "express": "^4.17.1", 12 "express": "^4.17.1",
12 "mocha": "^8.4.0", 13 "mocha": "^8.4.0",
14 + "mocha-steps": "^1.3.0",
13 "nodemon": "^2.0.7", 15 "nodemon": "^2.0.7",
14 "socket.io": "^4.1.2", 16 "socket.io": "^4.1.2",
15 "socket.io-client": "^4.1.2", 17 "socket.io-client": "^4.1.2",
...@@ -19,6 +21,6 @@ ...@@ -19,6 +21,6 @@
19 }, 21 },
20 "scripts": { 22 "scripts": {
21 "start": "nodemon index.ts", 23 "start": "nodemon index.ts",
22 - "test": "mocha -r ts-node/register ./**/*.test.ts" 24 + "test": "mocha -r ts-node/register --require mocha-steps ./**/*.test.ts"
23 } 25 }
24 } 26 }
......
1 import ioclient, { Socket } from "socket.io-client"; 1 import ioclient, { Socket } from "socket.io-client";
2 -import { MessageResponse, MessageType } from "./message/types"; 2 +import {
3 + LoginMessage,
4 + MessageResponse,
5 + MessageType,
6 + RoomChatMessage,
7 + RoomJoinMessage,
8 + RoomLeaveMessage,
9 + RoomListRequestMessage,
10 + RoomUserUpdateMessage,
11 +} from "./message/types";
3 import { expect } from "chai"; 12 import { expect } from "chai";
4 import { Server } from "./Server"; 13 import { Server } from "./Server";
14 +import { RoomDescription, RoomInfo } from "./room/types";
15 +import { response } from "express";
5 16
6 describe("server", () => { 17 describe("server", () => {
7 const PORT = 3000; 18 const PORT = 3000;
8 19
9 var server: Server; 20 var server: Server;
10 - var client: Socket; 21 + var client1: Socket;
22 + var client2: Socket;
11 23
12 before((done) => { 24 before((done) => {
13 - client = ioclient(`http://localhost:${PORT}`); 25 + let connected = 0;
14 - client.on("connect", done); 26 +
27 + client1 = ioclient(`http://localhost:${PORT}`);
28 + client1.on("connect", () => ++connected == 2 && done());
29 +
30 + client2 = ioclient(`http://localhost:${PORT}`);
31 + client2.on("connect", () => ++connected == 2 && done());
15 32
16 server = new Server(3000); 33 server = new Server(3000);
17 }); 34 });
18 35
19 after(() => { 36 after(() => {
20 server.close(); 37 server.close();
21 - client.close(); 38 + client1.close();
39 + client2.close();
40 + });
41 +
42 + var roomUserUpdateMessage: RoomUserUpdateMessage;
43 + var roomChatMessage: RoomChatMessage;
44 +
45 + step("register listeners", () => {
46 + client1.on(
47 + MessageType.ROOM_USER_UPDATE,
48 + (message: RoomUserUpdateMessage) => {
49 + roomUserUpdateMessage = message;
50 + }
51 + );
52 +
53 + client1.on(MessageType.ROOM_CHAT, (message: RoomChatMessage) => {
54 + roomChatMessage = message;
55 + });
22 }); 56 });
23 57
24 - it("login", (done) => { 58 + step("login 1", (done) => {
25 - client.emit( 59 + client1.emit(
26 MessageType.LOGIN, 60 MessageType.LOGIN,
27 - { username: "guest" }, 61 + new LoginMessage("guest1"),
28 - (response: MessageResponse) => { 62 + (response: MessageResponse<undefined>) => {
29 expect(response.ok).to.equals(true); 63 expect(response.ok).to.equals(true);
30 done(); 64 done();
31 } 65 }
32 ); 66 );
33 }); 67 });
68 +
69 + step("login 2", (done) => {
70 + client2.emit(
71 + MessageType.LOGIN,
72 + new LoginMessage("guest2"),
73 + (response: MessageResponse<undefined>) => {
74 + expect(response.ok).to.equals(true);
75 + done();
76 + }
77 + );
78 + });
79 +
80 + var roomToJoin: string;
81 +
82 + step("room list", (done) => {
83 + client1.emit(
84 + MessageType.ROOM_LIST_REQUEST,
85 + new RoomListRequestMessage(),
86 + (response: MessageResponse<RoomDescription[]>) => {
87 + expect(response.ok).to.eq(true);
88 + expect(response.result !== undefined).to.eq(true);
89 + if (response.result) {
90 + expect(response.result[0].name).to.eq("테스트 방 #1");
91 + roomToJoin = response.result[0].uuid;
92 + }
93 + done();
94 + }
95 + );
96 + });
97 +
98 + step("room join 1", (done) => {
99 + client1.emit(
100 + MessageType.ROOM_JOIN,
101 + new RoomJoinMessage(roomToJoin),
102 + (response: MessageResponse<RoomInfo>) => {
103 + expect(response.ok).to.eq(true);
104 + expect(response.result !== undefined).to.eq(true);
105 + if (response.result) {
106 + expect(response.result.uuid).to.eq(roomToJoin);
107 + expect(response.result.users.length).to.eq(1);
108 + expect(response.result.users[0].username).to.eq("guest1");
109 + }
110 + done();
111 + }
112 + );
113 + });
114 +
115 + step("room join 2", (done) => {
116 + client2.emit(
117 + MessageType.ROOM_JOIN,
118 + new RoomJoinMessage(roomToJoin),
119 + (response: MessageResponse<RoomInfo>) => {
120 + expect(response.ok).to.eq(true);
121 + expect(response.result !== undefined).to.eq(true);
122 + if (response.result) {
123 + expect(response.result.uuid).to.eq(roomToJoin);
124 + expect(response.result.users.length).to.eq(2);
125 + }
126 + done();
127 + }
128 + );
129 + });
130 +
131 + // TODO: RoomUserUpdateMessage가 아직 도착하지 않았는데 실행되는 경우
132 + step("client 1 received user update", () => {
133 + expect(roomUserUpdateMessage !== undefined).to.eq(true);
134 + if (roomUserUpdateMessage) {
135 + expect(roomUserUpdateMessage.state).to.eq("added");
136 + expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
137 + }
138 + });
139 +
140 + step("client 2 send chat", (done) => {
141 + client2.emit(
142 + MessageType.ROOM_CHAT,
143 + new RoomChatMessage("Hello World"),
144 + (response: MessageResponse<undefined>) => {
145 + expect(response.ok).to.eq(true);
146 + done();
147 + }
148 + );
149 + });
150 +
151 + step("client 1 received chat", () => {
152 + expect(roomChatMessage !== undefined).to.eq(true);
153 + if (roomChatMessage) {
154 + expect(roomChatMessage.sender).to.eq("guest2");
155 + expect(roomChatMessage.message).to.eq("Hello World");
156 + }
157 + });
158 +
159 + step("client 2 leave", (done) => {
160 + client2.emit(
161 + MessageType.ROOM_LEAVE,
162 + new RoomLeaveMessage(),
163 + (response: MessageResponse<undefined>) => {
164 + expect(response.ok).to.eq(true);
165 + done();
166 + }
167 + );
168 + });
169 +
170 + step("client 1 received user update", () => {
171 + expect(roomUserUpdateMessage !== undefined).to.eq(true);
172 + if (roomUserUpdateMessage) {
173 + expect(roomUserUpdateMessage.state).to.eq("removed");
174 + expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
175 + }
176 + });
34 }); 177 });
......
...@@ -73,7 +73,14 @@ ...@@ -73,7 +73,14 @@
73 resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a" 73 resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
74 integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw== 74 integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==
75 75
76 -"@types/mocha@^8.2.2": 76 +"@types/mocha-steps@^1.3.0":
77 + version "1.3.0"
78 + resolved "https://registry.yarnpkg.com/@types/mocha-steps/-/mocha-steps-1.3.0.tgz#3086c74675f45359c514f80c5090f9bac23097aa"
79 + integrity sha512-hI0P9rS20BhHSXWTqLYcRYy6PGYk9vMZFNX7UF0ZWUrDMuqawtVRuTkYq7rG25sBSpL28BZggABBecFDK6ZZyg==
80 + dependencies:
81 + "@types/mocha" "*"
82 +
83 +"@types/mocha@*", "@types/mocha@^8.2.2":
77 version "8.2.2" 84 version "8.2.2"
78 resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-8.2.2.tgz#91daa226eb8c2ff261e6a8cbf8c7304641e095e0" 85 resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-8.2.2.tgz#91daa226eb8c2ff261e6a8cbf8c7304641e095e0"
79 integrity sha512-Lwh0lzzqT5Pqh6z61P3c3P5nm6fzQK/MMHl9UKeneAeInVflBSz1O2EkX6gM6xfJd7FBXBY5purtLx7fUiZ7Hw== 86 integrity sha512-Lwh0lzzqT5Pqh6z61P3c3P5nm6fzQK/MMHl9UKeneAeInVflBSz1O2EkX6gM6xfJd7FBXBY5purtLx7fUiZ7Hw==
...@@ -1103,6 +1110,11 @@ minimist@^1.2.0: ...@@ -1103,6 +1110,11 @@ minimist@^1.2.0:
1103 resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" 1110 resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
1104 integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== 1111 integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
1105 1112
1113 +mocha-steps@^1.3.0:
1114 + version "1.3.0"
1115 + resolved "https://registry.yarnpkg.com/mocha-steps/-/mocha-steps-1.3.0.tgz#2449231ec45ec56810f65502cb22e2571862957f"
1116 + integrity sha512-KZvpMJTqzLZw3mOb+EEuYi4YZS41C9iTnb7skVFRxHjUd1OYbl64tCMSmpdIRM9LnwIrSOaRfPtNpF5msgv6Eg==
1117 +
1106 mocha@^8.4.0: 1118 mocha@^8.4.0:
1107 version "8.4.0" 1119 version "8.4.0"
1108 resolved "https://registry.yarnpkg.com/mocha/-/mocha-8.4.0.tgz#677be88bf15980a3cae03a73e10a0fc3997f0cff" 1120 resolved "https://registry.yarnpkg.com/mocha/-/mocha-8.4.0.tgz#677be88bf15980a3cae03a73e10a0fc3997f0cff"
......