1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-25 01:31:47 +03:00
leon/test/e2e/nlp-modules.spec.js

91 lines
3.0 KiB
JavaScript
Raw Normal View History

import { command } from 'execa'
import fs from 'node:fs'
import path from 'node:path'
2019-02-10 15:26:50 +03:00
import Nlu from '@/core/nlu'
import Brain from '@/core/brain'
/**
* This test will test the Leon's NLP (Natural Language Processing):
* 1. Browse every utterance sample for each module
2019-02-10 15:26:50 +03:00
* 2. Check if it matches its respective module
*
* Do not forget to train your utterance samples after
* this test (already included in e2e npm script)
2019-02-10 15:26:50 +03:00
*/
jest.setTimeout(60000) // Specify jest.setTimeout here as this test does not have config file
describe('NLU modules', () => {
2022-09-03 14:12:41 +03:00
const { langs } = JSON.parse(
fs.readFileSync(path.join(global.paths.root, 'core', 'langs.json'), 'utf8')
)
2019-02-10 15:26:50 +03:00
const langKeys = Object.keys(langs)
2022-09-03 14:12:41 +03:00
const packages = fs
.readdirSync(global.paths.packages)
.filter((entity) =>
fs.statSync(path.join(global.paths.packages, entity)).isDirectory()
)
2019-02-10 15:26:50 +03:00
for (let i = 0; i < langKeys.length; i += 1) {
describe(`${langKeys[i]} language`, () => {
const lang = langs[langKeys[i]]
const nlu = new Nlu()
2022-02-03 14:37:56 +03:00
const brain = new Brain(lang.short)
2022-09-03 14:12:41 +03:00
let utteranceSamplesObj = {}
2019-02-10 15:26:50 +03:00
2022-09-03 14:12:41 +03:00
nlu.brain = {
wernicke: jest.fn(),
talk: jest.fn(),
socket: { emit: jest.fn() }
}
2019-02-10 15:26:50 +03:00
brain.talk = jest.fn()
beforeAll(async () => {
process.env.LEON_LANG = langKeys[i]
// Generate new NLP model for the tested language
2021-12-27 12:09:12 +03:00
await command(`npm run train ${lang.short}`, { shell: true })
// Load the new NLP model
await nlu.loadModel(global.paths.nlp_model)
2019-02-10 15:26:50 +03:00
})
for (let j = 0; j < packages.length; j += 1) {
describe(`${packages[j]} package`, () => {
const utteranceSamplesFile = `${global.paths.packages}/${packages[j]}/data/expressions/${lang.short}.json`
2022-09-03 14:12:41 +03:00
utteranceSamplesObj = JSON.parse(
fs.readFileSync(utteranceSamplesFile, 'utf8')
)
2019-02-10 15:26:50 +03:00
const modules = Object.keys(utteranceSamplesObj)
2019-02-10 15:26:50 +03:00
for (let k = 0; k < modules.length; k += 1) {
const module = modules[k]
const actions = Object.keys(utteranceSamplesObj[module])
describe(`${module} module`, () => {
for (let l = 0; l < actions.length; l += 1) {
const action = actions[l]
2022-09-03 14:12:41 +03:00
const exprs =
utteranceSamplesObj[module][action].utterance_samples
2019-02-10 15:26:50 +03:00
for (let m = 0; m < exprs.length; m += 1) {
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
await nlu.process(exprs[m])
const [obj] = nlu.brain.execute.mock.calls
2019-02-10 15:26:50 +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
}
})
}
})
}
})
}
})