강동현

DummySocket, SocketTester 구현

import { expect } from "chai";
import { Connection } from "../connection/Connection";
import { SocketWrapper } from "../connection/SocketWrapper";
import { RoomManager } from "../room/RoomManager";
import { DummySocket } from "./util/DummySocket";
import { SocketTester } from "./util/SocketTester";
describe("유효하지 않은 메세지", () => {
it("빈 메세지는 실패합니다", () => {
const socket = new SocketTester();
const response = socket.testAny({});
expect(response.ok).eq(false);
});
it("유효하지 않은 타입의 메세지는 실패합니다", () => {
const socket = new SocketTester();
const response = socket.testRaw({ type: "wtf", message: {} });
expect(response.ok).eq(false);
});
it("유효한 타입이지만 내용이 유효하지 않은 메세지는 실패합니다", () => {
const socket = new SocketTester();
const response = socket.testRaw({ type: "login", message: {} });
expect(response.ok).eq(false);
});
});
import { RawMessage, ServerResponse } from "../../../common";
import { SocketWrapper } from "../../connection/SocketWrapper";
export class DummySocket implements SocketWrapper {
public handler?: (raw: RawMessage) => ServerResponse<any>;
public setHandler(handler: (raw: RawMessage) => ServerResponse<any>) {
this.handler = handler;
}
public send(raw: RawMessage): void {}
}
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);
}
}