admin.test.ts 1.97 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("guest");

    expect(socket.connection.user !== undefined).eq(true);
    const room = roomManager.create("테스트", 2, socket.connection.user);
    expect(room.admin).eq(socket.connection.user);
  });
  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);
    const room = roomManager.create("테스트", 2, socket1.connection.user);
    const response = socket2.test("joinRoom", { uuid: room.uuid });
    expect(response.ok).eq(true);
    expect(room.admin).eq(socket1.connection.user);
    expect(room.admin).not.eq(socket2.connection.user);
    expect(response.result?.users[0]?.username).eq("guest1");
    expect(response.result?.users[0]?.admin).eq(true);
  });
  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);
    const room = roomManager.create("테스트", 2, socket1.connection.user);
    expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);

    expect(room.admin).eq(socket1.connection.user);
    expect(room.admin).not.eq(socket2.connection.user);

    expect(socket1.test("leaveRoom", {}).ok).eq(true);
    expect(room.admin).eq(socket2.connection.user);
  });
});