1- import { describe , expect , it } from "vitest" ;
1+ import { describe , expect , it , vi } from "vitest" ;
22import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
33import {
44 expectDefined ,
55 validateToolMetadata ,
66 validateThrowsForInvalidArguments ,
77 getResponseElements ,
8+ getDataFromUntrustedContent ,
89} from "../../helpers.js" ;
910import { 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+ } ) ) ;
1017
1118describeWithAssistant ( "search-knowledge" , ( integration ) => {
1219 const { mockSearchResults, mockAPIError, mockNetworkError } = makeMockAssistantAPI ( ) ;
@@ -84,20 +91,34 @@ describeWithAssistant("search-knowledge", (integration) => {
8491
8592 const elements = getResponseElements ( response . content ) ;
8693
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+
87102 // 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+ } ,
94111 } ) ;
95112
96113 // 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+ } ,
101122 } ) ;
102123 } ) ;
103124
@@ -127,15 +148,22 @@ describeWithAssistant("search-knowledge", (integration) => {
127148
128149 expect ( response . isError ) . toBeFalsy ( ) ;
129150 expect ( response . content ) . toBeInstanceOf ( Array ) ;
130- expect ( response . content ) . toHaveLength ( 1 ) ;
151+ expect ( response . content ) . toHaveLength ( 2 ) ;
131152
132153 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+ } ,
139167 } ) ;
140168 } ) ;
141169
@@ -148,7 +176,10 @@ describeWithAssistant("search-knowledge", (integration) => {
148176
149177 expect ( response . isError ) . toBeFalsy ( ) ;
150178 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." ) ;
152183 } ) ;
153184
154185 it ( "uses default limit when not specified" , async ( ) => {
@@ -168,7 +199,14 @@ describeWithAssistant("search-knowledge", (integration) => {
168199 . callTool ( { name : "search-knowledge" , arguments : { query : "test query" } } ) ) as CallToolResult ;
169200
170201 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 ) ;
172210 } ) ;
173211 } ) ;
174212
0 commit comments