강동현
Builds for 1 pipeline passed in 7 minutes 45 seconds

라운드 테스트 작성

import { expect } from "chai";
import { prepareGame } from "./util/prepare";
describe("라운드", () => {
it("첫 라운드가 시작되면 startRound와 wordSet을 받습니다", () => {
const {
sockets: [socket1, socket2],
drawerSocket,
} = prepareGame(2);
expect(socket1.socket.received("startRound").round).eq(1);
expect(socket2.socket.received("startRound").round).eq(1);
// drawer는 wordSet을 받습니다.
expect(drawerSocket.socket.received("wordSet").words.length).eq(3);
});
it("drawer가 단어를 선택하면 모두가 wordChosen과 timer를 받습니다", () => {
const { drawerSocket, guesserSockets } = prepareGame(2);
const word = drawerSocket.socket.received("wordSet").words[0];
drawerSocket.testOk("chooseWord", { word });
expect(drawerSocket.socket.received("wordChosen").length).eq(word.length);
expect(guesserSockets[0].socket.received("wordChosen").length).eq(
word.length
);
expect(drawerSocket.socket.received("timer")).deep.eq({
state: "started",
time: 60,
});
expect(guesserSockets[0].socket.received("timer")).deep.eq({
state: "started",
time: 60,
});
});
it("drawer가 단어를 선택하지 않으면 라운드가 진행되지 않습니다", (done) => {
const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.1);
// 0.2초 뒤에도 라운드가 종료되지 않습니다.
setTimeout(() => {
drawerSocket.socket.notReceived("finishRound");
guesserSockets[0].socket.notReceived("finishRound");
done();
}, 200);
});
it("아무도 단어를 맞추지 못하고 시간이 지나면 라운드가 종료됩니다", (done) => {
const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.2);
const word = drawerSocket.socket.received("wordSet").words[0];
drawerSocket.testOk("chooseWord", { word });
// 0.1초 뒤에는 라운드가 종료되지 않습니다.
setTimeout(() => {
drawerSocket.socket.notReceived("finishRound");
guesserSockets[0].socket.notReceived("finishRound");
}, 100);
// 0.3초 뒤에는 라운드가 종료됩니다.
setTimeout(() => {
expect(drawerSocket.socket.received("finishRound").answer).eq(word);
expect(guesserSockets[0].socket.received("finishRound").answer).eq(word);
done();
}, 300);
});
it("모든 guesser가 단어를 맞추면 라운드가 종료됩니다", (done) => {
const { drawerSocket, guesserSockets } = prepareGame(3, 5, 0.5);
const word = drawerSocket.socket.received("wordSet").words[0];
expect(drawerSocket.test("chooseWord", { word }));
// 0.1초 뒤에는 라운드가 종료되지 않습니다.
setTimeout(() => {
drawerSocket.socket.notReceived("finishRound");
// 첫번째 guesser가 단어를 맞춥니다.
guesserSockets[0].test("chat", { message: word });
expect(guesserSockets[0].socket.received("answerAccepted").answer).eq(
word
);
}, 100);
// 0.2초 뒤에도 라운드가 종료되지 않습니다.
setTimeout(() => {
drawerSocket.socket.notReceived("finishRound");
// 두번째 guesser가 단어를 맞춥니다.
guesserSockets[1].test("chat", { message: word });
expect(guesserSockets[1].socket.received("answerAccepted").answer).eq(
word
);
}, 200);
// 0.3초 뒤에는 라운드가 종료됩니다.
setTimeout(() => {
drawerSocket.socket.received("finishRound");
done();
}, 300);
});
it("라운드가 종료되면 다음 라운드가 시작됩니다", (done) => {
const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.2, 0.2);
drawerSocket.socket.received("startRound");
guesserSockets[0].socket.received("startRound");
const word = drawerSocket.socket.received("wordSet").words[0];
drawerSocket.testOk("chooseWord", { word });
// 0.1초 뒤에는 라운드가 종료되지 않습니다.
setTimeout(() => {
drawerSocket.socket.notReceived("finishRound");
guesserSockets[0].socket.notReceived("finishRound");
}, 100);
// 0.3초 뒤에는 라운드가 종료됩니다.
setTimeout(() => {
expect(drawerSocket.socket.received("finishRound").answer).eq(word);
expect(guesserSockets[0].socket.received("finishRound").answer).eq(word);
drawerSocket.socket.notReceived("startRound");
}, 300);
// 0.5초 뒤에는 다음 라운드가 시작됩니다.
setTimeout(() => {
expect(drawerSocket.socket.received("startRound").round).eq(2);
expect(guesserSockets[0].socket.received("startRound").round).eq(2);
done();
}, 500);
});
});
import { expect } from "chai";
import { prepareJoinedRoom, prepareUsersEmptyRooms } from "./util/prepare";
describe("게임 시작", () => {
it("방장만 게임 시작을 요청할 수 있습니다.", () => {
const {
sockets: [socket1, socket2],
room,
} = prepareJoinedRoom(2);
expect(room.admin).eq(socket1.connection.user);
expect(socket2.testOk("ready", { ready: true }));
expect(room.canStart().ok).eq(true);
expect(socket2.testNotOk("startGame", {}));
expect(socket1.testOk("startGame", {}));
});
it("인원이 충분해야 게임을 시작할 수 있습니다.", () => {
const {
sockets: [socket1],
} = prepareJoinedRoom(1);
expect(socket1.testNotOk("startGame", {}));
});
it("게임이 시작되면 startRound를 받습니다.", () => {
const {
sockets: [socket1, socket2],
} = prepareJoinedRoom(2);
expect(socket2.testOk("ready", { ready: true }));
expect(socket1.testOk("startGame", {}));
expect(socket1.socket.received("startRound"));
expect(socket2.socket.received("startRound"));
});
});
import { Game } from "../../game/Game";
import { Room } from "../../room/Room";
import { RoomManager } from "../../room/RoomManager";
import { User } from "../../user/User";
......@@ -92,3 +93,58 @@ export function prepareJoinedRoom(
}
return { sockets, users, room };
}
export function prepareGame(
userCount: number,
maxRound: number = 5,
roundDuration: number = 60,
roundTerm: number = 5,
roomMaxConnections: number = 2
): {
sockets: SocketTester[];
users: User[];
room: Room;
game: Game;
drawerSocket: SocketTester;
guesserSockets: SocketTester[];
} {
const { sockets, users, room } = prepareJoinedRoom(
userCount,
roomMaxConnections
);
for (let i = 1; i < userCount; i++) {
sockets[i].testOk("ready", { ready: true });
}
sockets[0].testOk("startGame", { maxRound, roundDuration, roundTerm });
if (!room.game) {
throw new Error("Game is not initialized.");
}
let drawerSocket = undefined;
let guesserSockets: SocketTester[] = [];
sockets.forEach((socket) => {
if (socket.connection.user === room.game?.drawer) {
drawerSocket = socket;
} else {
guesserSockets.push(socket);
}
});
if (!drawerSocket) {
throw new Error("There is no drawer!");
}
if (guesserSockets.length == 0) {
throw new Error("There is no guesser!");
}
return {
sockets,
users,
room,
game: room.game,
drawerSocket,
guesserSockets,
};
}
......