avallef commited on
Commit
d32b1fb
·
1 Parent(s): 2ca92eb

Formatted with prettier

Browse files
server/.prettierrc CHANGED
@@ -2,5 +2,6 @@
2
  "trailingComma": "es5",
3
  "tabWidth": 4,
4
  "singleQuote": false,
5
- "semi": true
 
6
  }
 
2
  "trailingComma": "es5",
3
  "tabWidth": 4,
4
  "singleQuote": false,
5
+ "semi": true,
6
+ "printWidth": 100
7
  }
server/src/index.ts CHANGED
@@ -7,66 +7,66 @@ import WebSocketWrapper from "./socket/WebSocketAdapter";
7
  import pino from "pino";
8
 
9
  (() => {
10
- const args = process.argv;
11
- if (args.includes("--silent")) {
12
- return main(false);
13
- }
14
 
15
- return main(true);
16
  })();
17
 
18
  function main(logging: boolean = true) {
19
- const port: number = parseInt(process.env.PORT as string, 10) || 3000;
20
- const logger = pino({
21
- level: logging ? "info" : "silent",
22
- formatters: {
23
- bindings(bindings) {
24
- return {
25
- level: bindings.level,
26
- time: bindings.time,
27
- msg: bindings.msg,
28
- };
29
- },
30
- },
31
- });
32
 
33
- const app: Application = express();
34
- const server = http.createServer(app);
35
- const wss = new WebSocket.Server({ server });
36
 
37
- app.get("/", (_req: Request, res: Response) => {
38
- res.send("Bonjour");
39
- });
40
 
41
- // Routes are in socket/routes/*.ts
42
- // registerRoutes imports and adds them all to router
43
- const hapticLink = new HapticLinkServer();
44
- registerRoutes(hapticLink);
45
 
46
- // When a user sends a message, the router checks if that route is available
47
- // and then calls the handler. It also generates a User object for them to store
48
- // data for later requests such as an ID
49
- wss.on("connection", (ws: WebSocket) => {
50
- logger.info("Client Connected");
51
- const wsw = new WebSocketWrapper(ws);
52
 
53
- ws.on("message", (message: string) => {
54
- logger.info(`Received Message: ${message}`);
55
- hapticLink.handleRoute(wsw, message);
56
- });
57
 
58
- // When a user disconnects, their account is removed, and they are removed from all groups
59
- ws.on("close", () => {
60
- hapticLink.removeUser(wsw);
61
- });
62
- ws.on("error", () => {
63
- hapticLink.removeUser(wsw);
64
- });
65
 
66
- ws.send("Welcome");
67
- });
68
 
69
- server.listen(port, () => {
70
- logger.info(`Server is running on port ${port}`);
71
- });
72
  }
 
7
  import pino from "pino";
8
 
9
  (() => {
10
+ const args = process.argv;
11
+ if (args.includes("--silent")) {
12
+ return main(false);
13
+ }
14
 
15
+ return main(true);
16
  })();
17
 
18
  function main(logging: boolean = true) {
19
+ const port: number = parseInt(process.env.PORT as string, 10) || 3000;
20
+ const logger = pino({
21
+ level: logging ? "info" : "silent",
22
+ formatters: {
23
+ bindings(bindings) {
24
+ return {
25
+ level: bindings.level,
26
+ time: bindings.time,
27
+ msg: bindings.msg,
28
+ };
29
+ },
30
+ },
31
+ });
32
 
33
+ const app: Application = express();
34
+ const server = http.createServer(app);
35
+ const wss = new WebSocket.Server({ server });
36
 
37
+ app.get("/", (_req: Request, res: Response) => {
38
+ res.send("Bonjour");
39
+ });
40
 
41
+ // Routes are in socket/routes/*.ts
42
+ // registerRoutes imports and adds them all to router
43
+ const hapticLink = new HapticLinkServer();
44
+ registerRoutes(hapticLink);
45
 
46
+ // When a user sends a message, the router checks if that route is available
47
+ // and then calls the handler. It also generates a User object for them to store
48
+ // data for later requests such as an ID
49
+ wss.on("connection", (ws: WebSocket) => {
50
+ logger.info("Client Connected");
51
+ const wsw = new WebSocketWrapper(ws);
52
 
53
+ ws.on("message", (message: string) => {
54
+ logger.info(`Received Message: ${message}`);
55
+ hapticLink.handleRoute(wsw, message);
56
+ });
57
 
58
+ // When a user disconnects, their account is removed, and they are removed from all groups
59
+ ws.on("close", () => {
60
+ hapticLink.removeUser(wsw);
61
+ });
62
+ ws.on("error", () => {
63
+ hapticLink.removeUser(wsw);
64
+ });
65
 
66
+ ws.send("Welcome");
67
+ });
68
 
69
+ server.listen(port, () => {
70
+ logger.info(`Server is running on port ${port}`);
71
+ });
72
  }
