@@ -8,7 +8,11 @@ import {
8
8
import { AssistantChat } from './assistant-chat' ;
9
9
import { expect } from 'chai' ;
10
10
import { createMockChat } from '../test/utils' ;
11
- import type { AssistantMessage } from './compass-assistant-provider' ;
11
+ import {
12
+ AssistantActionsContext ,
13
+ type AssistantMessage ,
14
+ } from './compass-assistant-provider' ;
15
+ import sinon from 'sinon' ;
12
16
13
17
describe ( 'AssistantChat' , function ( ) {
14
18
const mockMessages : AssistantMessage [ ] = [
@@ -38,10 +42,30 @@ describe('AssistantChat', function () {
38
42
} = { }
39
43
) {
40
44
const chat = createMockChat ( { messages, status } ) ;
41
- const result = render ( < AssistantChat chat = { chat } /> ) ;
45
+
46
+ // The chat component does not use chat.sendMessage() directly, it uses
47
+ // ensureOptInAndSend() via the AssistantActionsContext.
48
+ const ensureOptInAndSendStub = sinon
49
+ . stub ( )
50
+ . callsFake ( async ( message , options , callback ) => {
51
+ // call the callback so we can test the tracking
52
+ callback ( ) ;
53
+
54
+ await chat . sendMessage ( message , options ) ;
55
+ } ) ;
56
+
57
+ const assistantActionsContext = {
58
+ ensureOptInAndSend : ensureOptInAndSendStub ,
59
+ } ;
60
+ const result = render (
61
+ < AssistantActionsContext . Provider value = { assistantActionsContext as any } >
62
+ < AssistantChat chat = { chat } />
63
+ </ AssistantActionsContext . Provider >
64
+ ) ;
42
65
return {
43
66
result,
44
67
chat,
68
+ ensureOptInAndSendStub,
45
69
} ;
46
70
}
47
71
@@ -156,8 +180,8 @@ describe('AssistantChat', function () {
156
180
) ;
157
181
} ) ;
158
182
159
- it ( 'calls sendMessage when form is submitted' , async function ( ) {
160
- const { chat , result } = renderWithChat ( [ ] ) ;
183
+ it ( 'calls ensureOptInAndSend when form is submitted' , async function ( ) {
184
+ const { ensureOptInAndSendStub , result } = renderWithChat ( [ ] ) ;
161
185
const { track } = result ;
162
186
const inputField = screen . getByPlaceholderText (
163
187
'Ask MongoDB Assistant a question'
@@ -167,8 +191,7 @@ describe('AssistantChat', function () {
167
191
userEvent . type ( inputField , 'What is aggregation?' ) ;
168
192
userEvent . click ( sendButton ) ;
169
193
170
- expect ( chat . sendMessage . calledWith ( { text : 'What is aggregation?' } ) ) . to . be
171
- . true ;
194
+ expect ( ensureOptInAndSendStub . called ) . to . be . true ;
172
195
173
196
await waitFor ( ( ) => {
174
197
expect ( track ) . to . have . been . calledWith ( 'Assistant Prompt Submitted' , {
@@ -193,7 +216,7 @@ describe('AssistantChat', function () {
193
216
} ) ;
194
217
195
218
it ( 'trims whitespace from input before sending' , async function ( ) {
196
- const { chat , result } = renderWithChat ( [ ] ) ;
219
+ const { ensureOptInAndSendStub , result } = renderWithChat ( [ ] ) ;
197
220
const { track } = result ;
198
221
199
222
const inputField = screen . getByPlaceholderText (
@@ -203,8 +226,7 @@ describe('AssistantChat', function () {
203
226
userEvent . type ( inputField , ' What is sharding? ' ) ;
204
227
userEvent . click ( screen . getByLabelText ( 'Send message' ) ) ;
205
228
206
- expect ( chat . sendMessage . calledWith ( { text : 'What is sharding?' } ) ) . to . be
207
- . true ;
229
+ expect ( ensureOptInAndSendStub . called ) . to . be . true ;
208
230
209
231
await waitFor ( ( ) => {
210
232
expect ( track ) . to . have . been . calledWith ( 'Assistant Prompt Submitted' , {
@@ -213,8 +235,8 @@ describe('AssistantChat', function () {
213
235
} ) ;
214
236
} ) ;
215
237
216
- it ( 'does not call sendMessage when input is empty or whitespace-only' , function ( ) {
217
- const { chat } = renderWithChat ( [ ] ) ;
238
+ it ( 'does not call ensureOptInAndSend when input is empty or whitespace-only' , function ( ) {
239
+ const { ensureOptInAndSendStub } = renderWithChat ( [ ] ) ;
218
240
219
241
const inputField = screen . getByPlaceholderText (
220
242
'Ask MongoDB Assistant a question'
@@ -223,12 +245,12 @@ describe('AssistantChat', function () {
223
245
224
246
// Test empty input
225
247
userEvent . click ( chatForm ) ;
226
- expect ( chat . sendMessage . notCalled ) . to . be . true ;
248
+ expect ( ensureOptInAndSendStub . notCalled ) . to . be . true ;
227
249
228
250
// Test whitespace-only input
229
251
userEvent . type ( inputField , ' ' ) ;
230
252
userEvent . click ( chatForm ) ;
231
- expect ( chat . sendMessage . notCalled ) . to . be . true ;
253
+ expect ( ensureOptInAndSendStub . notCalled ) . to . be . true ;
232
254
} ) ;
233
255
234
256
it ( 'displays user and assistant messages with different styling' , function ( ) {
0 commit comments