Skip to content

Commit

Permalink
removed generating of intent names
Browse files Browse the repository at this point in the history
  • Loading branch information
misyak committed Dec 22, 2016
1 parent 75b9ec7 commit 4253a21
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
18 changes: 3 additions & 15 deletions src/create-intent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const _ = require('lodash');
const bases = require('bases');
const builtInIntentsMap = require('./built-in-intents-map');
const validator = require('./validator');
const parseError = require('./error-handler').parseError;
Expand All @@ -17,12 +16,11 @@ module.exports = (intents, name, richUtterances, handler) => {
// Convert utterances to array
richUtterances = _.isArray(richUtterances) ? richUtterances : [richUtterances];

// If intent name is not specified, try to generate unique one
if (!name) {
name = generateIntentName(intents);

const e = parseError(new Error(`All intents must have name.`));
throw e;
} else if (!validator.isNameValid(name)) {
const e = parseError(new Error(`Intent name ${name} is invalid. Only lowercase and uppercase letters are allowed`));
const e = parseError(new Error(`Intent name ${name} is invalid. Only lowercase and uppercase letters are allowed.`));
throw e;
} else if (builtInIntentsMap[name]) {
// If built-in intent name was used map intent name to it
Expand All @@ -42,13 +40,3 @@ module.exports = (intents, name, richUtterances, handler) => {
handler: handler
};
};

const generateIntentName = (intents) => {
let position = 0;
let generatedName;

// While generated name is not already used and generatedName is not built-in intent (just in case)
while (intents[(generatedName = bases.toBase52(position++))] && !builtInIntentsMap[generatedName]);

return generatedName;
};
19 changes: 14 additions & 5 deletions test/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('basic app handler', () => {
});

it('should handle intent with slots', (done) => {
const request = alexia.createIntentRequest(intents[5].name, {name: 'Borimir'}, null, false, 'appId1');
const request = alexia.createIntentRequest(intents[4].name, {name: 'Borimir'}, null, false, 'appId1');

app.handle(request, (response) => {
expect(response.response.outputSpeech.text).equal('okay sir your name is Borimir');
Expand Down Expand Up @@ -126,8 +126,8 @@ describe('basic app handler', () => {
});
});

it('should handle async intent', (done) => {
const request = alexia.createIntentRequest(intents[12].name, null, null, false, 'appId1');
it('should handle Async intent', (done) => {
const request = alexia.createIntentRequest(intents[11].name, null, null, false, 'appId1');

app.handle(request, (response) => {
expect(response.response.outputSpeech.text).equal('I just did stuff asynchronously. Thank you for this opportunity');
Expand Down Expand Up @@ -224,13 +224,23 @@ describe('basic app handler', () => {
}
});

it('should not create intent without name', () => {
try {
const app2 = alexia.createApp('App2');
app2.intent(null, 'Name', () => 'Must have name');
throw new Error('Name of intent was not specified.');
} catch (e) {
expect(e).to.include('All intents must have name.');
}
});

it('should not create intent with invalid name', () => {
try {
const app2 = alexia.createApp('App2');
app2.intent('Mega -.- Intent o/', 'Hi', () => 'Nope bye');
throw new Error('App was handled with invalid intent name');
} catch (e) {
expect(e).to.include('Intent name Mega -.- Intent o/ is invalid. Only lowercase and uppercase letters are allowed');
expect(e).to.include('Intent name Mega -.- Intent o/ is invalid. Only lowercase and uppercase letters are allowed.');
}
});

Expand Down Expand Up @@ -298,5 +308,4 @@ describe('basic app handler', () => {
done();
});
});

});
20 changes: 9 additions & 11 deletions test/mock/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ exports.intentSchema = {
intents: [
{intent: 'FirstIntent'},
{intent: 'NamedIntent'},
{intent: 'a'},
{intent: 'b'},
{intent: 'Multiple'},
{
intent: 'c',
intent: 'Age',
slots: [{name: 'age', type: 'AMAZON.NUMBER'}]
},
{
intent: 'd',
intent: 'Name',
slots: [{name: 'name', type: 'Name'}]
},
{intent: 'AMAZON.StopIntent'},
Expand All @@ -20,7 +19,7 @@ exports.intentSchema = {
{intent: 'IntentA'},
{intent: 'IntentB'},
{intent: 'IntentC'},
{intent: 'e'},
{intent: 'Async'},
{intent: 'AnotherCardIntentSample'},
{intent: 'IntentWithoutUtterances'}
]
Expand All @@ -30,18 +29,17 @@ exports.utterances = [
'FirstIntent utterance',
'NamedIntent utteranceB',
'NamedIntent utteranceC',
'a utterance for intent without name',
'b multiple',
'b utterances',
'c I am {age} years old',
'd My name is {name}',
'Multiple multiple',
'Multiple utterances',
'Age I am {age} years old',
'Name My name is {name}',
'AMAZON.StopIntent one utterance',
'AMAZON.HelpIntent two utterances',
'AMAZON.HelpIntent yup',
'IntentA another utterance',
'IntentB another utterance',
'IntentC another utterance',
'e async response',
'Async async response',
'AnotherCardIntentSample card intent'
];

Expand Down
12 changes: 4 additions & 8 deletions test/test-apps/basic-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ app.intent('NamedIntent', ['utteranceB', 'utteranceC'], () => {
return 'All good';
});

app.intent(null, 'utterance for intent without name', () => {
return 'All good sir';
});

app.intent(null, ['multiple', 'utterances'], () => {
app.intent('Multiple', ['multiple', 'utterances'], () => {
return {
text: 'All good sir again'
};
});

app.intent(null, 'I am {age:Number} years old', (slots) => {
app.intent('Age', 'I am {age:Number} years old', (slots) => {
return `okay sir you are ${slots.age} years old`;
});

app.customSlot('Name', ['Borimir', 'Vlasto']);
app.intent(null, 'My name is {name:Name}', (slots) => {
app.intent('Name', 'My name is {name:Name}', (slots) => {
return `okay sir your name is ${slots.name}`;
});

Expand Down Expand Up @@ -71,7 +67,7 @@ app.intent('IntentC', 'another utterance', (slots, attrs) => {
};
});

app.intent(null, 'async response', (slots, attrs, data, done) => {
app.intent('Async', 'async response', (slots, attrs, data, done) => {
setTimeout(() => {
done('I just did stuff asynchronously. Thank you for this opportunity');
}, 120);
Expand Down

0 comments on commit 4253a21

Please sign in to comment.