Skip to content

Commit

Permalink
Properly closed existing bot when switching into debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwilaby committed Apr 24, 2019
1 parent 51a9c79 commit 2c4ca36
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
10 changes: 6 additions & 4 deletions packages/app/client/src/commands/uiCommands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ import {
SecretPromptDialogContainer,
} from '../ui/dialogs';
import { CommandServiceImpl } from '../platform/commands/commandServiceImpl';
import { BotActionType } from '../data/action/botActions';
import { ExplorerActions } from '../data/action/explorerActions';
import { SWITCH_DEBUG_MODE } from '../data/action/debugModeAction';
import { ActiveBotHelper } from '../ui/helpers/activeBotHelper';

import { registerCommands } from './uiCommands';

Expand Down Expand Up @@ -156,9 +156,11 @@ describe('the uiCommands', () => {
dispatchedActions.push(action);
return action;
};
registry.getCommand(Commands.SwitchDebugMode).handler(DebugMode.Sidecar);
expect(dispatchedActions.length).toBe(3);
[BotActionType.close, ExplorerActions.Show, SWITCH_DEBUG_MODE].forEach((type, index) =>
const closeActiveBotSpy = jest.spyOn(ActiveBotHelper, 'closeActiveBot').mockResolvedValueOnce(true);
await registry.getCommand(Commands.SwitchDebugMode).handler(DebugMode.Sidecar);
expect(dispatchedActions.length).toBe(2);
expect(closeActiveBotSpy).toHaveBeenCalled();
[ExplorerActions.Show, SWITCH_DEBUG_MODE].forEach((type, index) =>
expect(type).toEqual(dispatchedActions[index].type)
);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/app/client/src/commands/uiCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { ServiceTypes } from 'botframework-config/lib/schema';

import * as Constants from '../constants';
import { azureArmTokenDataChanged, beginAzureAuthWorkflow, invalidateArmToken } from '../data/action/azureAuthActions';
import { closeBot } from '../data/action/botActions';
import { switchDebugMode } from '../data/action/debugModeAction';
import * as EditorActions from '../data/action/editorActions';
import * as NavBarActions from '../data/action/navBarActions';
Expand All @@ -63,6 +62,7 @@ import {
import * as ExplorerActions from '../data/action/explorerActions';
import { closeConversation } from '../data/action/chatActions';
import { close } from '../data/action/editorActions';
import { ActiveBotHelper } from '../ui/helpers/activeBotHelper';

/** Register UI commands (toggling UI) */
export function registerCommands(commandRegistry: CommandRegistry) {
Expand Down Expand Up @@ -135,12 +135,12 @@ export function registerCommands(commandRegistry: CommandRegistry) {

// ---------------------------------------------------------------------------
// Debug mode from main
commandRegistry.registerCommand(UI.SwitchDebugMode, (debugMode: DebugMode) => {
commandRegistry.registerCommand(UI.SwitchDebugMode, async (debugMode: DebugMode) => {
const {
editor: { editors, activeEditor },
} = store.getState();
const { documents } = editors[activeEditor];
store.dispatch(closeBot());
await ActiveBotHelper.closeActiveBot();
store.dispatch(ExplorerActions.showExplorer(debugMode !== DebugMode.Sidecar));
store.dispatch(switchDebugMode(debugMode));
// Close all active conversations - this is a clean wipe of all active conversations
Expand Down
28 changes: 13 additions & 15 deletions packages/app/client/src/ui/helpers/activeBotHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const ActiveBotHelper = new class {
/** Sets a bot as active
* @param bot Bot to set as active
*/
async setActiveBot(bot: BotConfigWithPath): Promise<any> {
async setActiveBot(bot: BotConfigWithPath): Promise<void> {
try {
// set the bot as active on the server side
const botDirectory = await CommandServiceImpl.remoteCall(SharedConstants.Commands.Bot.SetActive, bot);
Expand All @@ -104,21 +104,19 @@ export const ActiveBotHelper = new class {
}

/** tell the server-side the active bot is now closed */
closeActiveBot(): Promise<any> {
return CommandServiceImpl.remoteCall(Bot.Close)
.then(() => {
store.dispatch(BotActions.closeBot());
CommandServiceImpl.remoteCall(SharedConstants.Commands.Electron.SetTitleBar, '');
})
.catch(err => {
const errMsg = `Error while closing active bot: ${err}`;
const notification = newNotification(errMsg);
store.dispatch(beginAdd(notification));
throw new Error(errMsg);
});
async closeActiveBot(): Promise<void> {
try {
await CommandServiceImpl.remoteCall(Bot.Close);
store.dispatch(BotActions.closeBot());
await CommandServiceImpl.remoteCall(SharedConstants.Commands.Electron.SetTitleBar, '');
} catch (err) {
const errMsg = `Error while closing active bot: ${err}`;
const notification = newNotification(errMsg);
store.dispatch(beginAdd(notification));
}
}

async botAlreadyOpen(): Promise<any> {
async botAlreadyOpen(): Promise<void> {
// TODO - localization
return await CommandServiceImpl.remoteCall(Electron.ShowMessageBox, true, {
buttons: ['OK'],
Expand All @@ -131,7 +129,7 @@ export const ActiveBotHelper = new class {
});
}

async confirmAndCreateBot(botToCreate: BotConfigWithPath, secret: string): Promise<any> {
async confirmAndCreateBot(botToCreate: BotConfigWithPath, secret: string): Promise<void> {
// prompt the user to confirm the switch
const result = await this.confirmSwitchBot();

Expand Down
11 changes: 3 additions & 8 deletions packages/app/main/src/botHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,9 @@ export async function removeBotFromList(botPath: string): Promise<void> {
}

export function getTranscriptsPath(activeBot: BotConfigWithPath, conversation: Conversation): string {
if (conversation.mode === 'livechat-url') {
if (!activeBot || conversation.mode === 'livechat-url') {
return path.join(electron.app.getPath('downloads'), './transcripts');
}

if (activeBot) {
const dirName = path.dirname(activeBot.path);
return path.join(dirName, './transcripts');
}

return '/';
const dirName = path.dirname(activeBot.path);
return path.join(dirName, './transcripts');
}

0 comments on commit 2c4ca36

Please sign in to comment.