|
| 1 | +import { WebSocket } from 'ws' |
| 2 | +import TaloSocket from '../../../src/socket' |
| 3 | +import SocketConnection from '../../../src/socket/socketConnection' |
| 4 | +import createAPIKeyAndToken from '../../utils/createAPIKeyAndToken' |
| 5 | +import { IncomingMessage } from 'http' |
| 6 | +import { Socket } from 'net' |
| 7 | +import { logConnection, logConnectionClosed, logRequest, logResponse } from '../../../src/socket/messages/socketLogger' |
| 8 | +import { EntityManager } from '@mikro-orm/mysql' |
| 9 | + |
| 10 | +describe('Socket logger', () => { |
| 11 | + const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => undefined) |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + consoleMock.mockReset() |
| 15 | + }) |
| 16 | + |
| 17 | + async function createSocketConnection(): Promise<[TaloSocket, SocketConnection, () => void]>{ |
| 18 | + const [apiKey] = await createAPIKeyAndToken([]) |
| 19 | + await (<EntityManager>global.em).persistAndFlush(apiKey) |
| 20 | + |
| 21 | + const socket = new TaloSocket(global.server, global.em) |
| 22 | + const conn = new SocketConnection(new WebSocket(null, [], {}), apiKey, new IncomingMessage(new Socket())) |
| 23 | + vi.spyOn(conn, 'getRemoteAddress').mockReturnValue('0.0.0.0') |
| 24 | + |
| 25 | + return [ |
| 26 | + socket, |
| 27 | + conn, |
| 28 | + () => { |
| 29 | + socket.getServer().close() |
| 30 | + } |
| 31 | + ] |
| 32 | + } |
| 33 | + |
| 34 | + it('should log requests', async () => { |
| 35 | + const [, conn, cleanup] = await createSocketConnection() |
| 36 | + |
| 37 | + logRequest(conn, JSON.stringify({ req: 'v1.fake', data: {} })) |
| 38 | + |
| 39 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 40 | + expect(consoleMock).toHaveBeenLastCalledWith(` <-- WSS /games/${conn.game.id}/{v1.fake} 0.0.0.0 27b`) |
| 41 | + |
| 42 | + cleanup() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should log requests with aliases', async () => { |
| 46 | + const [, conn, cleanup] = await createSocketConnection() |
| 47 | + conn.playerAliasId = 2 |
| 48 | + |
| 49 | + logRequest(conn, JSON.stringify({ req: 'v1.fake', data: {} })) |
| 50 | + |
| 51 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 52 | + expect(consoleMock).toHaveBeenLastCalledWith(` <-- WSS /games/${conn.game.id}/aliases/2/{v1.fake} 0.0.0.0 27b`) |
| 53 | + |
| 54 | + cleanup() |
| 55 | + }) |
| 56 | + |
| 57 | + it('should log responses', async () => { |
| 58 | + const [, conn, cleanup] = await createSocketConnection() |
| 59 | + |
| 60 | + logResponse(conn, 'v1.players.identify.success', JSON.stringify({ res: 'v1.players.identify.success', data: {} })) |
| 61 | + |
| 62 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 63 | + expect(consoleMock).toHaveBeenLastCalledWith(` --> WSS /games/${conn.game.id}/{v1.players.identify.success} 0.0.0.0 47b`) |
| 64 | + |
| 65 | + cleanup() |
| 66 | + }) |
| 67 | + |
| 68 | + it('should log connections', async () => { |
| 69 | + logConnection(new IncomingMessage(new Socket())) |
| 70 | + |
| 71 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 72 | + expect(consoleMock).toHaveBeenLastCalledWith(' <-- WSS /open undefined') |
| 73 | + }) |
| 74 | + |
| 75 | + it('should log pre-closed connections', async () => { |
| 76 | + const [, conn, cleanup] = await createSocketConnection() |
| 77 | + |
| 78 | + logConnectionClosed(conn, true, 3000) |
| 79 | + |
| 80 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 81 | + expect(consoleMock).toHaveBeenLastCalledWith(` <-- WSS /games/${conn.game.id}/close 0.0.0.0`) |
| 82 | + |
| 83 | + cleanup() |
| 84 | + }) |
| 85 | + |
| 86 | + it('should log manually-closed connections', async () => { |
| 87 | + const [, conn, cleanup] = await createSocketConnection() |
| 88 | + |
| 89 | + logConnectionClosed(conn, false, 3000, 'Unauthorised') |
| 90 | + |
| 91 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 92 | + expect(consoleMock).toHaveBeenLastCalledWith(` --> WSS /games/${conn.game.id}/close 0.0.0.0 3000 Unauthorised`) |
| 93 | + |
| 94 | + cleanup() |
| 95 | + }) |
| 96 | + |
| 97 | + it('should log manually-closed connections without a reason', async () => { |
| 98 | + const [, conn, cleanup] = await createSocketConnection() |
| 99 | + |
| 100 | + logConnectionClosed(conn, false, 3000) |
| 101 | + |
| 102 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 103 | + expect(consoleMock).toHaveBeenLastCalledWith(` --> WSS /games/${conn.game.id}/close 0.0.0.0 3000`) |
| 104 | + |
| 105 | + cleanup() |
| 106 | + }) |
| 107 | + |
| 108 | + it('should log manually-closed connections without a SocketConnection', async () => { |
| 109 | + logConnectionClosed(undefined, false, 3000) |
| 110 | + |
| 111 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 112 | + expect(consoleMock).toHaveBeenLastCalledWith(' --> WSS /close unknown 3000') |
| 113 | + }) |
| 114 | + |
| 115 | + it('should log pre-closed connection with aliases', async () => { |
| 116 | + const [, conn, cleanup] = await createSocketConnection() |
| 117 | + conn.playerAliasId = 2 |
| 118 | + |
| 119 | + logConnectionClosed(conn, true, 3000) |
| 120 | + |
| 121 | + expect(consoleMock).toHaveBeenCalledOnce() |
| 122 | + expect(consoleMock).toHaveBeenLastCalledWith(` <-- WSS /games/${conn.game.id}/aliases/2/close 0.0.0.0`) |
| 123 | + |
| 124 | + cleanup() |
| 125 | + }) |
| 126 | +}) |
0 commit comments