2019-02-10 15:26:50 +03:00
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
|
2022-02-03 14:37:56 +03:00
|
|
|
import server from '@/core/http-server/server'
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
describe('server', () => {
|
|
|
|
describe('init()', () => {
|
2022-02-03 15:42:31 +03:00
|
|
|
test('uses default language if the given one is unsupported', async () => {
|
2021-04-02 12:32:23 +03:00
|
|
|
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')
|
|
|
|
})
|
2022-02-03 14:37:56 +03:00
|
|
|
|
|
|
|
test('initializes server configurations', async () => {
|
|
|
|
await expect(server.init()).resolves.not.toThrow()
|
|
|
|
})
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('bootstrap()', () => {
|
2021-04-02 12:32:23 +03:00
|
|
|
test('initializes HTTP server', async () => {
|
|
|
|
await server.bootstrap()
|
2022-02-03 14:37:56 +03:00
|
|
|
expect(server.httpServer).not.toBe({ })
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('listen()', () => {
|
2021-04-02 14:02:37 +03:00
|
|
|
test('listens for request', async () => {
|
|
|
|
console.log = jest.fn()
|
|
|
|
|
|
|
|
await server.listen(process.env.LEON_PORT)
|
2022-02-03 14:37:56 +03:00
|
|
|
expect(console.log.mock.calls[1][1].indexOf(`${process.env.LEON_HOST}:${process.env.LEON_PORT}`)).not.toEqual(-1)
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-02-03 14:37:56 +03:00
|
|
|
describe('handleOnConnection()', () => {
|
2021-12-27 13:37:42 +03:00
|
|
|
test('initializes main nodes', async () => {
|
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()
|
|
|
|
|
2022-02-03 14:37:56 +03:00
|
|
|
server.handleOnConnection(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')
|
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
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|