ready.test.ts 2.22 KB
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { prepareJoinedRoom, prepareUsersEmptyRooms } from "./util/prepare";
import { SocketTester } from "./util/SocketTester";

describe("준비", () => {
  it("방에 입장하면 준비되지 않은 상태입니다", () => {
    const {
      sockets: [socket],
      rooms: [room],
    } = prepareUsersEmptyRooms(1, 1);

    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 {
      sockets: [socket],
      users: [user],
      room,
    } = prepareJoinedRoom(1, 2, true);

    expect(room.isReady(user)).eq(false);
    socket.testOk("ready", { ready: true });
    expect(room.isReady(user)).eq(true);
    socket.testOk("ready", { ready: false });
    expect(room.isReady(user)).eq(false);
  });
  it("방장은 준비할 수 없습니다", () => {
    const {
      sockets: [socket],
      users: [user],
      room,
    } = prepareJoinedRoom(1, 2);

    socket.testNotOk("ready", { ready: true });
    expect(room.isReady(user)).eq(false);
  });
  it("방장이 되면 준비 상태가 해제됩니다", () => {
    const {
      sockets: [socket1, socket2],
      users: [user1, user2],
      room,
    } = prepareJoinedRoom(2);

    socket2.testOk("ready", { ready: true });
    expect(room.isReady(user2)).eq(true);
    room.setAdmin(user2);
    expect(room.isReady(user2)).eq(false);
  });
  it("모두가 준비해야 게임을 시작할 수 있습니다", () => {
    const {
      sockets: [socket1, socket2, socket3],
      room,
    } = prepareJoinedRoom(3);

    // 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);
  });
});