|
7 | 7 | import { toIncludeSameMembers } from "jest-extended";
|
8 | 8 | import { McpError } from "@modelcontextprotocol/sdk/types.js";
|
9 | 9 | import { ObjectId } from "bson";
|
| 10 | +import config from "../../../../../src/config.js"; |
10 | 11 |
|
11 | 12 | describe("createCollection tool", () => {
|
12 | 13 | const integration = setupIntegrationTest();
|
@@ -48,69 +49,90 @@ describe("createCollection tool", () => {
|
48 | 49 | describe("with non-existent database", () => {
|
49 | 50 | it("creates a new collection", async () => {
|
50 | 51 | const mongoClient = integration.mongoClient();
|
51 |
| - let collections = await mongoClient.db("foo").listCollections().toArray(); |
| 52 | + let collections = await mongoClient.db(integration.randomDbName()).listCollections().toArray(); |
52 | 53 | expect(collections).toHaveLength(0);
|
53 | 54 |
|
54 | 55 | await integration.connectMcpClient();
|
55 | 56 | const response = await integration.mcpClient().callTool({
|
56 | 57 | name: "create-collection",
|
57 |
| - arguments: { database: "foo", collection: "bar" }, |
| 58 | + arguments: { database: integration.randomDbName(), collection: "bar" }, |
58 | 59 | });
|
59 | 60 | const content = getResponseContent(response.content);
|
60 |
| - expect(content).toEqual('Collection "bar" created in database "foo".'); |
| 61 | + expect(content).toEqual(`Collection "bar" created in database "${integration.randomDbName()}".`); |
61 | 62 |
|
62 |
| - collections = await mongoClient.db("foo").listCollections().toArray(); |
| 63 | + collections = await mongoClient.db(integration.randomDbName()).listCollections().toArray(); |
63 | 64 | expect(collections).toHaveLength(1);
|
64 | 65 | expect(collections[0].name).toEqual("bar");
|
65 | 66 | });
|
66 | 67 | });
|
67 | 68 |
|
68 | 69 | describe("with existing database", () => {
|
69 |
| - let dbName: string; |
70 |
| - beforeEach(() => { |
71 |
| - dbName = new ObjectId().toString(); |
72 |
| - }); |
73 |
| - |
74 | 70 | it("creates new collection", async () => {
|
75 | 71 | const mongoClient = integration.mongoClient();
|
76 |
| - await mongoClient.db(dbName).createCollection("collection1"); |
77 |
| - let collections = await mongoClient.db(dbName).listCollections().toArray(); |
| 72 | + await mongoClient.db(integration.randomDbName()).createCollection("collection1"); |
| 73 | + let collections = await mongoClient.db(integration.randomDbName()).listCollections().toArray(); |
78 | 74 | expect(collections).toHaveLength(1);
|
79 | 75 |
|
80 | 76 | await integration.connectMcpClient();
|
81 | 77 | const response = await integration.mcpClient().callTool({
|
82 | 78 | name: "create-collection",
|
83 |
| - arguments: { database: dbName, collection: "collection2" }, |
| 79 | + arguments: { database: integration.randomDbName(), collection: "collection2" }, |
84 | 80 | });
|
85 | 81 | const content = getResponseContent(response.content);
|
86 |
| - expect(content).toEqual(`Collection "collection2" created in database "${dbName}".`); |
87 |
| - collections = await mongoClient.db(dbName).listCollections().toArray(); |
| 82 | + expect(content).toEqual(`Collection "collection2" created in database "${integration.randomDbName()}".`); |
| 83 | + collections = await mongoClient.db(integration.randomDbName()).listCollections().toArray(); |
88 | 84 | expect(collections).toHaveLength(2);
|
89 | 85 | expect(collections.map((c) => c.name)).toIncludeSameMembers(["collection1", "collection2"]);
|
90 | 86 | });
|
91 | 87 |
|
92 | 88 | it("does nothing if collection already exists", async () => {
|
93 | 89 | const mongoClient = integration.mongoClient();
|
94 |
| - await mongoClient.db(dbName).collection("collection1").insertOne({}); |
95 |
| - let collections = await mongoClient.db(dbName).listCollections().toArray(); |
| 90 | + await mongoClient.db(integration.randomDbName()).collection("collection1").insertOne({}); |
| 91 | + let collections = await mongoClient.db(integration.randomDbName()).listCollections().toArray(); |
96 | 92 | expect(collections).toHaveLength(1);
|
97 |
| - let documents = await mongoClient.db(dbName).collection("collection1").find({}).toArray(); |
| 93 | + let documents = await mongoClient |
| 94 | + .db(integration.randomDbName()) |
| 95 | + .collection("collection1") |
| 96 | + .find({}) |
| 97 | + .toArray(); |
98 | 98 | expect(documents).toHaveLength(1);
|
99 | 99 |
|
100 | 100 | await integration.connectMcpClient();
|
101 | 101 | const response = await integration.mcpClient().callTool({
|
102 | 102 | name: "create-collection",
|
103 |
| - arguments: { database: dbName, collection: "collection1" }, |
| 103 | + arguments: { database: integration.randomDbName(), collection: "collection1" }, |
104 | 104 | });
|
105 | 105 | const content = getResponseContent(response.content);
|
106 |
| - expect(content).toEqual(`Collection "collection1" created in database "${dbName}".`); |
107 |
| - collections = await mongoClient.db(dbName).listCollections().toArray(); |
| 106 | + expect(content).toEqual(`Collection "collection1" created in database "${integration.randomDbName()}".`); |
| 107 | + collections = await mongoClient.db(integration.randomDbName()).listCollections().toArray(); |
108 | 108 | expect(collections).toHaveLength(1);
|
109 | 109 | expect(collections[0].name).toEqual("collection1");
|
110 | 110 |
|
111 | 111 | // Make sure we didn't drop the existing collection
|
112 |
| - documents = await mongoClient.db(dbName).collection("collection1").find({}).toArray(); |
| 112 | + documents = await mongoClient.db(integration.randomDbName()).collection("collection1").find({}).toArray(); |
113 | 113 | expect(documents).toHaveLength(1);
|
114 | 114 | });
|
115 | 115 | });
|
| 116 | + |
| 117 | + describe("when not connected", () => { |
| 118 | + it("connects automatically if connection string is configured", async () => { |
| 119 | + config.connectionString = integration.connectionString(); |
| 120 | + |
| 121 | + const response = await integration.mcpClient().callTool({ |
| 122 | + name: "create-collection", |
| 123 | + arguments: { database: integration.randomDbName(), collection: "new-collection" }, |
| 124 | + }); |
| 125 | + const content = getResponseContent(response.content); |
| 126 | + expect(content).toEqual(`Collection "new-collection" created in database "${integration.randomDbName()}".`); |
| 127 | + }); |
| 128 | + |
| 129 | + it("throw an error if connection string is not configured", async () => { |
| 130 | + const response = await integration.mcpClient().callTool({ |
| 131 | + name: "create-collection", |
| 132 | + arguments: { database: integration.randomDbName(), collection: "new-collection" }, |
| 133 | + }); |
| 134 | + const content = getResponseContent(response.content); |
| 135 | + expect(content).toContain("You need to connect to a MongoDB instance before you can access its data."); |
| 136 | + }); |
| 137 | + }); |
116 | 138 | });
|
0 commit comments