강동현

테스트 코드 리팩토링

1 import { expect } from "chai"; 1 import { expect } from "chai";
2 import { RoomManager } from "../room/RoomManager"; 2 import { RoomManager } from "../room/RoomManager";
3 +import { prepareJoinedRoom, prepareUsers } from "./util/prepare";
3 import { SocketTester } from "./util/SocketTester"; 4 import { SocketTester } from "./util/SocketTester";
4 5
5 describe("방장", () => { 6 describe("방장", () => {
6 - const roomManager = new RoomManager();
7 it("방을 만든 유저가 방장이 됩니다", () => { 7 it("방을 만든 유저가 방장이 됩니다", () => {
8 - const socket = new SocketTester(roomManager); 8 + const {
9 - socket.login("guest"); 9 + users: [user],
10 + room,
11 + } = prepareJoinedRoom(1);
10 12
11 - expect(socket.connection.user !== undefined).eq(true); 13 + expect(room.admin).eq(user);
12 - const room = roomManager.create("테스트", 2, socket.connection.user);
13 - expect(room.admin).eq(socket.connection.user);
14 }); 14 });
15 it("나중에 들어온 유저는 방장 정보를 받지만 방장이 되지 않습니다", () => { 15 it("나중에 들어온 유저는 방장 정보를 받지만 방장이 되지 않습니다", () => {
16 - const socket1 = new SocketTester(roomManager); 16 + const {
17 - const socket2 = new SocketTester(roomManager); 17 + sockets: [socket1, socket2],
18 - socket1.login("guest1"); 18 + users: [user1, user2],
19 - socket2.login("guest2"); 19 + roomManager,
20 + } = prepareUsers(2);
21 +
22 + const room = roomManager.create("테스트", 2, user1);
20 23
21 - expect(socket1.connection.user !== undefined).eq(true);
22 - expect(socket2.connection.user !== undefined).eq(true);
23 - const room = roomManager.create("테스트", 2, socket1.connection.user);
24 const response = socket2.test("joinRoom", { uuid: room.uuid }); 24 const response = socket2.test("joinRoom", { uuid: room.uuid });
25 expect(response.ok).eq(true); 25 expect(response.ok).eq(true);
26 - expect(room.admin).eq(socket1.connection.user); 26 + expect(room.admin).eq(user1);
27 - expect(room.admin).not.eq(socket2.connection.user); 27 + expect(response.result?.users[0]).eq({
28 - expect(response.result?.users[0]?.username).eq("guest1"); 28 + username: user1.username,
29 - expect(response.result?.users[0]?.admin).eq(true); 29 + admin: true,
30 + ready: false,
31 + });
32 + expect(response.result?.users[1]).eq({
33 + username: user2.username,
34 + admin: false,
35 + ready: false,
36 + });
30 }); 37 });
31 it("방장이 나가면 방장이 인계됩니다", () => { 38 it("방장이 나가면 방장이 인계됩니다", () => {
32 - const socket1 = new SocketTester(roomManager); 39 + const {
33 - const socket2 = new SocketTester(roomManager); 40 + sockets: [socket1, socket2],
34 - socket1.login("guest1"); 41 + users: [user1, user2],
35 - socket2.login("guest2"); 42 + room,
36 - 43 + } = prepareJoinedRoom(2);
37 - expect(socket1.connection.user !== undefined).eq(true);
38 - expect(socket2.connection.user !== undefined).eq(true);
39 - const room = roomManager.create("테스트", 2, socket1.connection.user);
40 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
41 44
42 - expect(room.admin).eq(socket1.connection.user); 45 + expect(room.admin).eq(user1);
43 - expect(room.admin).not.eq(socket2.connection.user); 46 + socket1.testOk("leaveRoom", {});
44 - 47 + expect(room.admin).eq(user2);
45 - expect(socket1.test("leaveRoom", {}).ok).eq(true);
46 - expect(room.admin).eq(socket2.connection.user);
47 }); 48 });
48 it("방장을 인계하면 정보가 업데이트됩니다", () => { 49 it("방장을 인계하면 정보가 업데이트됩니다", () => {
49 - const socket1 = new SocketTester(roomManager); 50 + const {
50 - const socket2 = new SocketTester(roomManager); 51 + sockets: [socket1, socket2],
51 - socket1.login("guest1"); 52 + users: [user1, user2],
52 - socket2.login("guest2"); 53 + room,
53 - 54 + } = prepareJoinedRoom(2);
54 - expect(socket1.connection.user !== undefined).eq(true);
55 - expect(socket2.connection.user !== undefined).eq(true);
56 - const room = roomManager.create("테스트", 2, socket1.connection.user);
57 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
58 55
59 - expect(room.admin).eq(socket1.connection.user); 56 + expect(room.admin).eq(user1);
60 - expect(room.admin).not.eq(socket2.connection.user);
61 57
62 - if (socket2.connection.user) { 58 + room.setAdmin(user2);
63 - room.setAdmin(socket2.connection.user); 59 + expect(room.admin).eq(user2);
64 - expect(room.admin).eq(socket2.connection.user);
65 60
66 - // guest1은 guest2가 접속했다는 메세지를 먼저 받은 상태 61 + // guest1은 guest2가 접속했다는 메세지를 먼저 받은 상태
67 - let message = socket1.socket.received("updateRoomUser"); 62 + let message = socket1.socket.received("updateRoomUser");
68 - expect(message.state).eq("added"); 63 + expect(message.state).eq("added");
69 - expect(message.user.username).eq("guest2"); 64 + expect(message.user.username).eq(user2.username);
70 65
71 - // 먼저 이전 방장의 업데이트를 받음 66 + // 먼저 이전 방장의 업데이트를 받음
72 - message = socket1.socket.received("updateRoomUser"); 67 + message = socket1.socket.received("updateRoomUser");
73 - expect(message.state).eq("updated"); 68 + expect(message.state).eq("updated");
74 - expect(message.user.username).eq("guest1"); 69 + expect(message.user.username).eq(user1.username);
75 - expect(message.user.admin).eq(false); 70 + expect(message.user.admin).eq(false);
76 71
77 - message = socket2.socket.received("updateRoomUser"); 72 + message = socket2.socket.received("updateRoomUser");
78 - expect(message.state).eq("updated"); 73 + expect(message.state).eq("updated");
79 - expect(message.user.username).eq("guest1"); 74 + expect(message.user.username).eq(user1.username);
80 - expect(message.user.admin).eq(false); 75 + expect(message.user.admin).eq(false);
81 76
82 - // 현재 방장의 업데이트를 받음 77 + // 현재 방장의 업데이트를 받음
83 - message = socket1.socket.received("updateRoomUser"); 78 + message = socket1.socket.received("updateRoomUser");
84 - expect(message.state).eq("updated"); 79 + expect(message.state).eq("updated");
85 - expect(message.user.username).eq("guest2"); 80 + expect(message.user.username).eq(user2.username);
86 - expect(message.user.admin).eq(true); 81 + expect(message.user.admin).eq(true);
87 82
88 - message = socket2.socket.received("updateRoomUser"); 83 + message = socket2.socket.received("updateRoomUser");
89 - expect(message.state).eq("updated"); 84 + expect(message.state).eq("updated");
90 - expect(message.user.username).eq("guest2"); 85 + expect(message.user.username).eq(user2.username);
91 - expect(message.user.admin).eq(true); 86 + expect(message.user.admin).eq(true);
92 - }
93 }); 87 });
94 }); 88 });
......
1 import { expect } from "chai"; 1 import { expect } from "chai";
2 import { RoomManager } from "../room/RoomManager"; 2 import { RoomManager } from "../room/RoomManager";
3 import { DummySocket } from "./util/DummySocket"; 3 import { DummySocket } from "./util/DummySocket";
4 +import {
5 + prepareJoinedRoom,
6 + prepareUsers,
7 + prepareUsersEmptyRooms,
8 +} from "./util/prepare";
4 import { SocketTester } from "./util/SocketTester"; 9 import { SocketTester } from "./util/SocketTester";
5 10
6 describe("채팅", () => { 11 describe("채팅", () => {
7 - const roomManager = new RoomManager();
8 it("로그인하지 않은 유저는 요청할 수 없습니다", () => { 12 it("로그인하지 않은 유저는 요청할 수 없습니다", () => {
13 + const roomManager = new RoomManager();
9 const socket = new SocketTester(roomManager); 14 const socket = new SocketTester(roomManager);
10 - const response = socket.test("chat", { message: "Hello World" }); 15 + socket.testNotOk("chat", { message: "Hello World" });
11 - expect(response.ok).eq(false);
12 }); 16 });
13 it("방에 접속하지 않은 유저는 요청할 수 없습니다", () => { 17 it("방에 접속하지 않은 유저는 요청할 수 없습니다", () => {
14 - const socket = new SocketTester(roomManager); 18 + const {
15 - socket.login("guest"); 19 + sockets: [socket],
20 + } = prepareUsers(1);
16 21
17 - const response = socket.test("chat", { message: "Hello World" }); 22 + socket.testNotOk("chat", { message: "Hello World" });
18 - expect(response.ok).eq(false);
19 }); 23 });
20 it("채팅을 보냅니다", () => { 24 it("채팅을 보냅니다", () => {
21 - const socket = new SocketTester(roomManager); 25 + const {
22 - socket.login("guest"); 26 + sockets: [socket],
23 - 27 + } = prepareJoinedRoom(1);
24 - const room = roomManager.create("테스트", 2); 28 + socket.testOk("chat", { message: "Hello World" });
25 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
26 - expect(socket.test("chat", { message: "Hello World" }).ok).eq(true);
27 }); 29 });
28 it("자신을 제외한 다른 사람들이 채팅을 받습니다", () => { 30 it("자신을 제외한 다른 사람들이 채팅을 받습니다", () => {
29 - const socket1 = new SocketTester(roomManager); 31 + const {
30 - socket1.login("guest1"); 32 + sockets: [socket1, socket2],
31 - const socket2 = new SocketTester(roomManager); 33 + users: [user1, user2],
32 - socket2.login("guest2"); 34 + } = prepareJoinedRoom(2);
33 35
34 - const room = roomManager.create("테스트", 2); 36 + socket1.testOk("chat", { message: "Hello World" });
35 - expect(socket1.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
36 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
37 - expect(socket1.test("chat", { message: "Hello World" }).ok).eq(true);
38 37
39 expect(socket2.socket.received("chat").message).eq("Hello World"); 38 expect(socket2.socket.received("chat").message).eq("Hello World");
40 - expect(socket2.socket.received("chat").sender).eq("guest1"); 39 + expect(socket2.socket.received("chat").sender).eq(user1.username);
41 socket1.socket.notReceived("chat"); 40 socket1.socket.notReceived("chat");
42 }); 41 });
43 it("빈 채팅은 보낼 수 없습니다", () => { 42 it("빈 채팅은 보낼 수 없습니다", () => {
44 - const socket = new SocketTester(roomManager); 43 + const {
45 - socket.login("guest"); 44 + sockets: [socket],
45 + } = prepareJoinedRoom(1);
46 46
47 - const room = roomManager.create("테스트", 2);
48 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
49 expect(socket.test("chat", { message: "" }).ok).eq(false); 47 expect(socket.test("chat", { message: "" }).ok).eq(false);
50 expect(socket.test("chat", { message: " " }).ok).eq(false); 48 expect(socket.test("chat", { message: " " }).ok).eq(false);
51 }); 49 });
52 it("글자수가 300자를 넘는 채팅은 보낼 수 없습니다", () => { 50 it("글자수가 300자를 넘는 채팅은 보낼 수 없습니다", () => {
53 - const socket = new SocketTester(roomManager); 51 + const {
54 - socket.login("guest"); 52 + sockets: [socket],
53 + } = prepareJoinedRoom(1);
55 54
56 - const room = roomManager.create("테스트", 2);
57 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
58 expect(socket.test("chat", { message: "A".repeat(300) }).ok).eq(true); 55 expect(socket.test("chat", { message: "A".repeat(300) }).ok).eq(true);
59 expect(socket.test("chat", { message: "A".repeat(301) }).ok).eq(false); 56 expect(socket.test("chat", { message: "A".repeat(301) }).ok).eq(false);
60 expect(socket.test("chat", { message: "가".repeat(300) }).ok).eq(true); 57 expect(socket.test("chat", { message: "가".repeat(300) }).ok).eq(true);
......
1 import { expect } from "chai"; 1 import { expect } from "chai";
2 import { RoomManager } from "../room/RoomManager"; 2 import { RoomManager } from "../room/RoomManager";
3 import { DummySocket } from "./util/DummySocket"; 3 import { DummySocket } from "./util/DummySocket";
4 +import { prepareJoinedRoom, prepareUsersEmptyRooms } from "./util/prepare";
4 import { SocketTester } from "./util/SocketTester"; 5 import { SocketTester } from "./util/SocketTester";
5 6
6 describe("방 입장", () => { 7 describe("방 입장", () => {
7 - const roomManager = new RoomManager();
8 -
9 it("로그인하지 않은 유저는 요청할 수 없습니다", () => { 8 it("로그인하지 않은 유저는 요청할 수 없습니다", () => {
9 + const roomManager = new RoomManager();
10 const socket = new SocketTester(roomManager); 10 const socket = new SocketTester(roomManager);
11 const room = roomManager.create("테스트", 2); 11 const room = roomManager.create("테스트", 2);
12 - const response = socket.test("joinRoom", { uuid: room.uuid }); 12 + socket.testNotOk("joinRoom", { uuid: room.uuid });
13 - expect(response.ok).eq(false);
14 }); 13 });
15 it("방에 입장합니다", () => { 14 it("방에 입장합니다", () => {
16 - const socket = new SocketTester(roomManager); 15 + const {
17 - socket.login("guest1"); 16 + sockets: [socket],
18 - 17 + rooms: [room],
19 - const room = roomManager.create("테스트", 2); 18 + } = prepareUsersEmptyRooms(1, 1);
20 19
21 const response = socket.test("joinRoom", { uuid: room.uuid }); 20 const response = socket.test("joinRoom", { uuid: room.uuid });
22 expect(response.ok).eq(true); 21 expect(response.ok).eq(true);
...@@ -27,40 +26,30 @@ describe("방 입장", () => { ...@@ -27,40 +26,30 @@ describe("방 입장", () => {
27 expect(response.result?.users[0]?.username).eq("guest1"); 26 expect(response.result?.users[0]?.username).eq("guest1");
28 }); 27 });
29 it("방에 입장하면 유저 목록이 업데이트 됩니다", () => { 28 it("방에 입장하면 유저 목록이 업데이트 됩니다", () => {
30 - const socket1 = new SocketTester(roomManager); 29 + const {
31 - const socket2 = new SocketTester(roomManager); 30 + sockets: [socket1, socket2],
32 - 31 + } = prepareJoinedRoom(2);
33 - socket1.login("guest1");
34 - socket2.login("guest2");
35 32
36 - const room = roomManager.create("테스트", 2);
37 -
38 - expect(socket1.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
39 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
40 const updated = socket1.socket.received("updateRoomUser"); 33 const updated = socket1.socket.received("updateRoomUser");
41 expect(updated.state).eq("added"); 34 expect(updated.state).eq("added");
42 expect(updated.user.username).eq("guest2"); 35 expect(updated.user.username).eq("guest2");
43 }); 36 });
44 it("방에 이미 입장한 상태에서 다른 방에 입장할 수 없습니다", () => { 37 it("방에 이미 입장한 상태에서 다른 방에 입장할 수 없습니다", () => {
45 - const socket = new SocketTester(roomManager); 38 + const {
46 - socket.login("guest"); 39 + sockets: [socket],
40 + rooms: [room1, room2],
41 + } = prepareUsersEmptyRooms(1, 2);
47 42
48 - const room1 = roomManager.create("테스트1", 2); 43 + socket.testOk("joinRoom", { uuid: room1.uuid });
49 - const room2 = roomManager.create("테스트2", 2); 44 + socket.testNotOk("joinRoom", { uuid: room2.uuid });
50 -
51 - expect(socket.test("joinRoom", { uuid: room1.uuid }).ok).eq(true);
52 - expect(socket.test("joinRoom", { uuid: room2.uuid }).ok).eq(false);
53 }); 45 });
54 it("가득 찬 방에는 입장할 수 없습니다", () => { 46 it("가득 찬 방에는 입장할 수 없습니다", () => {
55 - const socket1 = new SocketTester(roomManager); 47 + const {
56 - const socket2 = new SocketTester(roomManager); 48 + sockets: [socket1, socket2],
57 - 49 + rooms: [room],
58 - socket1.login("guest1"); 50 + } = prepareUsersEmptyRooms(2, 1, 1);
59 - socket2.login("guest2");
60 -
61 - const room = roomManager.create("테스트", 1);
62 51
63 - expect(socket1.test("joinRoom", { uuid: room.uuid }).ok).eq(true); 52 + socket1.testOk("joinRoom", { uuid: room.uuid });
64 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(false); 53 + socket2.testNotOk("joinRoom", { uuid: room.uuid });
65 }); 54 });
66 }); 55 });
......
...@@ -2,63 +2,57 @@ import { expect } from "chai"; ...@@ -2,63 +2,57 @@ import { expect } from "chai";
2 import { RoomManager } from "../room/RoomManager"; 2 import { RoomManager } from "../room/RoomManager";
3 import { User } from "../user/User"; 3 import { User } from "../user/User";
4 import { DummySocket } from "./util/DummySocket"; 4 import { DummySocket } from "./util/DummySocket";
5 +import { prepareJoinedRoom, prepareUsers } from "./util/prepare";
5 import { SocketTester } from "./util/SocketTester"; 6 import { SocketTester } from "./util/SocketTester";
6 7
7 describe("방 퇴장", () => { 8 describe("방 퇴장", () => {
8 - const roomManager = new RoomManager();
9 -
10 it("로그인하지 않은 유저는 요청할 수 없습니다", () => { 9 it("로그인하지 않은 유저는 요청할 수 없습니다", () => {
10 + const roomManager = new RoomManager();
11 const socket = new SocketTester(roomManager); 11 const socket = new SocketTester(roomManager);
12 - const response = socket.test("leaveRoom", {}); 12 + socket.testNotOk("leaveRoom", {});
13 - expect(response.ok).eq(false);
14 }); 13 });
15 it("방에 입장하지 않은 유저는 요청할 수 없습니다", () => { 14 it("방에 입장하지 않은 유저는 요청할 수 없습니다", () => {
16 - const socket = new SocketTester(roomManager); 15 + const {
17 - socket.login("guest"); 16 + sockets: [socket],
17 + } = prepareUsers(1);
18 18
19 - const response = socket.test("leaveRoom", {}); 19 + socket.testNotOk("leaveRoom", {});
20 - expect(response.ok).eq(false);
21 }); 20 });
22 it("방에서 퇴장합니다", () => { 21 it("방에서 퇴장합니다", () => {
23 - const socket = new SocketTester(roomManager); 22 + const {
24 - socket.login("guest"); 23 + sockets: [socket],
25 - 24 + room,
26 - const room = roomManager.create("테스트", 2); 25 + } = prepareJoinedRoom(1);
27 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true); 26 +
28 - expect(socket.test("leaveRoom", {}).ok).eq(true); 27 + expect(room.users.length).eq(1);
28 + socket.testOk("leaveRoom", {});
29 + expect(room.users.length).eq(0);
29 }); 30 });
30 it("방에서 퇴장한 뒤 다시 요청을 보낼 수 없습니다", () => { 31 it("방에서 퇴장한 뒤 다시 요청을 보낼 수 없습니다", () => {
31 - const socket = new SocketTester(roomManager); 32 + const {
32 - socket.login("guest"); 33 + sockets: [socket],
34 + } = prepareJoinedRoom(1);
33 35
34 - const room = roomManager.create("테스트", 2); 36 + socket.testOk("leaveRoom", {});
35 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true); 37 + socket.testNotOk("leaveRoom", {});
36 - expect(socket.test("leaveRoom", {}).ok).eq(true);
37 - expect(socket.test("leaveRoom", {}).ok).eq(false);
38 }); 38 });
39 it("방에서 퇴장하면 유저 목록이 업데이트 됩니다", () => { 39 it("방에서 퇴장하면 유저 목록이 업데이트 됩니다", () => {
40 - const socket1 = new SocketTester(roomManager); 40 + const {
41 - const socket2 = new SocketTester(roomManager); 41 + sockets: [socket1, socket2],
42 - 42 + users: [user1, user2],
43 - socket1.login("guest1"); 43 + } = prepareJoinedRoom(2);
44 - socket2.login("guest2");
45 -
46 - const room = roomManager.create("테스트", 2);
47 44
48 - expect(socket1.test("joinRoom", { uuid: room.uuid }).ok).eq(true); 45 + socket1.testOk("leaveRoom", {});
49 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true); 46 + const updated = socket2.socket.received("updateRoomUser");
50 - expect(socket2.test("leaveRoom", {}).ok).eq(true);
51 - const updated = socket1.socket.received("updateRoomUser");
52 expect(updated.state).eq("removed"); 47 expect(updated.state).eq("removed");
53 - expect(updated.user.username).eq("guest2"); 48 + expect(updated.user.username).eq(user1.username);
54 }); 49 });
55 it("방에서 퇴장한 뒤 아무도 없으면 방이 닫힙니다", () => { 50 it("방에서 퇴장한 뒤 아무도 없으면 방이 닫힙니다", () => {
56 - const socket = new SocketTester(roomManager); 51 + const {
57 - socket.login("guest1"); 52 + sockets: [socket],
58 - 53 + room,
59 - const room = roomManager.create("테스트", 1); 54 + } = prepareJoinedRoom(1);
60 55
61 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
62 expect(room.closed).eq(false); 56 expect(room.closed).eq(false);
63 expect(socket.test("leaveRoom", {}).ok).eq(true); 57 expect(socket.test("leaveRoom", {}).ok).eq(true);
64 expect(room.closed).eq(true); 58 expect(room.closed).eq(true);
......
...@@ -3,11 +3,10 @@ import { RoomManager } from "../room/RoomManager"; ...@@ -3,11 +3,10 @@ import { RoomManager } from "../room/RoomManager";
3 import { SocketTester } from "./util/SocketTester"; 3 import { SocketTester } from "./util/SocketTester";
4 4
5 describe("로그인", () => { 5 describe("로그인", () => {
6 - const roomManager = new RoomManager();
7 it("로그인합니다", () => { 6 it("로그인합니다", () => {
7 + const roomManager = new RoomManager();
8 const socket = new SocketTester(roomManager); 8 const socket = new SocketTester(roomManager);
9 - const response = socket.test("login", { username: "guest" }); 9 + socket.testOk("login", { username: "guest" });
10 - expect(response.ok).eq(true);
11 expect(socket.connection.user?.username).eq("guest"); 10 expect(socket.connection.user?.username).eq("guest");
12 }); 11 });
13 }); 12 });
......
1 import { expect } from "chai"; 1 import { expect } from "chai";
2 import { RoomManager } from "../room/RoomManager"; 2 import { RoomManager } from "../room/RoomManager";
3 +import { prepareJoinedRoom, prepareUsersEmptyRooms } from "./util/prepare";
3 import { SocketTester } from "./util/SocketTester"; 4 import { SocketTester } from "./util/SocketTester";
4 5
5 describe("준비", () => { 6 describe("준비", () => {
6 - const roomManager = new RoomManager();
7 it("방에 입장하면 준비되지 않은 상태입니다", () => { 7 it("방에 입장하면 준비되지 않은 상태입니다", () => {
8 - const socket = new SocketTester(roomManager); 8 + const {
9 - socket.login("guest1"); 9 + sockets: [socket],
10 + rooms: [room],
11 + } = prepareUsersEmptyRooms(1, 1);
10 12
11 - const room = roomManager.create("테스트", 2);
12 const response = socket.test("joinRoom", { uuid: room.uuid }); 13 const response = socket.test("joinRoom", { uuid: room.uuid });
13 expect(response.ok).eq(true); 14 expect(response.ok).eq(true);
14 expect(response.result?.users[0]?.username).eq("guest1"); 15 expect(response.result?.users[0]?.username).eq("guest1");
15 expect(response.result?.users[0]?.ready).eq(false); 16 expect(response.result?.users[0]?.ready).eq(false);
16 }); 17 });
17 it("준비 상태를 설정합니다", () => { 18 it("준비 상태를 설정합니다", () => {
18 - const socket = new SocketTester(roomManager); 19 + const {
19 - socket.login("guest1"); 20 + sockets: [socket],
21 + users: [user],
22 + room,
23 + } = prepareJoinedRoom(1, 2, true);
20 24
21 - const room = roomManager.create("테스트", 2); 25 + expect(room.isReady(user)).eq(false);
22 - expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true); 26 + socket.testOk("ready", { ready: true });
23 - expect(socket.connection.user !== undefined).eq(true); 27 + expect(room.isReady(user)).eq(true);
24 - if (socket.connection.user) { 28 + socket.testOk("ready", { ready: false });
25 - expect(room.isReady(socket.connection.user)).eq(false); 29 + expect(room.isReady(user)).eq(false);
26 - expect(socket.test("ready", { ready: true }).ok).eq(true);
27 - expect(room.isReady(socket.connection.user)).eq(true);
28 - expect(socket.test("ready", { ready: false }).ok).eq(true);
29 - expect(room.isReady(socket.connection.user)).eq(false);
30 - }
31 }); 30 });
32 it("방장은 준비할 수 없습니다", () => { 31 it("방장은 준비할 수 없습니다", () => {
33 - const socket = new SocketTester(roomManager); 32 + const {
34 - socket.login("guest1"); 33 + sockets: [socket],
34 + users: [user],
35 + room,
36 + } = prepareJoinedRoom(1, 2);
35 37
36 - expect(socket.connection.user !== undefined).eq(true); 38 + socket.testNotOk("ready", { ready: true });
37 - if (socket.connection.user) { 39 + expect(room.isReady(user)).eq(false);
38 - const room = roomManager.create("테스트", 2, socket.connection.user);
39 - expect(socket.test("ready", { ready: true }).ok).eq(false);
40 - expect(room.isReady(socket.connection.user)).eq(false);
41 - }
42 }); 40 });
43 it("방장이 되면 준비 상태가 해제됩니다", () => { 41 it("방장이 되면 준비 상태가 해제됩니다", () => {
44 - const socket1 = new SocketTester(roomManager); 42 + const {
45 - const socket2 = new SocketTester(roomManager); 43 + sockets: [socket1, socket2],
46 - socket1.login("guest1"); 44 + users: [user1, user2],
47 - socket2.login("guest2"); 45 + room,
46 + } = prepareJoinedRoom(2);
48 47
49 - expect(socket1.connection.user !== undefined).eq(true); 48 + socket2.testOk("ready", { ready: true });
50 - expect(socket2.connection.user !== undefined).eq(true); 49 + expect(room.isReady(user2)).eq(true);
51 - 50 + room.setAdmin(user2);
52 - if (socket1.connection.user && socket2.connection.user) { 51 + expect(room.isReady(user2)).eq(false);
53 - const room = roomManager.create("테스트", 2, socket1.connection.user);
54 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
55 - expect(socket2.test("ready", { ready: true }).ok).eq(true);
56 - expect(room.isReady(socket2.connection.user));
57 - room.setAdmin(socket2.connection.user);
58 - expect(room.isReady(socket2.connection.user)).eq(false);
59 - }
60 }); 52 });
61 it("모두가 준비해야 게임을 시작할 수 있습니다", () => { 53 it("모두가 준비해야 게임을 시작할 수 있습니다", () => {
62 - const socket1 = new SocketTester(roomManager); 54 + const {
63 - const socket2 = new SocketTester(roomManager); 55 + sockets: [socket1, socket2, socket3],
64 - const socket3 = new SocketTester(roomManager); 56 + room,
65 - socket1.login("guest1"); 57 + } = prepareJoinedRoom(3);
66 - socket2.login("guest2");
67 - socket3.login("guest3");
68 -
69 - expect(socket1.connection.user !== undefined).eq(true);
70 - expect(socket2.connection.user !== undefined).eq(true);
71 - expect(socket3.connection.user !== undefined).eq(true);
72 -
73 - if (
74 - socket1.connection.user &&
75 - socket2.connection.user &&
76 - socket3.connection.user
77 - ) {
78 - const room = roomManager.create("테스트", 3, socket1.connection.user);
79 - expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
80 - expect(socket3.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
81 58
82 - // 2, 3 모두 준비 안함 59 + // 2, 3 모두 준비 안함
83 - expect(room.canStart()).eq(false); 60 + expect(room.canStart()).eq(false);
84 61
85 - // 2만 준비 62 + // 2만 준비
86 - expect(socket2.test("ready", { ready: true }).ok).eq(true); 63 + expect(socket2.test("ready", { ready: true }).ok).eq(true);
87 64
88 - // 3만 준비 65 + // 3만 준비
89 - expect(socket2.test("ready", { ready: false }).ok).eq(true); 66 + expect(socket2.test("ready", { ready: false }).ok).eq(true);
90 - expect(socket3.test("ready", { ready: true }).ok).eq(true); 67 + expect(socket3.test("ready", { ready: true }).ok).eq(true);
91 - expect(room.canStart()).eq(false); 68 + expect(room.canStart()).eq(false);
92 69
93 - // 2, 3 모두 준비 70 + // 2, 3 모두 준비
94 - expect(socket2.test("ready", { ready: true }).ok).eq(true); 71 + expect(socket2.test("ready", { ready: true }).ok).eq(true);
95 - expect(room.canStart()).eq(true); 72 + expect(room.canStart()).eq(true);
96 - }
97 }); 73 });
98 }); 74 });
......
1 import { expect } from "chai"; 1 import { expect } from "chai";
2 import { RoomManager } from "../room/RoomManager"; 2 import { RoomManager } from "../room/RoomManager";
3 import { DummySocket } from "./util/DummySocket"; 3 import { DummySocket } from "./util/DummySocket";
4 +import { prepareUsersEmptyRooms } from "./util/prepare";
4 import { SocketTester } from "./util/SocketTester"; 5 import { SocketTester } from "./util/SocketTester";
5 6
6 describe("방 목록 요청", () => { 7 describe("방 목록 요청", () => {
7 const roomManager = new RoomManager(); 8 const roomManager = new RoomManager();
8 it("로그인하지 않은 유저는 요청할 수 없습니다.", () => { 9 it("로그인하지 않은 유저는 요청할 수 없습니다.", () => {
9 const socket = new SocketTester(roomManager); 10 const socket = new SocketTester(roomManager);
10 - const response = socket.test("roomList", {}); 11 + socket.testNotOk("roomList", {});
11 - expect(response.ok).eq(false);
12 }); 12 });
13 it("방 목록을 가져옵니다.", () => { 13 it("방 목록을 가져옵니다.", () => {
14 - const roomManager = new RoomManager(); 14 + const {
15 - roomManager.create("테스트1", 4); 15 + sockets: [socket],
16 - roomManager.create("테스트2", 2); 16 + rooms: [room1, room2],
17 - 17 + } = prepareUsersEmptyRooms(1, 2);
18 - const socket = new SocketTester(roomManager);
19 - socket.login("guest");
20 18
21 const response = socket.test("roomList", {}); 19 const response = socket.test("roomList", {});
22 expect(response.ok).eq(true); 20 expect(response.ok).eq(true);
23 expect(response.result?.length).eq(2); 21 expect(response.result?.length).eq(2);
24 - expect(response.result[0].name).eq("테스트1"); 22 + expect(response.result[0].name).eq(room1.name);
25 - expect(response.result[1].name).eq("테스트2"); 23 + expect(response.result[1].name).eq(room2.name);
26 }); 24 });
27 }); 25 });
......
1 +import { expect } from "chai";
1 import { 2 import {
2 RawMessage, 3 RawMessage,
3 ServerInboundMessage, 4 ServerInboundMessage,
...@@ -26,6 +27,20 @@ export class SocketTester { ...@@ -26,6 +27,20 @@ export class SocketTester {
26 }); 27 });
27 } 28 }
28 29
30 + public testOk<T extends ServerInboundMessageKey>(
31 + type: T,
32 + message: ServerInboundMessage<T>
33 + ): void {
34 + expect(this.test(type, message).ok).eq(true);
35 + }
36 +
37 + public testNotOk<T extends ServerInboundMessageKey>(
38 + type: T,
39 + message: ServerInboundMessage<T>
40 + ): void {
41 + expect(this.test(type, message).ok).eq(false);
42 + }
43 +
29 public testRaw(raw: RawMessage): ServerResponse<any> { 44 public testRaw(raw: RawMessage): ServerResponse<any> {
30 return this.connection.handleRaw(raw); 45 return this.connection.handleRaw(raw);
31 } 46 }
...@@ -35,6 +50,7 @@ export class SocketTester { ...@@ -35,6 +50,7 @@ export class SocketTester {
35 } 50 }
36 51
37 public login(username: string): void { 52 public login(username: string): void {
38 - this.test("login", { username }); 53 + this.testOk("login", { username });
54 + expect(this.connection.user !== undefined).eq(true);
39 } 55 }
40 } 56 }
......
1 +import { Room } from "../../room/Room";
2 +import { RoomManager } from "../../room/RoomManager";
3 +import { User } from "../../user/User";
4 +import { SocketTester } from "./SocketTester";
5 +
6 +/**
7 + * 유저들을 만듭니다.
8 + * @param userCount 만들어지는 유저 수입니다.
9 + * @returns 만들어진 유저들을 반환합니다.
10 + */
11 +export function prepareUsers(userCount: number): {
12 + sockets: SocketTester[];
13 + users: User[];
14 + roomManager: RoomManager;
15 +} {
16 + const roomManager = new RoomManager();
17 + let sockets = [];
18 + let users = [];
19 + for (let i = 0; i < userCount; i++) {
20 + const socket = new SocketTester(roomManager);
21 + sockets.push(socket);
22 +
23 + socket.login(`guest${i + 1}`);
24 + if (!socket.connection.user) {
25 + throw new Error("User is not initialized.");
26 + }
27 + users.push(socket.connection.user);
28 + }
29 + return { sockets, users, roomManager };
30 +}
31 +
32 +/**
33 + * 유저들과 빈 방들을 만듭니다.
34 + * @param userCount 만들어지는 유저 수입니다.
35 + * @param roomCount 만들어지는 방 수입니다.
36 + * @param roomMaxConnections 각 방의 최대 접속 인원 수 입니다.
37 + * @returns 생성된 소켓, 유저, 방을 반환합니다.
38 + */
39 +export function prepareUsersEmptyRooms(
40 + userCount: number,
41 + roomCount: number,
42 + roomMaxConnections: number = 2
43 +): {
44 + sockets: SocketTester[];
45 + users: User[];
46 + rooms: Room[];
47 + roomManager: RoomManager;
48 +} {
49 + const { sockets, users, roomManager } = prepareUsers(userCount);
50 +
51 + let rooms = [];
52 + for (let i = 0; i < roomCount; i++) {
53 + rooms.push(roomManager.create(`room${i + 1}`, roomMaxConnections));
54 + }
55 + return { sockets, users, rooms, roomManager };
56 +}
57 +
58 +/**
59 + * 방 하나를 만들고 유저들을 접속시킵니다. 이때 첫번째 유저가 방장이 됩니다.
60 + * @param userCount 방에 접속하는 유저 수 입니다.
61 + * @param roomMaxConnections 방에 최대 접속할 수 있는 인원 수 입니다.만약 userCount보다 작다면 userCount값으로 변경됩니다.
62 + * @param emptyAdmin 참이면 방장이 없는 상태로 방이 생성됩니다.
63 + * @returns 생성된 소켓, 유저, 방을 반환합니다.
64 + */
65 +export function prepareJoinedRoom(
66 + userCount: number,
67 + roomMaxConnections: number = 2,
68 + emptyAdmin: boolean = false
69 +): { sockets: SocketTester[]; users: User[]; room: Room } {
70 + roomMaxConnections = Math.max(roomMaxConnections, userCount);
71 + const roomManager = new RoomManager();
72 + let sockets = [];
73 + let users = [];
74 + for (let i = 0; i < userCount; i++) {
75 + const socket = new SocketTester(roomManager);
76 + sockets.push(socket);
77 +
78 + socket.login(`guest${i + 1}`);
79 + if (!socket.connection.user) {
80 + throw new Error("User is not initialized.");
81 + }
82 + users.push(socket.connection.user);
83 + }
84 +
85 + const room = roomManager.create(
86 + "room1",
87 + roomMaxConnections,
88 + emptyAdmin ? undefined : users[0]
89 + );
90 + for (let i = emptyAdmin ? 0 : 1; i < userCount; i++) {
91 + sockets[i].testOk("joinRoom", { uuid: room.uuid });
92 + }
93 + return { sockets, users, room };
94 +}