Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
강동현
2021-06-04 12:35:52 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
46a55b4fc31a51fe0d1d0ae37526aada4c0528f9
46a55b4f
1 parent
d3c66a45
테스트 코드 리팩토링
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
309 additions
and
252 deletions
server/test/admin.test.ts
server/test/chat.test.ts
server/test/joinRoom.test.ts
server/test/leaveRoom.test.ts
server/test/login.test.ts
server/test/ready.test.ts
server/test/roomList.test.ts
server/test/util/SocketTester.ts
server/test/util/prepare.ts
server/test/admin.test.ts
View file @
46a55b4
import
{
expect
}
from
"chai"
;
import
{
RoomManager
}
from
"../room/RoomManager"
;
import
{
prepareJoinedRoom
,
prepareUsers
}
from
"./util/prepare"
;
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"방장"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"방을 만든 유저가 방장이 됩니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
users
:
[
user
],
room
,
}
=
prepareJoinedRoom
(
1
);
expect
(
socket
.
connection
.
user
!==
undefined
).
eq
(
true
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
socket
.
connection
.
user
);
expect
(
room
.
admin
).
eq
(
socket
.
connection
.
user
);
expect
(
room
.
admin
).
eq
(
user
);
});
it
(
"나중에 들어온 유저는 방장 정보를 받지만 방장이 되지 않습니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
const
{
sockets
:
[
socket1
,
socket2
],
users
:
[
user1
,
user2
],
roomManager
,
}
=
prepareUsers
(
2
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
user1
);
expect
(
socket1
.
connection
.
user
!==
undefined
).
eq
(
true
);
expect
(
socket2
.
connection
.
user
!==
undefined
).
eq
(
true
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
socket1
.
connection
.
user
);
const
response
=
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
});
expect
(
response
.
ok
).
eq
(
true
);
expect
(
room
.
admin
).
eq
(
socket1
.
connection
.
user
);
expect
(
room
.
admin
).
not
.
eq
(
socket2
.
connection
.
user
);
expect
(
response
.
result
?.
users
[
0
]?.
username
).
eq
(
"guest1"
);
expect
(
response
.
result
?.
users
[
0
]?.
admin
).
eq
(
true
);
expect
(
room
.
admin
).
eq
(
user1
);
expect
(
response
.
result
?.
users
[
0
]).
eq
({
username
:
user1
.
username
,
admin
:
true
,
ready
:
false
,
});
expect
(
response
.
result
?.
users
[
1
]).
eq
({
username
:
user2
.
username
,
admin
:
false
,
ready
:
false
,
});
});
it
(
"방장이 나가면 방장이 인계됩니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
expect
(
socket1
.
connection
.
user
!==
undefined
).
eq
(
true
);
expect
(
socket2
.
connection
.
user
!==
undefined
).
eq
(
true
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
socket1
.
connection
.
user
);
expect
(
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
const
{
sockets
:
[
socket1
,
socket2
],
users
:
[
user1
,
user2
],
room
,
}
=
prepareJoinedRoom
(
2
);
expect
(
room
.
admin
).
eq
(
socket1
.
connection
.
user
);
expect
(
room
.
admin
).
not
.
eq
(
socket2
.
connection
.
user
);
expect
(
socket1
.
test
(
"leaveRoom"
,
{}).
ok
).
eq
(
true
);
expect
(
room
.
admin
).
eq
(
socket2
.
connection
.
user
);
expect
(
room
.
admin
).
eq
(
user1
);
socket1
.
testOk
(
"leaveRoom"
,
{});
expect
(
room
.
admin
).
eq
(
user2
);
});
it
(
"방장을 인계하면 정보가 업데이트됩니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
expect
(
socket1
.
connection
.
user
!==
undefined
).
eq
(
true
);
expect
(
socket2
.
connection
.
user
!==
undefined
).
eq
(
true
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
socket1
.
connection
.
user
);
expect
(
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
const
{
sockets
:
[
socket1
,
socket2
],
users
:
[
user1
,
user2
],
room
,
}
=
prepareJoinedRoom
(
2
);
expect
(
room
.
admin
).
eq
(
socket1
.
connection
.
user
);
expect
(
room
.
admin
).
not
.
eq
(
socket2
.
connection
.
user
);
expect
(
room
.
admin
).
eq
(
user1
);
if
(
socket2
.
connection
.
user
)
{
room
.
setAdmin
(
socket2
.
connection
.
user
);
expect
(
room
.
admin
).
eq
(
socket2
.
connection
.
user
);
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
(
"guest2"
);
// 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
(
"guest1"
);
expect
(
message
.
user
.
admin
).
eq
(
false
);
// 먼저 이전 방장의 업데이트를 받음
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
(
"guest1"
);
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
(
"guest2"
);
expect
(
message
.
user
.
admin
).
eq
(
true
);
// 현재 방장의 업데이트를 받음
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
(
"guest2"
);
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
);
});
});
...
...
server/test/chat.test.ts
View file @
46a55b4
import
{
expect
}
from
"chai"
;
import
{
RoomManager
}
from
"../room/RoomManager"
;
import
{
DummySocket
}
from
"./util/DummySocket"
;
import
{
prepareJoinedRoom
,
prepareUsers
,
prepareUsersEmptyRooms
,
}
from
"./util/prepare"
;
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"채팅"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"로그인하지 않은 유저는 요청할 수 없습니다"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
const
socket
=
new
SocketTester
(
roomManager
);
const
response
=
socket
.
test
(
"chat"
,
{
message
:
"Hello World"
});
expect
(
response
.
ok
).
eq
(
false
);
socket
.
testNotOk
(
"chat"
,
{
message
:
"Hello World"
});
});
it
(
"방에 접속하지 않은 유저는 요청할 수 없습니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
sockets
:
[
socket
],
}
=
prepareUsers
(
1
);
const
response
=
socket
.
test
(
"chat"
,
{
message
:
"Hello World"
});
expect
(
response
.
ok
).
eq
(
false
);
socket
.
testNotOk
(
"chat"
,
{
message
:
"Hello World"
});
});
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
);
const
{
sockets
:
[
socket
],
}
=
prepareJoinedRoom
(
1
);
socket
.
testOk
(
"chat"
,
{
message
:
"Hello World"
});
});
it
(
"자신을 제외한 다른 사람들이 채팅을 받습니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket2
.
login
(
"guest2"
);
const
{
sockets
:
[
socket1
,
socket2
],
users
:
[
user1
,
user2
],
}
=
prepareJoinedRoom
(
2
);
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
);
socket1
.
testOk
(
"chat"
,
{
message
:
"Hello World"
});
expect
(
socket2
.
socket
.
received
(
"chat"
).
message
).
eq
(
"Hello World"
);
expect
(
socket2
.
socket
.
received
(
"chat"
).
sender
).
eq
(
"guest1"
);
expect
(
socket2
.
socket
.
received
(
"chat"
).
sender
).
eq
(
user1
.
username
);
socket1
.
socket
.
notReceived
(
"chat"
);
});
it
(
"빈 채팅은 보낼 수 없습니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
sockets
:
[
socket
],
}
=
prepareJoinedRoom
(
1
);
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
{
sockets
:
[
socket
],
}
=
prepareJoinedRoom
(
1
);
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
);
...
...
server/test/joinRoom.test.ts
View file @
46a55b4
import
{
expect
}
from
"chai"
;
import
{
RoomManager
}
from
"../room/RoomManager"
;
import
{
DummySocket
}
from
"./util/DummySocket"
;
import
{
prepareJoinedRoom
,
prepareUsersEmptyRooms
}
from
"./util/prepare"
;
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"방 입장"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"로그인하지 않은 유저는 요청할 수 없습니다"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
const
socket
=
new
SocketTester
(
roomManager
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
);
const
response
=
socket
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
});
expect
(
response
.
ok
).
eq
(
false
);
socket
.
testNotOk
(
"joinRoom"
,
{
uuid
:
room
.
uuid
});
});
it
(
"방에 입장합니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest1"
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
);
const
{
sockets
:
[
socket
],
rooms
:
[
room
],
}
=
prepareUsersEmptyRooms
(
1
,
1
);
const
response
=
socket
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
});
expect
(
response
.
ok
).
eq
(
true
);
...
...
@@ -27,40 +26,30 @@ describe("방 입장", () => {
expect
(
response
.
result
?.
users
[
0
]?.
username
).
eq
(
"guest1"
);
});
it
(
"방에 입장하면 유저 목록이 업데이트 됩니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
const
{
sockets
:
[
socket1
,
socket2
],
}
=
prepareJoinedRoom
(
2
);
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
);
const
updated
=
socket1
.
socket
.
received
(
"updateRoomUser"
);
expect
(
updated
.
state
).
eq
(
"added"
);
expect
(
updated
.
user
.
username
).
eq
(
"guest2"
);
});
it
(
"방에 이미 입장한 상태에서 다른 방에 입장할 수 없습니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
sockets
:
[
socket
],
rooms
:
[
room1
,
room2
],
}
=
prepareUsersEmptyRooms
(
1
,
2
);
const
room1
=
roomManager
.
create
(
"테스트1"
,
2
);
const
room2
=
roomManager
.
create
(
"테스트2"
,
2
);
expect
(
socket
.
test
(
"joinRoom"
,
{
uuid
:
room1
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket
.
test
(
"joinRoom"
,
{
uuid
:
room2
.
uuid
}).
ok
).
eq
(
false
);
socket
.
testOk
(
"joinRoom"
,
{
uuid
:
room1
.
uuid
});
socket
.
testNotOk
(
"joinRoom"
,
{
uuid
:
room2
.
uuid
});
});
it
(
"가득 찬 방에는 입장할 수 없습니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
const
room
=
roomManager
.
create
(
"테스트"
,
1
);
const
{
sockets
:
[
socket1
,
socket2
],
rooms
:
[
room
],
}
=
prepareUsersEmptyRooms
(
2
,
1
,
1
);
expect
(
socket1
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
false
);
socket1
.
testOk
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}
);
socket2
.
testNotOk
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}
);
});
});
...
...
server/test/leaveRoom.test.ts
View file @
46a55b4
...
...
@@ -2,63 +2,57 @@ import { expect } from "chai";
import
{
RoomManager
}
from
"../room/RoomManager"
;
import
{
User
}
from
"../user/User"
;
import
{
DummySocket
}
from
"./util/DummySocket"
;
import
{
prepareJoinedRoom
,
prepareUsers
}
from
"./util/prepare"
;
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"방 퇴장"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"로그인하지 않은 유저는 요청할 수 없습니다"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
const
socket
=
new
SocketTester
(
roomManager
);
const
response
=
socket
.
test
(
"leaveRoom"
,
{});
expect
(
response
.
ok
).
eq
(
false
);
socket
.
testNotOk
(
"leaveRoom"
,
{});
});
it
(
"방에 입장하지 않은 유저는 요청할 수 없습니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
sockets
:
[
socket
],
}
=
prepareUsers
(
1
);
const
response
=
socket
.
test
(
"leaveRoom"
,
{});
expect
(
response
.
ok
).
eq
(
false
);
socket
.
testNotOk
(
"leaveRoom"
,
{});
});
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
(
"leaveRoom"
,
{}).
ok
).
eq
(
true
);
const
{
sockets
:
[
socket
],
room
,
}
=
prepareJoinedRoom
(
1
);
expect
(
room
.
users
.
length
).
eq
(
1
);
socket
.
testOk
(
"leaveRoom"
,
{});
expect
(
room
.
users
.
length
).
eq
(
0
);
});
it
(
"방에서 퇴장한 뒤 다시 요청을 보낼 수 없습니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
sockets
:
[
socket
],
}
=
prepareJoinedRoom
(
1
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
);
expect
(
socket
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket
.
test
(
"leaveRoom"
,
{}).
ok
).
eq
(
true
);
expect
(
socket
.
test
(
"leaveRoom"
,
{}).
ok
).
eq
(
false
);
socket
.
testOk
(
"leaveRoom"
,
{});
socket
.
testNotOk
(
"leaveRoom"
,
{});
});
it
(
"방에서 퇴장하면 유저 목록이 업데이트 됩니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
);
const
{
sockets
:
[
socket1
,
socket2
],
users
:
[
user1
,
user2
],
}
=
prepareJoinedRoom
(
2
);
expect
(
socket1
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket2
.
test
(
"leaveRoom"
,
{}).
ok
).
eq
(
true
);
const
updated
=
socket1
.
socket
.
received
(
"updateRoomUser"
);
socket1
.
testOk
(
"leaveRoom"
,
{});
const
updated
=
socket2
.
socket
.
received
(
"updateRoomUser"
);
expect
(
updated
.
state
).
eq
(
"removed"
);
expect
(
updated
.
user
.
username
).
eq
(
"guest2"
);
expect
(
updated
.
user
.
username
).
eq
(
user1
.
username
);
});
it
(
"방에서 퇴장한 뒤 아무도 없으면 방이 닫힙니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest1"
);
const
room
=
roomManager
.
create
(
"테스트"
,
1
);
const
{
sockets
:
[
socket
],
room
,
}
=
prepareJoinedRoom
(
1
);
expect
(
socket
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
room
.
closed
).
eq
(
false
);
expect
(
socket
.
test
(
"leaveRoom"
,
{}).
ok
).
eq
(
true
);
expect
(
room
.
closed
).
eq
(
true
);
...
...
server/test/login.test.ts
View file @
46a55b4
...
...
@@ -3,11 +3,10 @@ import { RoomManager } from "../room/RoomManager";
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"로그인"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"로그인합니다"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
const
socket
=
new
SocketTester
(
roomManager
);
const
response
=
socket
.
test
(
"login"
,
{
username
:
"guest"
});
expect
(
response
.
ok
).
eq
(
true
);
socket
.
testOk
(
"login"
,
{
username
:
"guest"
});
expect
(
socket
.
connection
.
user
?.
username
).
eq
(
"guest"
);
});
});
...
...
server/test/ready.test.ts
View file @
46a55b4
import
{
expect
}
from
"chai"
;
import
{
RoomManager
}
from
"../room/RoomManager"
;
import
{
prepareJoinedRoom
,
prepareUsersEmptyRooms
}
from
"./util/prepare"
;
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"준비"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"방에 입장하면 준비되지 않은 상태입니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest1"
);
const
{
sockets
:
[
socket
],
rooms
:
[
room
],
}
=
prepareUsersEmptyRooms
(
1
,
1
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
);
const
response
=
socket
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
});
expect
(
response
.
ok
).
eq
(
true
);
expect
(
response
.
result
?.
users
[
0
]?.
username
).
eq
(
"guest1"
);
expect
(
response
.
result
?.
users
[
0
]?.
ready
).
eq
(
false
);
});
it
(
"준비 상태를 설정합니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest1"
);
const
{
sockets
:
[
socket
],
users
:
[
user
],
room
,
}
=
prepareJoinedRoom
(
1
,
2
,
true
);
const
room
=
roomManager
.
create
(
"테스트"
,
2
);
expect
(
socket
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket
.
connection
.
user
!==
undefined
).
eq
(
true
);
if
(
socket
.
connection
.
user
)
{
expect
(
room
.
isReady
(
socket
.
connection
.
user
)).
eq
(
false
);
expect
(
socket
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
expect
(
room
.
isReady
(
socket
.
connection
.
user
)).
eq
(
true
);
expect
(
socket
.
test
(
"ready"
,
{
ready
:
false
}).
ok
).
eq
(
true
);
expect
(
room
.
isReady
(
socket
.
connection
.
user
)).
eq
(
false
);
}
expect
(
room
.
isReady
(
user
)).
eq
(
false
);
socket
.
testOk
(
"ready"
,
{
ready
:
true
});
expect
(
room
.
isReady
(
user
)).
eq
(
true
);
socket
.
testOk
(
"ready"
,
{
ready
:
false
});
expect
(
room
.
isReady
(
user
)).
eq
(
false
);
});
it
(
"방장은 준비할 수 없습니다"
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest1"
);
const
{
sockets
:
[
socket
],
users
:
[
user
],
room
,
}
=
prepareJoinedRoom
(
1
,
2
);
expect
(
socket
.
connection
.
user
!==
undefined
).
eq
(
true
);
if
(
socket
.
connection
.
user
)
{
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
socket
.
connection
.
user
);
expect
(
socket
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
false
);
expect
(
room
.
isReady
(
socket
.
connection
.
user
)).
eq
(
false
);
}
socket
.
testNotOk
(
"ready"
,
{
ready
:
true
});
expect
(
room
.
isReady
(
user
)).
eq
(
false
);
});
it
(
"방장이 되면 준비 상태가 해제됩니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
const
{
sockets
:
[
socket1
,
socket2
],
users
:
[
user1
,
user2
],
room
,
}
=
prepareJoinedRoom
(
2
);
expect
(
socket1
.
connection
.
user
!==
undefined
).
eq
(
true
);
expect
(
socket2
.
connection
.
user
!==
undefined
).
eq
(
true
);
if
(
socket1
.
connection
.
user
&&
socket2
.
connection
.
user
)
{
const
room
=
roomManager
.
create
(
"테스트"
,
2
,
socket1
.
connection
.
user
);
expect
(
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
expect
(
room
.
isReady
(
socket2
.
connection
.
user
));
room
.
setAdmin
(
socket2
.
connection
.
user
);
expect
(
room
.
isReady
(
socket2
.
connection
.
user
)).
eq
(
false
);
}
socket2
.
testOk
(
"ready"
,
{
ready
:
true
});
expect
(
room
.
isReady
(
user2
)).
eq
(
true
);
room
.
setAdmin
(
user2
);
expect
(
room
.
isReady
(
user2
)).
eq
(
false
);
});
it
(
"모두가 준비해야 게임을 시작할 수 있습니다"
,
()
=>
{
const
socket1
=
new
SocketTester
(
roomManager
);
const
socket2
=
new
SocketTester
(
roomManager
);
const
socket3
=
new
SocketTester
(
roomManager
);
socket1
.
login
(
"guest1"
);
socket2
.
login
(
"guest2"
);
socket3
.
login
(
"guest3"
);
expect
(
socket1
.
connection
.
user
!==
undefined
).
eq
(
true
);
expect
(
socket2
.
connection
.
user
!==
undefined
).
eq
(
true
);
expect
(
socket3
.
connection
.
user
!==
undefined
).
eq
(
true
);
if
(
socket1
.
connection
.
user
&&
socket2
.
connection
.
user
&&
socket3
.
connection
.
user
)
{
const
room
=
roomManager
.
create
(
"테스트"
,
3
,
socket1
.
connection
.
user
);
expect
(
socket2
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
expect
(
socket3
.
test
(
"joinRoom"
,
{
uuid
:
room
.
uuid
}).
ok
).
eq
(
true
);
const
{
sockets
:
[
socket1
,
socket2
,
socket3
],
room
,
}
=
prepareJoinedRoom
(
3
);
// 2, 3 모두 준비 안함
expect
(
room
.
canStart
()).
eq
(
false
);
// 2, 3 모두 준비 안함
expect
(
room
.
canStart
()).
eq
(
false
);
// 2만 준비
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
// 2만 준비
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
// 3만 준비
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
false
}).
ok
).
eq
(
true
);
expect
(
socket3
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
expect
(
room
.
canStart
()).
eq
(
false
);
// 3만 준비
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
false
}).
ok
).
eq
(
true
);
expect
(
socket3
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
expect
(
room
.
canStart
()).
eq
(
false
);
// 2, 3 모두 준비
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
expect
(
room
.
canStart
()).
eq
(
true
);
}
// 2, 3 모두 준비
expect
(
socket2
.
test
(
"ready"
,
{
ready
:
true
}).
ok
).
eq
(
true
);
expect
(
room
.
canStart
()).
eq
(
true
);
});
});
...
...
server/test/roomList.test.ts
View file @
46a55b4
import
{
expect
}
from
"chai"
;
import
{
RoomManager
}
from
"../room/RoomManager"
;
import
{
DummySocket
}
from
"./util/DummySocket"
;
import
{
prepareUsersEmptyRooms
}
from
"./util/prepare"
;
import
{
SocketTester
}
from
"./util/SocketTester"
;
describe
(
"방 목록 요청"
,
()
=>
{
const
roomManager
=
new
RoomManager
();
it
(
"로그인하지 않은 유저는 요청할 수 없습니다."
,
()
=>
{
const
socket
=
new
SocketTester
(
roomManager
);
const
response
=
socket
.
test
(
"roomList"
,
{});
expect
(
response
.
ok
).
eq
(
false
);
socket
.
testNotOk
(
"roomList"
,
{});
});
it
(
"방 목록을 가져옵니다."
,
()
=>
{
const
roomManager
=
new
RoomManager
();
roomManager
.
create
(
"테스트1"
,
4
);
roomManager
.
create
(
"테스트2"
,
2
);
const
socket
=
new
SocketTester
(
roomManager
);
socket
.
login
(
"guest"
);
const
{
sockets
:
[
socket
],
rooms
:
[
room1
,
room2
],
}
=
prepareUsersEmptyRooms
(
1
,
2
);
const
response
=
socket
.
test
(
"roomList"
,
{});
expect
(
response
.
ok
).
eq
(
true
);
expect
(
response
.
result
?.
length
).
eq
(
2
);
expect
(
response
.
result
[
0
].
name
).
eq
(
"테스트1"
);
expect
(
response
.
result
[
1
].
name
).
eq
(
"테스트2"
);
expect
(
response
.
result
[
0
].
name
).
eq
(
room1
.
name
);
expect
(
response
.
result
[
1
].
name
).
eq
(
room2
.
name
);
});
});
...
...
server/test/util/SocketTester.ts
View file @
46a55b4
import
{
expect
}
from
"chai"
;
import
{
RawMessage
,
ServerInboundMessage
,
...
...
@@ -26,6 +27,20 @@ export class SocketTester {
});
}
public
testOk
<
T
extends
ServerInboundMessageKey
>
(
type
:
T
,
message
:
ServerInboundMessage
<
T
>
):
void
{
expect
(
this
.
test
(
type
,
message
).
ok
).
eq
(
true
);
}
public
testNotOk
<
T
extends
ServerInboundMessageKey
>
(
type
:
T
,
message
:
ServerInboundMessage
<
T
>
):
void
{
expect
(
this
.
test
(
type
,
message
).
ok
).
eq
(
false
);
}
public
testRaw
(
raw
:
RawMessage
):
ServerResponse
<
any
>
{
return
this
.
connection
.
handleRaw
(
raw
);
}
...
...
@@ -35,6 +50,7 @@ export class SocketTester {
}
public
login
(
username
:
string
):
void
{
this
.
test
(
"login"
,
{
username
});
this
.
testOk
(
"login"
,
{
username
});
expect
(
this
.
connection
.
user
!==
undefined
).
eq
(
true
);
}
}
...
...
server/test/util/prepare.ts
0 → 100644
View file @
46a55b4
import
{
Room
}
from
"../../room/Room"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
SocketTester
}
from
"./SocketTester"
;
/**
* 유저들을 만듭니다.
* @param userCount 만들어지는 유저 수입니다.
* @returns 만들어진 유저들을 반환합니다.
*/
export
function
prepareUsers
(
userCount
:
number
):
{
sockets
:
SocketTester
[];
users
:
User
[];
roomManager
:
RoomManager
;
}
{
const
roomManager
=
new
RoomManager
();
let
sockets
=
[];
let
users
=
[];
for
(
let
i
=
0
;
i
<
userCount
;
i
++
)
{
const
socket
=
new
SocketTester
(
roomManager
);
sockets
.
push
(
socket
);
socket
.
login
(
`guest
${
i
+
1
}
`
);
if
(
!
socket
.
connection
.
user
)
{
throw
new
Error
(
"User is not initialized."
);
}
users
.
push
(
socket
.
connection
.
user
);
}
return
{
sockets
,
users
,
roomManager
};
}
/**
* 유저들과 빈 방들을 만듭니다.
* @param userCount 만들어지는 유저 수입니다.
* @param roomCount 만들어지는 방 수입니다.
* @param roomMaxConnections 각 방의 최대 접속 인원 수 입니다.
* @returns 생성된 소켓, 유저, 방을 반환합니다.
*/
export
function
prepareUsersEmptyRooms
(
userCount
:
number
,
roomCount
:
number
,
roomMaxConnections
:
number
=
2
):
{
sockets
:
SocketTester
[];
users
:
User
[];
rooms
:
Room
[];
roomManager
:
RoomManager
;
}
{
const
{
sockets
,
users
,
roomManager
}
=
prepareUsers
(
userCount
);
let
rooms
=
[];
for
(
let
i
=
0
;
i
<
roomCount
;
i
++
)
{
rooms
.
push
(
roomManager
.
create
(
`room
${
i
+
1
}
`
,
roomMaxConnections
));
}
return
{
sockets
,
users
,
rooms
,
roomManager
};
}
/**
* 방 하나를 만들고 유저들을 접속시킵니다. 이때 첫번째 유저가 방장이 됩니다.
* @param userCount 방에 접속하는 유저 수 입니다.
* @param roomMaxConnections 방에 최대 접속할 수 있는 인원 수 입니다.만약 userCount보다 작다면 userCount값으로 변경됩니다.
* @param emptyAdmin 참이면 방장이 없는 상태로 방이 생성됩니다.
* @returns 생성된 소켓, 유저, 방을 반환합니다.
*/
export
function
prepareJoinedRoom
(
userCount
:
number
,
roomMaxConnections
:
number
=
2
,
emptyAdmin
:
boolean
=
false
):
{
sockets
:
SocketTester
[];
users
:
User
[];
room
:
Room
}
{
roomMaxConnections
=
Math
.
max
(
roomMaxConnections
,
userCount
);
const
roomManager
=
new
RoomManager
();
let
sockets
=
[];
let
users
=
[];
for
(
let
i
=
0
;
i
<
userCount
;
i
++
)
{
const
socket
=
new
SocketTester
(
roomManager
);
sockets
.
push
(
socket
);
socket
.
login
(
`guest
${
i
+
1
}
`
);
if
(
!
socket
.
connection
.
user
)
{
throw
new
Error
(
"User is not initialized."
);
}
users
.
push
(
socket
.
connection
.
user
);
}
const
room
=
roomManager
.
create
(
"room1"
,
roomMaxConnections
,
emptyAdmin
?
undefined
:
users
[
0
]
);
for
(
let
i
=
emptyAdmin
?
0
:
1
;
i
<
userCount
;
i
++
)
{
sockets
[
i
].
testOk
(
"joinRoom"
,
{
uuid
:
room
.
uuid
});
}
return
{
sockets
,
users
,
room
};
}
Please
register
or
login
to post a comment