admin.test.ts
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { expect } from "chai";
import { RoomManager } from "../room/RoomManager";
import { prepareJoinedRoom, prepareUsers } from "./util/prepare";
import { SocketTester } from "./util/SocketTester";
describe("방장", () => {
it("방을 만든 유저가 방장이 됩니다", () => {
const {
users: [user],
room,
} = prepareJoinedRoom(1);
expect(room.admin).eq(user);
});
it("나중에 들어온 유저는 방장 정보를 받지만 방장이 되지 않습니다", () => {
const {
sockets: [socket1, socket2],
users: [user1, user2],
roomManager,
} = prepareUsers(2);
const room = roomManager.create("테스트", 2, user1);
const response = socket2.test("joinRoom", { uuid: room.uuid });
expect(response.ok).eq(true);
expect(room.admin).eq(user1);
expect(response.result?.users[0].nickname).eq(user1.nickname);
expect(response.result?.users[0].admin).eq(true);
expect(response.result?.users[0].ready).eq(false);
expect(response.result?.users[1].nickname).eq(user2.nickname);
expect(response.result?.users[1].admin).eq(false);
expect(response.result?.users[1].ready).eq(false);
});
it("방장이 나가면 방장이 인계됩니다", () => {
const {
sockets: [socket1, socket2],
users: [user1, user2],
room,
} = prepareJoinedRoom(2);
expect(room.admin).eq(user1);
socket1.testOk("leaveRoom", {});
expect(room.admin).eq(user2);
});
it("방장을 인계하면 정보가 업데이트됩니다", () => {
const {
sockets: [socket1, socket2],
users: [user1, user2],
room,
} = prepareJoinedRoom(2);
expect(room.admin).eq(user1);
room.setAdmin(user2);
expect(room.admin).eq(user2);
// guest1은 guest2가 접속했다는 메세지를 먼저 받은 상태
let message = socket1.socket.received("updateRoomUser");
expect(message.state).eq("added");
expect(message.user.username).eq(user2.username);
// 먼저 이전 방장의 업데이트를 받음
message = socket1.socket.received("updateRoomUser");
expect(message.state).eq("updated");
expect(message.user.username).eq(user1.username);
expect(message.user.admin).eq(false);
message = socket2.socket.received("updateRoomUser");
expect(message.state).eq("updated");
expect(message.user.username).eq(user1.username);
expect(message.user.admin).eq(false);
// 현재 방장의 업데이트를 받음
message = socket1.socket.received("updateRoomUser");
expect(message.state).eq("updated");
expect(message.user.username).eq(user2.username);
expect(message.user.admin).eq(true);
message = socket2.socket.received("updateRoomUser");
expect(message.state).eq("updated");
expect(message.user.username).eq(user2.username);
expect(message.user.admin).eq(true);
});
});