diff --git a/src/webhook/handlers/__tests__/askingArticleSource.test.ts b/src/webhook/handlers/__tests__/askingArticleSource.test.ts index 8399bdb9..8917d7fb 100644 --- a/src/webhook/handlers/__tests__/askingArticleSource.test.ts +++ b/src/webhook/handlers/__tests__/askingArticleSource.test.ts @@ -14,7 +14,7 @@ beforeEach(() => { it('throws on incorrect input', async () => { const incorrectParam: ChatbotPostbackHandlerParams = { - data: { sessionId: 0, searchedText: 'foo' }, + context: { sessionId: 0, msgs: [{ id: 'foo', type: 'text', text: 'foo' }] }, postbackData: { sessionId: 0, state: 'ASKING_ARTICLE_SOURCE', @@ -30,7 +30,7 @@ it('throws on incorrect input', async () => { it('returns instructions if user did not forward the whole message', async () => { const didNotForwardParam: ChatbotPostbackHandlerParams = { - data: { searchedText: 'foo', sessionId: 0 }, + context: { sessionId: 0, msgs: [{ id: 'foo', type: 'text', text: 'foo' }] }, postbackData: { sessionId: 0, state: 'ASKING_ARTICLE_SOURCE', @@ -212,7 +212,7 @@ it('returns instructions if user did not forward the whole message', async () => it('sends user submission consent if user forwarded the whole message', async () => { const didForwardParam: ChatbotPostbackHandlerParams = { - data: { searchedText: 'foo', sessionId: 0 }, + context: { sessionId: 0, msgs: [{ id: 'foo', type: 'text', text: 'foo' }] }, postbackData: { sessionId: 0, state: 'ASKING_ARTICLE_SOURCE', diff --git a/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.ts b/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.ts index 4f79ff15..5fedeef2 100644 --- a/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.ts +++ b/src/webhook/handlers/__tests__/askingArticleSubmissionConsent.test.ts @@ -28,7 +28,7 @@ beforeEach(() => { it('throws on incorrect input', async () => { const incorrectParam: ChatbotPostbackHandlerParams = { - data: { sessionId: 0, searchedText: 'foo' }, + context: { sessionId: 0, msgs: [] }, postbackData: { sessionId: 0, state: 'ASKING_ARTICLE_SUBMISSION_CONSENT', @@ -47,9 +47,11 @@ it('throws on incorrect input', async () => { it('should thank the user if user does not agree to submit', async () => { const inputSession = new Date('2020-01-01T18:10:18.314Z').getTime(); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: inputSession, - searchedText: 'Some text forwarded by the user', + msgs: [ + { id: 'foo', type: 'text', text: 'Some text forwarded by the user' }, + ], }, postbackData: { sessionId: inputSession, @@ -87,9 +89,11 @@ it('should thank the user if user does not agree to submit', async () => { it('should submit article if user agrees to submit', async () => { const inputSession = new Date('2020-01-01T18:10:18.314Z').getTime(); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: inputSession, - searchedText: 'Some text forwarded by the user', + msgs: [ + { id: 'foo', type: 'text', text: 'Some text forwarded by the user' }, + ], }, postbackData: { sessionId: inputSession, @@ -108,7 +112,7 @@ it('should submit article if user agrees to submit', async () => { expect(gql.__finished()).toBe(true); expect(result).toMatchSnapshot('has AI reply'); - expect(result.data.sessionId).not.toEqual(inputSession); + expect(result.context.sessionId).not.toEqual(inputSession); expect(ga.eventMock.mock.calls).toMatchInlineSnapshot(` Array [ Array [ @@ -137,11 +141,9 @@ it('should submit article if user agrees to submit', async () => { it('should submit image article if user agrees to submit', async () => { const inputSession = new Date('2020-01-01T18:10:18.314Z').getTime(); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: inputSession, - searchedText: '', - messageId: '6530038889933', - messageType: 'image', + msgs: [{ id: '6530038889933', type: 'image' }], }, postbackData: { sessionId: inputSession, @@ -158,7 +160,7 @@ it('should submit image article if user agrees to submit', async () => { expect(gql.__finished()).toBe(true); expect(result).toMatchSnapshot(); - expect(result.data.sessionId).not.toEqual(inputSession); + expect(result.context.sessionId).not.toEqual(inputSession); expect(ga.eventMock.mock.calls).toMatchInlineSnapshot(` Array [ Array [ @@ -176,9 +178,11 @@ it('should submit image article if user agrees to submit', async () => { it('should create a UserArticleLink when creating a Article', async () => { const userId = 'user-id-0'; const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: 'Some text forwarded by the user', + msgs: [ + { id: 'foo', type: 'text', text: 'Some text forwarded by the user' }, + ], }, postbackData: { sessionId: 0, @@ -201,9 +205,11 @@ it('should create a UserArticleLink when creating a Article', async () => { it('should ask user to turn on notification settings if they did not turn it on after creating an Article', async () => { const userId = 'user-id-0'; const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: 'Some text forwarded by the user', + msgs: [ + { id: 'foo', type: 'text', text: 'Some text forwarded by the user' }, + ], }, postbackData: { sessionId: 0, diff --git a/src/webhook/handlers/__tests__/choosingArticle.test.ts b/src/webhook/handlers/__tests__/choosingArticle.test.ts index 1402a309..e566e496 100644 --- a/src/webhook/handlers/__tests__/choosingArticle.test.ts +++ b/src/webhook/handlers/__tests__/choosingArticle.test.ts @@ -32,10 +32,15 @@ it('should select article by articleId', async () => { gql.__push(apiGetArticleResult.selectedArticleId); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: - '《緊急通知》\n台北馬偕醫院傳來訊息:\n資深醫生(林清風)傳來:「請大家以後千萬不要再吃生魚片了!」\n因為最近已經發現- 好多病人因為吃了生魚片,胃壁附著《海獸胃腺蟲》,大小隻不一定,有的病人甚至胃壁上滿滿都是無法夾出來,驅蟲藥也很難根治,罹患機率每個國家的人都一樣。\n尤其;鮭魚的含蟲量最高、最可怕!\n請傳給朋友,讓他們有所警惕!', + msgs: [ + { + id: 'foo', + type: 'text', + text: '《緊急通知》\n台北馬偕醫院傳來訊息:\n資深醫生(林清風)傳來:「請大家以後千萬不要再吃生魚片了!」\n因為最近已經發現- 好多病人因為吃了生魚片,胃壁附著《海獸胃腺蟲》,大小隻不一定,有的病人甚至胃壁上滿滿都是無法夾出來,驅蟲藥也很難根治,罹患機率每個國家的人都一樣。\n尤其;鮭魚的含蟲量最高、最可怕!\n請傳給朋友,讓他們有所警惕!', + }, + ], }, postbackData: { sessionId: 0, @@ -81,7 +86,7 @@ it('throws ManipulationError when articleId is not valid', async () => { gql.__push({ data: { GetArticle: null } }); const params: ChatbotPostbackHandlerParams = { - data: { sessionId: 0, searchedText: '' }, + context: { sessionId: 0, msgs: [] }, postbackData: { sessionId: 0, state: 'CHOOSING_ARTICLE', @@ -100,10 +105,15 @@ it('should select article and have OPINIONATED and NOT_ARTICLE replies', async ( gql.__push(apiGetArticleResult.multipleReplies); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 1497994017447, - searchedText: - '老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人', + msgs: [ + { + id: 'foo', + type: 'text', + text: '老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人', + }, + ], }, postbackData: { input: 'article-id', @@ -168,9 +178,15 @@ it('should select article with no replies', async () => { gql.__push(apiGetArticleResult.createOrUpdateReplyRequest); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: '老司機車裡總備一塊香皂,知道內情的新手默默也準備了一塊', + msgs: [ + { + id: 'foo', + type: 'text', + text: '老司機車裡總備一塊香皂,知道內情的新手默默也準備了一塊', + }, + ], }, postbackData: { input: 'article-id', @@ -223,10 +239,15 @@ it('should select article and choose the only one reply for user', async () => { gql.__push(apiGetReplyResult.oneReply2); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: - 'Just One Reply Just One Reply Just One Reply Just One Reply Just One Reply', + msgs: [ + { + id: 'foo', + type: 'text', + text: 'Just One Reply Just One Reply Just One Reply Just One Reply Just One Reply', + }, + ], }, postbackData: { input: 'article-id', @@ -269,10 +290,15 @@ it('should select article and choose the only one reply for user', async () => { it('should block incorrect interactions', async () => { const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: - 'Just One Reply Just One Reply Just One Reply Just One Reply Just One Reply', + msgs: [ + { + id: 'foo', + type: 'text', + text: 'Just One Reply Just One Reply Just One Reply Just One Reply Just One Reply', + }, + ], }, postbackData: { sessionId: 0, @@ -291,10 +317,15 @@ it('should select article and slice replies when over 10', async () => { gql.__push(apiGetArticleResult.elevenReplies); const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: - '老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人', + msgs: [ + { + id: 'foo', + type: 'text', + text: '老榮民九成存款全部捐給慈濟,如今窮了卻得不到慈濟醫院社工的幫忙,竟翻臉不認人', + }, + ], }, postbackData: { input: 'article-id', @@ -310,10 +341,15 @@ it('should select article and slice replies when over 10', async () => { it('should ask users if they want to submit article when user say not found', async () => { const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: - '這一篇文章確實是一個轉傳文章,他夠長,看起來很轉傳,但是使用者覺得資料庫裡沒有。', + msgs: [ + { + id: 'foo', + type: 'text', + text: '這一篇文章確實是一個轉傳文章,他夠長,看起來很轉傳,但是使用者覺得資料庫裡沒有。', + }, + ], }, postbackData: { input: POSTBACK_NO_ARTICLE_FOUND, @@ -344,10 +380,14 @@ it('should ask users if they want to submit article when user say not found', as it('should ask users if they want to submit image article when user say not found', async () => { const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - messageType: 'image', - messageId: '6530038889933', + msgs: [ + { + id: '6530038889933', + type: 'image', + }, + ], }, postbackData: { input: POSTBACK_NO_ARTICLE_FOUND, @@ -379,9 +419,15 @@ it('should ask users if they want to submit image article when user say not foun it('should create a UserArticleLink when selecting a article', async () => { const userId = 'user-id-0'; const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: '《緊急通知》', + msgs: [ + { + id: 'foo', + type: 'text', + text: '《緊急通知》', + }, + ], }, postbackData: { input: 'article-id', diff --git a/src/webhook/handlers/__tests__/choosingReply.test.ts b/src/webhook/handlers/__tests__/choosingReply.test.ts index 60372b96..b1b17b1c 100644 --- a/src/webhook/handlers/__tests__/choosingReply.test.ts +++ b/src/webhook/handlers/__tests__/choosingReply.test.ts @@ -31,9 +31,9 @@ describe('should select reply by replyId', () => { }; const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: '貼圖', + msgs: [{ id: 'foo', type: 'text', text: '貼圖' }], }, postbackData: { sessionId: 0, @@ -101,9 +101,9 @@ describe('should select reply by replyId', () => { it('should block invalid postback input', async () => { const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: '貼圖', + msgs: [{ id: 'foo', type: 'text', text: '貼圖' }], }, postbackData: { sessionId: 0, @@ -126,9 +126,9 @@ it('should handle graphql error gracefully', async () => { }; const params: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 0, - searchedText: '貼圖', + msgs: [{ id: 'foo', type: 'text', text: '貼圖' }], }, postbackData: { sessionId: 0, diff --git a/src/webhook/handlers/__tests__/handlePostback.test.ts b/src/webhook/handlers/__tests__/handlePostback.test.ts index b1f5dc77..03f408a6 100644 --- a/src/webhook/handlers/__tests__/handlePostback.test.ts +++ b/src/webhook/handlers/__tests__/handlePostback.test.ts @@ -228,7 +228,7 @@ describe('tutorial', () => { tutorial.mockImplementationOnce(() => { return { - data: { sessionId: 0, searchedText: '' }, + context: { sessionId: 0, msgs: [] }, replies: [], }; }); diff --git a/src/webhook/handlers/__tests__/singleUserHandler.test.ts b/src/webhook/handlers/__tests__/singleUserHandler.test.ts index 3915b2a2..8c1cf25b 100644 --- a/src/webhook/handlers/__tests__/singleUserHandler.test.ts +++ b/src/webhook/handlers/__tests__/singleUserHandler.test.ts @@ -422,9 +422,9 @@ it('Resets session on free-form input, triggers fast-forward', async () => { const input = 'Newly forwarded message'; const event = createTextMessageEvent(input); - initState.mockImplementationOnce(({ data }) => { + initState.mockImplementationOnce(({ context }) => { return Promise.resolve({ - data, + context, replies: [ { type: 'text', diff --git a/src/webhook/handlers/__tests__/tutorial.test.ts b/src/webhook/handlers/__tests__/tutorial.test.ts index 83117cac..cd719cca 100644 --- a/src/webhook/handlers/__tests__/tutorial.test.ts +++ b/src/webhook/handlers/__tests__/tutorial.test.ts @@ -12,9 +12,9 @@ import { ChatbotPostbackHandlerParams } from 'src/types/chatbotState'; const ga = originalGa as MockedGa; const param: ChatbotPostbackHandlerParams = { - data: { + context: { sessionId: 1497994017447, - searchedText: '', + msgs: [], }, postbackData: { sessionId: 1497994017447, @@ -242,6 +242,6 @@ it('createGreetingMessage()', () => { }); it('createTutorialMessage()', () => { - const result = createTutorialMessage(param.data.sessionId); + const result = createTutorialMessage(param.context.sessionId); expect(result).toMatchSnapshot(); });