Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
FIX: Change the values of the files generated by bst init when type e…
Browse files Browse the repository at this point in the history
…2e and multi locale. (#690)

* FIX: Change the values of the files generated by bst init when type e2e and multi locale.

* FIX: Replacing all interactions and descriptions with variables from locale

* FIX: Changes for pass code review
  • Loading branch information
maxbespoken committed Feb 10, 2022
1 parent d4e4c44 commit 0a58eb1
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 18 deletions.
47 changes: 32 additions & 15 deletions lib/init/init-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class InitUtil {
private dialogFlow?: string,
private testingExist?: boolean,
private phoneNumber?: string,
) {
) {
this.isMultilocale = locales.split(",").length > 1;
this.projectName = projectName || "voice hello world";
this.handler = handler || "index.js";
Expand Down Expand Up @@ -117,7 +117,7 @@ export class InitUtil {

private getTestSuiteDescription(type: string): string {
if (this.isMultilocale) {
return "testSuiteDescription";
return "$testSuiteDescription";
}

if (type === "unit") {
Expand All @@ -130,7 +130,7 @@ export class InitUtil {

private getTestName(): string {
if (this.isMultilocale) {
return "firstTestName";
return "$firstTestName";
} else if (this.platform === "phone") {
return "Dial and ask for help";
} else if (["whatsapp", "sms"].indexOf(this.platform) > -1) {
Expand All @@ -143,8 +143,8 @@ export class InitUtil {
let expected = "";
let input = "";
if (this.isMultilocale) {
input = "LaunchRequest";
expected = "launchPrompt";
input = "$INVOCATION_UTTERANCE";
expected = "$launchPrompt";
} else {
if (type === "unit") {
input = "LaunchRequest";
Expand Down Expand Up @@ -190,7 +190,8 @@ export class InitUtil {
if (type === "unit") {
input = platform === "alexa" ? "AMAZON.HelpIntent" : "HelpIntent";
} else if (type === "e2e") {
input = "helpUtterance";
input = "$HELP_UTTERANCE";
expectedPrompt = "$helpPrompt";
}
} else {
if (type === "unit") {
Expand Down Expand Up @@ -268,14 +269,8 @@ export class InitUtil {
fs.mkdirSync(`${currentFolder}/test/${type}/locales`);
}

const localizedValues = {
testSuiteDescription: "My first unit test suite",
firstTestName: "Launch and ask for help",
launchPrompt: `Welcome to ${this.projectName}`,
helpPrompt: "What can I help you with?",
helpCardContent: "What can I help you with?",
helpCardTitle: this.projectName,
};
const localizedValues = this.getLocalizedProperties();

await Promise.all(this.locales.split(",").filter((x) => x).map((locale) => {
locale = locale.trim();
const enOnlyComment = locale === "en-US" ? " for en-US" : "";
Expand All @@ -299,6 +294,28 @@ export class InitUtil {
}));
}

getLocalizedProperties(): Object {
if (this.isMultilocale && this.type === "e2e") {
return {
$testSuiteDescription: "My first unit test suite",
$firstTestName: "Launch and ask for help",
$launchPrompt: `okay here's`,
$helpPrompt: "What can I help you with?",
$INVOCATION_UTTERANCE: `Open ${this.projectName} overview`,
$HELP_UTTERANCE: "help"
};
}

return {
testSuiteDescription: "My first unit test suite",
firstTestName: "Launch and ask for help",
launchPrompt: `Welcome to ${this.projectName}`,
helpPrompt: "What can I help you with?",
helpCardContent: "What can I help you with?",
helpCardTitle: this.projectName,
};
}

private async writeFile(path: string, toWrite: any): Promise<void> {
return new Promise((resolve, reject) => {
fs.writeFile(path, toWrite, (err) => {
Expand All @@ -309,5 +326,5 @@ export class InitUtil {
resolve();
});
});
}
}
}
93 changes: 90 additions & 3 deletions test/init/init-util-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as assert from "assert";
import {InitUtil} from "../../lib/init/init-util";
import { InitUtil } from "../../lib/init/init-util";
import * as fs from "fs";
import { get } from "lodash";
import { TestParser } from "skill-testing-ml";
import { Global } from "../../lib/core/global";

describe("init util", function() {
describe("init util", function () {
beforeEach(() => {
if (!fs.existsSync("test/init/temp")) {
fs.mkdirSync("test/init/temp");
Expand Down Expand Up @@ -98,9 +101,93 @@ describe("init util", function() {
});
});

describe("init with multi locale and e2e", () => {
it("Should create only $INVOCATION_UTTERANCE and $HELP_UTTERANCE and use variables $firstTestName and $testSuiteDescription for test and test suite descriptions", async () => {
const locales = ["en-US", "es-PE"];
const projectName = "hello world";
await new InitUtil("e2e", "alexa", "index.js", locales.join(","), projectName, undefined, undefined, false).createFiles();

const existUnitTestFile = "test/unit/index.test.yml";
const existE2eTestFile = "test/e2e/index.e2e.yml";
const existTestingFile = "testing.json";


assert.ok(!fs.existsSync(existUnitTestFile));
assert.ok(fs.existsSync(existE2eTestFile));
assert.ok(fs.existsSync(existTestingFile));

assert.ok(fs.existsSync(existTestingFile));
assert.ok(fs.existsSync(existTestingFile));

// validate files were create
assert.ok(locales.map(f => fs.existsSync(`test/e2e/locales/${f}.yml`)).filter(v => v === false).length === 0);

await Global.loadConfig();
const parser: TestParser = new TestParser("test/e2e/index.e2e.yml");
const testSuite = parser.parse({});
await testSuite.loadLocalizedValues();

assert.ok(testSuite.tests.length === 1);

assert.deepStrictEqual("$testSuiteDescription", get(testSuite, "configuration.description", "").toString());
assert.deepStrictEqual("$firstTestName", get(testSuite, "tests[0].description", "").toString());
assert.deepStrictEqual(["$INVOCATION_UTTERANCE", "$HELP_UTTERANCE"], get(testSuite, "tests[0].interactions", []).map(v => get(v, "utterance", null)));
});

it("Should localized have only these keys: $testSuiteDescription, $firstTestName, $launchPrompt, $helpPrompt when is multi-localized", async () => {
const expectedKey = ["$testSuiteDescription", "$firstTestName", "$launchPrompt", "$helpPrompt", "$INVOCATION_UTTERANCE", "$HELP_UTTERANCE"];
const locales = ["en-US", "es-PE"];
const projectName = "hello world";
await new InitUtil("e2e", "alexa", "index.js", locales.join(","), projectName, undefined, undefined, false).createFiles();

const existUnitTestFile = "test/unit/index.test.yml";
const existE2eTestFile = "test/e2e/index.e2e.yml";
const existTestingFile = "testing.json";


assert.ok(!fs.existsSync(existUnitTestFile));
assert.ok(fs.existsSync(existE2eTestFile));
assert.ok(fs.existsSync(existTestingFile));

assert.ok(fs.existsSync(existTestingFile));
assert.ok(fs.existsSync(existTestingFile));

// validate files were create
assert.ok(locales.map(f => fs.existsSync(`test/e2e/locales/${f}.yml`)).filter(v => v === false).length === 0);

await Global.loadConfig();
const parser: TestParser = new TestParser("test/e2e/index.e2e.yml");
const testSuite = parser.parse({});
await testSuite.loadLocalizedValues();

assert.deepStrictEqual(Object.keys(get(testSuite, `localizedValues[${locales[0]}]`, {})), expectedKey);
});

it("Should not create locale files if only one locale", async () => {
const locales = ["en-US"];
const projectName = "hello world";
await new InitUtil("e2e", "alexa", "index.js", locales.join(","), projectName, undefined, undefined, false).createFiles();

const existUnitTestFile = "test/unit/index.test.yml";
const existE2eTestFile = "test/e2e/index.e2e.yml";
const existTestingFile = "testing.json";


assert.ok(!fs.existsSync(existUnitTestFile));
assert.ok(fs.existsSync(existE2eTestFile));
assert.ok(fs.existsSync(existTestingFile));

assert.ok(fs.existsSync(existTestingFile));
assert.ok(fs.existsSync(existTestingFile));

// validate files were create
assert.ok(locales.map(f => fs.existsSync(`test/e2e/locales/${f}.yml`)).filter(v => v === true).length === 0);
});
});

function deleteFolderRecursive(path: string) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index) {
fs.readdirSync(path).forEach(function (file, index) {
const curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
Expand Down

0 comments on commit 0a58eb1

Please sign in to comment.