test.test.ts 4.61 KB
import ioclient, { Socket } from "socket.io-client";
import {
  LoginMessage,
  MessageResponse,
  MessageType,
  RoomChatMessage,
  RoomJoinMessage,
  RoomLeaveMessage,
  RoomListRequestMessage,
  RoomUserUpdateMessage,
} from "./message/types";
import { expect } from "chai";
import { Server } from "./Server";
import { RoomDescription, RoomInfo } from "./room/types";
import { response } from "express";

describe("server", () => {
  const PORT = 3000;

  var server: Server;
  var client1: Socket;
  var client2: Socket;

  before((done) => {
    let connected = 0;

    client1 = ioclient(`http://localhost:${PORT}`);
    client1.on("connect", () => ++connected == 2 && done());

    client2 = ioclient(`http://localhost:${PORT}`);
    client2.on("connect", () => ++connected == 2 && done());

    server = new Server(3000);
  });

  after(() => {
    server.close();
    client1.close();
    client2.close();
  });

  var roomUserUpdateMessage: RoomUserUpdateMessage;
  var roomChatMessage: RoomChatMessage;

  step("register listeners", () => {
    client1.on(
      MessageType.ROOM_USER_UPDATE,
      (message: RoomUserUpdateMessage) => {
        roomUserUpdateMessage = message;
      }
    );

    client1.on(MessageType.ROOM_CHAT, (message: RoomChatMessage) => {
      roomChatMessage = message;
    });
  });

  step("login 1", (done) => {
    client1.emit(
      MessageType.LOGIN,
      new LoginMessage("guest1"),
      (response: MessageResponse<undefined>) => {
        expect(response.ok).to.equals(true);
        done();
      }
    );
  });

  step("login 2", (done) => {
    client2.emit(
      MessageType.LOGIN,
      new LoginMessage("guest2"),
      (response: MessageResponse<undefined>) => {
        expect(response.ok).to.equals(true);
        done();
      }
    );
  });

  var roomToJoin: string;

  step("room list", (done) => {
    client1.emit(
      MessageType.ROOM_LIST_REQUEST,
      new RoomListRequestMessage(),
      (response: MessageResponse<RoomDescription[]>) => {
        expect(response.ok).to.eq(true);
        expect(response.result !== undefined).to.eq(true);
        if (response.result) {
          expect(response.result[0].name).to.eq("테스트 방 #1");
          roomToJoin = response.result[0].uuid;
        }
        done();
      }
    );
  });

  step("room join 1", (done) => {
    client1.emit(
      MessageType.ROOM_JOIN,
      new RoomJoinMessage(roomToJoin),
      (response: MessageResponse<RoomInfo>) => {
        expect(response.ok).to.eq(true);
        expect(response.result !== undefined).to.eq(true);
        if (response.result) {
          expect(response.result.uuid).to.eq(roomToJoin);
          expect(response.result.users.length).to.eq(1);
          expect(response.result.users[0].username).to.eq("guest1");
        }
        done();
      }
    );
  });

  step("room join 2", (done) => {
    client2.emit(
      MessageType.ROOM_JOIN,
      new RoomJoinMessage(roomToJoin),
      (response: MessageResponse<RoomInfo>) => {
        expect(response.ok).to.eq(true);
        expect(response.result !== undefined).to.eq(true);
        if (response.result) {
          expect(response.result.uuid).to.eq(roomToJoin);
          expect(response.result.users.length).to.eq(2);
        }
        done();
      }
    );
  });

  // TODO: RoomUserUpdateMessage가 아직 도착하지 않았는데 실행되는 경우
  step("client 1 received user update", () => {
    expect(roomUserUpdateMessage !== undefined).to.eq(true);
    if (roomUserUpdateMessage) {
      expect(roomUserUpdateMessage.state).to.eq("added");
      expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
    }
  });

  step("client 2 send chat", (done) => {
    client2.emit(
      MessageType.ROOM_CHAT,
      new RoomChatMessage("Hello World"),
      (response: MessageResponse<undefined>) => {
        expect(response.ok).to.eq(true);
        done();
      }
    );
  });

  step("client 1 received chat", () => {
    expect(roomChatMessage !== undefined).to.eq(true);
    if (roomChatMessage) {
      expect(roomChatMessage.sender).to.eq("guest2");
      expect(roomChatMessage.message).to.eq("Hello World");
    }
  });

  step("client 2 leave", (done) => {
    client2.emit(
      MessageType.ROOM_LEAVE,
      new RoomLeaveMessage(),
      (response: MessageResponse<undefined>) => {
        expect(response.ok).to.eq(true);
        done();
      }
    );
  });

  step("client 1 received user update", () => {
    expect(roomUserUpdateMessage !== undefined).to.eq(true);
    if (roomUserUpdateMessage) {
      expect(roomUserUpdateMessage.state).to.eq("removed");
      expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
    }
  });
});