Builds for
1 pipeline
passed
in
7 minutes 45 seconds
라운드 테스트 작성
Showing
3 changed files
with
216 additions
and
0 deletions
server/test/round.test.ts
0 → 100644
| 1 | +import { expect } from "chai"; | ||
| 2 | +import { prepareGame } from "./util/prepare"; | ||
| 3 | + | ||
| 4 | +describe("라운드", () => { | ||
| 5 | + it("첫 라운드가 시작되면 startRound와 wordSet을 받습니다", () => { | ||
| 6 | + const { | ||
| 7 | + sockets: [socket1, socket2], | ||
| 8 | + drawerSocket, | ||
| 9 | + } = prepareGame(2); | ||
| 10 | + | ||
| 11 | + expect(socket1.socket.received("startRound").round).eq(1); | ||
| 12 | + expect(socket2.socket.received("startRound").round).eq(1); | ||
| 13 | + | ||
| 14 | + // drawer는 wordSet을 받습니다. | ||
| 15 | + expect(drawerSocket.socket.received("wordSet").words.length).eq(3); | ||
| 16 | + }); | ||
| 17 | + it("drawer가 단어를 선택하면 모두가 wordChosen과 timer를 받습니다", () => { | ||
| 18 | + const { drawerSocket, guesserSockets } = prepareGame(2); | ||
| 19 | + | ||
| 20 | + const word = drawerSocket.socket.received("wordSet").words[0]; | ||
| 21 | + drawerSocket.testOk("chooseWord", { word }); | ||
| 22 | + | ||
| 23 | + expect(drawerSocket.socket.received("wordChosen").length).eq(word.length); | ||
| 24 | + expect(guesserSockets[0].socket.received("wordChosen").length).eq( | ||
| 25 | + word.length | ||
| 26 | + ); | ||
| 27 | + | ||
| 28 | + expect(drawerSocket.socket.received("timer")).deep.eq({ | ||
| 29 | + state: "started", | ||
| 30 | + time: 60, | ||
| 31 | + }); | ||
| 32 | + expect(guesserSockets[0].socket.received("timer")).deep.eq({ | ||
| 33 | + state: "started", | ||
| 34 | + time: 60, | ||
| 35 | + }); | ||
| 36 | + }); | ||
| 37 | + it("drawer가 단어를 선택하지 않으면 라운드가 진행되지 않습니다", (done) => { | ||
| 38 | + const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.1); | ||
| 39 | + | ||
| 40 | + // 0.2초 뒤에도 라운드가 종료되지 않습니다. | ||
| 41 | + setTimeout(() => { | ||
| 42 | + drawerSocket.socket.notReceived("finishRound"); | ||
| 43 | + guesserSockets[0].socket.notReceived("finishRound"); | ||
| 44 | + done(); | ||
| 45 | + }, 200); | ||
| 46 | + }); | ||
| 47 | + it("아무도 단어를 맞추지 못하고 시간이 지나면 라운드가 종료됩니다", (done) => { | ||
| 48 | + const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.2); | ||
| 49 | + | ||
| 50 | + const word = drawerSocket.socket.received("wordSet").words[0]; | ||
| 51 | + drawerSocket.testOk("chooseWord", { word }); | ||
| 52 | + | ||
| 53 | + // 0.1초 뒤에는 라운드가 종료되지 않습니다. | ||
| 54 | + setTimeout(() => { | ||
| 55 | + drawerSocket.socket.notReceived("finishRound"); | ||
| 56 | + guesserSockets[0].socket.notReceived("finishRound"); | ||
| 57 | + }, 100); | ||
| 58 | + // 0.3초 뒤에는 라운드가 종료됩니다. | ||
| 59 | + setTimeout(() => { | ||
| 60 | + expect(drawerSocket.socket.received("finishRound").answer).eq(word); | ||
| 61 | + expect(guesserSockets[0].socket.received("finishRound").answer).eq(word); | ||
| 62 | + done(); | ||
| 63 | + }, 300); | ||
| 64 | + }); | ||
| 65 | + it("모든 guesser가 단어를 맞추면 라운드가 종료됩니다", (done) => { | ||
| 66 | + const { drawerSocket, guesserSockets } = prepareGame(3, 5, 0.5); | ||
| 67 | + | ||
| 68 | + const word = drawerSocket.socket.received("wordSet").words[0]; | ||
| 69 | + expect(drawerSocket.test("chooseWord", { word })); | ||
| 70 | + | ||
| 71 | + // 0.1초 뒤에는 라운드가 종료되지 않습니다. | ||
| 72 | + setTimeout(() => { | ||
| 73 | + drawerSocket.socket.notReceived("finishRound"); | ||
| 74 | + | ||
| 75 | + // 첫번째 guesser가 단어를 맞춥니다. | ||
| 76 | + guesserSockets[0].test("chat", { message: word }); | ||
| 77 | + expect(guesserSockets[0].socket.received("answerAccepted").answer).eq( | ||
| 78 | + word | ||
| 79 | + ); | ||
| 80 | + }, 100); | ||
| 81 | + // 0.2초 뒤에도 라운드가 종료되지 않습니다. | ||
| 82 | + setTimeout(() => { | ||
| 83 | + drawerSocket.socket.notReceived("finishRound"); | ||
| 84 | + | ||
| 85 | + // 두번째 guesser가 단어를 맞춥니다. | ||
| 86 | + guesserSockets[1].test("chat", { message: word }); | ||
| 87 | + expect(guesserSockets[1].socket.received("answerAccepted").answer).eq( | ||
| 88 | + word | ||
| 89 | + ); | ||
| 90 | + }, 200); | ||
| 91 | + // 0.3초 뒤에는 라운드가 종료됩니다. | ||
| 92 | + setTimeout(() => { | ||
| 93 | + drawerSocket.socket.received("finishRound"); | ||
| 94 | + done(); | ||
| 95 | + }, 300); | ||
| 96 | + }); | ||
| 97 | + it("라운드가 종료되면 다음 라운드가 시작됩니다", (done) => { | ||
| 98 | + const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.2, 0.2); | ||
| 99 | + | ||
| 100 | + drawerSocket.socket.received("startRound"); | ||
| 101 | + guesserSockets[0].socket.received("startRound"); | ||
| 102 | + | ||
| 103 | + const word = drawerSocket.socket.received("wordSet").words[0]; | ||
| 104 | + drawerSocket.testOk("chooseWord", { word }); | ||
| 105 | + | ||
| 106 | + // 0.1초 뒤에는 라운드가 종료되지 않습니다. | ||
| 107 | + setTimeout(() => { | ||
| 108 | + drawerSocket.socket.notReceived("finishRound"); | ||
| 109 | + guesserSockets[0].socket.notReceived("finishRound"); | ||
| 110 | + }, 100); | ||
| 111 | + // 0.3초 뒤에는 라운드가 종료됩니다. | ||
| 112 | + setTimeout(() => { | ||
| 113 | + expect(drawerSocket.socket.received("finishRound").answer).eq(word); | ||
| 114 | + expect(guesserSockets[0].socket.received("finishRound").answer).eq(word); | ||
| 115 | + drawerSocket.socket.notReceived("startRound"); | ||
| 116 | + }, 300); | ||
| 117 | + // 0.5초 뒤에는 다음 라운드가 시작됩니다. | ||
| 118 | + setTimeout(() => { | ||
| 119 | + expect(drawerSocket.socket.received("startRound").round).eq(2); | ||
| 120 | + expect(guesserSockets[0].socket.received("startRound").round).eq(2); | ||
| 121 | + done(); | ||
| 122 | + }, 500); | ||
| 123 | + }); | ||
| 124 | +}); |
server/test/startGame.test.ts
0 → 100644
| 1 | +import { expect } from "chai"; | ||
| 2 | +import { prepareJoinedRoom, prepareUsersEmptyRooms } from "./util/prepare"; | ||
| 3 | + | ||
| 4 | +describe("게임 시작", () => { | ||
| 5 | + it("방장만 게임 시작을 요청할 수 있습니다.", () => { | ||
| 6 | + const { | ||
| 7 | + sockets: [socket1, socket2], | ||
| 8 | + room, | ||
| 9 | + } = prepareJoinedRoom(2); | ||
| 10 | + | ||
| 11 | + expect(room.admin).eq(socket1.connection.user); | ||
| 12 | + expect(socket2.testOk("ready", { ready: true })); | ||
| 13 | + expect(room.canStart().ok).eq(true); | ||
| 14 | + | ||
| 15 | + expect(socket2.testNotOk("startGame", {})); | ||
| 16 | + expect(socket1.testOk("startGame", {})); | ||
| 17 | + }); | ||
| 18 | + it("인원이 충분해야 게임을 시작할 수 있습니다.", () => { | ||
| 19 | + const { | ||
| 20 | + sockets: [socket1], | ||
| 21 | + } = prepareJoinedRoom(1); | ||
| 22 | + | ||
| 23 | + expect(socket1.testNotOk("startGame", {})); | ||
| 24 | + }); | ||
| 25 | + it("게임이 시작되면 startRound를 받습니다.", () => { | ||
| 26 | + const { | ||
| 27 | + sockets: [socket1, socket2], | ||
| 28 | + } = prepareJoinedRoom(2); | ||
| 29 | + | ||
| 30 | + expect(socket2.testOk("ready", { ready: true })); | ||
| 31 | + expect(socket1.testOk("startGame", {})); | ||
| 32 | + | ||
| 33 | + expect(socket1.socket.received("startRound")); | ||
| 34 | + expect(socket2.socket.received("startRound")); | ||
| 35 | + }); | ||
| 36 | +}); |
| 1 | +import { Game } from "../../game/Game"; | ||
| 1 | import { Room } from "../../room/Room"; | 2 | import { Room } from "../../room/Room"; |
| 2 | import { RoomManager } from "../../room/RoomManager"; | 3 | import { RoomManager } from "../../room/RoomManager"; |
| 3 | import { User } from "../../user/User"; | 4 | import { User } from "../../user/User"; |
| ... | @@ -92,3 +93,58 @@ export function prepareJoinedRoom( | ... | @@ -92,3 +93,58 @@ export function prepareJoinedRoom( |
| 92 | } | 93 | } |
| 93 | return { sockets, users, room }; | 94 | return { sockets, users, room }; |
| 94 | } | 95 | } |
| 96 | + | ||
| 97 | +export function prepareGame( | ||
| 98 | + userCount: number, | ||
| 99 | + maxRound: number = 5, | ||
| 100 | + roundDuration: number = 60, | ||
| 101 | + roundTerm: number = 5, | ||
| 102 | + roomMaxConnections: number = 2 | ||
| 103 | +): { | ||
| 104 | + sockets: SocketTester[]; | ||
| 105 | + users: User[]; | ||
| 106 | + room: Room; | ||
| 107 | + game: Game; | ||
| 108 | + drawerSocket: SocketTester; | ||
| 109 | + guesserSockets: SocketTester[]; | ||
| 110 | +} { | ||
| 111 | + const { sockets, users, room } = prepareJoinedRoom( | ||
| 112 | + userCount, | ||
| 113 | + roomMaxConnections | ||
| 114 | + ); | ||
| 115 | + | ||
| 116 | + for (let i = 1; i < userCount; i++) { | ||
| 117 | + sockets[i].testOk("ready", { ready: true }); | ||
| 118 | + } | ||
| 119 | + sockets[0].testOk("startGame", { maxRound, roundDuration, roundTerm }); | ||
| 120 | + | ||
| 121 | + if (!room.game) { | ||
| 122 | + throw new Error("Game is not initialized."); | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + let drawerSocket = undefined; | ||
| 126 | + let guesserSockets: SocketTester[] = []; | ||
| 127 | + sockets.forEach((socket) => { | ||
| 128 | + if (socket.connection.user === room.game?.drawer) { | ||
| 129 | + drawerSocket = socket; | ||
| 130 | + } else { | ||
| 131 | + guesserSockets.push(socket); | ||
| 132 | + } | ||
| 133 | + }); | ||
| 134 | + | ||
| 135 | + if (!drawerSocket) { | ||
| 136 | + throw new Error("There is no drawer!"); | ||
| 137 | + } | ||
| 138 | + if (guesserSockets.length == 0) { | ||
| 139 | + throw new Error("There is no guesser!"); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + return { | ||
| 143 | + sockets, | ||
| 144 | + users, | ||
| 145 | + room, | ||
| 146 | + game: room.game, | ||
| 147 | + drawerSocket, | ||
| 148 | + guesserSockets, | ||
| 149 | + }; | ||
| 150 | +} | ... | ... |
-
Please register or login to post a comment