SocketTester.ts 967 Bytes
import {
  RawMessage,
  ServerInboundMessage,
  ServerInboundMessageKey,
  ServerResponse,
} from "../../../common";
import { Connection } from "../../connection/Connection";
import { RoomManager } from "../../room/RoomManager";
import { DummySocket } from "./DummySocket";

export class SocketTester {
  public readonly connection: Connection;
  constructor(
    public socket: DummySocket = new DummySocket(),
    roomManager: RoomManager = new RoomManager()
  ) {
    this.connection = new Connection(socket, roomManager);
  }

  public test<T extends ServerInboundMessageKey>(
    type: T,
    message: ServerInboundMessage<T>
  ): ServerResponse<any> {
    return this.connection.handleRaw({
      type: type as string,
      message: message,
    });
  }

  public testRaw(raw: RawMessage): ServerResponse<any> {
    return this.connection.handleRaw(raw);
  }

  public testAny(obj: any): ServerResponse<any> {
    return this.connection.handleRaw(obj);
  }
}