This repository has been archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
phrases.test.js
72 lines (69 loc) · 2.16 KB
/
phrases.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* globals test, expect */
const { metadata } = require("./metadata.js");
const { entityTypes } = require("../background/entityTypes.js");
const { PhraseSet } = require("../language/findMatch.js");
const { compile, splitPhraseLines } = require("../language/compiler.js");
const intents = {};
const phrases = [];
for (const category in metadata) {
for (const subname in metadata[category]) {
const data = metadata[category][subname];
const intentName = `${category}.${subname}`;
intents[intentName] = data;
if (!data.match) {
throw new Error(`Intent ${intentName} is missing "match"`);
}
for (const line of splitPhraseLines(data.match)) {
const compiled = compile(line, { entities: entityTypes, intentName });
phrases.push(compiled);
}
}
}
const phraseSet = new PhraseSet(phrases);
for (const intentName in intents) {
const intent = intents[intentName];
let examples = intent.examples || intent.example || [];
examples = examples.filter(e => !e.noTest);
if (!examples || !examples.length) {
continue;
}
test(`test phrases for ${intentName}`, () => {
for (const example of examples) {
let result = phraseSet.match(example.phrase);
if (!result) {
if (intentName === "search.search") {
result = {
name: "search.search",
slots: { query: example.phrase },
parameters: {},
utterance: example.phrase,
fallback: true,
};
} else {
throw new Error(
`Phrase "${example.phrase}" (from ${intentName}) doesn't match anything`
);
}
} else {
result = {
name: result.intentName,
slots: result.stringSlots(),
parameters: result.parameters,
utterance: example.phrase,
fallback: false,
};
}
const expected = {
name: intentName,
utterance: example.phrase,
};
if (example.expectSlots) {
expected.slots = example.expectSlots;
}
if (example.expectParameters) {
expected.parameters = example.expectParameters;
}
expect(result).toMatchObject(expected);
}
});
}