admin.test.ts 2.82 KB
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { prepareJoinedRoom, prepareUsers } from "./util/prepare";
import { SocketTester } from "./util/SocketTester";

describe("방장", () => {
  it("방을 만든 유저가 방장이 됩니다", () => {
    const {
      users: [user],
      room,
    } = prepareJoinedRoom(1);

    expect(room.admin).eq(user);
  });
  it("나중에 들어온 유저는 방장 정보를 받지만 방장이 되지 않습니다", () => {
    const {
      sockets: [socket1, socket2],
      users: [user1, user2],
      roomManager,
    } = prepareUsers(2);

    const room = roomManager.create("테스트", 2, user1);

    const response = socket2.test("joinRoom", { uuid: room.uuid });
    expect(response.ok).eq(true);
    expect(room.admin).eq(user1);

    expect(response.result?.users[0].nickname).eq(user1.nickname);
    expect(response.result?.users[0].admin).eq(true);
    expect(response.result?.users[0].ready).eq(false);

    expect(response.result?.users[1].nickname).eq(user2.nickname);
    expect(response.result?.users[1].admin).eq(false);
    expect(response.result?.users[1].ready).eq(false);
  });
  it("방장이 나가면 방장이 인계됩니다", () => {
    const {
      sockets: [socket1, socket2],
      users: [user1, user2],
      room,
    } = prepareJoinedRoom(2);

    expect(room.admin).eq(user1);
    socket1.testOk("leaveRoom", {});
    expect(room.admin).eq(user2);
  });
  it("방장을 인계하면 정보가 업데이트됩니다", () => {
    const {
      sockets: [socket1, socket2],
      users: [user1, user2],
      room,
    } = prepareJoinedRoom(2);

    expect(room.admin).eq(user1);

    room.setAdmin(user2);
    expect(room.admin).eq(user2);

    // guest1은 guest2가 접속했다는 메세지를 먼저 받은 상태
    let message = socket1.socket.received("updateRoomUser");
    expect(message.state).eq("added");
    expect(message.user.username).eq(user2.username);

    // 먼저 이전 방장의 업데이트를 받음
    message = socket1.socket.received("updateRoomUser");
    expect(message.state).eq("updated");
    expect(message.user.username).eq(user1.username);
    expect(message.user.admin).eq(false);

    message = socket2.socket.received("updateRoomUser");
    expect(message.state).eq("updated");
    expect(message.user.username).eq(user1.username);
    expect(message.user.admin).eq(false);

    // 현재 방장의 업데이트를 받음
    message = socket1.socket.received("updateRoomUser");
    expect(message.state).eq("updated");
    expect(message.user.username).eq(user2.username);
    expect(message.user.admin).eq(true);

    message = socket2.socket.received("updateRoomUser");
    expect(message.state).eq("updated");
    expect(message.user.username).eq(user2.username);
    expect(message.user.admin).eq(true);
  });
});