roundChat.test.ts 3.06 KB
import { expect } from "chai";
import { prepareGame } from "./util/prepare";

describe("라운드 채팅", () => {
  it("guesser가 정답을 채팅으로 보내면 정답 처리되고 다른 사람들에게 채팅이 보이지 않습니다", () => {
    const { drawerSocket, guesserSockets } = prepareGame(3);

    const word = drawerSocket.socket.received("wordSet").words[0];
    drawerSocket.testOk("chooseWord", { word });

    guesserSockets[0].testOk("chat", { message: "Not Answer" });
    guesserSockets[0].socket.notReceived("answerAccepted");
    guesserSockets[1].socket.received("chat");

    guesserSockets[0].testOk("chat", { message: word });
    expect(guesserSockets[0].socket.received("answerAccepted").answer).eq(word);
    guesserSockets[1].socket.notReceived("chat");
  });
  it("guesser가 정답을 채팅으로 보내면 역할이 winner로 변경됩니다", () => {
    const { drawerSocket, guesserSockets } = prepareGame(2);

    const word = drawerSocket.socket.received("wordSet").words[0];
    drawerSocket.testOk("chooseWord", { word });

    guesserSockets[0].testOk("chat", { message: word });

    expect(guesserSockets[0].socket.received("role")).deep.eq({
      username: guesserSockets[0].connection.user?.username,
      role: "winner",
    });
    expect(drawerSocket.socket.received("role")).deep.eq({
      username: guesserSockets[0].connection.user?.username,
      role: "winner",
    });
  });
  it("라운드가 끝나고 다음 라운드를 준비하는 시간에 답을 채팅으로 보내도 정답 처리되지 않습니다", (done) => {
    const { drawerSocket, guesserSockets } = prepareGame(2, 5, 0.1, 0.3);

    const word = drawerSocket.socket.received("wordSet").words[0];
    drawerSocket.testOk("chooseWord", { word });

    guesserSockets[0].socket.notReceived("finishRound");
    setTimeout(() => {
      guesserSockets[0].socket.received("finishRound");
      guesserSockets[0].testOk("chat", { message: word });
      guesserSockets[0].socket.notReceived("answerAccepted");
      guesserSockets[0].socket.notReceived("role");
      done();
    }, 200);
  });
  it("다음 라운드의 단어가 선택되지 않았을 때 이전 라운드의 답을 채팅으로 보내도 정답 처리되지 않습니다", (done) => {
    const { drawerSocket, guesserSockets, game } = prepareGame(2, 5, 0.2, 0.1);

    const word = drawerSocket.socket.received("wordSet").words[0];
    drawerSocket.testOk("chooseWord", { word });

    expect(guesserSockets[0].socket.received("startRound").round).eq(1);
    setTimeout(() => {
      expect(guesserSockets[0].socket.received("startRound").round).eq(2);

      if (game.drawer === drawerSocket.connection.user) {
        guesserSockets[0].testOk("chat", { message: word });
        guesserSockets[0].socket.notReceived("answerAccepted");
      } else if (game.drawer === guesserSockets[0].connection.user) {
        drawerSocket.testOk("chat", { message: word });
        drawerSocket.socket.notReceived("answerAccepted");
      } else {
        throw new Error("There is no drawer!");
      }
      done();
    }, 400);
  });
});