1
- import { describe , expect , it } from "vitest" ;
1
+ import { describe , expect , it , vi } from "vitest" ;
2
2
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
3
3
import {
4
4
expectDefined ,
5
5
validateToolMetadata ,
6
6
validateThrowsForInvalidArguments ,
7
7
getResponseElements ,
8
+ getDataFromUntrustedContent ,
8
9
} from "../../helpers.js" ;
9
10
import { describeWithAssistant , makeMockAssistantAPI } from "./assistantHelpers.js" ;
11
+ import { parse as yamlParse } from "yaml" ;
12
+
13
+ // Mock the devtools-proxy-support module
14
+ vi . mock ( "@mongodb-js/devtools-proxy-support" , ( ) => ( {
15
+ createFetch : vi . fn ( ) ,
16
+ } ) ) ;
10
17
11
18
describeWithAssistant ( "search-knowledge" , ( integration ) => {
12
19
const { mockSearchResults, mockAPIError, mockNetworkError } = makeMockAssistantAPI ( ) ;
@@ -84,20 +91,34 @@ describeWithAssistant("search-knowledge", (integration) => {
84
91
85
92
const elements = getResponseElements ( response . content ) ;
86
93
94
+ // First element is the description
95
+ expect ( elements [ 0 ] ?. text ) . toBe ( "Found 2 results in the MongoDB Assistant knowledge base." ) ;
96
+
97
+ // Second element contains the YAML data
98
+ expect ( elements [ 1 ] ?. text ) . toContain ( "<untrusted-user-data-" ) ;
99
+ const yamlData = getDataFromUntrustedContent ( elements [ 1 ] ?. text ?? "" ) ;
100
+ const results = yamlParse ( yamlData ) ;
101
+
87
102
// Check first result
88
- expect ( elements [ 0 ] ?. text ) . toBe (
89
- "The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines."
90
- ) ;
91
- expect ( elements [ 0 ] ?. _meta ) . toEqual ( {
92
- tags : [ "aggregation" , "pipeline" ] ,
93
- source : "mongodb-manual" ,
103
+ expect ( results [ 0 ] ) . toMatchObject ( {
104
+ url : "https://docs.mongodb.com/manual/aggregation/" ,
105
+ title : "Aggregation Pipeline" ,
106
+ text : "The aggregation pipeline is a framework for data aggregation modeled on the concept of data processing pipelines." ,
107
+ metadata : {
108
+ tags : [ "aggregation" , "pipeline" ] ,
109
+ source : "mongodb-manual" ,
110
+ } ,
94
111
} ) ;
95
112
96
113
// Check second result
97
- expect ( elements [ 1 ] ?. text ) . toBe ( "Aggregation pipeline operations have an array of operators available." ) ;
98
- expect ( elements [ 1 ] ?. _meta ) . toEqual ( {
99
- tags : [ "aggregation" , "operators" ] ,
100
- source : "mongodb-manual" ,
114
+ expect ( results [ 1 ] ) . toMatchObject ( {
115
+ url : "https://docs.mongodb.com/manual/reference/operator/aggregation/" ,
116
+ title : "Aggregation Pipeline Operators" ,
117
+ text : "Aggregation pipeline operations have an array of operators available." ,
118
+ metadata : {
119
+ tags : [ "aggregation" , "operators" ] ,
120
+ source : "mongodb-manual" ,
121
+ } ,
101
122
} ) ;
102
123
} ) ;
103
124
@@ -127,15 +148,22 @@ describeWithAssistant("search-knowledge", (integration) => {
127
148
128
149
expect ( response . isError ) . toBeFalsy ( ) ;
129
150
expect ( response . content ) . toBeInstanceOf ( Array ) ;
130
- expect ( response . content ) . toHaveLength ( 1 ) ;
151
+ expect ( response . content ) . toHaveLength ( 2 ) ;
131
152
132
153
const elements = getResponseElements ( response . content ) ;
133
- expect ( elements [ 0 ] ?. text ) . toBe (
134
- "The official MongoDB driver for Node.js provides a high-level API on top of mongodb-core."
135
- ) ;
136
- expect ( elements [ 0 ] ?. _meta ) . toEqual ( {
137
- tags : [ "driver" , "nodejs" ] ,
138
- source : "node-driver" ,
154
+ expect ( elements [ 0 ] ?. text ) . toBe ( "Found 1 results in the MongoDB Assistant knowledge base." ) ;
155
+
156
+ const yamlData = getDataFromUntrustedContent ( elements [ 1 ] ?. text ?? "" ) ;
157
+ const results = yamlParse ( yamlData ) ;
158
+
159
+ expect ( results [ 0 ] ) . toMatchObject ( {
160
+ url : "https://mongodb.github.io/node-mongodb-native/" ,
161
+ title : "Node.js Driver" ,
162
+ text : "The official MongoDB driver for Node.js provides a high-level API on top of mongodb-core." ,
163
+ metadata : {
164
+ tags : [ "driver" , "nodejs" ] ,
165
+ source : "node-driver" ,
166
+ } ,
139
167
} ) ;
140
168
} ) ;
141
169
@@ -148,7 +176,10 @@ describeWithAssistant("search-knowledge", (integration) => {
148
176
149
177
expect ( response . isError ) . toBeFalsy ( ) ;
150
178
expect ( response . content ) . toBeInstanceOf ( Array ) ;
151
- expect ( response . content ) . toHaveLength ( 0 ) ;
179
+ expect ( response . content ) . toHaveLength ( 2 ) ;
180
+
181
+ const elements = getResponseElements ( response . content ) ;
182
+ expect ( elements [ 0 ] ?. text ) . toBe ( "Found 0 results in the MongoDB Assistant knowledge base." ) ;
152
183
} ) ;
153
184
154
185
it ( "uses default limit when not specified" , async ( ) => {
@@ -168,7 +199,14 @@ describeWithAssistant("search-knowledge", (integration) => {
168
199
. callTool ( { name : "search-knowledge" , arguments : { query : "test query" } } ) ) as CallToolResult ;
169
200
170
201
expect ( response . isError ) . toBeFalsy ( ) ;
171
- expect ( response . content ) . toHaveLength ( 5 ) ;
202
+ expect ( response . content ) . toHaveLength ( 2 ) ;
203
+
204
+ const elements = getResponseElements ( response . content ) ;
205
+ expect ( elements [ 0 ] ?. text ) . toBe ( "Found 5 results in the MongoDB Assistant knowledge base." ) ;
206
+
207
+ const yamlData = getDataFromUntrustedContent ( elements [ 1 ] ?. text ?? "" ) ;
208
+ const results = yamlParse ( yamlData ) ;
209
+ expect ( results ) . toHaveLength ( 5 ) ;
172
210
} ) ;
173
211
} ) ;
174
212
0 commit comments