uci-socket/test/socket.test.mjs

50 lines
1.1 KiB
JavaScript

import { spawn } from 'child_process'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
const expect = chai.expect
import { Consumer } from '../src'
const USOCKET = __dirname + '/sample.sock'
let consumer = new Consumer(USOCKET)
const app = {
ucpp: 'cprocessPacket',
cprocessPacket: async function (packet) {
if (packet.processed) consumer.emit(packet.cmd,packet.payload)
}
}
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
;
(async () => {
let socket ={}
before(async function(){
socket = spawn('node',['-r', '@std/esm', './test/socket'])
await delay(500) // wait for socket to get going
})
after(async function(){
socket.kill()
})
it('Connects and Processes some payload', async function () {
console.log('connection is ',await consumer.connect(app))
consumer.on('test1', function(payload){
expect(payload).to.equal('payload1')
})
let packet = {id: 'test consumer', cmd:'test1', payload:'payload1'}
consumer.send(packet)
})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
})