|
| 1 | +import { |
| 2 | + connect, |
| 3 | + jestTestCluster, |
| 4 | + jestTestMCPClient, |
| 5 | + getResponseContent, |
| 6 | + validateParameters, |
| 7 | + dbOperationParameters, |
| 8 | +} from "../../../helpers.js"; |
| 9 | +import { toIncludeSameMembers } from "jest-extended"; |
| 10 | +import { McpError } from "@modelcontextprotocol/sdk/types.js"; |
| 11 | +import { ObjectId } from "mongodb"; |
| 12 | + |
| 13 | +describe("count tool", () => { |
| 14 | + const client = jestTestMCPClient(); |
| 15 | + const cluster = jestTestCluster(); |
| 16 | + |
| 17 | + let randomDbName: string; |
| 18 | + beforeEach(() => { |
| 19 | + randomDbName = new ObjectId().toString(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should have correct metadata", async () => { |
| 23 | + const { tools } = await client().listTools(); |
| 24 | + const listCollections = tools.find((tool) => tool.name === "count")!; |
| 25 | + expect(listCollections).toBeDefined(); |
| 26 | + expect(listCollections.description).toBe("Gets the number of documents in a MongoDB collection"); |
| 27 | + |
| 28 | + validateParameters(listCollections, [ |
| 29 | + { |
| 30 | + name: "query", |
| 31 | + description: |
| 32 | + "The query filter to count documents. Matches the syntax of the filter argument of db.collection.count()", |
| 33 | + type: "object", |
| 34 | + required: false, |
| 35 | + }, |
| 36 | + ...dbOperationParameters, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("with invalid arguments", () => { |
| 41 | + const args = [ |
| 42 | + {}, |
| 43 | + { database: 123, collection: "bar" }, |
| 44 | + { foo: "bar", database: "test", collection: "bar" }, |
| 45 | + { collection: [], database: "test" }, |
| 46 | + { collection: "bar", database: "test", query: "{ $gt: { foo: 5 } }" }, |
| 47 | + ]; |
| 48 | + for (const arg of args) { |
| 49 | + it(`throws a schema error for: ${JSON.stringify(arg)}`, async () => { |
| 50 | + await connect(client(), cluster()); |
| 51 | + try { |
| 52 | + await client().callTool({ name: "count", arguments: arg }); |
| 53 | + expect.fail("Expected an error to be thrown"); |
| 54 | + } catch (error) { |
| 55 | + expect(error).toBeInstanceOf(McpError); |
| 56 | + const mcpError = error as McpError; |
| 57 | + expect(mcpError.code).toEqual(-32602); |
| 58 | + expect(mcpError.message).toContain("Invalid arguments for tool count"); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns 0 when database doesn't exist", async () => { |
| 65 | + await connect(client(), cluster()); |
| 66 | + const response = await client().callTool({ |
| 67 | + name: "count", |
| 68 | + arguments: { database: "non-existent", collection: "foos" }, |
| 69 | + }); |
| 70 | + const content = getResponseContent(response.content); |
| 71 | + expect(content).toEqual('Found 0 documents in the collection "foos"'); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns 0 when collection doesn't exist", async () => { |
| 75 | + await connect(client(), cluster()); |
| 76 | + const mongoClient = cluster().getClient(); |
| 77 | + await mongoClient.db(randomDbName).collection("bar").insertOne({}); |
| 78 | + const response = await client().callTool({ |
| 79 | + name: "count", |
| 80 | + arguments: { database: randomDbName, collection: "non-existent" }, |
| 81 | + }); |
| 82 | + const content = getResponseContent(response.content); |
| 83 | + expect(content).toEqual('Found 0 documents in the collection "non-existent"'); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("with existing database", () => { |
| 87 | + beforeEach(async () => { |
| 88 | + const mongoClient = cluster().getClient(); |
| 89 | + await mongoClient |
| 90 | + .db(randomDbName) |
| 91 | + .collection("foo") |
| 92 | + .insertMany([ |
| 93 | + { name: "Peter", age: 5 }, |
| 94 | + { name: "Parker", age: 10 }, |
| 95 | + { name: "George", age: 15 }, |
| 96 | + ]); |
| 97 | + }); |
| 98 | + |
| 99 | + const testCases = [ |
| 100 | + { filter: undefined, expectedCount: 3 }, |
| 101 | + { filter: {}, expectedCount: 3 }, |
| 102 | + { filter: { age: { $lt: 15 } }, expectedCount: 2 }, |
| 103 | + { filter: { age: { $gt: 5 }, name: { $regex: "^P" } }, expectedCount: 1 }, |
| 104 | + ]; |
| 105 | + for (const testCase of testCases) { |
| 106 | + it(`returns ${testCase.expectedCount} documents for filter ${JSON.stringify(testCase.filter)}`, async () => { |
| 107 | + await connect(client(), cluster()); |
| 108 | + const response = await client().callTool({ |
| 109 | + name: "count", |
| 110 | + arguments: { database: randomDbName, collection: "foo", query: testCase.filter }, |
| 111 | + }); |
| 112 | + |
| 113 | + const content = getResponseContent(response.content); |
| 114 | + expect(content).toEqual(`Found ${testCase.expectedCount} documents in the collection "foo"`); |
| 115 | + }); |
| 116 | + } |
| 117 | + }); |
| 118 | +}); |
0 commit comments