80 lines
3.7 KiB
JavaScript
80 lines
3.7 KiB
JavaScript
const assert = require('node:assert/strict')
|
|
const fs = require('node:fs')
|
|
const path = require('node:path')
|
|
const vm = require('node:vm')
|
|
const { gzipSync } = require('node:zlib')
|
|
|
|
// Run the actual imported client against independent protocol fixtures.
|
|
let source = fs.readFileSync(path.resolve(__dirname, '../src/cmvr/common/plugins/websocket.js'), 'utf8')
|
|
source = source.replace(/^import .+getToken.+;\r?\n/m, '')
|
|
source = source.slice(0, source.lastIndexOf('export default')) + '\nglobalThis.WebSocketManager = WebSocketManager;'
|
|
class Socket {
|
|
static CLOSED = 3
|
|
constructor(url) { this.url = url; this.readyState = 1; this.sent = [] }
|
|
send(data) { this.sent.push(data) }
|
|
close() { this.readyState = Socket.CLOSED }
|
|
}
|
|
const context = vm.createContext({
|
|
URL, ArrayBuffer, DataView, Uint8Array, Blob, Response, DecompressionStream,
|
|
TextDecoder, TextEncoder, WebSocket: Socket,
|
|
window: { WebSocket: Socket, location: { href: 'http://localhost:5180/' } },
|
|
getToken: () => 'target-token/with+url-characters',
|
|
console: { log() {}, warn() {}, error: console.error },
|
|
setInterval: () => 1, clearInterval() {}, setTimeout: () => 1, clearTimeout() {}, queueMicrotask,
|
|
})
|
|
vm.runInContext(source, context)
|
|
const manager = new context.WebSocketManager('ws://localhost:48090/admin-api/cmvr/ws', () => 1)
|
|
const events = []
|
|
manager.on('AimaTaskInstance', payload => events.push(payload))
|
|
manager.on('camera.rgb', payload => events.push(payload))
|
|
const bufferOf = bytes => bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength)
|
|
|
|
async function main() {
|
|
manager.connect()
|
|
assert.equal(new URL(manager.ws.url).searchParams.get('token'), 'target-token/with+url-characters')
|
|
assert.equal(new URL(manager.ws.url).searchParams.get('userId'), '1')
|
|
manager.ws.onopen()
|
|
await manager.ws.onmessage({ data: JSON.stringify({ channel: 'AimaTaskInstance', payload: { id: 'plain' } }) })
|
|
const gzipDelivered = new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => reject(new Error('gzip event was not delivered')), 5000)
|
|
const onGzip = payload => {
|
|
if (payload.id !== 'gzip') return
|
|
clearTimeout(timer)
|
|
manager.off('AimaTaskInstance', onGzip)
|
|
resolve()
|
|
}
|
|
manager.on('AimaTaskInstance', onGzip)
|
|
})
|
|
await manager.ws.onmessage({ data: bufferOf(gzipSync(JSON.stringify({ channel: 'AimaTaskInstance', payload: { id: 'gzip' } }))) })
|
|
await gzipDelivered
|
|
assert.deepEqual(events.map(event => event.id), ['plain', 'gzip'])
|
|
|
|
const channel = new TextEncoder().encode('camera.rgb')
|
|
const payload = new Uint8Array([0, 0, 0, 1, 103])
|
|
const media = new ArrayBuffer(26 + channel.length + payload.length)
|
|
const bytes = new Uint8Array(media)
|
|
bytes.set([0x43, 0x4d, 0x56, 0x46, 1, 1])
|
|
const view = new DataView(media)
|
|
view.setUint32(12, 1234)
|
|
view.setUint32(16, 7)
|
|
view.setUint16(20, channel.length)
|
|
view.setUint32(22, payload.length)
|
|
bytes.set(channel, 26)
|
|
bytes.set(payload, 26 + channel.length)
|
|
await manager.ws.onmessage({ data: media })
|
|
assert.equal(events[2].media, true)
|
|
assert.equal(events[2].sequence, 7)
|
|
assert.equal(events[2].timestamp, 1234)
|
|
assert.deepEqual(new Uint8Array(events[2].data), payload)
|
|
|
|
manager.send({ type: 'channel_subscription', action: 'subscribe', channel: 'camera.rgb' })
|
|
manager.close()
|
|
manager.isManualClose = false
|
|
manager.connect()
|
|
manager.ws.onopen()
|
|
assert.ok(manager.ws.sent.map(JSON.parse).some(message => message.action === 'subscribe' && message.channel === 'camera.rgb'))
|
|
manager.close()
|
|
console.log('PASS: token query encoding, JSON, gzip binary messages, CMVF 26-byte media header, and subscription restoration.')
|
|
}
|
|
main().catch(error => { console.error(error); process.exitCode = 1 })
|