|
| 1 | +import { expect } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { |
| 4 | + DocsProviderTransport, |
| 5 | + shouldExcludeMessage, |
| 6 | +} from './docs-provider-transport'; |
| 7 | +import type { AssistantMessage } from './compass-assistant-provider'; |
| 8 | +import { MockLanguageModelV2 } from 'ai/test'; |
| 9 | +import type { UIMessageChunk } from 'ai'; |
| 10 | +import { waitFor } from '@mongodb-js/testing-library-compass'; |
| 11 | + |
| 12 | +describe.only('DocsProviderTransport', function () { |
| 13 | + describe('shouldExcludeMessage', function () { |
| 14 | + it('returns false for messages without confirmation metadata', function () { |
| 15 | + const message: AssistantMessage = { |
| 16 | + id: 'test-1', |
| 17 | + role: 'user', |
| 18 | + parts: [{ type: 'text', text: 'Hello' }], |
| 19 | + }; |
| 20 | + |
| 21 | + expect(shouldExcludeMessage(message)).to.be.false; |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns true for confirmation messages', function () { |
| 25 | + const message: AssistantMessage = { |
| 26 | + id: 'test-5', |
| 27 | + role: 'assistant', |
| 28 | + parts: [{ type: 'text', text: 'Response' }], |
| 29 | + metadata: { |
| 30 | + confirmation: { |
| 31 | + state: 'pending', |
| 32 | + description: 'Confirm this action', |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | + |
| 37 | + expect(shouldExcludeMessage(message)).to.be.true; |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('sending messages', function () { |
| 42 | + let mockModel: MockLanguageModelV2; |
| 43 | + let doStream: sinon.SinonStub; |
| 44 | + let transport: DocsProviderTransport; |
| 45 | + let abortController: AbortController; |
| 46 | + let sendMessages: ( |
| 47 | + params: Partial<Parameters<typeof transport.sendMessages>[0]> |
| 48 | + ) => Promise<ReadableStream<UIMessageChunk>>; |
| 49 | + |
| 50 | + beforeEach(function () { |
| 51 | + // Mock the OpenAI client |
| 52 | + doStream = sinon.stub().returns({ |
| 53 | + stream: DocsProviderTransport.emptyStream, |
| 54 | + request: { |
| 55 | + body: { |
| 56 | + messages: [], |
| 57 | + }, |
| 58 | + }, |
| 59 | + }); |
| 60 | + mockModel = new MockLanguageModelV2({ |
| 61 | + doStream, |
| 62 | + }); |
| 63 | + abortController = new AbortController(); |
| 64 | + transport = new DocsProviderTransport({ |
| 65 | + instructions: 'Test instructions for MongoDB assistance', |
| 66 | + model: mockModel, |
| 67 | + }); |
| 68 | + sendMessages = (params) => |
| 69 | + transport.sendMessages({ |
| 70 | + trigger: 'submit-message', |
| 71 | + chatId: 'test-chat', |
| 72 | + messageId: undefined, |
| 73 | + abortSignal: abortController.signal, |
| 74 | + messages: [], |
| 75 | + ...params, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(function () { |
| 80 | + sinon.restore(); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('sendMessages', function () { |
| 84 | + const userMessage: AssistantMessage = { |
| 85 | + id: 'included1', |
| 86 | + role: 'user', |
| 87 | + parts: [{ type: 'text', text: 'User message' }], |
| 88 | + }; |
| 89 | + const confirmationPendingMessage: AssistantMessage = { |
| 90 | + id: 'test', |
| 91 | + role: 'assistant', |
| 92 | + parts: [{ type: 'text', text: 'Response' }], |
| 93 | + metadata: { |
| 94 | + confirmation: { |
| 95 | + state: 'pending', |
| 96 | + description: 'Confirm this action', |
| 97 | + }, |
| 98 | + }, |
| 99 | + }; |
| 100 | + const confirmationConfirmedMessage: AssistantMessage = { |
| 101 | + id: 'test', |
| 102 | + role: 'assistant', |
| 103 | + parts: [{ type: 'text', text: 'Response' }], |
| 104 | + metadata: { |
| 105 | + confirmation: { |
| 106 | + state: 'confirmed', |
| 107 | + description: 'Confirmed action', |
| 108 | + }, |
| 109 | + }, |
| 110 | + }; |
| 111 | + it('returns empty stream when last message should be excluded', async function () { |
| 112 | + const messages: AssistantMessage[] = [ |
| 113 | + userMessage, |
| 114 | + confirmationConfirmedMessage, |
| 115 | + ]; |
| 116 | + |
| 117 | + const result = await sendMessages({ |
| 118 | + messages, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(result).to.equal(DocsProviderTransport.emptyStream); |
| 122 | + expect(mockModel.doStreamCalls).to.be.empty; |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns empty stream when all messages are filtered out', async function () { |
| 126 | + const messages: AssistantMessage[] = [ |
| 127 | + confirmationPendingMessage, |
| 128 | + confirmationConfirmedMessage, |
| 129 | + ]; |
| 130 | + |
| 131 | + const result = await sendMessages({ |
| 132 | + messages, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(result).to.equal(DocsProviderTransport.emptyStream); |
| 136 | + expect(mockModel.doStreamCalls).to.be.empty; |
| 137 | + }); |
| 138 | + |
| 139 | + it('sends filtered messages to AI when valid messages exist', async function () { |
| 140 | + await sendMessages({ |
| 141 | + messages: [userMessage], |
| 142 | + }); |
| 143 | + |
| 144 | + await waitFor(() => { |
| 145 | + expect(doStream).to.have.been.calledOnce; |
| 146 | + expect(doStream.firstCall.args[0]).to.deep.include({ |
| 147 | + prompt: [ |
| 148 | + { |
| 149 | + role: 'user', |
| 150 | + providerOptions: undefined, |
| 151 | + content: [ |
| 152 | + { |
| 153 | + type: 'text', |
| 154 | + text: 'User message', |
| 155 | + providerOptions: undefined, |
| 156 | + }, |
| 157 | + ], |
| 158 | + }, |
| 159 | + ], |
| 160 | + }); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it('sends only valid messages when confirmation required messages exist', async function () { |
| 165 | + await sendMessages({ |
| 166 | + messages: [ |
| 167 | + confirmationConfirmedMessage, |
| 168 | + confirmationPendingMessage, |
| 169 | + userMessage, |
| 170 | + ], |
| 171 | + }); |
| 172 | + |
| 173 | + await waitFor(() => { |
| 174 | + expect(doStream).to.have.been.calledOnce; |
| 175 | + expect(doStream.firstCall.args[0]).to.deep.include({ |
| 176 | + prompt: [ |
| 177 | + { |
| 178 | + role: 'user', |
| 179 | + providerOptions: undefined, |
| 180 | + content: [ |
| 181 | + { |
| 182 | + type: 'text', |
| 183 | + text: 'User message', |
| 184 | + providerOptions: undefined, |
| 185 | + }, |
| 186 | + ], |
| 187 | + }, |
| 188 | + ], |
| 189 | + }); |
| 190 | + }); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + // We currently do not support reconnecting to streams but we may want to in the future |
| 195 | + describe('reconnectToStream', function () { |
| 196 | + it('always returns null', async function () { |
| 197 | + const result = await transport.reconnectToStream(); |
| 198 | + expect(result).to.be.null; |
| 199 | + }); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments