From cd0c91b35c56762dd91f396a96795197c9d5fe2c Mon Sep 17 00:00:00 2001 From: Jorge Ricaldi Date: Wed, 20 Nov 2019 23:50:12 -0500 Subject: [PATCH] FEAT-595: changes for bst init, adding dialogFlow and handler questions (#615) --- bin/bst-init.ts | 45 +++++++++++++++++++++++-------------- lib/init/init-util.ts | 42 +++++++++++++++++----------------- test/init/init-util-test.ts | 19 +++------------- 3 files changed, 52 insertions(+), 54 deletions(-) diff --git a/bin/bst-init.ts b/bin/bst-init.ts index 06eed7ed..014253aa 100644 --- a/bin/bst-init.ts +++ b/bin/bst-init.ts @@ -10,7 +10,7 @@ const questions = [ { type: "list", name: "type", - message: "What type of tests are you creating - unit, end-to-end or both:", + message: "What type of tests are you creating - unit, end-to-end:", choices: [ { name: "unit", @@ -20,12 +20,7 @@ const questions = [ name: "end-to-end", value: "e2e", }, - { - name: "both", - value: "both", - }, ], - default: "both", }, { type: "input", @@ -36,13 +31,23 @@ const questions = [ { type: "list", name: "platform", - message: "Are you developing for Alexa, Google, or both?", + message: "Are you developing for Alexa, Google?", choices: [ - "Alexa", - "Google", - "both", - ], - default: "both", + { + name: "Alexa", + value: "alexa", + }, + { + name: "Google", + value: "google", + }, + ], + }, + { + type: "input", + name: "handler", + message: "Please provide the name of your handler file (or leave blank for index.js):", + default: "index.js", }, { type: "input", @@ -54,7 +59,13 @@ const questions = [ type: "input", name: "virtualDevice", message: "For end-to-end tests, we require a virtual device token.\nIf you don't have one or are not sure just leave it blank.\nYou can create virtual devices here: https://apps.bespoken.io/dashboard/virtualdevice\nEnter your token:", - when: (answers: any) => ["e2e", "both"].indexOf(answers["type"]) > -1, + when: (answers: any) => answers["type"].includes("e2e"), + }, + { + type: "input", + name: "dialogFlow", + message: "Please provide the path to your Dialogflow directory\n(if you don't know what this is, please take a look at https://read.bespoken.io/unit-testing/guide-google/#configuration-google-specific):", + when: (answers: any) => answers["type"].includes("unit") && answers["platform"].includes("Alexa"), }, ]; @@ -65,11 +76,11 @@ program console.log(chalk.yellow("We'll set up all you need for you to start testing your voice apps.")); console.log(chalk.yellow("Please tell us:")); prompt(questions).then(answers => { - const platform = answers["platform"] ? answers["platform"].toLowerCase() : ""; - const initUtil = new InitUtil(answers["type"], platform, - answers["locales"], answers["projectName"], answers["virtualDevice"]); + const { type, platform, handler, locales, projectName, virtualDevice, dialogFlow } = answers; + const initUtil = new InitUtil(type, platform, handler, locales, projectName, virtualDevice, dialogFlow); initUtil.createFiles(); + console.log(chalk.green("\nThat's it! We've created your test files for you. To run them, simply type: `bst test`\nYou can learn more advanced topics about testing at https://read.bespoken.io")); }); }); -program.parse(process.argv); \ No newline at end of file +program.parse(process.argv); diff --git a/lib/init/init-util.ts b/lib/init/init-util.ts index b279bf63..d433e0a9 100644 --- a/lib/init/init-util.ts +++ b/lib/init/init-util.ts @@ -5,25 +5,24 @@ export class InitUtil { private isMultilocale: boolean; constructor( - public type: string, - public platform: string, - public locales: string, - public projectName: string, - public virtualDeviceToken?: string + private type: string, + private platform: string, + private handler: string, + private locales: string, + private projectName: string, + private virtualDeviceToken?: string, + private dialogFlow?: string, ) { this.isMultilocale = locales.split(",").length > 1; + this.projectName = projectName || "voice hello world"; + this.handler = handler || "index.js"; + this.locales = locales || "en-US"; + this.virtualDeviceToken = virtualDeviceToken || "[your virtual device token goes here]"; + this.dialogFlow = dialogFlow || "Path to your Dialogflow directory. Read more at https://read.bespoken.io/unit-testing/guide-google/#configuration"; } public async createFiles(): Promise { - // utterances will change on unit test for google, - // if plaform both was selected, utterances will have google's form - const finalPlatform = ["both", "google"].indexOf(this.platform) > -1 ? "google" : this.platform; - if (this.type === "both") { - await this.createTestFilesForType("unit", finalPlatform); - await this.createTestFilesForType("e2e", finalPlatform); - } else { - await this.createTestFilesForType(this.type, finalPlatform); - } + await this.createTestFilesForType(this.type, this.platform); } private async createTestFilesForType(type: string, platform: string): Promise { @@ -39,7 +38,7 @@ export class InitUtil { await this.createMultilocaleFiles(type); const ymlContent = this.getYmlContent(type, platform); - const testingFileContent = this.getTestingJson(type); + const testingFileContent = this.getTestingJson(); await this.writeFile(`${testFolder}/index.test.yml`, ymlContent); await this.writeFile(`${currentFolder}/test/${type}/testing.json`, JSON.stringify(testingFileContent, null, 4)); } @@ -164,20 +163,21 @@ export class InitUtil { }; } - private getTestingJson(type: string): any { + private getTestingJson(): any { const testingJsonForUnit = { - handler: "relative or absolute path to your voice app entry point", + handler: this.handler, locales: this.locales, }; const testingJsonForE2e = { - virtualDeviceToken: this.virtualDeviceToken || "[your virtual device token goes here]", - locales: this.locales, + virtualDeviceToken: this.virtualDeviceToken, + ...testingJsonForUnit, type: "e2e", }; if (this.platform === "google") { testingJsonForUnit["platform"] = "google"; + testingJsonForUnit["dialogFlow"] = this.dialogFlow; } - return type === "unit" ? testingJsonForUnit : testingJsonForE2e; + return this.type === "unit" ? testingJsonForUnit : testingJsonForE2e; } private getHeaderComment(type: string): string { @@ -254,4 +254,4 @@ export class InitUtil { }); }); } -} \ No newline at end of file +} diff --git a/test/init/init-util-test.ts b/test/init/init-util-test.ts index 58171ffc..a97a3ae9 100644 --- a/test/init/init-util-test.ts +++ b/test/init/init-util-test.ts @@ -17,7 +17,7 @@ describe("init util", function() { describe("createFilesStructure()", () => { it("create file structure for unit tests", async () => { - await new InitUtil("unit", "alexa", "en-US", "hello world").createFiles(); + await new InitUtil("unit", "alexa", "index.js", "en-US", "hello world").createFiles(); const existUnitTestFile = fs.existsSync("test/unit/index.test.yml"); const existUnitTestingFile = fs.existsSync("test/unit/testing.json"); @@ -31,7 +31,7 @@ describe("init util", function() { }); it("create file structure for e2e tests", async () => { - await new InitUtil("e2e", "alexa", "en-US", "hello world").createFiles(); + await new InitUtil("e2e", "alexa", "index.js", "en-US", "hello world").createFiles(); const existUnitTestFile = fs.existsSync("test/unit/index.test.yml"); const existUnitTestingFile = fs.existsSync("test/unit/testing.json"); @@ -42,19 +42,6 @@ describe("init util", function() { assert.equal(existE2eTestFile, true); assert.equal(existE2eTestingFile, true); }); - - it("create file structure for unit and e2e tests", async () => { - await new InitUtil("both", "alexa", "en-US", "hello world").createFiles(); - - const existUnitTestFile = fs.existsSync("test/unit/index.test.yml"); - const existUnitTestingFile = fs.existsSync("test/unit/testing.json"); - const existE2eTestFile = fs.existsSync("test/e2e/index.test.yml"); - const existE2eTestingFile = fs.existsSync("test/e2e/testing.json"); - assert.equal(existUnitTestFile, true); - assert.equal(existUnitTestingFile, true); - assert.equal(existE2eTestFile, true); - assert.equal(existE2eTestingFile, true); - }); }); function deleteFolderRecursive(path: string) { @@ -70,4 +57,4 @@ describe("init util", function() { fs.rmdirSync(path); } } -}); \ No newline at end of file +});