Skip to content

Commit 0c63c5a

Browse files
web: first test case for AIAnswer
1 parent 7f5a361 commit 0c63c5a

File tree

4 files changed

+1114
-3
lines changed

4 files changed

+1114
-3
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
2+
import ReactDOM from 'react-dom';
3+
import React from 'react';
4+
import renderer, { act } from 'react-test-renderer';
5+
import ReactiveBase from '../basic/ReactiveBase';
6+
import AIAnswer from './AIAnswer/index';
7+
8+
9+
const MOCK_HITS_DATA = [
10+
{
11+
_index: 'good-books-ds',
12+
_type: '_doc',
13+
_id: '2TftXXEBdEU4aeo6Gdqs',
14+
_score: 70.69977,
15+
_source: {
16+
image: 'https://images.gr-assets.com/books/1373059909l/18079719.jpg',
17+
average_rating_rounded: 4,
18+
books_count: 24,
19+
original_title: 'Grasshopper Jungle',
20+
image_medium: 'https://images.gr-assets.com/books/1373059909m/18079719.jpg',
21+
isbn: '525426035',
22+
average_rating: 3.66,
23+
original_publication_year: 2014,
24+
title: 'Grasshopper Jungle',
25+
language_code: 'eng',
26+
id: 9837,
27+
ratings_count: 11890,
28+
original_series: '',
29+
authors: 'Andrew Smith',
30+
},
31+
},
32+
{
33+
_index: 'good-books-ds',
34+
_type: '_doc',
35+
_id: 'nj7tXXEBhDwVijd9MkkX',
36+
_score: 8.691772,
37+
_source: {
38+
image: 'https://images.gr-assets.com/books/1332140681l/41681.jpg',
39+
average_rating_rounded: 4,
40+
books_count: 534,
41+
original_title: 'The Jungle',
42+
image_medium: 'https://images.gr-assets.com/books/1332140681m/41681.jpg',
43+
isbn: '1884365302',
44+
average_rating: 3.72,
45+
original_publication_year: 1906,
46+
title: 'The Jungle',
47+
language_code: 'eng',
48+
id: 879,
49+
ratings_count: 97468,
50+
original_series: '',
51+
authors: 'Upton Sinclair, Earl Lee, Kathleen DeGrave',
52+
},
53+
},
54+
{
55+
_index: 'good-books-ds',
56+
_type: '_doc',
57+
_id: '2TftXXEBdEU4aeo6hOBa',
58+
_score: 8.691772,
59+
_source: {
60+
image: 'https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png',
61+
average_rating_rounded: 4,
62+
books_count: 35,
63+
original_title: 'Rubyfruit Jungle',
64+
image_medium: 'https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png',
65+
isbn: '553146963',
66+
average_rating: 3.88,
67+
original_publication_year: 1973,
68+
title: 'Rubyfruit Jungle',
69+
language_code: 'eng',
70+
id: 4455,
71+
ratings_count: 22583,
72+
original_series: '',
73+
authors: 'Rita Mae Brown',
74+
},
75+
},
76+
{
77+
_index: 'good-books-ds',
78+
_type: '_doc',
79+
_id: 'grDtXXEB2-YohfeSsMfi',
80+
_score: 8.691772,
81+
_source: {
82+
image: 'https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png',
83+
average_rating_rounded: 3,
84+
books_count: 71,
85+
original_title: 'Lipstick Jungle',
86+
image_medium: 'https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png',
87+
isbn: '786887079',
88+
average_rating: 3.39,
89+
original_publication_year: 2005,
90+
title: 'Lipstick Jungle',
91+
language_code: 'eng',
92+
id: 2546,
93+
ratings_count: 35792,
94+
original_series: '',
95+
authors: 'Candace Bushnell',
96+
},
97+
},
98+
{
99+
_index: 'good-books-ds',
100+
_type: '_doc',
101+
_id: 'GLDtXXEB2-YohfeSysvk',
102+
_score: 7.720322,
103+
_source: {
104+
image: 'https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png',
105+
average_rating_rounded: 4,
106+
books_count: 227,
107+
original_title: 'The Jungle Books',
108+
image_medium: 'https://s.gr-assets.com/assets/nophoto/book/111x148-bcc042a9c91a29c1d680899eff700a03.png',
109+
isbn: '451529758',
110+
average_rating: 4,
111+
original_publication_year: 1895,
112+
title: 'The Jungle Books',
113+
language_code: 'eng',
114+
id: 1695,
115+
ratings_count: 68750,
116+
original_series: '',
117+
authors: 'Rudyard Kipling, Alev Lytle Croutier',
118+
},
119+
},
120+
{
121+
_index: 'good-books-ds',
122+
_type: '_doc',
123+
_id: 'kD7tXXEBhDwVijd94lyY',
124+
_score: 7.720322,
125+
_source: {
126+
image: 'https://images.gr-assets.com/books/1327873594l/77270.jpg',
127+
average_rating_rounded: 4,
128+
books_count: 1306,
129+
original_title: 'The Jungle Book',
130+
image_medium: 'https://images.gr-assets.com/books/1327873594m/77270.jpg',
131+
isbn: '812504690',
132+
average_rating: 3.94,
133+
original_publication_year: 1894,
134+
title: 'The Jungle Book',
135+
language_code: 'eng',
136+
id: 1327,
137+
ratings_count: 67107,
138+
original_series: '',
139+
authors: 'Rudyard Kipling',
140+
},
141+
},
142+
{
143+
_index: 'good-books-ds',
144+
_type: '_doc',
145+
_id: 'FT7tXXEBhDwVijd9MksX',
146+
_score: 6.944194,
147+
_source: {
148+
image: 'https://images.gr-assets.com/books/1320418408l/2637138.jpg',
149+
average_rating_rounded: 4,
150+
books_count: 9,
151+
original_title: 'Welcome to the Jungle',
152+
image_medium: 'https://images.gr-assets.com/books/1320418408m/2637138.jpg',
153+
isbn: '345507460',
154+
average_rating: 4.09,
155+
original_publication_year: 2008,
156+
title: 'Welcome to the Jungle (The Dresden Files, #0.5)',
157+
language_code: 'eng',
158+
id: 8424,
159+
ratings_count: 13833,
160+
original_series: 'The Dresden Files',
161+
authors: 'Jim Butcher',
162+
},
163+
},
164+
];
165+
166+
const MOCK_AI_RESPONSE = {
167+
sessionId: 'mLjPtoymnJNnQxdcnGgJCy',
168+
response: {
169+
answer: {
170+
documentIds: [
171+
'2TftXXEBdEU4aeo6Gdqs',
172+
'nj7tXXEBhDwVijd9MkkX',
173+
'2TftXXEBdEU4aeo6hOBa',
174+
'grDtXXEB2-YohfeSsMfi',
175+
'GLDtXXEB2-YohfeSysvk',
176+
'kD7tXXEBhDwVijd94lyY',
177+
'FT7tXXEBhDwVijd9MksX',
178+
],
179+
model: 'gpt-3.5-turbo-0613',
180+
text: '"Grasshopper Jungle" is a young adult science fiction novel written by Andrew Smith. It tells the story of Austin Szerba, a teenager living in the small town of Ealing, Iowa. The town is suddenly invaded by giant, man-eating praying mantises that were created as part of a military experiment gone wrong.As Austin and his best friend Robby navigate through the chaos, they also grapple with their own personal struggles. Austin finds himself torn between his girlfriend, Shann, and his confusing feelings towards both her and his openly gay best friend, Robby.The novel touches on themes of sexuality, friendship, and identity, while also exploring the idea of humanity and what it means to be human. It combines elements of coming-of-age with science fiction, creating a unique and thought-provoking story."Grasshopper Jungle" received critical acclaim for its unconventional storytelling, strong character development, and exploration of complex social issues. It has been described as a dark, thrilling, and sometimes humorous tale that challenges traditional notions of literature for young adults.',
181+
},
182+
question: 'Answer the following: Grasshopper Jungle',
183+
},
184+
messages: [
185+
{
186+
content: 'Answer the following: Grasshopper Jungle',
187+
role: 'user',
188+
},
189+
{
190+
content: '"Grasshopper Jungle" is a young adult science fiction novel written by Andrew Smith. It tells the story of Austin Szerba, a teenager living in the small town of Ealing, Iowa. The town is suddenly invaded by giant, man-eating praying mantises that were created as part of a military experiment gone wrong.\n\nAs Austin and his best friend Robby navigate through the chaos, they also grapple with their own personal struggles. Austin finds himself torn between his girlfriend, Shann, and his confusing feelings towards both her and his openly gay best friend, Robby.The novel touches on themes of sexuality, friendship, and identity, while also exploring the idea of humanity and what it means to be human. It combines elements of coming-of-age with science fiction, creating a unique and thought-provoking story."Grasshopper Jungle" received critical acclaim for its unconventional storytelling, strong character development, and exploration of complex social issues. It has been described as a dark, thrilling, and sometimes humorous tale that challenges traditional notions of literature for young adults.',
191+
role: 'assistant',
192+
},
193+
],
194+
isTyping: false,
195+
};
196+
197+
198+
it('should render AIAnswer', async () => {
199+
const container = document.createElement('div');
200+
const elem = renderer.create(
201+
<ReactiveBase app="test" url="https://foo:bar@localhost:800">
202+
<AIAnswer
203+
testMode
204+
componentId="MockAIAnswer"
205+
mockData={{ hits: MOCK_HITS_DATA, AI_RESPONSE: MOCK_AI_RESPONSE }}
206+
/>
207+
</ReactiveBase>, container,
208+
);
209+
210+
// Wait for all updates to complete
211+
await act(async () => {
212+
await new Promise(resolve => setTimeout(resolve, 1000));
213+
});
214+
215+
expect(elem.toJSON()).toMatchSnapshot();
216+
217+
// Unmount the component
218+
ReactDOM.unmountComponentAtNode(container);
219+
});

