Skip to content

Commit 76c6025

Browse files
committed
chore(compass-assistant): fix assistant tests
1 parent c1c698c commit 76c6025

File tree

3 files changed

+99
-48
lines changed

3 files changed

+99
-48
lines changed

packages/compass-assistant/src/assistant-chat.spec.tsx

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import React from 'react';
2-
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
2+
import {
3+
render,
4+
screen,
5+
userEvent,
6+
waitFor,
7+
} from '@mongodb-js/testing-library-compass';
38
import { AssistantChat } from './assistant-chat';
49
import { expect } from 'chai';
510
import { createMockChat } from '../test/utils';
611
import type { AssistantMessage } from './compass-assistant-provider';
712

8-
// TODO: some internal logic in lg-chat breaks all these tests, re-enable the tests
9-
describe.skip('AssistantChat', function () {
13+
describe('AssistantChat', function () {
14+
// Mock scrollTo method for DOM elements to prevent test failures
15+
before(function () {
16+
if (!Element.prototype.scrollTo) {
17+
Element.prototype.scrollTo = function () {
18+
// No-op implementation for testing
19+
};
20+
}
21+
});
1022
const mockMessages: AssistantMessage[] = [
1123
{
1224
id: 'user',
@@ -36,8 +48,10 @@ describe.skip('AssistantChat', function () {
3648
it('renders input field and send button', function () {
3749
renderWithChat([]);
3850

39-
const inputField = screen.getByTestId('assistant-chat-input');
40-
const sendButton = screen.getByTestId('assistant-chat-send-button');
51+
const inputField = screen.getByPlaceholderText(
52+
'Ask MongoDB Assistant a question'
53+
);
54+
const sendButton = screen.getByLabelText('Send message');
4155

4256
expect(inputField).to.exist;
4357
expect(sendButton).to.exist;
@@ -47,9 +61,9 @@ describe.skip('AssistantChat', function () {
4761
renderWithChat([]);
4862

4963
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
50-
const inputField = screen.getByTestId(
51-
'assistant-chat-input'
52-
) as HTMLInputElement;
64+
const inputField = screen.getByPlaceholderText(
65+
'Ask MongoDB Assistant a question'
66+
) as HTMLTextAreaElement;
5367

5468
userEvent.type(inputField, 'What is MongoDB?');
5569

@@ -60,58 +74,67 @@ describe.skip('AssistantChat', function () {
6074
renderWithChat([]);
6175

6276
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
63-
const sendButton = screen.getByTestId(
64-
'assistant-chat-send-button'
77+
const sendButton = screen.getByLabelText(
78+
'Send message'
6579
) as HTMLButtonElement;
6680

67-
expect(sendButton.disabled).to.be.true;
81+
expect(sendButton.getAttribute('aria-disabled')).to.equal('true');
6882
});
6983

7084
it('send button is enabled when input has text', function () {
7185
renderWithChat([]);
7286

73-
const inputField = screen.getByTestId('assistant-chat-input');
87+
const inputField = screen.getByPlaceholderText(
88+
'Ask MongoDB Assistant a question'
89+
);
7490
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
75-
const sendButton = screen.getByTestId(
76-
'assistant-chat-send-button'
91+
const sendButton = screen.getByLabelText(
92+
'Send message'
7793
) as HTMLButtonElement;
7894

7995
userEvent.type(inputField, 'What is MongoDB?');
8096

8197
expect(sendButton.disabled).to.be.false;
8298
});
8399

84-
it('send button is disabled for whitespace-only input', function () {
100+
// Not currently supported by the LeafyGreen Input Bar
101+
it.skip('send button is disabled for whitespace-only input', async function () {
85102
renderWithChat([]);
86103

87-
const inputField = screen.getByTestId('assistant-chat-input');
104+
const inputField = screen.getByPlaceholderText(
105+
'Ask MongoDB Assistant a question'
106+
);
88107
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
89-
const sendButton = screen.getByTestId(
90-
'assistant-chat-send-button'
108+
const sendButton = screen.getByLabelText(
109+
'Send message'
91110
) as HTMLButtonElement;
92111

93112
userEvent.type(inputField, ' ');
94113

95-
expect(sendButton.disabled).to.be.true;
114+
await waitFor(() => {
115+
expect(sendButton.getAttribute('aria-disabled')).to.equal('true');
116+
});
96117
});
97118

98119
it('displays messages in the chat feed', function () {
99120
renderWithChat(mockMessages);
100121

101122
expect(screen.getByTestId('assistant-message-user')).to.exist;
102123
expect(screen.getByTestId('assistant-message-assistant')).to.exist;
103-
expect(screen.getByTestId('assistant-message-user')).to.have.text(
124+
expect(screen.getByTestId('assistant-message-user')).to.contain.text(
104125
'Hello, MongoDB Assistant!'
105126
);
106-
expect(screen.getByTestId('assistant-message-assistant')).to.have.text(
127+
expect(screen.getByTestId('assistant-message-assistant')).to.contain.text(
107128
'Hello! How can I help you with MongoDB today?'
108129
);
109130
});
110131

111132
it('calls sendMessage when form is submitted', function () {
112133
const { chat } = renderWithChat([]);
113-
const inputField = screen.getByTestId('assistant-chat-input');
114-
const sendButton = screen.getByTestId('assistant-chat-send-button');
134+
const inputField = screen.getByPlaceholderText(
135+
'Ask MongoDB Assistant a question'
136+
);
137+
const sendButton = screen.getByLabelText('Send message');
115138

116139
userEvent.type(inputField, 'What is aggregation?');
117140
userEvent.click(sendButton);
@@ -124,24 +147,26 @@ describe.skip('AssistantChat', function () {
124147
renderWithChat([]);
125148

126149
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
127-
const inputField = screen.getByTestId(
128-
'assistant-chat-input'
129-
) as HTMLInputElement;
150+
const inputField = screen.getByPlaceholderText(
151+
'Ask MongoDB Assistant a question'
152+
) as HTMLTextAreaElement;
130153

131154
userEvent.type(inputField, 'Test message');
132155
expect(inputField.value).to.equal('Test message');
133156

134-
userEvent.click(screen.getByTestId('assistant-chat-send-button'));
157+
userEvent.click(screen.getByLabelText('Send message'));
135158
expect(inputField.value).to.equal('');
136159
});
137160

138161
it('trims whitespace from input before sending', function () {
139162
const { chat } = renderWithChat([]);
140163

141-
const inputField = screen.getByTestId('assistant-chat-input');
164+
const inputField = screen.getByPlaceholderText(
165+
'Ask MongoDB Assistant a question'
166+
);
142167

143168
userEvent.type(inputField, ' What is sharding? ');
144-
userEvent.click(screen.getByTestId('assistant-chat-send-button'));
169+
userEvent.click(screen.getByLabelText('Send message'));
145170

146171
expect(chat.sendMessage.calledWith({ text: 'What is sharding?' })).to.be
147172
.true;
@@ -150,8 +175,10 @@ describe.skip('AssistantChat', function () {
150175
it('does not call sendMessage when input is empty or whitespace-only', function () {
151176
const { chat } = renderWithChat([]);
152177

153-
const inputField = screen.getByTestId('assistant-chat-input');
154-
const chatForm = screen.getByTestId('assistant-chat-form');
178+
const inputField = screen.getByPlaceholderText(
179+
'Ask MongoDB Assistant a question'
180+
);
181+
const chatForm = screen.getByTestId('assistant-chat-input');
155182

156183
// Test empty input
157184
userEvent.click(chatForm);
@@ -169,16 +196,12 @@ describe.skip('AssistantChat', function () {
169196
const userMessage = screen.getByTestId('assistant-message-user');
170197
const assistantMessage = screen.getByTestId('assistant-message-assistant');
171198

172-
// User messages should have different background color than assistant messages
199+
// User messages should have different class names than assistant messages
173200
expect(userMessage).to.exist;
174201
expect(assistantMessage).to.exist;
175202

176-
const userStyle = window.getComputedStyle(userMessage);
177-
const assistantStyle = window.getComputedStyle(assistantMessage);
178-
179-
expect(userStyle.backgroundColor).to.not.equal(
180-
assistantStyle.backgroundColor
181-
);
203+
// Check that they have different class names (indicating different styling)
204+
expect(userMessage.className).to.not.equal(assistantMessage.className);
182205
});
183206

184207
it('handles messages with multiple text parts', function () {

packages/compass-assistant/src/assistant-chat.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
LgChatMessage,
99
LgChatMessageFeed,
1010
LgChatInputBar,
11+
spacing,
12+
css,
1113
} from '@mongodb-js/compass-components';
1214

1315
const { ChatWindow } = LgChatChatWindow;
@@ -20,6 +22,20 @@ interface AssistantChatProps {
2022
chat: Chat<AssistantMessage>;
2123
}
2224

25+
// TODO(COMPASS-9751): These are temporary patches to make the Assistant chat take the entire
26+
// width and height of the drawer since Leafygreen doesn't support this yet.
27+
const assistantChatFixesStyles = css({
28+
// Negative margin to patch the padding of the drawer.
29+
margin: -spacing[400],
30+
'> div, > div > div, > div > div > div, > div > div > div > div': {
31+
height: '100%',
32+
},
33+
});
34+
const messageFeedFixesStyles = css({ height: '100%' });
35+
const chatWindowFixesStyles = css({
36+
height: '100%',
37+
});
38+
2339
export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
2440
chat,
2541
}) => {
@@ -42,19 +58,30 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
4258

4359
const handleMessageSend = useCallback(
4460
(messageBody: string) => {
45-
void sendMessage({ text: messageBody });
61+
const trimmedMessageBody = messageBody.trim();
62+
if (trimmedMessageBody) {
63+
void sendMessage({ text: trimmedMessageBody });
64+
}
4665
},
4766
[sendMessage]
4867
);
4968

5069
return (
51-
<div data-testid="assistant-chat" style={{ height: '100%', width: '100%' }}>
70+
<div
71+
data-testid="assistant-chat"
72+
className={assistantChatFixesStyles}
73+
style={{ height: '100%', width: '100%' }}
74+
>
5275
<LeafyGreenChatProvider variant={Variant.Compact}>
53-
<ChatWindow title="MongoDB Assistant">
54-
<MessageFeed data-testid="assistant-chat-messages">
76+
<ChatWindow title="MongoDB Assistant" className={chatWindowFixesStyles}>
77+
<MessageFeed
78+
data-testid="assistant-chat-messages"
79+
className={messageFeedFixesStyles}
80+
>
5581
{lgMessages.map((messageFields) => (
5682
<Message
5783
key={messageFields.id}
84+
sourceType="markdown"
5885
{...messageFields}
5986
data-testid={`assistant-message-${messageFields.id}`}
6087
/>

packages/compass-assistant/src/compass-assistant-provider.spec.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ describe('AssistantProvider', function () {
8484
);
8585
});
8686

87-
// TODO: some internal logic in lg-chat breaks all these tests, re-enable the tests
88-
describe.skip('with existing chat instance', function () {
87+
describe('with existing chat instance', function () {
8988
before(function () {
9089
// TODO(COMPASS-9618): skip in electron runtime for now, drawer has issues rendering
9190
if ((process as any).type === 'renderer') {
@@ -138,8 +137,10 @@ describe('AssistantProvider', function () {
138137

139138
await renderOpenAssistantDrawer(mockChat);
140139

141-
const input = screen.getByTestId('assistant-chat-input');
142-
const sendButton = screen.getByTestId('assistant-chat-send-button');
140+
const input = screen.getByPlaceholderText(
141+
'Ask MongoDB Assistant a question'
142+
);
143+
const sendButton = screen.getByLabelText('Send message');
143144

144145
userEvent.type(input, 'Hello assistant');
145146
userEvent.click(sendButton);
@@ -176,10 +177,10 @@ describe('AssistantProvider', function () {
176177
await renderOpenAssistantDrawer(mockChat);
177178

178179
userEvent.type(
179-
screen.getByTestId('assistant-chat-input'),
180+
screen.getByPlaceholderText('Ask MongoDB Assistant a question'),
180181
'Hello assistant!'
181182
);
182-
userEvent.click(screen.getByTestId('assistant-chat-send-button'));
183+
userEvent.click(screen.getByLabelText('Send message'));
183184

184185
expect(sendMessageSpy.calledOnce).to.be.true;
185186
expect(sendMessageSpy.firstCall.args[0]).to.deep.include({

0 commit comments

Comments
 (0)