chat.test.ts 2.7 KB
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { DummySocket } from "./util/DummySocket";
import { SocketTester } from "./util/SocketTester";

describe("채팅", () => {
  const roomManager = new RoomManager();
  it("로그인하지 않은 유저는 요청할 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    const response = socket.test("chat", { message: "Hello World" });
    expect(response.ok).eq(false);
  });
  it("방에 접속하지 않은 유저는 요청할 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const response = socket.test("chat", { message: "Hello World" });
    expect(response.ok).eq(false);
  });
  it("채팅을 보냅니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const room = roomManager.create("테스트", 2);
    expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket.test("chat", { message: "Hello World" }).ok).eq(true);
  });
  it("자신을 제외한 다른 사람들이 채팅을 받습니다", () => {
    const socket1 = new SocketTester(roomManager);
    socket1.login("guest1");
    const socket2 = new SocketTester(roomManager);
    socket2.login("guest2");

    const room = roomManager.create("테스트", 2);
    expect(socket1.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket2.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket1.test("chat", { message: "Hello World" }).ok).eq(true);

    expect(socket2.socket.received("chat").message).eq("Hello World");
    expect(socket2.socket.received("chat").sender).eq("guest1");
    socket1.socket.notReceived("chat");
  });
  it("빈 채팅은 보낼 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const room = roomManager.create("테스트", 2);
    expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket.test("chat", { message: "" }).ok).eq(false);
    expect(socket.test("chat", { message: "      " }).ok).eq(false);
  });
  it("글자수가 300자를 넘는 채팅은 보낼 수 없습니다", () => {
    const socket = new SocketTester(roomManager);
    socket.login("guest");

    const room = roomManager.create("테스트", 2);
    expect(socket.test("joinRoom", { uuid: room.uuid }).ok).eq(true);
    expect(socket.test("chat", { message: "A".repeat(300) }).ok).eq(true);
    expect(socket.test("chat", { message: "A".repeat(301) }).ok).eq(false);
    expect(socket.test("chat", { message: "가".repeat(300) }).ok).eq(true);
    expect(socket.test("chat", { message: "가".repeat(301) }).ok).eq(false);
  });
});