ready.test.ts 3.81 KB
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { SocketTester } from "./util/SocketTester";

describe("준비", () => {
  const roomManager = new RoomManager();
  it("방에 입장하면 준비되지 않은 상태입니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest1");

    const room = roomManager.create("테스트", 2);
    const response = socket.test("joinRoom", { uuid: room.uuid });
    expect(response.ok).eq(true);
    expect(response.result?.users[0]?.username).eq("guest1");
    expect(response.result?.users[0]?.ready).eq(false);
  });
  it("준비 상태를 설정합니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest1");

    const room = roomManager.create("테스트", 2);
    expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket.connection.user !== undefined).eq(true);
    if (socket.connection.user) {
      expect(room.isReady(socket.connection.user)).eq(false);
      expect(socket.test("ready", { ready: true }).ok).eq(true);
      expect(room.isReady(socket.connection.user)).eq(true);
      expect(socket.test("ready", { ready: false }).ok).eq(true);
      expect(room.isReady(socket.connection.user)).eq(false);
    }
  });
  it("방장은 준비할 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest1");

    expect(socket.connection.user !== undefined).eq(true);
    if (socket.connection.user) {
      const room = roomManager.create("테스트", 2, socket.connection.user);
      expect(socket.test("ready", { ready: true }).ok).eq(false);
      expect(room.isReady(socket.connection.user)).eq(false);
    }
  });
  it("방장이 되면 준비 상태가 해제됩니다", () => {
    const socket1 = new SocketTester(roomManager);
    const socket2 = new SocketTester(roomManager);
    socket1.login("guest1");
    socket2.login("guest2");

    expect(socket1.connection.user !== undefined).eq(true);
    expect(socket2.connection.user !== undefined).eq(true);

    if (socket1.connection.user && socket2.connection.user) {
      const room = roomManager.create("테스트", 2, socket1.connection.user);
      expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
      expect(socket2.test("ready", { ready: true }).ok).eq(true);
      expect(room.isReady(socket2.connection.user));
      room.setAdmin(socket2.connection.user);
      expect(room.isReady(socket2.connection.user)).eq(false);
    }
  });
  it("모두가 준비해야 게임을 시작할 수 있습니다", () => {
    const socket1 = new SocketTester(roomManager);
    const socket2 = new SocketTester(roomManager);
    const socket3 = new SocketTester(roomManager);
    socket1.login("guest1");
    socket2.login("guest2");
    socket3.login("guest3");

    expect(socket1.connection.user !== undefined).eq(true);
    expect(socket2.connection.user !== undefined).eq(true);
    expect(socket3.connection.user !== undefined).eq(true);

    if (
      socket1.connection.user &&
      socket2.connection.user &&
      socket3.connection.user
    ) {
      const room = roomManager.create("테스트", 3, socket1.connection.user);
      expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
      expect(socket3.test("joinRoom", { uuid: room.uuid }).ok).eq(true);

      // 2, 3 모두 준비 안함
      expect(room.canStart()).eq(false);

      // 2만 준비
      expect(socket2.test("ready", { ready: true }).ok).eq(true);

      // 3만 준비
      expect(socket2.test("ready", { ready: false }).ok).eq(true);
      expect(socket3.test("ready", { ready: true }).ok).eq(true);
      expect(room.canStart()).eq(false);

      // 2, 3 모두 준비
      expect(socket2.test("ready", { ready: true }).ok).eq(true);
      expect(room.canStart()).eq(true);
    }
  });
});