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)
|
2021-04-02 12:32:23 +03:00
|
|
|
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 () => {
|
2021-04-02 12:32:23 +03:00
|
|
|
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'
|
|
|
|
|
2021-04-02 12:32:23 +03:00
|
|
|
await server.init()
|
2019-02-10 15:26:50 +03:00
|
|
|
expect(process.env.LEON_LANG).toBe('en-US')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('bootstrap()', () => {
|
2021-04-02 12:32:23 +03:00
|
|
|
test('initializes HTTP server', async () => {
|
|
|
|
const server = new Server()
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2021-04-02 12:32:23 +03:00
|
|
|
await server.bootstrap()
|
|
|
|
expect(server.httpServer).not.toBeEmpty()
|
2021-04-02 14:02:37 +03:00
|
|
|
await server.httpServer.close()
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('listen()', () => {
|
|
|
|
test('listens port already in use', async () => {
|
2021-04-02 12:32:23 +03:00
|
|
|
const fakeServer = net.createServer()
|
|
|
|
|
|
|
|
fakeServer.once('error', (err) => {
|
2019-02-10 15:26:50 +03:00
|
|
|
expect(err.code).toBe('EADDRINUSE')
|
2021-04-02 12:32:23 +03:00
|
|
|
fakeServer.close()
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
|
2021-04-02 12:32:23 +03:00
|
|
|
const server = new Server()
|
|
|
|
await server.init()
|
|
|
|
|
|
|
|
fakeServer.listen(process.env.LEON_PORT)
|
2021-04-02 14:02:37 +03:00
|
|
|
await server.httpServer.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('listens for request', async () => {
|
|
|
|
const server = new Server()
|
|
|
|
console.log = jest.fn()
|
|
|
|
|
|
|
|
await server.listen(process.env.LEON_PORT)
|
|
|
|
expect(console.log.mock.calls[0][1].indexOf(process.env.LEON_PORT)).not.toBe(-1)
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('connection()', () => {
|
2021-12-27 13:37:42 +03:00
|
|
|
test('initializes main nodes', async () => {
|
2021-04-02 12:32:23 +03:00
|
|
|
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()
|
|
|
|
|
2021-04-02 12:32:23 +03:00
|
|
|
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()
|
|
|
|
|
2021-03-21 09:58:14 +03:00
|
|
|
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
|
|
|
|
2021-04-02 14:02:37 +03:00
|
|
|
ee.emit('init', 'jest')
|
2021-04-02 12:32:23 +03:00
|
|
|
expect(server.brain).not.toBeEmpty()
|
|
|
|
expect(server.nlu).not.toBeEmpty()
|
|
|
|
expect(server.asr).not.toBeEmpty()
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2021-12-27 14:46:18 +03:00
|
|
|
/* setTimeout(() => {
|
2021-04-02 14:02:37 +03:00
|
|
|
ee.emit('query', { client: 'jest', value: 'Hello' })
|
|
|
|
}, 50)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2021-12-27 12:09:12 +03:00
|
|
|
expect(console.log.mock.calls[26][1]).toBe('Query found')
|
2021-04-02 14:02:37 +03:00
|
|
|
console.log = jest.fn()
|
|
|
|
}, 100)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
ee.emit('recognize', 'blob')
|
|
|
|
}, 150)
|
|
|
|
|
2021-04-03 06:43:55 +03:00
|
|
|
setTimeout(async () => {
|
2021-04-02 14:02:37 +03:00
|
|
|
expect(console.log.mock.calls[0][1]).toBe('ASR')
|
2019-02-10 15:26:50 +03:00
|
|
|
console.log = jest.fn()
|
|
|
|
|
2021-04-03 06:43:55 +03:00
|
|
|
await server.httpServer.close()
|
2021-12-27 14:46:18 +03:00
|
|
|
}, 200) */
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|