1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-30 12:17:42 +03:00
leon/test/unit/server/core/server.spec.js

99 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-02-10 15:26:50 +03:00
import net from 'net'
import { EventEmitter } from 'events'
import Server from '@/core/server'
describe('server', () => {
const bootstrapTmp = Server.bootstrap
const listenTmp = Server.listen
describe('constructor()', () => {
test('creates a new instance of Server', () => {
const server = new Server()
expect(server).toBeInstanceOf(Server)
})
})
describe('init()', () => {
test('uses default language if there is an unsupported one', async () => {
Server.bootstrap = jest.fn() // Need to mock bootstrap method to not continue the init
process.env.LEON_LANG = 'fake-lang'
await Server.init()
expect(process.env.LEON_LANG).toBe('en-US')
Server.bootstrap = bootstrapTmp // Need to give back the real bootstrap method
})
test('executes bootstrap', async () => {
Server.bootstrap = jest.fn()
await Server.init()
expect(Server.bootstrap).toHaveBeenCalledTimes(1)
Server.bootstrap = bootstrapTmp // Need to give back the real bootstrap method
})
})
describe('bootstrap()', () => {
test('executes listening', async () => {
Server.listen = jest.fn()
await Server.bootstrap()
expect(Server.listen).toHaveBeenCalledTimes(1)
Server.listen = listenTmp // Need to give back the real listen method
})
})
describe('listen()', () => {
test('listens port already in use', async () => {
const server = net.createServer()
server.once('error', (err) => {
expect(err.code).toBe('EADDRINUSE')
server.close()
Server.server.close()
})
2019-05-25 07:07:11 +03:00
await Server.listen(process.env.LEON_PORT)
server.listen(process.env.LEON_PORT)
2019-02-10 15:26:50 +03:00
})
})
// TODO: something more elegant
describe('connection()', () => {
test('initializes main nodes', (done) => {
// Mock the WebSocket with an EventEmitter
const ee = new EventEmitter()
ee.broadcast = { emit: jest.fn() }
console.log = jest.fn()
Server.connection(ee)
expect(console.log.mock.calls[0][1]).toBe('CLIENT')
console.log = jest.fn()
ee.emit('init', 'hotword-node')
console.log = jest.fn()
ee.emit('hotword-detected', { })
expect(console.log.mock.calls[0][1]).toBe('SOCKET')
console.log = jest.fn()
2019-02-10 15:26:50 +03:00
ee.emit('init', 'testing')
expect(console.log.mock.calls[0][1]).toBe('BRAIN')
console.log = jest.fn()
2019-02-10 15:26:50 +03:00
setImmediate(() => {
2019-02-10 15:26:50 +03:00
ee.emit('query', { client: 'jest', value: 'Hello' })
2019-03-16 02:50:30 +03:00
expect(['NLU', 'SOCKET']).toContain(console.log.mock.calls[0][1])
2019-02-10 15:26:50 +03:00
console.log = jest.fn()
ee.emit('recognize', { })
expect(console.log.mock.calls[0][1]).toBe('ASR')
console.log = jest.fn()
done()
})
2019-02-10 15:26:50 +03:00
})
})
})