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

Refactor search bubble utils #378

Merged
merged 13 commits into from
Jan 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/webhook/handlers/__fixtures__/initState.ts

Large diffs are not rendered by default.

209 changes: 132 additions & 77 deletions src/webhook/handlers/__tests__/__snapshots__/initState.test.ts.snap

Large diffs are not rendered by default.

111 changes: 99 additions & 12 deletions src/webhook/handlers/__tests__/__snapshots__/processMedia.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,42 @@ Object {
},
"replies": Array [
Object {
"text": "🔍 There are some messages that looks similar to the one you have sent to me.",
"type": "text",
"altText": "🔍 There are some messages that looks similar to the one you have sent to me.",
"contents": Object {
"body": Object {
"contents": Array [
Object {
"text": "🔍 There are some messages that looks similar to the one you have sent to me.",
"type": "text",
"wrap": true,
},
],
"layout": "vertical",
"type": "box",
},
"type": "bubble",
},
"type": "flex",
},
Object {
"text": "Internet rumors are often mutated and shared.
"altText": "Internet rumors are often mutated and shared.
Please choose the version that looks the most similar👇",
"type": "text",
"contents": Object {
"body": Object {
"contents": Array [
Object {
"text": "Internet rumors are often mutated and shared.
Please choose the version that looks the most similar👇",
"type": "text",
"wrap": true,
},
],
"layout": "vertical",
"type": "box",
},
"type": "bubble",
},
"type": "flex",
},
Object {
"altText": "Please choose the most similar message from the list.",
Expand Down Expand Up @@ -336,13 +365,42 @@ Object {
},
"replies": Array [
Object {
"text": "🔍 There are some messages that looks similar to the one you have sent to me.",
"type": "text",
"altText": "🔍 There are some messages that looks similar to the one you have sent to me.",
"contents": Object {
"body": Object {
"contents": Array [
Object {
"text": "🔍 There are some messages that looks similar to the one you have sent to me.",
"type": "text",
"wrap": true,
},
],
"layout": "vertical",
"type": "box",
},
"type": "bubble",
},
"type": "flex",
},
Object {
"text": "Internet rumors are often mutated and shared.
"altText": "Internet rumors are often mutated and shared.
Please choose the version that looks the most similar👇",
"type": "text",
"contents": Object {
"body": Object {
"contents": Array [
Object {
"text": "Internet rumors are often mutated and shared.
Please choose the version that looks the most similar👇",
"type": "text",
"wrap": true,
},
],
"layout": "vertical",
"type": "box",
},
"type": "bubble",
},
"type": "flex",
},
Object {
"altText": "Please choose the most similar message from the list.",
Expand Down Expand Up @@ -584,13 +642,42 @@ Object {
},
"replies": Array [
Object {
"text": "🔍 There are some messages that looks similar to the one you have sent to me.",
"type": "text",
"altText": "🔍 There are some messages that looks similar to the one you have sent to me.",
"contents": Object {
"body": Object {
"contents": Array [
Object {
"text": "🔍 There are some messages that looks similar to the one you have sent to me.",
"type": "text",
"wrap": true,
},
],
"layout": "vertical",
"type": "box",
},
"type": "bubble",
},
"type": "flex",
},
Object {
"text": "Internet rumors are often mutated and shared.
"altText": "Internet rumors are often mutated and shared.
Please choose the version that looks the most similar👇",
"type": "text",
"contents": Object {
"body": Object {
"contents": Array [
Object {
"text": "Internet rumors are often mutated and shared.
Please choose the version that looks the most similar👇",
"type": "text",
"wrap": true,
},
],
"layout": "vertical",
"type": "box",
},
"type": "bubble",
},
"type": "flex",
},
Object {
"altText": "Please choose the most similar message from the list.",
Expand Down
18 changes: 7 additions & 11 deletions src/webhook/handlers/__tests__/initState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,16 @@ it('long article replies still below flex message limit', async () => {
});
expect(gql.__finished()).toBe(true);
expect(result.replies.length).toBeLessThanOrEqual(5); // Reply message API limit
const flexReply = result.replies.find(
(reply): reply is FlexMessage => reply.type === 'flex'
const carouselReply = result.replies.find(
(reply): reply is FlexMessage & { contents: { type: 'carousel' } } =>
reply.type === 'flex' && reply.contents.type === 'carousel'
);

// Make TS happy
/* istanbul ignore if */
if (!flexReply) throw new Error('No flex reply in replies');
if (!carouselReply) throw new Error('No carouselReply reply in replies');

const carousel = flexReply.contents;

// Make TS happy
/* istanbul ignore if */
if (carousel.type !== 'carousel')
throw new Error('Flex reply content is not carousel');
const carousel = carouselReply.contents;

expect(carousel.contents.length).toBeLessThanOrEqual(10); // Flex message carousel 10 bubble limit
expect(JSON.stringify(carousel).length).toBeLessThan(50 * 1000); // Flex message carousel 50K limit
Expand Down Expand Up @@ -252,15 +248,15 @@ it('should handle message matches only hyperlinks', async () => {
Object {
"ea": "Search",
"ec": "Article",
"el": "AVvY-yizyCdS-nWhuYGA",
"el": "AVvY-yizyCdS-nWhuYGB",
"ni": true,
},
],
Array [
Object {
"ea": "Search",
"ec": "Article",
"el": "AVvY-yizyCdS-nWhuYGB",
"el": "AVvY-yizyCdS-nWhuYGA",
"ni": true,
},
],
Expand Down
6 changes: 4 additions & 2 deletions src/webhook/handlers/__tests__/processMedia.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ it('twelve articles found', async () => {
expect(result).toMatchSnapshot();
expect(gql.__finished()).toBe(true);
expect(result.replies.length).toBeLessThanOrEqual(5); // Reply message API limit
const carousel = result.replies.find(({ type }) => type === 'flex').contents;
expect(carousel.type).toBe('carousel');
const carousel = result.replies.find(
({ type, contents }) => type === 'flex' && contents.type === 'carousel'
).contents;
expect(carousel).not.toBe(null);
expect(carousel.contents.length).toBeLessThanOrEqual(10); // Flex message carousel 10 bubble limit
expect(JSON.stringify(carousel).length).toBeLessThan(50 * 1000); // Flex message carousel 50K limit
});
Expand Down
2 changes: 1 addition & 1 deletion src/webhook/handlers/askingArticleSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const askingArticleSource: ChatbotPostbackHandler = async ({
try {
input = inputSchema.parse(postbackInput);
} catch (e) {
console.error('[choosingReply]', e);
console.error('[askingArticleSource]', e);
throw new ManipulationError(t`Please choose from provided options.`);
}

Expand Down
Loading
Loading