11import { AskAiAnswer } from './AskAiAnswer'
2- import { render , screen , act } from '@testing-library/react'
2+ import { LlmGatewayMessage , useLlmGateway } from './useLlmGateway'
3+ import { render , screen } from '@testing-library/react'
34import userEvent from '@testing-library/user-event'
45import * as React from 'react'
6+ import { act } from 'react'
7+
8+ const mockUseLlmGateway = jest . mocked ( useLlmGateway )
59
6- // Mock the dependencies
710const mockSendQuestion = jest . fn ( ( ) => Promise . resolve ( ) )
811const mockRetry = jest . fn ( )
12+ const mockAbort = jest . fn ( )
913
1014jest . mock ( './search.store' , ( ) => ( {
1115 useAskAiTerm : jest . fn ( ( ) => 'What is Elasticsearch?' ) ,
@@ -15,6 +19,7 @@ jest.mock('./useLlmGateway', () => ({
1519 useLlmGateway : jest . fn ( ( ) => ( {
1620 messages : [ ] ,
1721 error : null ,
22+ abort : mockAbort ,
1823 retry : mockRetry ,
1924 sendQuestion : mockSendQuestion ,
2025 } ) ) ,
@@ -26,22 +31,19 @@ jest.mock('uuid', () => ({
2631} ) )
2732
2833describe ( 'AskAiAnswer Component' , ( ) => {
29- const {
30- useLlmGateway,
31- } = require ( './useLlmGateway' )
32-
3334 beforeEach ( ( ) => {
3435 jest . clearAllMocks ( )
3536 } )
3637
3738 describe ( 'Initial Loading State' , ( ) => {
3839 test ( 'should show loading spinner when no messages are present' , ( ) => {
3940 // Arrange
40- useLlmGateway . mockReturnValue ( {
41+ mockUseLlmGateway . mockReturnValue ( {
4142 messages : [ ] ,
4243 error : null ,
4344 retry : mockRetry ,
4445 sendQuestion : mockSendQuestion ,
46+ abort : mockAbort ,
4547 } )
4648
4749 // Act
@@ -57,27 +59,32 @@ describe('AskAiAnswer Component', () => {
5759 describe ( 'Message Display' , ( ) => {
5860 test ( 'should display AI message content correctly' , ( ) => {
5961 // Arrange
60- const mockMessages = [
62+ const mockMessages : LlmGatewayMessage [ ] = [
6163 {
64+ id : 'some-id-1' ,
65+ timestamp : 0 ,
6266 type : 'ai_message' ,
6367 data : {
6468 content :
6569 'Elasticsearch is a distributed search engine...' ,
6670 } ,
6771 } ,
6872 {
73+ id : 'some-id-2' ,
74+ timestamp : 0 ,
6975 type : 'ai_message_chunk' ,
7076 data : {
7177 content : ' It provides real-time search capabilities.' ,
7278 } ,
7379 } ,
7480 ]
7581
76- useLlmGateway . mockReturnValue ( {
82+ mockUseLlmGateway . mockReturnValue ( {
7783 messages : mockMessages ,
7884 error : null ,
7985 retry : mockRetry ,
8086 sendQuestion : mockSendQuestion ,
87+ abort : mockAbort ,
8188 } )
8289
8390 // Act
@@ -93,11 +100,12 @@ describe('AskAiAnswer Component', () => {
93100 describe ( 'Error State' , ( ) => {
94101 test ( 'should display error message when there is an error' , ( ) => {
95102 // Arrange
96- useLlmGateway . mockReturnValue ( {
103+ mockUseLlmGateway . mockReturnValue ( {
97104 messages : [ ] ,
98105 error : new Error ( 'Network error' ) ,
99106 retry : mockRetry ,
100107 sendQuestion : mockSendQuestion ,
108+ abort : mockAbort ,
101109 } )
102110
103111 // Act
@@ -118,24 +126,29 @@ describe('AskAiAnswer Component', () => {
118126 describe ( 'Finished State with Feedback Buttons' , ( ) => {
119127 test ( 'should show feedback buttons when answer is finished' , ( ) => {
120128 // Arrange
121- let onMessageCallback : ( message : any ) => void = ( ) => { }
129+ let onMessageCallback : (
130+ message : LlmGatewayMessage
131+ ) => void = ( ) => { }
122132
123- const mockMessages = [
133+ const mockMessages : LlmGatewayMessage [ ] = [
124134 {
135+ id : 'some-id-1' ,
136+ timestamp : 1 ,
125137 type : 'ai_message' ,
126138 data : {
127139 content : 'Here is your answer about Elasticsearch.' ,
128140 } ,
129141 } ,
130142 ]
131143
132- useLlmGateway . mockImplementation ( ( { onMessage } ) => {
133- onMessageCallback = onMessage
144+ mockUseLlmGateway . mockImplementation ( ( { onMessage } ) => {
145+ onMessageCallback = onMessage !
134146 return {
135147 messages : mockMessages ,
136148 error : null ,
137149 retry : mockRetry ,
138150 sendQuestion : mockSendQuestion ,
151+ abort : mockAbort ,
139152 }
140153 } )
141154
@@ -144,7 +157,12 @@ describe('AskAiAnswer Component', () => {
144157
145158 // Simulate the component receiving an 'agent_end' message to finish loading
146159 act ( ( ) => {
147- onMessageCallback ( { type : 'agent_end' } )
160+ onMessageCallback ( {
161+ type : 'agent_end' ,
162+ id : 'some-id' ,
163+ timestamp : 12345 ,
164+ data : { } ,
165+ } )
148166 } )
149167
150168 // Assert
@@ -162,30 +180,46 @@ describe('AskAiAnswer Component', () => {
162180 test ( 'should call retry function when refresh button is clicked' , async ( ) => {
163181 // Arrange
164182 const user = userEvent . setup ( )
165- let onMessageCallback : ( message : any ) => void = ( ) => { }
183+ let onMessageCallback : (
184+ message : LlmGatewayMessage
185+ ) => void = ( ) => { }
166186
167- const mockMessages = [
187+ const mockMessages : LlmGatewayMessage [ ] = [
168188 {
189+ id : 'some-id-1' ,
190+ timestamp : 12345 ,
169191 type : 'ai_message' ,
170192 data : { content : 'Here is your answer.' } ,
171193 } ,
172194 ]
173195
174- useLlmGateway . mockImplementation ( ( { onMessage } ) => {
175- onMessageCallback = onMessage
196+ mockUseLlmGateway . mockImplementation ( ( { onMessage } ) => {
197+ onMessageCallback = onMessage !
176198 return {
177199 messages : mockMessages ,
178200 error : null ,
179201 retry : mockRetry ,
180202 sendQuestion : mockSendQuestion ,
203+ abort : mockAbort ,
181204 }
182205 } )
183206
184207 render ( < AskAiAnswer /> )
185208
186209 // Simulate finished state
187210 act ( ( ) => {
188- onMessageCallback ( { type : 'agent_end' } )
211+ onMessageCallback ( {
212+ type : 'agent_start' ,
213+ id : 'some-id' ,
214+ timestamp : 12345 ,
215+ data : { input : { } , thread : { } } ,
216+ } )
217+ onMessageCallback ( {
218+ type : 'agent_end' ,
219+ id : 'some-id' ,
220+ timestamp : 12346 ,
221+ data : { } ,
222+ } )
189223 } )
190224
191225 // Act
@@ -203,11 +237,12 @@ describe('AskAiAnswer Component', () => {
203237 describe ( 'Question Sending' , ( ) => {
204238 test ( 'should send question on component mount' , ( ) => {
205239 // Arrange
206- useLlmGateway . mockReturnValue ( {
240+ mockUseLlmGateway . mockReturnValue ( {
207241 messages : [ ] ,
208242 error : null ,
209243 retry : mockRetry ,
210244 sendQuestion : mockSendQuestion ,
245+ abort : mockAbort ,
211246 } )
212247
213248 // Act
0 commit comments