1- import { beforeEach , describe , expect , it , vi } from "vitest" ;
1+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
22import type { Document , Collection } from "mongodb" ;
33import {
44 getResponseContent ,
@@ -228,37 +228,38 @@ describeWithMongoDB("find tool", (integration) => {
228228describeWithMongoDB (
229229 "find tool with configured max documents per query" ,
230230 ( integration ) => {
231+ beforeEach ( async ( ) => {
232+ await freshInsertDocuments ( {
233+ collection : integration . mongoClient ( ) . db ( integration . randomDbName ( ) ) . collection ( "foo" ) ,
234+ count : 1000 ,
235+ } ) ;
236+ } ) ;
237+
238+ afterEach ( ( ) => {
239+ vi . resetAllMocks ( ) ;
240+ } ) ;
241+
231242 describe ( "when the provided limit is lower than the configured max limit" , ( ) => {
232243 it ( "should return documents limited to the provided limit" , async ( ) => {
233- await freshInsertDocuments ( {
234- collection : integration . mongoClient ( ) . db ( integration . randomDbName ( ) ) . collection ( "foo" ) ,
235- count : 1000 ,
236- } ) ;
237244 await integration . connectMcpClient ( ) ;
238245 const response = await integration . mcpClient ( ) . callTool ( {
239246 name : "find" ,
240247 arguments : {
241248 database : integration . randomDbName ( ) ,
242249 collection : "foo" ,
243250 filter : { } ,
244- // default is 10
245- limit : undefined ,
251+ limit : 8 ,
246252 } ,
247253 } ) ;
248254
249255 const content = getResponseContent ( response ) ;
250- expect ( content ) . toContain ( `Query on collection "foo" resulted in 10 documents.` ) ;
251- expect ( content ) . toContain ( `Returning 10 documents while respecting the applied limits.` ) ;
256+ expect ( content ) . toContain ( `Query on collection "foo" resulted in 8 documents.` ) ;
257+ expect ( content ) . toContain ( `Returning 8 documents while respecting the applied limits.` ) ;
252258 } ) ;
253259 } ) ;
254260
255261 describe ( "when the provided limit is larger than the configured max limit" , ( ) => {
256262 it ( "should return documents limited to the configured max limit" , async ( ) => {
257- await freshInsertDocuments ( {
258- collection : integration . mongoClient ( ) . db ( integration . randomDbName ( ) ) . collection ( "foo" ) ,
259- count : 1000 ,
260- } ) ;
261-
262263 await integration . connectMcpClient ( ) ;
263264 const response = await integration . mcpClient ( ) . callTool ( {
264265 name : "find" ,
@@ -272,17 +273,13 @@ describeWithMongoDB(
272273
273274 const content = getResponseContent ( response ) ;
274275 expect ( content ) . toContain ( `Query on collection "foo" resulted in 1000 documents.` ) ;
275- expect ( content ) . toContain ( `Returning 20 documents while respecting the applied limits.` ) ;
276+ expect ( content ) . toContain ( `Returning 10 documents while respecting the applied limits.` ) ;
276277 } ) ;
277278 } ) ;
278279
279280 describe ( "when counting documents exceed the configured count maxTimeMS" , ( ) => {
280- it ( "should abort discard count operation and respond with indeterminable count" , async ( ) => {
281+ it ( "should abort count operation and respond with indeterminable count" , async ( ) => {
281282 vi . spyOn ( constants , "QUERY_COUNT_MAX_TIME_MS_CAP" , "get" ) . mockReturnValue ( 0.1 ) ;
282- await freshInsertDocuments ( {
283- collection : integration . mongoClient ( ) . db ( integration . randomDbName ( ) ) . collection ( "foo" ) ,
284- count : 1000 ,
285- } ) ;
286283 await integration . connectMcpClient ( ) ;
287284 const response = await integration . mcpClient ( ) . callTool ( {
288285 name : "find" ,
@@ -293,22 +290,23 @@ describeWithMongoDB(
293290
294291 const docs = getDocsFromUntrustedContent ( content ) ;
295292 expect ( docs . length ) . toEqual ( 10 ) ;
296- vi . resetAllMocks ( ) ;
297293 } ) ;
298294 } ) ;
299295 } ,
300- ( ) => ( { ...defaultTestConfig , maxDocumentsPerQuery : 20 } )
296+ ( ) => ( { ...defaultTestConfig , maxDocumentsPerQuery : 10 } )
301297) ;
302298
303299describeWithMongoDB (
304300 "find tool with configured max bytes per query" ,
305301 ( integration ) => {
302+ beforeEach ( async ( ) => {
303+ await freshInsertDocuments ( {
304+ collection : integration . mongoClient ( ) . db ( integration . randomDbName ( ) ) . collection ( "foo" ) ,
305+ count : 1000 ,
306+ } ) ;
307+ } ) ;
306308 describe ( "when the provided maxBytesPerQuery is hit" , ( ) => {
307309 it ( "should return only the documents that could fit in maxBytesPerQuery limit" , async ( ) => {
308- await freshInsertDocuments ( {
309- collection : integration . mongoClient ( ) . db ( integration . randomDbName ( ) ) . collection ( "foo" ) ,
310- count : 1000 ,
311- } ) ;
312310 await integration . connectMcpClient ( ) ;
313311 const response = await integration . mcpClient ( ) . callTool ( {
314312 name : "find" ,
0 commit comments