Skip to content

Commit

Permalink
Fixed issue where userId was out of sync between bot and user.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyanziano committed Mar 14, 2019
1 parent ccfe83c commit 2ec5ef4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/app/main/src/commands/emulatorCommands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ import { newBot, newEndpoint, SharedConstants } from '@bfemulator/app-shared';
import { Conversation } from '@bfemulator/emulator-core';

import * as store from '../botData/store';
import { getStore as getSettingsStore } from '../settingsData/store';
import { emulator } from '../emulator';
import * as utils from '../utils';
import * as botHelpers from '../botHelpers';
import { bot } from '../botData/reducers/bot';
import * as BotActions from '../botData/actions/botActions';
import { TelemetryService } from '../telemetry';
import { mainWindow } from '../main';
import { setCurrentUser } from '../settingsData/actions/userActions';

import { registerCommands } from './emulatorCommands';

Expand Down Expand Up @@ -129,10 +131,14 @@ jest.mock('../emulator', () => ({
},
}));

let mockCallsMade = [];
jest.mock('../main', () => ({
mainWindow: {
commandService: {
call: async () => true,
call: async (commandName, ...args) => {
mockCallsMade.push({ commandName, args });
return Promise.resolve(true);
},
remoteCall: async () => true,
},
browserWindow: {},
Expand Down Expand Up @@ -388,6 +394,7 @@ describe('The emulatorCommands', () => {
mockUsers = { users: {} };
mockTrackEvent = jest.fn(() => Promise.resolve());
TelemetryService.trackEvent = mockTrackEvent;
mockCallsMade = [];
});

beforeAll(() => {
Expand Down Expand Up @@ -480,11 +487,16 @@ describe('The emulatorCommands', () => {
});

it('should set current user', async () => {
const dispatchSpy = jest.spyOn(getSettingsStore(), 'dispatch');
await mockCommandRegistry.getCommand(SharedConstants.Commands.Emulator.SetCurrentUser).handler('userId123');

expect(mockUsers.currentUserId).toBe('userId123');
expect(mockUsers.users.userId123).toEqual({
id: 'userId123',
name: 'User',
});
expect(dispatchSpy).toHaveBeenCalledWith(setCurrentUser({ id: 'userId123', name: 'User' }));
expect(mockCallsMade).toHaveLength(1);
expect(mockCallsMade[0].commandName).toBe(SharedConstants.Commands.Settings.PushClientAwareSettings);
});
});
12 changes: 9 additions & 3 deletions packages/app/main/src/commands/emulatorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ import { getStore } from '../botData/store';
import { getActiveBot, getBotInfoByPath, patchBotsJson, toSavableBot } from '../botHelpers';
import { emulator } from '../emulator';
import { mainWindow } from '../main';
import { getStore as getSettingsStore } from '../settingsData/store';
import { dispatch, getStore as getSettingsStore } from '../settingsData/store';
import { parseActivitiesFromChatFile, showSaveDialog, writeFile } from '../utils';
import { cleanupId as cleanupActivityChannelAccountId, CustomActivity } from '../utils/conversation';
import { botProjectFileWatcher } from '../watchers';
import { TelemetryService } from '../telemetry';
import { setCurrentUser } from '../settingsData/actions/userActions';

/** Registers emulator (actual conversation emulation logic) commands */
export function registerCommands(commandRegistry: CommandRegistryImpl) {
Expand Down Expand Up @@ -217,11 +218,16 @@ export function registerCommands(commandRegistry: CommandRegistryImpl) {

// ---------------------------------------------------------------------------
// Sets the current user id (in memory)
commandRegistry.registerCommand(Commands.SetCurrentUser, (userId: string) => {
commandRegistry.registerCommand(Commands.SetCurrentUser, async (userId: string) => {
const { facilities } = emulator.framework.server.botEmulator;
const { users } = facilities;
const user = { id: userId, name: 'User' };
users.currentUserId = userId;
users.users[userId] = { id: userId, name: 'User' };
users.users[userId] = user;
facilities.users = users;

// update the settings state on both main and client
dispatch(setCurrentUser(user));
await mainWindow.commandService.call(SharedConstants.Commands.Settings.PushClientAwareSettings);
});
}

0 comments on commit 2ec5ef4

Please sign in to comment.