diff --git a/fixtures/apiai/test_response_empty.json b/fixtures/apiai/test_response_empty.json new file mode 100644 index 0000000..f525e3e --- /dev/null +++ b/fixtures/apiai/test_response_empty.json @@ -0,0 +1,34 @@ +{ + "id": "efe087b8-8486-4117-8863-c63b09a0dd5a", + "timestamp": "2017-08-08T17:06:28.086Z", + "lang": "ru", + "result": { + "source": "agent", + "resolvedQuery": "turbo test", + "action": "action1", + "actionIncomplete": false, + "parameters": { "binType": "Yellow Bin" }, + "contexts": [ + { "name": "missed_pickup", + "parameters": { "bintype.original": "yellow", "bintype": "Yellow Bin" }, + "lifespan": 1 + } + ], + "metadata": { + "intentId": "762939a1-36f6-4a99-9de3-349e72ecb7be", + "webhookUsed": "false", + "webhookForSlotFillingUsed": "false", + "intentName": "turboTest" + }, + "fulfillment": { + "speech": "", + "messages": [] + }, + "score": 1 + }, + "status": { + "code": 200, + "errorType": "success" + }, + "sessionId": "35cb38b7-cc7a-4f56-a43c-c00fc3eb13d6" +} diff --git a/package.json b/package.json index 8886134..c2af0e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "botstack", - "version": "0.1.3", + "version": "0.1.4", "description": "Bot Stack", "main": "index.js", "scripts": { diff --git a/src/api-ai.js b/src/api-ai.js index c3cebe6..bade11e 100644 --- a/src/api-ai.js +++ b/src/api-ai.js @@ -65,14 +65,12 @@ function processResponse(response, senderID) { senderId: senderID }); return null; - } else if (!lodash.isEmpty(messages)) { - const returnData = { - messages, - response - }; - return returnData; } - return null; + const returnData = { + messages, + response + }; + return returnData; } return null; } diff --git a/tests/test_fb.js b/tests/test_fb.js index d269198..acf514d 100644 --- a/tests/test_fb.js +++ b/tests/test_fb.js @@ -242,4 +242,33 @@ describe('Testing FB', () => { assert.equal(lodash.get(data, '[0].attachment.type'), 'image'); assert.equal(lodash.get(data, '[0].attachment.payload.url'), 'http://example.com/image.jpg'); }); + + it('Testing with empty response', async () => { + const apiAiTextResponse = require('../fixtures/apiai/test_response_empty.json'); + const data = []; + + const apiai = require('../src/api-ai'); + const apiAiResult = apiai.processResponse(apiAiTextResponse, '1234567890'); + + rewiremock('./reply').callThought().with({ + reply: async (message, senderId) => { + data.push(message); + return true; + } + }); + + rewiremock.enable(); + rewiremock.isolation(); + + const fb = require(rewiremock.resolve('../src/fb')); + + const senderID = '1234567890'; + const res = await fb.processMessagesFromApiAi(apiAiResult, senderID); + + rewiremock.disable(); + rewiremock.clear(); + + assert.equal(lodash.get(apiAiResult, 'messages').length, 0); + assert.equal(!lodash.isEmpty(lodash.get(apiAiResult, 'response.result.action')), true); + }); });