Skip to content

Commit

Permalink
feat: expose simulation type field for dialog and simulation command …
Browse files Browse the repository at this point in the history
…to facilitate Dynamic Routing

cr https://code.amazon.com/reviews/CR-21723955
  • Loading branch information
pbheemag authored and RonWang committed Jul 19, 2021
1 parent 13c0634 commit 5cac3cf
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 16 deletions.
5 changes: 4 additions & 1 deletion lib/clients/smapi-client/resources/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ module.exports = (smapiHandle) => {
);
}

function simulateSkill(skillId, stage, input, newSession, locale, callback) {
function simulateSkill(skillId, stage, input, newSession, locale, simulationType, callback) {
const url = `skills/${skillId}/stages/${stage}/simulations`;
const payload = {
input: {
content: input
},
device: {
locale
},
simulation: {
type: simulationType
}
};
if (newSession) {
Expand Down
5 changes: 3 additions & 2 deletions lib/commands/dialog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DialogCommand extends AbstractCommand {
}

optionalOptions() {
return ['skill-id', 'locale', 'stage', 'replay', 'save-skill-io', 'profile', 'debug'];
return ['skill-id', 'locale', 'stage', 'replay', 'save-skill-io', 'simulation-type', 'profile', 'debug'];
}

handle(cmd, cb) {
Expand Down Expand Up @@ -125,7 +125,8 @@ class DialogCommand extends AbstractCommand {
Messenger.getInstance().info(`Defaulting locale to the first value from the skill manifest: ${firstLocaleFromManifest}`);
}
locale = locale || process.env.ASK_DEFAULT_DEVICE_LOCALE || firstLocaleFromManifest;
callback(null, { skillId, locale, stage, profile, debug, replay: cmd.replay, saveSkillIo, smapiClient, userInputs });
const simulationType = !stringUtils.isNonBlankString(cmd.simulationType) ? 'DEFAULT' : cmd.simulationType;
callback(null, { skillId, locale, stage, profile, debug, replay: cmd.replay, saveSkillIo, smapiClient, userInputs, simulationType });
});
}

Expand Down
10 changes: 10 additions & 0 deletions lib/commands/option-model.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
"alias": null,
"stringInput": "NONE"
},
"simulation-type": {
"name": "simulation-type",
"description": "run skill in the specified simulation mode (currently supports DEFAULT / NFI_ISOLATED_SIMULATION)",
"alias": null,
"stringInput": "REQUIRED",
"rule": [{
"type": "ENUM",
"values": ["DEFAULT", "NFI_ISOLATED_SIMULATION"]
}]
},
"skill-id": {
"name": "skill-id",
"description": "Skill id",
Expand Down
5 changes: 3 additions & 2 deletions lib/controllers/skill-simulation-controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ module.exports = class SkillSimulationController {
if (configuration === undefined) {
throw 'Cannot have an undefined configuration.';
}
const { skillId, locale, stage, profile, saveSkillIo, debug, smapiClient } = configuration;
const { skillId, locale, stage, profile, saveSkillIo, debug, smapiClient, simulationType } = configuration;
this.profile = profile;
this.doDebug = debug;
this.smapiClient = smapiClient || new SmapiClient({ profile: this.profile, doDebug: this.doDebug });
this.skillId = skillId;
this.locale = locale;
this.stage = stage;
this.simulationtype = simulationType;
this.skillIOInstance = new DialogSaveSkillIoFile(saveSkillIo);
}

Expand All @@ -33,7 +34,7 @@ module.exports = class SkillSimulationController {
*/
startSkillSimulation(utterance, newSession, callback) {
this.skillIOInstance.startInvocation({ utterance, newSession });
this.smapiClient.skill.test.simulateSkill(this.skillId, this.stage, utterance, newSession, this.locale, (err, res) => {
this.smapiClient.skill.test.simulateSkill(this.skillId, this.stage, utterance, newSession, this.locale, this.simulationtype, (err, res) => {
if (err) {
return callback(err);
}
Expand Down
15 changes: 11 additions & 4 deletions test/unit/clients/smapi-client-test/resources/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = (smapiClient) => {
const TEST_SKILL_ID = 'skillId';
const TEST_INPUT = 'input';
const TEST_LOCALE = ' locale1 , locale2 ';
const TEST_SIMULATION_TYPE = 'DEFAULT';
const TEST_SIMULATION_ID = 'simulationId';
const TEST_ENDPOINT_REGION = 'endpointRegion';
const TEST_INVOKE_PAYLOAD = 'invokePayload';
Expand All @@ -28,7 +29,7 @@ module.exports = (smapiClient) => {
{
testCase: 'simulate-skill without force new session',
apiFunc: smapiClient.skill.test.simulateSkill,
parameters: [TEST_SKILL_ID, CONSTANTS.SKILL.STAGE.DEVELOPMENT, TEST_INPUT, false, TEST_LOCALE, noop],
parameters: [TEST_SKILL_ID, CONSTANTS.SKILL.STAGE.DEVELOPMENT, TEST_INPUT, false, TEST_LOCALE, TEST_SIMULATION_TYPE, noop],
expectedOptions: {
url: `${CONSTANTS.SMAPI.ENDPOINT}/${CONSTANTS.SMAPI.VERSION.V2}/skills/${TEST_SKILL_ID}`
+ `/stages/${CONSTANTS.SKILL.STAGE.DEVELOPMENT}/simulations`,
Expand All @@ -38,15 +39,18 @@ module.exports = (smapiClient) => {
},
body: {
input: { content: TEST_INPUT },
device: { locale: TEST_LOCALE }
device: { locale: TEST_LOCALE },
simulation: {
type: TEST_SIMULATION_TYPE
}
},
json: true
}
},
{
testCase: 'simulate-skill with force new session',
apiFunc: smapiClient.skill.test.simulateSkill,
parameters: [TEST_SKILL_ID, CONSTANTS.SKILL.STAGE.DEVELOPMENT, TEST_INPUT, true, TEST_LOCALE, noop],
parameters: [TEST_SKILL_ID, CONSTANTS.SKILL.STAGE.DEVELOPMENT, TEST_INPUT, true, TEST_LOCALE, TEST_SIMULATION_TYPE, noop],
expectedOptions: {
url: `${CONSTANTS.SMAPI.ENDPOINT}/${CONSTANTS.SMAPI.VERSION.V2}/skills/${TEST_SKILL_ID}`
+ `/stages/${CONSTANTS.SKILL.STAGE.DEVELOPMENT}/simulations`,
Expand All @@ -57,7 +61,10 @@ module.exports = (smapiClient) => {
body: {
input: { content: TEST_INPUT },
device: { locale: TEST_LOCALE },
session: { mode: FORCE_NEW_SESSION }
session: { mode: FORCE_NEW_SESSION },
simulation: {
type: TEST_SIMULATION_TYPE
}
},
json: true
}
Expand Down
26 changes: 24 additions & 2 deletions test/unit/commands/dialog/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Commands Dialog test - command class test', () => {
expect(instance.name()).eq('dialog');
expect(instance.description()).eq('simulate your skill via an interactive dialog with Alexa');
expect(instance.requiredOptions()).deep.eq([]);
expect(instance.optionalOptions()).deep.eq(['skill-id', 'locale', 'stage', 'replay', 'save-skill-io', 'profile', 'debug']);
expect(instance.optionalOptions()).deep.eq(['skill-id', 'locale', 'stage', 'replay', 'save-skill-io', 'simulation-type', 'profile', 'debug']);
});

describe('# validate command handle', () => {
Expand Down Expand Up @@ -203,7 +203,8 @@ describe('Commands Dialog test - command class test', () => {
// setup
const TEST_CMD_WITH_VALUES = {
stage: '',
replay: DIALOG_REPLAY_FILE_JSON_PATH
replay: DIALOG_REPLAY_FILE_JSON_PATH,
simulationType: 'NFI_ISOLATED_SIMULATION'
};
sinon.stub(profileHelper, 'runtimeProfile').returns(TEST_PROFILE);
sinon.stub(httpClient, 'request').yields(null, { statusCode: 200, body: { manifest } });
Expand All @@ -216,11 +217,32 @@ describe('Commands Dialog test - command class test', () => {
expect(config.replay).equal(DIALOG_REPLAY_FILE_JSON_PATH);
expect(config.skillId).equal('amzn1.ask.skill.1234567890');
expect(config.stage).equal('development');
expect(config.simulationType).equal('NFI_ISOLATED_SIMULATION');
expect(config.userInputs).deep.equal(['hello', 'world']);
done();
});
});

it('| returns valid config with default simulation type', () => {
// setup
const TEST_CMD_WITH_VALUES = {
stage: '',
replay: DIALOG_REPLAY_FILE_JSON_PATH,
};
sinon.stub(profileHelper, 'runtimeProfile').returns(TEST_PROFILE);
// call
const config = instance._getDialogConfig(TEST_CMD_WITH_VALUES);
// verify
expect(config.debug).equal(false);
expect(config.locale).equal('en-US');
expect(config.profile).equal('default');
expect(config.replay).equal(DIALOG_REPLAY_FILE_JSON_PATH);
expect(config.skillId).equal('amzn1.ask.skill.1234567890');
expect(config.stage).equal('development');
expect(config.simulationType).equal('DEFAULT');
expect(config.userInputs).deep.equal(['hello', 'world']);
});

it('| returns error when initialization of DialogReplayFile fails', (done) => {
// setup
const TEST_CMD_WITH_VALUES = {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/controller/dialog-controller/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Controller test - dialog controller test', () => {

it('| test utterance input and response is stored into appropriate caches', (done) => {
// setup
simulateSkillStub.callsArgWith(5, null, { msg: TEST_MSG });
simulateSkillStub.callsArgWith(6, null, { msg: TEST_MSG });
// call
dialogController.startSkillSimulation(TEST_MSG, (err, response) => {
// verify
Expand All @@ -76,7 +76,7 @@ describe('Controller test - dialog controller test', () => {

it('| test an error in request does not effect caches', (done) => {
// setup
simulateSkillStub.callsArgWith(5, TEST_MSG, null);
simulateSkillStub.callsArgWith(6, TEST_MSG, null);
// call
dialogController.startSkillSimulation(TEST_MSG, (err, response) => {
// verify
Expand Down
6 changes: 3 additions & 3 deletions test/unit/controller/skill-simulation-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Controller test - skill simulation controller test', () => {
result: null
}
};
simulateSkillStub.callsArgWith(5, null, TEST_RES);
simulateSkillStub.callsArgWith(6, null, TEST_RES);
// call
simulationController.startSkillSimulation(TEST_MSG, true, (err, response) => {
// verify
Expand All @@ -82,7 +82,7 @@ describe('Controller test - skill simulation controller test', () => {
message: TEST_MSG
}
};
simulateSkillStub.callsArgWith(5, null, TEST_RES);
simulateSkillStub.callsArgWith(6, null, TEST_RES);
// call
simulationController.startSkillSimulation(TEST_MSG, true, (err, response) => {
// verify
Expand All @@ -94,7 +94,7 @@ describe('Controller test - skill simulation controller test', () => {

it('| test for SMAPI.test.simulateSkill gives correct error', (done) => {
// setup
simulateSkillStub.callsArgWith(5, TEST_MSG, null);
simulateSkillStub.callsArgWith(6, TEST_MSG, null);
// call
simulationController.startSkillSimulation(TEST_MSG, true, (err, response) => {
// verify
Expand Down

0 comments on commit 5cac3cf

Please sign in to comment.