강동현

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

......@@ -3,6 +3,7 @@
"@types/chai": "^4.2.18",
"@types/express": "^4.17.11",
"@types/mocha": "^8.2.2",
"@types/mocha-steps": "^1.3.0",
"@types/node": "^15.3.1",
"@types/socket.io": "^3.0.2",
"@types/socket.io-client": "^3.0.0",
......@@ -10,6 +11,7 @@
"chai": "^4.3.4",
"express": "^4.17.1",
"mocha": "^8.4.0",
"mocha-steps": "^1.3.0",
"nodemon": "^2.0.7",
"socket.io": "^4.1.2",
"socket.io-client": "^4.1.2",
......@@ -19,6 +21,6 @@
},
"scripts": {
"start": "nodemon index.ts",
"test": "mocha -r ts-node/register ./**/*.test.ts"
"test": "mocha -r ts-node/register --require mocha-steps ./**/*.test.ts"
}
}
......
import ioclient, { Socket } from "socket.io-client";
import { MessageResponse, MessageType } from "./message/types";
import {
LoginMessage,
MessageResponse,
MessageType,
RoomChatMessage,
RoomJoinMessage,
RoomLeaveMessage,
RoomListRequestMessage,
RoomUserUpdateMessage,
} from "./message/types";
import { expect } from "chai";
import { Server } from "./Server";
import { RoomDescription, RoomInfo } from "./room/types";
import { response } from "express";
describe("server", () => {
const PORT = 3000;
var server: Server;
var client: Socket;
var client1: Socket;
var client2: Socket;
before((done) => {
client = ioclient(`http://localhost:${PORT}`);
client.on("connect", done);
let connected = 0;
client1 = ioclient(`http://localhost:${PORT}`);
client1.on("connect", () => ++connected == 2 && done());
client2 = ioclient(`http://localhost:${PORT}`);
client2.on("connect", () => ++connected == 2 && done());
server = new Server(3000);
});
after(() => {
server.close();
client.close();
client1.close();
client2.close();
});
var roomUserUpdateMessage: RoomUserUpdateMessage;
var roomChatMessage: RoomChatMessage;
step("register listeners", () => {
client1.on(
MessageType.ROOM_USER_UPDATE,
(message: RoomUserUpdateMessage) => {
roomUserUpdateMessage = message;
}
);
client1.on(MessageType.ROOM_CHAT, (message: RoomChatMessage) => {
roomChatMessage = message;
});
});
step("login 1", (done) => {
client1.emit(
MessageType.LOGIN,
new LoginMessage("guest1"),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.equals(true);
done();
}
);
});
it("login", (done) => {
client.emit(
step("login 2", (done) => {
client2.emit(
MessageType.LOGIN,
{ username: "guest" },
(response: MessageResponse) => {
new LoginMessage("guest2"),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.equals(true);
done();
}
);
});
var roomToJoin: string;
step("room list", (done) => {
client1.emit(
MessageType.ROOM_LIST_REQUEST,
new RoomListRequestMessage(),
(response: MessageResponse<RoomDescription[]>) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result[0].name).to.eq("테스트 방 #1");
roomToJoin = response.result[0].uuid;
}
done();
}
);
});
step("room join 1", (done) => {
client1.emit(
MessageType.ROOM_JOIN,
new RoomJoinMessage(roomToJoin),
(response: MessageResponse<RoomInfo>) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result.uuid).to.eq(roomToJoin);
expect(response.result.users.length).to.eq(1);
expect(response.result.users[0].username).to.eq("guest1");
}
done();
}
);
});
step("room join 2", (done) => {
client2.emit(
MessageType.ROOM_JOIN,
new RoomJoinMessage(roomToJoin),
(response: MessageResponse<RoomInfo>) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result.uuid).to.eq(roomToJoin);
expect(response.result.users.length).to.eq(2);
}
done();
}
);
});
// TODO: RoomUserUpdateMessage가 아직 도착하지 않았는데 실행되는 경우
step("client 1 received user update", () => {
expect(roomUserUpdateMessage !== undefined).to.eq(true);
if (roomUserUpdateMessage) {
expect(roomUserUpdateMessage.state).to.eq("added");
expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
}
});
step("client 2 send chat", (done) => {
client2.emit(
MessageType.ROOM_CHAT,
new RoomChatMessage("Hello World"),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.eq(true);
done();
}
);
});
step("client 1 received chat", () => {
expect(roomChatMessage !== undefined).to.eq(true);
if (roomChatMessage) {
expect(roomChatMessage.sender).to.eq("guest2");
expect(roomChatMessage.message).to.eq("Hello World");
}
});
step("client 2 leave", (done) => {
client2.emit(
MessageType.ROOM_LEAVE,
new RoomLeaveMessage(),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.eq(true);
done();
}
);
});
step("client 1 received user update", () => {
expect(roomUserUpdateMessage !== undefined).to.eq(true);
if (roomUserUpdateMessage) {
expect(roomUserUpdateMessage.state).to.eq("removed");
expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
}
});
});
......
......@@ -73,7 +73,14 @@
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==
"@types/mocha@^8.2.2":
"@types/mocha-steps@^1.3.0":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@types/mocha-steps/-/mocha-steps-1.3.0.tgz#3086c74675f45359c514f80c5090f9bac23097aa"
integrity sha512-hI0P9rS20BhHSXWTqLYcRYy6PGYk9vMZFNX7UF0ZWUrDMuqawtVRuTkYq7rG25sBSpL28BZggABBecFDK6ZZyg==
dependencies:
"@types/mocha" "*"
"@types/mocha@*", "@types/mocha@^8.2.2":
version "8.2.2"
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-8.2.2.tgz#91daa226eb8c2ff261e6a8cbf8c7304641e095e0"
integrity sha512-Lwh0lzzqT5Pqh6z61P3c3P5nm6fzQK/MMHl9UKeneAeInVflBSz1O2EkX6gM6xfJd7FBXBY5purtLx7fUiZ7Hw==
......@@ -1103,6 +1110,11 @@ minimist@^1.2.0:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
mocha-steps@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/mocha-steps/-/mocha-steps-1.3.0.tgz#2449231ec45ec56810f65502cb22e2571862957f"
integrity sha512-KZvpMJTqzLZw3mOb+EEuYi4YZS41C9iTnb7skVFRxHjUd1OYbl64tCMSmpdIRM9LnwIrSOaRfPtNpF5msgv6Eg==
mocha@^8.4.0:
version "8.4.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-8.4.0.tgz#677be88bf15980a3cae03a73e10a0fc3997f0cff"
......