|
| 1 | +import { |
| 2 | + databaseCollectionParameters, |
| 3 | + validateToolMetadata, |
| 4 | + validateThrowsForInvalidArguments, |
| 5 | + getResponseElements, |
| 6 | +} from "../../../helpers.js"; |
| 7 | +import { describeWithMongoDB, validateAutoConnectBehavior } from "../mongodbHelpers.js"; |
| 8 | + |
| 9 | +describeWithMongoDB("aggregate tool", (integration) => { |
| 10 | + validateToolMetadata(integration, "aggregate", "Run an aggregation against a MongoDB collection", [ |
| 11 | + ...databaseCollectionParameters, |
| 12 | + { |
| 13 | + name: "pipeline", |
| 14 | + description: "An array of aggregation stages to execute", |
| 15 | + type: "array", |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + ]); |
| 19 | + |
| 20 | + validateThrowsForInvalidArguments(integration, "aggregate", [ |
| 21 | + {}, |
| 22 | + { database: "test", collection: "foo" }, |
| 23 | + { database: test, pipeline: [] }, |
| 24 | + { database: "test", collection: "foo", pipeline: {} }, |
| 25 | + { database: "test", collection: "foo", pipeline: [], extra: "extra" }, |
| 26 | + { database: "test", collection: [], pipeline: [] }, |
| 27 | + { database: 123, collection: "foo", pipeline: [] }, |
| 28 | + ]); |
| 29 | + |
| 30 | + it("can run aggragation on non-existent database", async () => { |
| 31 | + await integration.connectMcpClient(); |
| 32 | + const response = await integration.mcpClient().callTool({ |
| 33 | + name: "aggregate", |
| 34 | + arguments: { database: "non-existent", collection: "people", pipeline: [{ $match: { name: "Peter" } }] }, |
| 35 | + }); |
| 36 | + |
| 37 | + const elements = getResponseElements(response.content); |
| 38 | + expect(elements).toHaveLength(1); |
| 39 | + expect(elements[0].text).toEqual('Found 0 documents in the collection "people":'); |
| 40 | + }); |
| 41 | + |
| 42 | + it("can run aggragation on an empty collection", async () => { |
| 43 | + await integration.mongoClient().db(integration.randomDbName()).createCollection("people"); |
| 44 | + |
| 45 | + await integration.connectMcpClient(); |
| 46 | + const response = await integration.mcpClient().callTool({ |
| 47 | + name: "aggregate", |
| 48 | + arguments: { |
| 49 | + database: integration.randomDbName(), |
| 50 | + collection: "people", |
| 51 | + pipeline: [{ $match: { name: "Peter" } }], |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + const elements = getResponseElements(response.content); |
| 56 | + expect(elements).toHaveLength(1); |
| 57 | + expect(elements[0].text).toEqual('Found 0 documents in the collection "people":'); |
| 58 | + }); |
| 59 | + |
| 60 | + it("can run aggragation on an existing collection", async () => { |
| 61 | + const mongoClient = integration.mongoClient(); |
| 62 | + await mongoClient |
| 63 | + .db(integration.randomDbName()) |
| 64 | + .collection("people") |
| 65 | + .insertMany([ |
| 66 | + { name: "Peter", age: 5 }, |
| 67 | + { name: "Laura", age: 10 }, |
| 68 | + { name: "Søren", age: 15 }, |
| 69 | + ]); |
| 70 | + |
| 71 | + await integration.connectMcpClient(); |
| 72 | + const response = await integration.mcpClient().callTool({ |
| 73 | + name: "aggregate", |
| 74 | + arguments: { |
| 75 | + database: integration.randomDbName(), |
| 76 | + collection: "people", |
| 77 | + pipeline: [{ $match: { age: { $gt: 8 } } }, { $sort: { name: -1 } }], |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const elements = getResponseElements(response.content); |
| 82 | + expect(elements).toHaveLength(3); |
| 83 | + expect(elements[0].text).toEqual('Found 2 documents in the collection "people":'); |
| 84 | + expect(JSON.parse(elements[1].text)).toEqual({ _id: expect.any(Object), name: "Søren", age: 15 }); |
| 85 | + expect(JSON.parse(elements[2].text)).toEqual({ _id: expect.any(Object), name: "Laura", age: 10 }); |
| 86 | + }); |
| 87 | + |
| 88 | + validateAutoConnectBehavior(integration, "aggregate", () => { |
| 89 | + return { |
| 90 | + args: { |
| 91 | + database: integration.randomDbName(), |
| 92 | + collection: "coll1", |
| 93 | + pipeline: [{ $match: { name: "Liva" } }], |
| 94 | + }, |
| 95 | + expectedResponse: 'Found 0 documents in the collection "coll1"', |
| 96 | + }; |
| 97 | + }); |
| 98 | +}); |
0 commit comments