server/src/socket/WebSocketAdapter.ts CHANGED
@@ -6,7 +6,7 @@ export interface WebSocketInterface {
6
  }
7
 
8
  export default class WebSocketWrapper implements WebSocketInterface {
9
- ws: WebSocket
10
 
11
  constructor(ws: WebSocket) {
12
  this.ws = ws;
@@ -17,6 +17,6 @@ export default class WebSocketWrapper implements WebSocketInterface {
17
  }
18
 
19
  readyState() {
20
- return this.ws.readyState
21
  }
22
  }
 
6
  }
7
 
8
  export default class WebSocketWrapper implements WebSocketInterface {
9
+ ws: WebSocket;
10
 
11
  constructor(ws: WebSocket) {
12
  this.ws = ws;
 
17
  }
18
 
19
  readyState() {
20
+ return this.ws.readyState;
21
  }
22
  }
server/src/socket/hapticLinkServer.ts CHANGED
@@ -12,7 +12,7 @@ export interface Route<T> {
12
  }
13
 
14
  export class User {
15
- static userIdLength: number = 32
16
  id: string;
17
  username?: string;
18
  socket: WebSocketInterface;
@@ -51,11 +51,7 @@ export class HapticLinkServer {
51
  return true;
52
  }
53
 
54
- addRoute<T>(
55
- name: string,
56
- schema: ZodSchema<T>,
57
- handler: RouteHandler<T>
58
- ): boolean {
59
  if (name in this.routes) {
60
  return false;
61
  }
@@ -75,15 +71,10 @@ export class HapticLinkServer {
75
  try {
76
  payload = JSON.parse(message);
77
  } catch (e) {
78
- return ws.send(
79
- JSON.stringify({ error: "message not in JSON format" })
80
- );
81
  }
82
 
83
- if (
84
- typeof payload != "object" ||
85
- !Object.keys(payload).includes("route")
86
- ) {
87
  return ws.send(JSON.stringify({ error: "missing route" }));
88
  }
89
 
 
12
  }
13
 
14
  export class User {
15
+ static userIdLength: number = 32;
16
  id: string;
17
  username?: string;
18
  socket: WebSocketInterface;
 
51
  return true;
52
  }
53
 
54
+ addRoute<T>(name: string, schema: ZodSchema<T>, handler: RouteHandler<T>): boolean {
 
 
 
 
55
  if (name in this.routes) {
56
  return false;
57
  }
 
71
  try {
72
  payload = JSON.parse(message);
73
  } catch (e) {
74
+ return ws.send(JSON.stringify({ error: "message not in JSON format" }));
 
 
75
  }
76
 
77
+ if (typeof payload != "object" || !Object.keys(payload).includes("route")) {
 
 
 
78
  return ws.send(JSON.stringify({ error: "missing route" }));
79
  }
80
 
server/src/socket/room.ts CHANGED
@@ -26,24 +26,24 @@ export class Room {
26
 
27
  removeUserById(id: string): boolean {
28
  const oldSize = this.users.length;
29
- this.users = this.users.filter(user => user.id != id);
30
  this.updateRoom();
31
  return oldSize > this.users.length;
32
  }
33
 
34
  hasUser(id: string): boolean {
35
- return this.users.some(user => user.id == id);
36
  }
37
 
38
  /**
39
- * @param message Message to send to users
40
- * @param filter Array of user ID to ignore when sending
41
- **/
42
  broadcast(message: string, filter: string[] = []) {
43
- this.users.forEach(user => {
44
  if (filter.includes(user.id)) return;
45
  user.socket.send(message);
46
- })
47
  }
48
 
49
  updateRoom() {
@@ -54,13 +54,15 @@ export class Room {
54
  id: user.id,
55
  online: true,
56
  lastOnline: 0,
57
- })
58
- })
59
 
60
- this.broadcast(JSON.stringify({
61
- message: "room_update",
62
- roomId: this.id,
63
- users: usersData,
64
- }))
 
 
65
  }
66
  }
 
