|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + render, |
| 4 | + screen, |
| 5 | + userEvent, |
| 6 | + waitFor, |
| 7 | +} from '@mongodb-js/testing-library-compass'; |
| 8 | +import { |
| 9 | + AssistantProvider, |
| 10 | + CompassAssistantProvider, |
| 11 | +} from './compass-assistant-provider'; |
| 12 | +import { expect } from 'chai'; |
| 13 | +import sinon from 'sinon'; |
| 14 | +import type { UIMessage } from './@ai-sdk/react/use-chat'; |
| 15 | +import { Chat } from './@ai-sdk/react/chat-react'; |
| 16 | + |
| 17 | +import { |
| 18 | + DrawerAnchor, |
| 19 | + DrawerContentProvider, |
| 20 | +} from '@mongodb-js/compass-components'; |
| 21 | +import type { AtlasService } from '@mongodb-js/atlas-service/provider'; |
| 22 | +import { CompassAssistantDrawer } from './compass-assistant-drawer'; |
| 23 | +import { createMockChat } from '../test/utils'; |
| 24 | + |
| 25 | +// Test component that renders AssistantProvider with children |
| 26 | +const TestComponent: React.FunctionComponent<{ |
| 27 | + chat: Chat<UIMessage>; |
| 28 | + autoOpen?: boolean; |
| 29 | +}> = ({ chat, autoOpen }) => { |
| 30 | + return ( |
| 31 | + <DrawerContentProvider> |
| 32 | + <AssistantProvider chat={chat}> |
| 33 | + <DrawerAnchor> |
| 34 | + <div data-testid="provider-children">Provider children</div> |
| 35 | + <CompassAssistantDrawer autoOpen={autoOpen} /> |
| 36 | + </DrawerAnchor> |
| 37 | + </AssistantProvider> |
| 38 | + </DrawerContentProvider> |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +describe('AssistantProvider', function () { |
| 43 | + it('always renders children', function () { |
| 44 | + render(<TestComponent chat={createMockChat({ messages: [] })} />, { |
| 45 | + preferences: { enableAIAssistant: true }, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(screen.getByTestId('provider-children')).to.exist; |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not render assistant drawer when AI assistant is disabled', function () { |
| 52 | + render(<TestComponent chat={createMockChat({ messages: [] })} />, { |
| 53 | + preferences: { enableAIAssistant: false }, |
| 54 | + }); |
| 55 | + |
| 56 | + expect(screen.getByTestId('provider-children')).to.exist; |
| 57 | + // The drawer toolbar button should not exist when disabled |
| 58 | + expect(screen.queryByLabelText('MongoDB Assistant')).to.not.exist; |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders the assistant drawer as the first drawer item when AI assistant is enabled', function () { |
| 62 | + render(<TestComponent chat={createMockChat({ messages: [] })} />, { |
| 63 | + preferences: { enableAIAssistant: true }, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(screen.getByTestId('lg-drawer-toolbar-icon_button-0')).to.have.attr( |
| 67 | + 'aria-label', |
| 68 | + 'MongoDB Assistant' |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('with existing chat instance', function () { |
| 73 | + async function renderOpenAssistantDrawer( |
| 74 | + mockChat: Chat<UIMessage> |
| 75 | + ): Promise<ReturnType<typeof render>> { |
| 76 | + const result = render(<TestComponent chat={mockChat} autoOpen={true} />, { |
| 77 | + preferences: { enableAIAssistant: true }, |
| 78 | + }); |
| 79 | + |
| 80 | + await waitFor(() => { |
| 81 | + expect(screen.getByTestId('assistant-chat')).to.exist; |
| 82 | + }); |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + it('displays messages in the chat feed', async function () { |
| 88 | + const mockMessages: UIMessage[] = [ |
| 89 | + { |
| 90 | + id: '1', |
| 91 | + role: 'user', |
| 92 | + parts: [{ type: 'text', text: 'Test message' }], |
| 93 | + }, |
| 94 | + { |
| 95 | + id: '2', |
| 96 | + role: 'assistant', |
| 97 | + parts: [{ type: 'text', text: 'Test assistant message' }], |
| 98 | + }, |
| 99 | + ]; |
| 100 | + const mockChat = createMockChat({ messages: mockMessages }); |
| 101 | + |
| 102 | + await renderOpenAssistantDrawer(mockChat); |
| 103 | + |
| 104 | + expect(screen.getByTestId('assistant-message-1')).to.exist; |
| 105 | + expect(screen.getByTestId('assistant-message-1')).to.have.text( |
| 106 | + 'Test message' |
| 107 | + ); |
| 108 | + |
| 109 | + expect(screen.getByTestId('assistant-message-2')).to.exist; |
| 110 | + expect(screen.getByTestId('assistant-message-2')).to.have.text( |
| 111 | + 'Test assistant message' |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it('handles message sending with custom chat when drawer is open', async function () { |
| 116 | + const mockChat = new Chat<UIMessage>({ |
| 117 | + messages: [ |
| 118 | + { |
| 119 | + id: 'assistant', |
| 120 | + role: 'assistant', |
| 121 | + parts: [{ type: 'text', text: 'Hello user!' }], |
| 122 | + }, |
| 123 | + ], |
| 124 | + }); |
| 125 | + |
| 126 | + const sendMessageSpy = sinon.spy(mockChat, 'sendMessage'); |
| 127 | + |
| 128 | + await renderOpenAssistantDrawer(mockChat); |
| 129 | + |
| 130 | + const input = screen.getByTestId('assistant-chat-input'); |
| 131 | + const sendButton = screen.getByTestId('assistant-chat-send-button'); |
| 132 | + |
| 133 | + userEvent.type(input, 'Hello assistant'); |
| 134 | + userEvent.click(sendButton); |
| 135 | + |
| 136 | + expect(sendMessageSpy.calledOnce).to.be.true; |
| 137 | + await waitFor(() => { |
| 138 | + expect(sendMessageSpy.firstCall.args[0]).to.deep.include({ |
| 139 | + text: 'Hello assistant', |
| 140 | + }); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('new messages are added to the chat feed when the send button is clicked', async function () { |
| 145 | + const mockChat = new Chat<UIMessage>({ |
| 146 | + messages: [ |
| 147 | + { |
| 148 | + id: 'assistant', |
| 149 | + role: 'assistant', |
| 150 | + parts: [{ type: 'text', text: 'Hello user!' }], |
| 151 | + }, |
| 152 | + ], |
| 153 | + transport: { |
| 154 | + sendMessages: sinon.stub().returns( |
| 155 | + new Promise(() => { |
| 156 | + return new ReadableStream({}); |
| 157 | + }) |
| 158 | + ), |
| 159 | + reconnectToStream: sinon.stub(), |
| 160 | + }, |
| 161 | + }); |
| 162 | + |
| 163 | + const sendMessageSpy = sinon.spy(mockChat, 'sendMessage'); |
| 164 | + |
| 165 | + await renderOpenAssistantDrawer(mockChat); |
| 166 | + |
| 167 | + userEvent.type( |
| 168 | + screen.getByTestId('assistant-chat-input'), |
| 169 | + 'Hello assistant!' |
| 170 | + ); |
| 171 | + userEvent.click(screen.getByTestId('assistant-chat-send-button')); |
| 172 | + |
| 173 | + expect(sendMessageSpy.calledOnce).to.be.true; |
| 174 | + expect(sendMessageSpy.firstCall.args[0]).to.deep.include({ |
| 175 | + text: 'Hello assistant!', |
| 176 | + }); |
| 177 | + |
| 178 | + await waitFor(() => { |
| 179 | + expect(screen.getByText('Hello assistant!')).to.exist; |
| 180 | + }); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('CompassAssistantProvider', function () { |
| 185 | + beforeEach(function () { |
| 186 | + process.env.COMPASS_ASSISTANT_USE_ATLAS_SERVICE_URL = 'true'; |
| 187 | + }); |
| 188 | + |
| 189 | + afterEach(function () { |
| 190 | + delete process.env.COMPASS_ASSISTANT_USE_ATLAS_SERVICE_URL; |
| 191 | + }); |
| 192 | + |
| 193 | + it('uses the Atlas Service assistantApiEndpoint', function () { |
| 194 | + const mockAtlasService = { |
| 195 | + assistantApiEndpoint: sinon |
| 196 | + .stub() |
| 197 | + .returns('https://example.com/assistant/api/v1'), |
| 198 | + }; |
| 199 | + |
| 200 | + const MockedProvider = CompassAssistantProvider.withMockServices({ |
| 201 | + atlasService: mockAtlasService as unknown as AtlasService, |
| 202 | + }); |
| 203 | + |
| 204 | + render( |
| 205 | + <DrawerContentProvider> |
| 206 | + <DrawerAnchor /> |
| 207 | + <MockedProvider chat={new Chat({})} /> |
| 208 | + </DrawerContentProvider>, |
| 209 | + { |
| 210 | + preferences: { enableAIAssistant: true }, |
| 211 | + } |
| 212 | + ); |
| 213 | + |
| 214 | + expect(mockAtlasService.assistantApiEndpoint.calledOnce).to.be.true; |
| 215 | + }); |
| 216 | + }); |
| 217 | +}); |
0 commit comments