2019-02-10 15:26:50 +03:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import execa from 'execa'
|
|
|
|
|
|
|
|
import Nlu from '@/core/nlu'
|
|
|
|
import Brain from '@/core/brain'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test will test the Leon's NLP (Natural Language Processing):
|
|
|
|
* 1. Browse every expression for each module
|
|
|
|
* 2. Check if it matches its respective module
|
|
|
|
*
|
|
|
|
* Do not forget to train your expressions after this test (already included in e2e npm script)
|
|
|
|
*/
|
|
|
|
|
|
|
|
jest.setTimeout(60000) // Specify jest.setTimeout here as this test does not have config file
|
|
|
|
|
|
|
|
describe('NLU modules', () => {
|
|
|
|
const { langs } = JSON.parse(fs.readFileSync(path.join(global.paths.root, 'core', 'langs.json'), 'utf8'))
|
|
|
|
const langKeys = Object.keys(langs)
|
|
|
|
const packages = fs.readdirSync(global.paths.packages)
|
|
|
|
.filter(entity =>
|
|
|
|
fs.statSync(path.join(global.paths.packages, entity)).isDirectory())
|
|
|
|
|
|
|
|
for (let i = 0; i < langKeys.length; i += 1) {
|
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
describe(`${langKeys[i]} language`, () => {
|
|
|
|
const lang = langs[langKeys[i]]
|
|
|
|
const nlu = new Nlu()
|
|
|
|
const brain = new Brain({ emit: jest.fn() }, lang.short)
|
2019-05-04 04:41:37 +03:00
|
|
|
let expressionsObj = { }
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
nlu.brain = { wernicke: jest.fn(), talk: jest.fn(), socket: { emit: jest.fn() } }
|
|
|
|
brain.talk = jest.fn()
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2019-05-01 09:32:50 +03:00
|
|
|
process.env.LEON_LANG = langKeys[i]
|
|
|
|
|
2019-02-10 15:26:50 +03:00
|
|
|
// Generate new classifier for the tested language
|
|
|
|
await execa.shell(`npm run train expressions:${lang.short}`)
|
|
|
|
// Load the new classifier
|
|
|
|
await nlu.loadModel(global.paths.classifier)
|
|
|
|
})
|
|
|
|
|
|
|
|
for (let j = 0; j < packages.length; j += 1) {
|
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
describe(`${packages[j]} package`, () => {
|
|
|
|
const expressionsFile = `${global.paths.packages}/${packages[j]}/data/expressions/${lang.short}.json`
|
2019-05-04 04:41:37 +03:00
|
|
|
expressionsObj = JSON.parse(fs.readFileSync(expressionsFile, 'utf8'))
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2019-05-04 04:41:37 +03:00
|
|
|
const modules = Object.keys(expressionsObj)
|
2019-02-10 15:26:50 +03:00
|
|
|
for (let k = 0; k < modules.length; k += 1) {
|
2019-05-04 04:41:37 +03:00
|
|
|
const module = modules[k]
|
|
|
|
const actions = Object.keys(expressionsObj[module])
|
|
|
|
|
2019-02-10 15:26:50 +03:00
|
|
|
// eslint-disable-next-line no-loop-func
|
2019-05-04 04:41:37 +03:00
|
|
|
describe(`${module} module`, () => {
|
|
|
|
for (let l = 0; l < actions.length; l += 1) {
|
|
|
|
const action = actions[l]
|
|
|
|
const exprs = expressionsObj[module][action].expressions
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2019-05-04 04:41:37 +03:00
|
|
|
for (let m = 0; m < exprs.length; m += 1) {
|
|
|
|
// eslint-disable-next-line no-loop-func
|
|
|
|
test(`"${exprs[m]}" queries this module`, async () => {
|
|
|
|
// Need to redefine the NLU brain execution to update the mocking
|
|
|
|
nlu.brain.execute = jest.fn()
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2019-05-04 04:41:37 +03:00
|
|
|
await nlu.process(exprs[m])
|
|
|
|
const [obj] = nlu.brain.execute.mock.calls
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2019-05-04 04:41:37 +03:00
|
|
|
expect(obj[0].classification.package).toBe(packages[j])
|
|
|
|
expect(obj[0].classification.module).toBe(module)
|
|
|
|
})
|
|
|
|
}
|
2019-02-10 15:26:50 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|