uci-websocket/test/usocket.test.js

52 lines
1.5 KiB
JavaScript

import { spawn } from 'child_process'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import btc from 'better-try-catch'
chai.use(chaiAsPromised)
const expect = chai.expect
import { Consumer } from '../src'
const SOCKET_FILE = 'usocket'
let consumer = new Consumer({path:SOCKET_FILE,name:'unix-consumer'})
// const delay = time => new Promise(res=>setTimeout(()=>res(),time))
let socket = {}
describe('Connects and Processes a payload via Unix Socket using JSON packet with alt processor', function(){
before(async function(){
socket = spawn('node',['-r', '@std/esm', './test/sockets/'+SOCKET_FILE])
socket.stdout.on('data', function(buf) {
console.log('[Socket]', String(buf))
})
})
after(async function(){
socket.kill()
})
it('Tests alternate JSON packet procssing at socket and consumer', async function () {
let [err] = await btc(consumer.connect)()
if (err) {
console.log('unable to connect to socket to start test', consumer.path)
process.kill(process.pid, 'SIGTERM')
}
consumer.registerPacketProcessor(async function (packet) {
return new Promise((resolve) => {
packet.payload = 'local:'+packet.payload
resolve(packet)})
})
let packet = { payload:'payload'}
let res = await consumer.send(packet)
expect(res.payload).to.equal('local:overwrite default processor from instance at socket => payload')
}) // end unix socket test
}) // end describe