Skip to content

Commit

Permalink
fix: Coverage was complaining because of a mismatch in a type without…
Browse files Browse the repository at this point in the history
… timeout
  • Loading branch information
kevinszuchet committed Jan 14, 2025
1 parent 9a0cfdc commit 127d54f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
12 changes: 6 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
transform: {
"^.+\\.(ts|tsx)$": ["ts-jest", {tsconfig: "test/tsconfig.json"}]
'^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: 'test/tsconfig.json' }]
},
moduleFileExtensions: ["ts", "js"],
coverageDirectory: "coverage",
collectCoverageFrom: ["src/**/*.ts", "src/**/*.js"],
testMatch: ["**/*.spec.(ts)"],
testEnvironment: "node",
moduleFileExtensions: ['ts', 'js'],
coverageDirectory: 'coverage',
collectCoverageFrom: ['src/**/*.ts', 'src/**/*.js', '!src/migrations/**'],
testMatch: ['**/*.spec.(ts)'],
testEnvironment: 'node'
}
5 changes: 3 additions & 2 deletions src/controllers/handlers/ws-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { verify } from '@dcl/platform-crypto-middleware'
import { AppComponents, WsUserData } from '../../types'
import { normalizeAddress } from '../../utils/address'
import { IUWebSocketEventMap, UWebSocketTransport } from '../../utils/UWebSocketTransport'
import { isNotAuthenticated } from '../../utils/wsUserData'

const textDecoder = new TextDecoder()

Expand Down Expand Up @@ -38,7 +39,7 @@ export async function registerWsHandler(
logger.debug('ws open')
const data = ws.getUserData()
// just for type assertion
if (!data.auth) {
if (isNotAuthenticated(data)) {
data.timeout = setTimeout(() => {
try {
logger.error('closing connection, no authchain received')
Expand All @@ -53,7 +54,7 @@ export async function registerWsHandler(

if (data.auth) {
data.eventEmitter.emit('message', message)
} else {
} else if (isNotAuthenticated(data)) {
clearTimeout(data.timeout)
data.timeout = undefined

Expand Down
26 changes: 14 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ export type IHandler = {
f: (res: HttpResponse, req: HttpRequest) => Promise<IHandlerResult>
}

export type WsUserData =
| {
isConnected: boolean
auth: false
timeout?: NodeJS.Timeout
}
| {
isConnected: boolean
eventEmitter: Emitter<IUWebSocketEventMap>
auth: true
address: string
}
export type WsAuthenticatedUserData = {
isConnected: boolean
eventEmitter: Emitter<IUWebSocketEventMap>
auth: true
address: string
}

export type WsNotAuthenticatedUserData = {
isConnected: boolean
auth: false
timeout?: NodeJS.Timeout
}

export type WsUserData = WsAuthenticatedUserData | WsNotAuthenticatedUserData

export type InternalWebSocket = WebSocket<WsUserData>

Expand Down
5 changes: 5 additions & 0 deletions src/utils/wsUserData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WsNotAuthenticatedUserData, WsUserData } from '../types'

export function isNotAuthenticated(data: WsUserData): data is WsNotAuthenticatedUserData {
return !data.auth
}
28 changes: 28 additions & 0 deletions test/unit/utils/wsUserData.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { WsUserData, WsNotAuthenticatedUserData } from '../../../src/types'
import { isNotAuthenticated } from '../../../src/utils/wsUserData'
import { IUWebSocketEventMap } from '../../../src/utils/UWebSocketTransport'
import { Emitter } from 'mitt'

describe('wsUserData', () => {
describe('isNotAuthenticated', () => {
it('should return false if the user is authenticated', () => {
const data: WsUserData = {
auth: true,
isConnected: false,
eventEmitter: { emit: jest.fn() } as unknown as Emitter<IUWebSocketEventMap>,
address: '0x123'
}

expect(isNotAuthenticated(data)).toBe(false)
})

it('should return true with the correct type if the user is not authenticated', () => {
const data: WsUserData = {
auth: false,
isConnected: false
}

expect(isNotAuthenticated(data)).toBe(true)
})
})
})

0 comments on commit 127d54f

Please sign in to comment.