1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-21 07:41:39 +03:00
leon/test/unit/server/core/server.spec.js

93 lines
2.3 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', () => {
describe('constructor()', () => {
test('creates a new instance of Server', () => {
const server = new Server()
expect(server).toBeInstanceOf(Server)
expect(server.brain).toBeEmpty()
2019-02-10 15:26:50 +03:00
})
})
describe('init()', () => {
test('uses default language if there is an unsupported one', async () => {
const server = new Server()
server.bootstrap = jest.fn() // Need to mock bootstrap method to not continue the init
2019-02-10 15:26:50 +03:00
process.env.LEON_LANG = 'fake-lang'
await server.init()
2019-02-10 15:26:50 +03:00
expect(process.env.LEON_LANG).toBe('en-US')
})
})
describe('bootstrap()', () => {
test('initializes HTTP server', async () => {
const server = new Server()
2019-02-10 15:26:50 +03:00
await server.bootstrap()
expect(server.httpServer).not.toBeEmpty()
server.httpServer.close()
2019-02-10 15:26:50 +03:00
})
})
describe('listen()', () => {
test('listens port already in use', async () => {
const fakeServer = net.createServer()
fakeServer.once('error', (err) => {
2019-02-10 15:26:50 +03:00
expect(err.code).toBe('EADDRINUSE')
fakeServer.close()
2019-02-10 15:26:50 +03:00
})
const server = new Server()
await server.init()
fakeServer.listen(process.env.LEON_PORT)
server.httpServer.close()
2019-02-10 15:26:50 +03:00
})
})
describe('connection()', () => {
test('initializes main nodes', async (done) => {
const server = new Server()
await server.init()
2019-02-10 15:26:50 +03:00
// Mock the WebSocket with an EventEmitter
const ee = new EventEmitter()
ee.broadcast = { emit: jest.fn() }
console.log = jest.fn()
await server.connection(ee)
2019-02-10 15:26:50 +03:00
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(server.brain).not.toBeEmpty()
expect(server.nlu).not.toBeEmpty()
expect(server.asr).not.toBeEmpty()
2019-02-10 15:26:50 +03:00
ee.emit('query', { client: 'jest', value: 'Hello' })
setTimeout(() => {
expect(console.log.mock.calls[0][1]).toBe('BRAIN')
2019-02-10 15:26:50 +03:00
console.log = jest.fn()
done()
}, 50)
2019-02-10 15:26:50 +03:00
})
})
})