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

79 lines
2.1 KiB
JavaScript
Raw Normal View History

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()', () => {
test('uses default language if the given one is unsupported', async () => {
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')
})
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()', () => {
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()
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
/* 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)
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()
await server.httpServer.close()
}, 200) */
2019-02-10 15:26:50 +03:00
})
})
})