2019-02-10 15:26:50 +03:00
|
|
|
import Nlu from '@/core/nlu'
|
|
|
|
|
|
|
|
describe('NLU', () => {
|
|
|
|
describe('constructor()', () => {
|
|
|
|
test('creates a new instance of Nlu', () => {
|
|
|
|
const nlu = new Nlu()
|
|
|
|
|
|
|
|
expect(nlu).toBeInstanceOf(Nlu)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('loadModel()', () => {
|
2021-04-01 12:24:59 +03:00
|
|
|
test('returns warning NLP model does not exist', async () => {
|
2019-02-10 15:26:50 +03:00
|
|
|
const nlu = new Nlu()
|
|
|
|
|
|
|
|
try {
|
2021-04-01 12:24:59 +03:00
|
|
|
await nlu.loadModel('ghost-model.nlp')
|
2019-02-10 15:26:50 +03:00
|
|
|
} catch (e) {
|
|
|
|
expect(e.type).toBe('warning')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
test('rejects because of a broken NLP model', async () => {
|
2019-03-16 03:41:26 +03:00
|
|
|
const nlu = new Nlu()
|
|
|
|
nlu.brain = { talk: jest.fn(), wernicke: jest.fn(), socket: { emit: jest.fn() } }
|
|
|
|
|
|
|
|
try {
|
2021-04-01 12:24:59 +03:00
|
|
|
await nlu.loadModel(global.paths.broken_nlp_model)
|
2019-03-16 03:41:26 +03:00
|
|
|
} catch (e) {
|
|
|
|
expect(e.type).toBe('error')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
test('loads the NLP model', async () => {
|
2019-02-10 15:26:50 +03:00
|
|
|
const nlu = new Nlu()
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
await nlu.loadModel(global.paths.nlp_model)
|
2021-04-02 12:32:23 +03:00
|
|
|
expect(nlu.nlp.nluManager.domainManagers).not.toBeEmpty()
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('process()', () => {
|
|
|
|
const nluFallbackTmp = Nlu.fallback
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
test('returns false because the NLP model is empty', async () => {
|
2019-02-10 15:26:50 +03:00
|
|
|
const nlu = new Nlu()
|
|
|
|
nlu.brain = { talk: jest.fn(), wernicke: jest.fn(), socket: { emit: jest.fn() } }
|
|
|
|
|
|
|
|
expect(await nlu.process('Hello')).toBeFalsy()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns false because of query not found', async () => {
|
|
|
|
const nlu = new Nlu()
|
|
|
|
nlu.brain = { talk: jest.fn(), wernicke: jest.fn(), socket: { emit: jest.fn() } }
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
await nlu.loadModel(global.paths.nlp_model)
|
2019-03-05 03:38:18 +03:00
|
|
|
expect(await nlu.process('Unknown query')).toBeFalsy()
|
2019-02-10 15:26:50 +03:00
|
|
|
expect(nlu.brain.talk).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('executes brain with the fallback value (object)', async () => {
|
2019-05-11 14:31:35 +03:00
|
|
|
const query = 'Thisisaqueryexampletotestfallbacks'
|
|
|
|
const fallbackObj = {
|
|
|
|
query,
|
|
|
|
entities: [],
|
|
|
|
classification: { package: 'leon', module: 'randomnumber', action: 'run' }
|
|
|
|
}
|
2019-02-10 15:26:50 +03:00
|
|
|
const nlu = new Nlu()
|
|
|
|
nlu.brain = { execute: jest.fn() }
|
|
|
|
Nlu.fallback = jest.fn(() => fallbackObj)
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
await nlu.loadModel(global.paths.nlp_model)
|
2019-05-11 14:31:35 +03:00
|
|
|
expect(await nlu.process(query)).toBeTruthy()
|
2019-02-10 15:26:50 +03:00
|
|
|
expect(nlu.brain.execute.mock.calls[0][0]).toBe(fallbackObj)
|
|
|
|
Nlu.fallback = nluFallbackTmp // Need to give back the real fallback method
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns true thanks to query found', async () => {
|
|
|
|
const nlu = new Nlu()
|
|
|
|
nlu.brain = { execute: jest.fn() }
|
|
|
|
|
2021-04-01 12:24:59 +03:00
|
|
|
await nlu.loadModel(global.paths.nlp_model)
|
2019-03-05 03:38:18 +03:00
|
|
|
expect(await nlu.process('Hello')).toBeTruthy()
|
2019-02-10 15:26:50 +03:00
|
|
|
expect(nlu.brain.execute).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('fallback()', () => {
|
|
|
|
test('returns false because there is no fallback matching the query', () => {
|
|
|
|
expect(Nlu.fallback({ query: 'This is a query example to test fallbacks' }, [])).toBeFalsy()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('returns fallback injected object', () => {
|
|
|
|
const obj = {
|
|
|
|
query: 'This is a query example to test fallbacks',
|
|
|
|
classification: { }
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(Nlu.fallback(obj, [
|
2021-03-15 20:39:52 +03:00
|
|
|
{
|
|
|
|
words: ['query', 'example', 'test', 'fallbacks'], package: 'fake-pkg', module: 'fake-module', action: 'fake-action'
|
|
|
|
}
|
2019-05-04 06:20:35 +03:00
|
|
|
]).classification).toContainEntries([['package', 'fake-pkg'], ['module', 'fake-module'], ['action', 'fake-action'], ['confidence', 1]])
|
2019-02-10 15:26:50 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|