Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed opening a livechat session via protocol. #1446

Merged
merged 1 commit into from Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [client] Fixed issue where tab icon glyphs weren't working on Mac in PR [1428](https://github.com/Microsoft/BotFramework-Emulator/pull/1428)
- [client] Fixed issue where cancelling out of opening a transcript was creating a broken livechat window in PR [1441](https://github.com/Microsoft/BotFramework-Emulator/pull/1441)
- [client] Fixed invisible scrollbar styling in log panel in PR [1442](https://github.com/Microsoft/BotFramework-Emulator/pull/1442)
- [main] Fixed issue where opening a livechat or bot via protocol wasn't working because ngrok wasn't being started on startup in PR [1446](https://github.com/Microsoft/BotFramework-Emulator/pull/1446)

## Removed
- [main] Removed custom user agent string from outgoing requests in PR [1427](https://github.com/Microsoft/BotFramework-Emulator/pull/1427)
Expand Down
48 changes: 47 additions & 1 deletion packages/app/main/src/protocolHandler.spec.ts
Expand Up @@ -77,9 +77,13 @@ jest.mock('./settingsData/store', () => ({
},
}),
}));

let mockGetSpawnStatus: any = jest.fn(() => ({ triedToSpawn: true }));
const mockRecycle = jest.fn(() => null);
const mockEmulator = {
ngrok: {
getSpawnStatus: () => ({ triedToSpawn: true }),
getSpawnStatus: () => mockGetSpawnStatus(),
recycle: () => mockRecycle(),
},
};
jest.mock('./emulator', () => ({
Expand Down Expand Up @@ -134,6 +138,8 @@ describe('Protocol handler tests', () => {
statusCode: 200,
body: '["activity1", "activity2", "activity3"]',
};
mockRecycle.mockClear();
mockGetSpawnStatus.mockClear();
});

afterAll(() => {
Expand Down Expand Up @@ -283,8 +289,12 @@ describe('Protocol handler tests', () => {
const overrides = { endpoint: parseEndpointOverrides(protocol.parsedArgs) };
const overriddenBot = applyBotConfigOverrides(mockOpenedBot, overrides);

// ngrok should be kick-started if it hasn't tried to spawn yet
mockGetSpawnStatus = jest.fn(() => ({ triedToSpawn: false }));

await ProtocolHandler.openBot(protocol);

expect(mockRecycle).toHaveBeenCalled();
expect(mockCallsMade).toHaveLength(2);
expect(mockCallsMade[0].commandName).toBe(SharedConstants.Commands.Bot.Open);
expect(mockCallsMade[0].args).toEqual(['path/to/bot.bot', 'someSecret']);
Expand Down Expand Up @@ -355,6 +365,22 @@ describe('Protocol handler tests', () => {
});
});

it('should throw if ngrok failed to spawn while opening a bot', async () => {
try {
const protocol = {
parsedArgs: {
id: 'someIdOverride',
path: 'path/to/bot.bot',
secret: 'someSecret',
},
};
mockGetSpawnStatus = jest.fn(() => ({ triedToSpawn: true, err: 'Some ngrok error' }));
await ProtocolHandler.openBot(protocol);
} catch (e) {
expect(e).toEqual(new Error('Error while trying to spawn ngrok instance: Some ngrok error'));
}
});

it('should open a livechat if ngrok is running', async () => {
const protocol = {
parsedArgs: {
Expand All @@ -374,8 +400,12 @@ describe('Protocol handler tests', () => {
mockEndpoint.name = 'New livechat';
mockedBot.services.push(mockEndpoint);

// ngrok should be kick-started if it hasn't tried to spawn yet
mockGetSpawnStatus = jest.fn(() => ({ triedToSpawn: false }));

await ProtocolHandler.openLiveChat(protocol);

expect(mockRecycle).toHaveBeenCalled();
expect(mockCallsMade).toHaveLength(1);
expect(mockCallsMade[0].commandName).toBe(SharedConstants.Commands.Bot.RestartEndpointService);
expect(mockCallsMade[0].args).toEqual([]);
Expand Down Expand Up @@ -446,6 +476,22 @@ describe('Protocol handler tests', () => {
expect(mockRemoteCallsMade[0].args).toEqual([mockEndpoint]);
});

it('should throw if ngrok failed to spawn while opening a livechat', async () => {
try {
const protocol = {
parsedArgs: {
botUrl: 'someUrl',
msaAppId: 'someAppId',
msaPassword: 'somePw',
},
};
mockGetSpawnStatus = jest.fn(() => ({ triedToSpawn: true, err: 'Some ngrok error' }));
await ProtocolHandler.openBot(protocol);
} catch (e) {
expect(e).toEqual(new Error('Error while trying to spawn ngrok instance: Some ngrok error'));
}
});

it('should open a transcript from a url', async () => {
const protocol = {
parsedArgs: { url: 'https://www.test.com/convo1.transcript' },
Expand Down
18 changes: 14 additions & 4 deletions packages/app/main/src/protocolHandler.ts
Expand Up @@ -202,8 +202,13 @@ export const ProtocolHandler = new class ProtocolHandlerImpl implements Protocol
const appSettings: FrameworkSettings = getSettings().framework;

if (appSettings.ngrokPath) {
const ngrokSpawnStatus = Emulator.getInstance().ngrok.getSpawnStatus();
if (!ngrokSpawnStatus.triedToSpawn || (ngrokSpawnStatus.triedToSpawn && ngrokSpawnStatus.err)) {
let ngrokSpawnStatus = Emulator.getInstance().ngrok.getSpawnStatus();
// if ngrok hasn't spawned yet, we need to start it up
if (!ngrokSpawnStatus.triedToSpawn) {
await Emulator.getInstance().ngrok.recycle();
}
ngrokSpawnStatus = Emulator.getInstance().ngrok.getSpawnStatus();
if (ngrokSpawnStatus.triedToSpawn && ngrokSpawnStatus.err) {
throw new Error(`Error while trying to spawn ngrok instance: ${ngrokSpawnStatus.err || ''}`);
}

Expand Down Expand Up @@ -304,8 +309,13 @@ export const ProtocolHandler = new class ProtocolHandlerImpl implements Protocol

const appSettings: FrameworkSettings = getSettings().framework;
if (appSettings.ngrokPath) {
const ngrokSpawnStatus = Emulator.getInstance().ngrok.getSpawnStatus();
if (!ngrokSpawnStatus.triedToSpawn || (ngrokSpawnStatus.triedToSpawn && ngrokSpawnStatus.err)) {
let ngrokSpawnStatus = Emulator.getInstance().ngrok.getSpawnStatus();
// if ngrok hasn't spawned yet, we need to start it up
if (!ngrokSpawnStatus.triedToSpawn) {
await Emulator.getInstance().ngrok.recycle();
}
ngrokSpawnStatus = Emulator.getInstance().ngrok.getSpawnStatus();
if (ngrokSpawnStatus.triedToSpawn && ngrokSpawnStatus.err) {
throw new Error(`Error while trying to spawn ngrok instance: ${ngrokSpawnStatus.err || ''}`);
}

Expand Down