Skip to content

Commit

Permalink
fix(webhook): unit test case types
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Dec 18, 2023
1 parent 0430693 commit 8e21f91
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/webhook/handlers/__tests__/askingArticleSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 [
Expand Down Expand Up @@ -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,
Expand All @@ -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 [
Expand All @@ -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,
Expand All @@ -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,
Expand Down
98 changes: 72 additions & 26 deletions src/webhook/handlers/__tests__/choosingArticle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions src/webhook/handlers/__tests__/choosingReply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/webhook/handlers/__tests__/handlePostback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('tutorial', () => {

tutorial.mockImplementationOnce(() => {
return {
data: { sessionId: 0, searchedText: '' },
context: { sessionId: 0, msgs: [] },
replies: [],
};
});
Expand Down
4 changes: 2 additions & 2 deletions src/webhook/handlers/__tests__/singleUserHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions src/webhook/handlers/__tests__/tutorial.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -242,6 +242,6 @@ it('createGreetingMessage()', () => {
});

it('createTutorialMessage()', () => {
const result = createTutorialMessage(param.data.sessionId);
const result = createTutorialMessage(param.context.sessionId);
expect(result).toMatchSnapshot();
});

0 comments on commit 8e21f91

Please sign in to comment.