Skip to content

Commit

Permalink
fix: preserve the root files when creating a PVA bot (microsoft#4611)
Browse files Browse the repository at this point in the history
* fix: preserve the root file when create a PVA bot

* remove lowercase

* update the designer

* fix e2e test
  • Loading branch information
lei9444 committed Nov 2, 2020
1 parent ee344bd commit d0b5991
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Composer/cypress/integration/NotificationPage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ context('Notification Page', () => {
cy.findByTestId('LeftNav-CommandBarButtonNotifications').click();

cy.findByTestId('notifications-table-view').within(() => {
cy.findAllByText('__testtodobotwithluissample.en-us.lu').should('exist').first().dblclick();
cy.findAllByText('__TestToDoBotWithLuisSample.en-us.lu').should('exist').first().dblclick();
});

cy.findAllByText('__TestToDoBotWithLuisSample').should('exist');
Expand Down
2 changes: 0 additions & 2 deletions Composer/cypress/integration/ToDoBot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ context('ToDo Bot', () => {
before(() => {
cy.visit('/home');
cy.createBot('TodoSample');
cy.findByTestId('WelcomeModalCloseIcon').click();
cy.findByText('Yes').click();
});

it('can open the main dialog', () => {
Expand Down
36 changes: 19 additions & 17 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export class BotProject implements IBotProject {
return files;
}

public get rootDialogId() {
const mainDialogFile = this.dialogFiles.find((file) => !file.relativePath.includes('/'));

return Path.basename(mainDialogFile?.name ?? '', '.dialog');
}

public get formDialogSchemaFiles() {
const files: FileInfo[] = [];
this.files.forEach((file) => {
Expand Down Expand Up @@ -350,24 +356,20 @@ export class BotProject implements IBotProject {
public updateBotInfo = async (name: string, description: string, preserveRoot = false) => {
const mainDialogFile = this.dialogFiles.find((file) => !file.relativePath.includes('/'));
if (!mainDialogFile) return;
const botName = name.trim().toLowerCase();

const botName = name.trim();

const { relativePath } = mainDialogFile;
const content = JSON.parse(mainDialogFile.content);
if (!content.$designer) return;
const oldDesigner = content.$designer;
let newDesigner;
if (oldDesigner && oldDesigner.id) {
newDesigner = {
...oldDesigner,
name,
description,
};
} else {
newDesigner = getNewDesigner(name, description);
}
content.$designer = newDesigner;
content.id = name;
const updatedContent = autofixReferInDialog(botName, JSON.stringify(content, null, 2));

const { $designer } = content;

content.$designer = $designer?.id ? { ...$designer, name, description } : getNewDesigner(botName, description);

content.id = preserveRoot ? Path.basename(mainDialogFile.name, '.dialog') : botName;

const updatedContent = autofixReferInDialog(content.id, JSON.stringify(content, null, 2));

await this._updateFile(relativePath, updatedContent);

for (const botProjectFile of this.botProjectFiles) {
Expand Down Expand Up @@ -434,7 +436,7 @@ export class BotProject implements IBotProject {
this._validateFileContent(name, content);
const botName = this.name;
const defaultLocale = this.settings?.defaultLanguage || defaultLanguage;
const relativePath = defaultFilePath(botName, defaultLocale, filename);
const relativePath = defaultFilePath(botName, defaultLocale, filename, this.rootDialogId);
const file = this.files.get(filename);
if (file) {
throw new Error(`${filename} dialog already exist`);
Expand Down
18 changes: 12 additions & 6 deletions Composer/packages/server/src/models/bot/botStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export const parseFileName = (name: string, defaultLocale: string) => {
export const isRecognizer = (fileName: string) => fileName.endsWith('.lu.dialog') || fileName.endsWith('.qna.dialog');
export const isCrossTrainConfig = (fileName: string) => fileName.endsWith('cross-train.config.json');

export const defaultFilePath = (botName: string, defaultLocale: string, filename: string): string => {
export const defaultFilePath = (
botName: string,
defaultLocale: string,
filename: string,
rootDialogId = ''
): string => {
const BOTNAME = botName.toLowerCase();
const CommonFileId = 'common';

Expand All @@ -86,8 +91,8 @@ export const defaultFilePath = (botName: string, defaultLocale: string, filename

// now recognizer extension is .lu.dialog or .qna.dialog
if (isRecognizer(filename)) {
const isRoot = filename.startsWith(botName.toLowerCase());
const dialogId = filename.slice(0, filename.indexOf('.'));
const dialogId = filename.split('.')[0];
const isRoot = filename.startsWith(botName) || (rootDialogId && filename.startsWith(rootDialogId));
if (isRoot) {
return templateInterpolate(BotStructureTemplate.recognizer, {
RECOGNIZERNAME: filename,
Expand Down Expand Up @@ -170,14 +175,15 @@ export const defaultFilePath = (botName: string, defaultLocale: string, filename
// when create/saveAs bot, serialize entry dialog/lg/lu
export const serializeFiles = async (fileStorage, rootPath, botName, preserveRoot = false) => {
const entryPatterns = [
templateInterpolate(BotStructureTemplate.lg, { LOCALE: '*', BOTNAME: '*' }),
templateInterpolate(BotStructureTemplate.lu, { LOCALE: '*', BOTNAME: '*' }),
templateInterpolate(BotStructureTemplate.qna, { LOCALE: '*', BOTNAME: '*' }),
templateInterpolate(BotStructureTemplate.dialogSchema, { BOTNAME: '*' }),
templateInterpolate(BotStructureTemplate.botProject, { BOTNAME: '*' }),
];

if (!preserveRoot) {
entryPatterns.push(templateInterpolate(BotStructureTemplate.entry, { BOTNAME: '*' }));
entryPatterns.push(templateInterpolate(BotStructureTemplate.lg, { LOCALE: '*', BOTNAME: '*' }));
entryPatterns.push(templateInterpolate(BotStructureTemplate.lu, { LOCALE: '*', BOTNAME: '*' }));
entryPatterns.push(templateInterpolate(BotStructureTemplate.qna, { LOCALE: '*', BOTNAME: '*' }));
}

for (const pattern of entryPatterns) {
Expand Down

0 comments on commit d0b5991

Please sign in to comment.