packages/web/src/components/search/AIAnswer/index.ts renamed to packages/web/src/components/search/AIAnswer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22

33
import { CommonProps } from '../..';
44
import * as types from '../../types';
5-
import { children } from '../../../types';
5+
import { children } from '../../types';
66

77
export interface AIAnswerProps extends CommonProps {
88
children?: (data: any) => any;

packages/web/src/components/search/AIAnswer/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const AIAnswer = (props) => {
4040
const errorMessageForMissingSessionId = `AISessionId for ${props.componentId} is missing! AIAnswer component requires an AISession to function. Trying reloading the App.`;
4141

4242
const handleSendMessage = (text, isRetry = false, fetchMeta = false) => {
43+
if (props.testMode) {
44+
return;
45+
}
4346
// meta refers to source documents IDs here
4447
if (currentSessionId) {
4548
if (!isRetry) {
@@ -357,6 +360,7 @@ AIAnswer.propTypes = {
357360
onSourceClick: types.func,
358361
isAITyping: types.boolRequired,
359362
dependentComponentValue: types.string,
363+
testMode: types.bool,
360364
};
361365

362366
AIAnswer.defaultProps = {
@@ -378,15 +382,15 @@ AIAnswer.defaultProps = {
378382
};
379383

380384
const mapStateToProps = (state, props) => {
381-
let dependencyComponent = Object.values(props.react)[0];
385+
let dependencyComponent = Object.values(props.react || {})[0];
382386
if (Array.isArray(dependencyComponent)) {
383387
dependencyComponent = dependencyComponent[0];
384388
}
385389

386390
const showComponent = Boolean(
387391
state.selectedValues[dependencyComponent]
388392
&& state.selectedValues[dependencyComponent].value,
389-
);
393+
) || props.testMode;
390394
const dependentComponentValue
391395
= (state.selectedValues[dependencyComponent]
392396
&& state.selectedValues[dependencyComponent].value)

0 commit comments

Comments
 (0)