From bfdbda85e6e5c895d44dfca8b734b27446f17c8e Mon Sep 17 00:00:00 2001 From: Tony Anziano Date: Thu, 18 Apr 2019 16:01:50 -0700 Subject: [PATCH] Fixed opening a livechat session via protocol. --- CHANGELOG.md | 1 + packages/app/main/src/protocolHandler.spec.ts | 48 ++++++++++++++++++- packages/app/main/src/protocolHandler.ts | 18 +++++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a3236d68..9c03586d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/app/main/src/protocolHandler.spec.ts b/packages/app/main/src/protocolHandler.spec.ts index cc0ffbf81..e42fa035f 100644 --- a/packages/app/main/src/protocolHandler.spec.ts +++ b/packages/app/main/src/protocolHandler.spec.ts @@ -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', () => ({ @@ -134,6 +138,8 @@ describe('Protocol handler tests', () => { statusCode: 200, body: '["activity1", "activity2", "activity3"]', }; + mockRecycle.mockClear(); + mockGetSpawnStatus.mockClear(); }); afterAll(() => { @@ -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']); @@ -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: { @@ -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([]); @@ -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' }, diff --git a/packages/app/main/src/protocolHandler.ts b/packages/app/main/src/protocolHandler.ts index 66da41a6f..31a7d00c6 100644 --- a/packages/app/main/src/protocolHandler.ts +++ b/packages/app/main/src/protocolHandler.ts @@ -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 || ''}`); } @@ -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 || ''}`); }