26
 
27
  removeUserById(id: string): boolean {
28
  const oldSize = this.users.length;
29
+ this.users = this.users.filter((user) => user.id != id);
30
  this.updateRoom();
31
  return oldSize > this.users.length;
32
  }
33
 
34
  hasUser(id: string): boolean {
35
+ return this.users.some((user) => user.id == id);
36
  }
37
 
38
  /**
39
+ * @param message Message to send to users
40
+ * @param filter Array of user ID to ignore when sending
41
+ **/
42
  broadcast(message: string, filter: string[] = []) {
43
+ this.users.forEach((user) => {
44
  if (filter.includes(user.id)) return;
45
  user.socket.send(message);
46
+ });
47
  }
48
 
49
  updateRoom() {
 
54
  id: user.id,
55
  online: true,
56
  lastOnline: 0,
57
+ });
58
+ });
59
 
60
+ this.broadcast(
61
+ JSON.stringify({
62
+ message: "room_update",
63
+ roomId: this.id,
64
+ users: usersData,
65
+ })
66
+ );
67
  }
68
  }
server/src/socket/routes/join_room.ts CHANGED
@@ -9,7 +9,7 @@ export interface JoinRoomPayload {
9
  export const JoinRoomSchema = z.object({
10
  roomId: z.string().optional(),
11
  username: z.string().optional(),
12
- })
13
 
14
  export function JoinRoomHandler(ctx: Context<JoinRoomPayload>) {
15
  // Set username if payload has it
@@ -23,30 +23,32 @@ export function JoinRoomHandler(ctx: Context<JoinRoomPayload>) {
23
 
24
  if (ctx.payload.roomId && !ctx.server.rooms[ctx.payload.roomId]) {
25
  // User sent roomId, but room doesn't exist
26
- return ctx.ws.send(JSON.stringify({
27
- "message": "join_room_response",
28
- "status": "room doesn't exist",
29
- }))
 
 
30
  // Custom ID Code:
31
  // room = new Room(ctx.payload.roomId);
32
  // room.id = room.id.toUpperCase();
33
  // ctx.server.rooms[room.id] = room;
34
-
35
  } else if (!ctx.payload.roomId) {
36
  // Didn't include RoomID, creating new one
37
  room = new Room(ctx.payload.roomId);
38
  room.id = room.id.toUpperCase();
39
  ctx.payload.roomId = room.id;
40
  ctx.server.rooms[room.id] = room;
41
-
42
  } else {
43
  // RoomId included and room exists, joining...
44
- room = ctx.server.rooms[ctx.payload.roomId]
45
  if (room.hasUser(ctx.user.id)) {
46
- return ctx.ws.send(JSON.stringify({
47
- "message": "join_room_response",
48
- "status": "failed, you are already part of this room",
49
- }))
 
 
50
  }
51
  }
52
 
 
9
  export const JoinRoomSchema = z.object({
10
  roomId: z.string().optional(),
11
  username: z.string().optional(),
12
+ });
13
 
14
  export function JoinRoomHandler(ctx: Context<JoinRoomPayload>) {
15
  // Set username if payload has it
 
23
 
24
  if (ctx.payload.roomId && !ctx.server.rooms[ctx.payload.roomId]) {
25
  // User sent roomId, but room doesn't exist
26
+ return ctx.ws.send(
27
+ JSON.stringify({
28
+ message: "join_room_response",
29
+ status: "room doesn't exist",
30
+ })
31
+ );
32
  // Custom ID Code:
33
  // room = new Room(ctx.payload.roomId);
34
  // room.id = room.id.toUpperCase();
35
  // ctx.server.rooms[room.id] = room;
 
36
  } else if (!ctx.payload.roomId) {
37
  // Didn't include RoomID, creating new one
38
  room = new Room(ctx.payload.roomId);
39
  room.id = room.id.toUpperCase();
40
  ctx.payload.roomId = room.id;
41
  ctx.server.rooms[room.id] = room;
 
42
  } else {
43
  // RoomId included and room exists, joining...
44
+ room = ctx.server.rooms[ctx.payload.roomId];
45
  if (room.hasUser(ctx.user.id)) {
46
+ return ctx.ws.send(
47
+ JSON.stringify({
48
+ message: "join_room_response",
49
+ status: "failed, you are already part of this room",
50
+ })
51
+ );
52
  }
53
  }
54
 
server/src/socket/routes/leave_room.ts CHANGED
@@ -7,21 +7,25 @@ export interface LeaveRoomPayload {
7
 
8
  export const LeaveRoomSchema = z.object({
9
  roomId: z.string(),
10
- })
11
 
12
  export function LeaveRoomHandler(ctx: Context<LeaveRoomPayload>) {
13
  const room = ctx.user.currentRoom;
14
 
15
  if (!room) {
16
- return ctx.ws.send(JSON.stringify({
17
- message: "leave_room_response",
18
- status: "you are not part of that room"
19
- }))
 
 
20
  } else {
21
  room.removeUserById(ctx.user.id);
22
- return ctx.ws.send(JSON.stringify({
23
- message: "leave_room_response",
24
- status: "room left"
25
- }))
 
 
26
  }
27
  }
 
7
 
8
  export const LeaveRoomSchema = z.object({
9
  roomId: z.string(),
10
+ });
11
 
12
  export function LeaveRoomHandler(ctx: Context<LeaveRoomPayload>) {
13
  const room = ctx.user.currentRoom;
14
 
15
  if (!room) {
16
+ return ctx.ws.send(
17
+ JSON.stringify({
18
+ message: "leave_room_response",
19
+ status: "you are not part of that room",
20
+ })
21
+ );
22
  } else {
23
  room.removeUserById(ctx.user.id);
24
+ return ctx.ws.send(
25
+ JSON.stringify({
26
+ message: "leave_room_response",
27
+ status: "room left",
28
+ })
29
+ );
30
  }
31
  }
server/src/socket/routes/send_touch.ts CHANGED
@@ -5,8 +5,8 @@ export interface SendVibrationPayload {
5
  id: number;
6
  type: "enabled" | "disabled";
7
  position: {
8
- x: number,
9
- y: number,
10
  };
11
  color?: string;
12
  intensity?: number;
@@ -25,27 +25,31 @@ export const SendVibrationSchema = z.object({
25
 
26
  export function SendVibrationHandler(ctx: Context<SendVibrationPayload>) {
27
  if (!ctx.user.currentRoom) {
28
- return ctx.ws.send(JSON.stringify({
29
- "message": "send_vibration_response",
30
- "status": "not part of a room",
31
- }))
 
 
32
  }
33
 
34
  const p = ctx.payload;
35
 
36
- ctx.user.currentRoom.broadcast(JSON.stringify({
37
- message: "receive_touch",
38
- id: ctx.user.id + "_" + p.id,
39
- type: p.type,
40
- user: {
41
- username: ctx.user.username,
42
- id: ctx.user.id,
43
- },
44
- position: {
45
- x: p.position.x,
46
- y: p.position.y
47
- },
48
- color: p.color || "#FF0000",
49
- intensity: p.intensity ?? 1,
50
- }))
 
 
51
  }
 
5
  id: number;
6
  type: "enabled" | "disabled";
7
  position: {
8
+ x: number;
9
+ y: number;
10
  };
11
  color?: string;
12
  intensity?: number;
 
25
 
26
  export function SendVibrationHandler(ctx: Context<SendVibrationPayload>) {
27
  if (!ctx.user.currentRoom) {
28
+ return ctx.ws.send(
29
+ JSON.stringify({
30
+ message: "send_vibration_response",
31
+ status: "not part of a room",
32
+ })
33
+ );
34
  }
35
 
36
  const p = ctx.payload;
37
 
38
+ ctx.user.currentRoom.broadcast(
39
+ JSON.stringify({
40
+ message: "receive_touch",
41
+ id: ctx.user.id + "_" + p.id,
42
+ type: p.type,
43
+ user: {
44
+ username: ctx.user.username,
45
+ id: ctx.user.id,
46
+ },
47
+ position: {
48
+ x: p.position.x,
49
+ y: p.position.y,
50
+ },
51
+ color: p.color || "#FF0000",
52
+ intensity: p.intensity ?? 1,
53
+ })
54
+ );
55
  }
server/src/socket/routes/set_username.ts CHANGED
@@ -6,19 +6,20 @@ export interface SetUsernamePayload {
6
  }
7
  export const SetUsernameSchema = z.object({
8
  username: z.string(),
9
- })
10
 
11
  export function SetUsernameHandler(ctx: Context<SetUsernamePayload>) {
12
  ctx.user.username = ctx.payload.username;
13
 
14
- return ctx.ws.send(JSON.stringify({
15
- "message": "set_username_response",
16
- "status": "success",
17
- "user": {
18
- id: ctx.user.id,
19
- username: ctx.user.username,
20
- currentRoom: ctx.user.currentRoom
21
- }
22
- }))
 
 
23
  }
24
-
 
6
  }
7
  export const SetUsernameSchema = z.object({
8
  username: z.string(),
9
+ });
10
 
11
  export function SetUsernameHandler(ctx: Context<SetUsernamePayload>) {
12
  ctx.user.username = ctx.payload.username;
13
 
14
+ return ctx.ws.send(
15
+ JSON.stringify({
16
+ message: "set_username_response",
17
+ status: "success",
18
+ user: {
19
+ id: ctx.user.id,
20
+ username: ctx.user.username,
21
+ currentRoom: ctx.user.currentRoom,
22
+ },
23
+ })
24
+ );
25
  }
 
server/test/socket/routes/send_touch.spec.ts CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import { expect } from "chai";
3
  import { Context, HapticLinkServer, User } from "../../../src/socket/hapticLinkServer";
4
  import { JoinRoomHandler, JoinRoomPayload } from "../../../src/socket/routes/join_room";
@@ -6,72 +5,71 @@ import { WebSocketWrapper } from "../hapticLinkServer.spec";
6
  import { SendVibrationHandler, SendVibrationPayload } from "../../../src/socket/routes/send_touch";
7
 
8
  describe("Send Vibration", () => {
9
- let server: HapticLinkServer;
10
- let ws: WebSocketWrapper;
11
- let user: User;
12
- let ws2: WebSocketWrapper;
13
- let user2: User;
14
- let ctx: Context<JoinRoomPayload>;
15
- let ctx2: Context<JoinRoomPayload>;
16
- let ctx3: Context<SendVibrationPayload>;
17
-
18
- beforeEach(() => {
19
- server = new HapticLinkServer();
20
- ws = new WebSocketWrapper();
21
- user = new User(ws);
22
- ws2 = new WebSocketWrapper();
23
- user2 = new User(ws2);
24
- ctx = {
25
- ws: ws,
26
- user: user,
27
- payload: { username: "test" },
28
- server: server,
29
- };
30
- ctx2 = {
31
- ws: ws2,
32
- user: user2,
33
- payload: { username: "test2" },
34
- server: server,
35
- };
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- ctx3 = {
39
- ws: ws,
40
- user: user,
41
- server: server,
42
- payload: {
43
- id: 1,
44
- type: "enabled",
45
- position: {
46
- x: 3,
47
- y: 10,
48
- },
49
- color: "#FFF",
50
- intensity: 1,
51
- }
52
- }
53
- });
54
 
55
- it("should broadcast vibration", () => {
56
- JoinRoomHandler(ctx)
57
- const resData = JSON.parse(ws.sendData as string)
58
- ctx2.payload.roomId = resData.roomId
59
- JoinRoomHandler(ctx2)
60
- ws.sendData = "";
61
- ws2.sendData = "";
62
- SendVibrationHandler(ctx3);
63
 
64
- const res = JSON.parse(ws2.sendData as string)
65
 
66
- expect(ws.sendData).to.equal(ws2.sendData, "vibration wasn't broadcasted correctly");
67
- expect(res.message).to.equal("receive_touch", "invalid response");
68
- expect(res.id).to.equal(user.id + "_" + "1", "invalid touch id");
69
- expect(res.type).to.equal("enabled", "invalid touch type");
70
- expect(res.user.username).to.equal("test", "wrong username");
71
- expect(res.user.id).to.equal(user.id, "wrong user id");
72
- expect(res.position.x).to.equal(3, "wrong x position");
73
- expect(res.position.y).to.equal(10, "wrong y position");
74
- expect(res.color).to.equal("#FFF", "wrong color");
75
- expect(res.intensity).to.equal(1, "wrong intensity");
76
- });
77
  });
 
 
1
  import { expect } from "chai";
2
  import { Context, HapticLinkServer, User } from "../../../src/socket/hapticLinkServer";
3
  import { JoinRoomHandler, JoinRoomPayload } from "../../../src/socket/routes/join_room";
 
5
  import { SendVibrationHandler, SendVibrationPayload } from "../../../src/socket/routes/send_touch";
6
 
7
  describe("Send Vibration", () => {
8
+ let server: HapticLinkServer;
9
+ let ws: WebSocketWrapper;
10
+ let user: User;
11
+ let ws2: WebSocketWrapper;
12
+ let user2: User;
13
+ let ctx: Context<JoinRoomPayload>;
14
+ let ctx2: Context<JoinRoomPayload>;
15
+ let ctx3: Context<SendVibrationPayload>;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ beforeEach(() => {
18
+ server = new HapticLinkServer();
19
+ ws = new WebSocketWrapper();
20
+ user = new User(ws);
21
+ ws2 = new WebSocketWrapper();
22
+ user2 = new User(ws2);
23
+ ctx = {
24
+ ws: ws,
25
+ user: user,
26
+ payload: { username: "test" },
27
+ server: server,
28
+ };
29
+ ctx2 = {
30
+ ws: ws2,
31
+ user: user2,
32
+ payload: { username: "test2" },
33
+ server: server,
34
+ };
35
 
36
+ ctx3 = {
37
+ ws: ws,
38
+ user: user,
39
+ server: server,
40
+ payload: {
41
+ id: 1,
42
+ type: "enabled",
43
+ position: {
44
+ x: 3,
45
+ y: 10,
46
+ },
47
+ color: "#FFF",
48
+ intensity: 1,
49
+ },
50
+ };
51
+ });
52
 
53
+ it("should broadcast vibration", () => {
54
+ JoinRoomHandler(ctx);
55
+ const resData = JSON.parse(ws.sendData as string);
56
+ ctx2.payload.roomId = resData.roomId;
57
+ JoinRoomHandler(ctx2);
58
+ ws.sendData = "";
59
+ ws2.sendData = "";
60
+ SendVibrationHandler(ctx3);
61
 
62
+ const res = JSON.parse(ws2.sendData as string);
63
 
64
+ expect(ws.sendData).to.equal(ws2.sendData, "vibration wasn't broadcasted correctly");
65
+ expect(res.message).to.equal("receive_touch", "invalid response");
66
+ expect(res.id).to.equal(user.id + "_" + "1", "invalid touch id");
67
+ expect(res.type).to.equal("enabled", "invalid touch type");
68
+ expect(res.user.username).to.equal("test", "wrong username");
69
+ expect(res.user.id).to.equal(user.id, "wrong user id");
70
+ expect(res.position.x).to.equal(3, "wrong x position");
71
+ expect(res.position.y).to.equal(10, "wrong y position");
72
+ expect(res.color).to.equal("#FFF", "wrong color");
73
+ expect(res.intensity).to.equal(1, "wrong intensity");
74
+ });
75
  });
server/test/socket/routes/set_username.spec.ts CHANGED
@@ -4,27 +4,27 @@ import { SetUsernameHandler, SetUsernamePayload } from "../../../src/socket/rout
4
  import { WebSocketWrapper } from "../hapticLinkServer.spec";
5
 
6
  describe("Set Username", () => {
7
- let server: HapticLinkServer;
8
- let ws: WebSocketWrapper;
9
- let user: User;
10
- let ctx: Context<SetUsernamePayload>;
11
 
12
- beforeEach(() => {
13
- server = new HapticLinkServer();
14
- ws = new WebSocketWrapper();
15
- user = new User(ws);
16
- user.username = "old username"
17
- ctx = {
18
- ws: ws,
19
- user: user,
20
- payload: { username: "new username" },
21
- server: server,
22
- };
23
- });
24
 
25
- it("should change username", (done) => {
26
- SetUsernameHandler(ctx);
27
- expect(user.username).to.equal("new username", "name wasn't changed correctly")
28
- done();
29
- })
30
- })
 
4
  import { WebSocketWrapper } from "../hapticLinkServer.spec";
5
 
6
  describe("Set Username", () => {
7
+ let server: HapticLinkServer;
8
+ let ws: WebSocketWrapper;
9
+ let user: User;
10
+ let ctx: Context<SetUsernamePayload>;
11
 
12
+ beforeEach(() => {
13
+ server = new HapticLinkServer();
14
+ ws = new WebSocketWrapper();
15
+ user = new User(ws);
16
+ user.username = "old username";
17
+ ctx = {
18
+ ws: ws,
19
+ user: user,
20
+ payload: { username: "new username" },
21
+ server: server,
22
+ };
23
+ });
24
 
25
+ it("should change username", (done) => {
26
+ SetUsernameHandler(ctx);
27
+ expect(user.username).to.equal("new username", "name wasn't changed correctly");
28
+ done();
29
+ });
30
+ });
server/test/socket/routes/test_connection.spec.ts CHANGED
@@ -1,28 +1,30 @@
1
  import { expect } from "chai";
2
- import { Context, HapticLinkServer, User } from "../../../src/socket/hapticLinkServer"
3
- import { TestConnectionHandler, TestConnectionPayload } from "../../../src/socket/routes/test_connection"
 
 
 
4
  import { WebSocketWrapper } from "../hapticLinkServer.spec";
5
 
6
-
7
  describe("Test Connection", () => {
8
- let server: HapticLinkServer = new HapticLinkServer();
9
- const ws = new WebSocketWrapper();
10
- const user = new User(ws);
11
- user.username = "test"
12
- let ctx: Context<TestConnectionPayload> = {
13
- ws: ws,
14
- user: user,
15
- payload: {
16
- route: "test_connection",
17
- },
18
- server: server,
19
- }
20
 
21
- it("should return connection response with user", (done) => {
22
- TestConnectionHandler(ctx);
23
- const res = JSON.parse(ws.sendData as string);
24
- expect(res.message).to.equal("test_connection_response", "didn't return message")
25
- expect(res.username).to.equal("test", "didn't return message")
26
- done();
27
- })
28
- })
 
1
  import { expect } from "chai";
2
+ import { Context, HapticLinkServer, User } from "../../../src/socket/hapticLinkServer";
3
+ import {
4
+ TestConnectionHandler,
5
+ TestConnectionPayload,
6
+ } from "../../../src/socket/routes/test_connection";
7
  import { WebSocketWrapper } from "../hapticLinkServer.spec";
8
 
 
9
  describe("Test Connection", () => {
10
+ let server: HapticLinkServer = new HapticLinkServer();
11
+ const ws = new WebSocketWrapper();
12
+ const user = new User(ws);
13
+ user.username = "test";
14
+ let ctx: Context<TestConnectionPayload> = {
15
+ ws: ws,
16
+ user: user,
17
+ payload: {
18
+ route: "test_connection",
19
+ },
20
+ server: server,
21
+ };
22
 
23
+ it("should return connection response with user", (done) => {
24
+ TestConnectionHandler(ctx);
25
+ const res = JSON.parse(ws.sendData as string);
26
+ expect(res.message).to.equal("test_connection_response", "didn't return message");
27
+ expect(res.username).to.equal("test", "didn't return message");
28
+ done();
29
+ });
30
+ });