Test proper handling of invalid types
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||||
import { WebSocketServer } from 'ws'
|
import { WebSocketServer } from 'ws'
|
||||||
import { socket } from '../../src/lib/stores/socket'
|
import { socket } from '../../src/lib/stores/socket'
|
||||||
import { IMUData, RSSIData, WebsocketMessage } from '../../src/lib/platform_shared/websocket_message'
|
import { IMUData, RSSIData, WebsocketMessage, WebsocketMessage } from '../../src/lib/platform_shared/websocket_message'
|
||||||
|
|
||||||
// Helper function to create encoded WebSocket messages
|
// Helper function to create encoded WebSocket messages
|
||||||
function createEncodedMessage(messageType: 'imu' | 'rssi' | 'mode', data: any): Uint8Array {
|
function createEncodedMessage(messageType: 'imu' | 'rssi' | 'mode', data: any): Uint8Array {
|
||||||
@@ -90,6 +90,103 @@ describe.sequential('WebSocket Integration Tests', () => {
|
|||||||
unsubscribe()
|
unsubscribe()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should send IMU data from client to server using sendEvent', async () => {
|
||||||
|
let serverReceivedData: any = null
|
||||||
|
|
||||||
|
// Connect socket
|
||||||
|
socket.init(`ws://localhost:${TEST_PORT}`)
|
||||||
|
|
||||||
|
// Wait for client to connect and send data
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
reject(new Error('Test timeout - server did not receive message'))
|
||||||
|
}, 3000)
|
||||||
|
|
||||||
|
wss.on('connection', (ws) => {
|
||||||
|
// console.log('Server: Client connected')
|
||||||
|
|
||||||
|
// Server listens for messages from client
|
||||||
|
ws.on('message', (data: Buffer) => {
|
||||||
|
// console.log('Server: Received message, length:', data.length)
|
||||||
|
|
||||||
|
// Skip empty messages (from ping, etc.)
|
||||||
|
if (data.length === 0) {
|
||||||
|
console.log('Server: Skipping empty message (Probably a ping')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Decode the protobuf message
|
||||||
|
const decoded = WebsocketMessage.decode(new Uint8Array(data))
|
||||||
|
// console.log('Server: Decoded message:', JSON.stringify(decoded, null, 2))
|
||||||
|
|
||||||
|
// Only resolve if we got actual IMU data
|
||||||
|
if (decoded.imu) {
|
||||||
|
serverReceivedData = decoded
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve()
|
||||||
|
} else {
|
||||||
|
// console.log('Server: Message decoded but no IMU data, waiting...')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Server: Failed to decode:', error)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
reject(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Wait for WebSocket to be fully connected
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('Client: Sending IMU data...')
|
||||||
|
// Client sends IMU data to server
|
||||||
|
const imuData = {
|
||||||
|
x: 3,
|
||||||
|
y: 2,
|
||||||
|
z: 1,
|
||||||
|
temp: 22
|
||||||
|
}
|
||||||
|
socket.sendEvent(IMUData, imuData)
|
||||||
|
console.log('Client: sendEvent called')
|
||||||
|
}, 150)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Verify server received the data
|
||||||
|
expect(serverReceivedData).toBeDefined()
|
||||||
|
expect(serverReceivedData?.imu).toBeDefined()
|
||||||
|
expect(serverReceivedData?.imu?.x).toBe(3)
|
||||||
|
expect(serverReceivedData?.imu?.y).toBe(2)
|
||||||
|
expect(serverReceivedData?.imu?.z).toBe(1)
|
||||||
|
expect(serverReceivedData?.imu?.temp).toBe(22)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
it('should fail to serialize data on sendEvent', async () => {
|
||||||
|
// Connect socket
|
||||||
|
socket.init(`ws://localhost:${TEST_PORT}`)
|
||||||
|
|
||||||
|
await new Promise<void>((resolve, reject) => {
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
reject(new Error('Test timeout'))
|
||||||
|
}, 1000)
|
||||||
|
|
||||||
|
// Wait for WebSocket to be fully connected
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('Client: Sending invalid message type...')
|
||||||
|
// Send any invalid message type
|
||||||
|
const wsm = WebsocketMessage.create()
|
||||||
|
try {
|
||||||
|
socket.sendEvent(WebsocketMessage as any, wsm)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
reject(new Error('Expected sendEvent to throw, but it did not'))
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Client: sendEvent correctly threw error:', e)
|
||||||
|
clearTimeout(timeout)
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
}, 150)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('WebsocketMessage Protobuf Encoding/Decoding', () => {
|
describe('WebsocketMessage Protobuf Encoding/Decoding', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user