|  | 
|  | 1 | +import { | 
|  | 2 | +    connect, | 
|  | 3 | +    jestTestCluster, | 
|  | 4 | +    jestTestMCPClient, | 
|  | 5 | +    getResponseContent, | 
|  | 6 | +    validateParameters, | 
|  | 7 | +} from "../../../helpers.js"; | 
|  | 8 | +import { toIncludeSameMembers } from "jest-extended"; | 
|  | 9 | +import { McpError } from "@modelcontextprotocol/sdk/types.js"; | 
|  | 10 | +import { ObjectId } from "bson"; | 
|  | 11 | + | 
|  | 12 | +describe("createCollection tool", () => { | 
|  | 13 | +    const client = jestTestMCPClient(); | 
|  | 14 | +    const cluster = jestTestCluster(); | 
|  | 15 | + | 
|  | 16 | +    it("should have correct metadata", async () => { | 
|  | 17 | +        const { tools } = await client().listTools(); | 
|  | 18 | +        const listCollections = tools.find((tool) => tool.name === "create-collection")!; | 
|  | 19 | +        expect(listCollections).toBeDefined(); | 
|  | 20 | +        expect(listCollections.description).toBe( | 
|  | 21 | +            "Creates a new collection in a database. If the database doesn't exist, it will be created automatically." | 
|  | 22 | +        ); | 
|  | 23 | + | 
|  | 24 | +        validateParameters(listCollections, [ | 
|  | 25 | +            { | 
|  | 26 | +                name: "database", | 
|  | 27 | +                description: "Database name", | 
|  | 28 | +                type: "string", | 
|  | 29 | +            }, | 
|  | 30 | +            { | 
|  | 31 | +                name: "collection", | 
|  | 32 | +                description: "Collection name", | 
|  | 33 | +                type: "string", | 
|  | 34 | +            }, | 
|  | 35 | +        ]); | 
|  | 36 | +    }); | 
|  | 37 | + | 
|  | 38 | +    describe("with invalid arguments", () => { | 
|  | 39 | +        const args = [ | 
|  | 40 | +            {}, | 
|  | 41 | +            { database: 123, collection: "bar" }, | 
|  | 42 | +            { foo: "bar", database: "test", collection: "bar" }, | 
|  | 43 | +            { collection: [], database: "test" }, | 
|  | 44 | +        ]; | 
|  | 45 | +        for (const arg of args) { | 
|  | 46 | +            it(`throws a schema error for: ${JSON.stringify(arg)}`, async () => { | 
|  | 47 | +                await connect(client(), cluster()); | 
|  | 48 | +                try { | 
|  | 49 | +                    await client().callTool({ name: "create-collection", arguments: arg }); | 
|  | 50 | +                    expect.fail("Expected an error to be thrown"); | 
|  | 51 | +                } catch (error) { | 
|  | 52 | +                    expect(error).toBeInstanceOf(McpError); | 
|  | 53 | +                    const mcpError = error as McpError; | 
|  | 54 | +                    expect(mcpError.code).toEqual(-32602); | 
|  | 55 | +                    expect(mcpError.message).toContain("Invalid arguments for tool create-collection"); | 
|  | 56 | +                } | 
|  | 57 | +            }); | 
|  | 58 | +        } | 
|  | 59 | +    }); | 
|  | 60 | + | 
|  | 61 | +    describe("with non-existent database", () => { | 
|  | 62 | +        it("creates a new collection", async () => { | 
|  | 63 | +            const mongoClient = cluster().getClient(); | 
|  | 64 | +            let collections = await mongoClient.db("foo").listCollections().toArray(); | 
|  | 65 | +            expect(collections).toHaveLength(0); | 
|  | 66 | + | 
|  | 67 | +            await connect(client(), cluster()); | 
|  | 68 | +            const response = await client().callTool({ | 
|  | 69 | +                name: "create-collection", | 
|  | 70 | +                arguments: { database: "foo", collection: "bar" }, | 
|  | 71 | +            }); | 
|  | 72 | +            const content = getResponseContent(response.content); | 
|  | 73 | +            expect(content).toEqual('Collection "bar" created in database "foo".'); | 
|  | 74 | + | 
|  | 75 | +            collections = await mongoClient.db("foo").listCollections().toArray(); | 
|  | 76 | +            expect(collections).toHaveLength(1); | 
|  | 77 | +            expect(collections[0].name).toEqual("bar"); | 
|  | 78 | +        }); | 
|  | 79 | +    }); | 
|  | 80 | + | 
|  | 81 | +    describe("with existing database", () => { | 
|  | 82 | +        let dbName: string; | 
|  | 83 | +        beforeEach(() => { | 
|  | 84 | +            dbName = new ObjectId().toString(); | 
|  | 85 | +        }); | 
|  | 86 | + | 
|  | 87 | +        it("creates new collection", async () => { | 
|  | 88 | +            const mongoClient = cluster().getClient(); | 
|  | 89 | +            await mongoClient.db(dbName).createCollection("collection1"); | 
|  | 90 | +            let collections = await mongoClient.db(dbName).listCollections().toArray(); | 
|  | 91 | +            expect(collections).toHaveLength(1); | 
|  | 92 | + | 
|  | 93 | +            await connect(client(), cluster()); | 
|  | 94 | +            const response = await client().callTool({ | 
|  | 95 | +                name: "create-collection", | 
|  | 96 | +                arguments: { database: dbName, collection: "collection2" }, | 
|  | 97 | +            }); | 
|  | 98 | +            const content = getResponseContent(response.content); | 
|  | 99 | +            expect(content).toEqual(`Collection "collection2" created in database "${dbName}".`); | 
|  | 100 | +            collections = await mongoClient.db(dbName).listCollections().toArray(); | 
|  | 101 | +            expect(collections).toHaveLength(2); | 
|  | 102 | +            expect(collections.map((c) => c.name)).toIncludeSameMembers(["collection1", "collection2"]); | 
|  | 103 | +        }); | 
|  | 104 | + | 
|  | 105 | +        it("does nothing if collection already exists", async () => { | 
|  | 106 | +            const mongoClient = cluster().getClient(); | 
|  | 107 | +            await mongoClient.db(dbName).collection("collection1").insertOne({}); | 
|  | 108 | +            let collections = await mongoClient.db(dbName).listCollections().toArray(); | 
|  | 109 | +            expect(collections).toHaveLength(1); | 
|  | 110 | +            let documents = await mongoClient.db(dbName).collection("collection1").find({}).toArray(); | 
|  | 111 | +            expect(documents).toHaveLength(1); | 
|  | 112 | + | 
|  | 113 | +            await connect(client(), cluster()); | 
|  | 114 | +            const response = await client().callTool({ | 
|  | 115 | +                name: "create-collection", | 
|  | 116 | +                arguments: { database: dbName, collection: "collection1" }, | 
|  | 117 | +            }); | 
|  | 118 | +            const content = getResponseContent(response.content); | 
|  | 119 | +            expect(content).toEqual(`Collection "collection1" created in database "${dbName}".`); | 
|  | 120 | +            collections = await mongoClient.db(dbName).listCollections().toArray(); | 
|  | 121 | +            expect(collections).toHaveLength(1); | 
|  | 122 | +            expect(collections[0].name).toEqual("collection1"); | 
|  | 123 | + | 
|  | 124 | +            // Make sure we didn't drop the existing collection | 
|  | 125 | +            documents = await mongoClient.db(dbName).collection("collection1").find({}).toArray(); | 
|  | 126 | +            expect(documents).toHaveLength(1); | 
|  | 127 | +        }); | 
|  | 128 | +    }); | 
|  | 129 | +}); | 
0 commit comments