|
| 1 | +import { describe, beforeEach, it, afterEach, expect } from "vitest"; |
| 2 | +import type { Collection } from "mongodb"; |
| 3 | +import { |
| 4 | + databaseCollectionInvalidArgs, |
| 5 | + databaseCollectionParameters, |
| 6 | + defaultDriverOptions, |
| 7 | + defaultTestConfig, |
| 8 | + getDataFromUntrustedContent, |
| 9 | + getResponseContent, |
| 10 | + setupIntegrationTest, |
| 11 | + validateThrowsForInvalidArguments, |
| 12 | + validateToolMetadata, |
| 13 | +} from "../../../helpers.js"; |
| 14 | +import { describeWithMongoDB, setupMongoDBIntegrationTest } from "../mongodbHelpers.js"; |
| 15 | +import { createMockElicitInput } from "../../../../utils/elicitationMocks.js"; |
| 16 | +import { Elicitation } from "../../../../../src/elicitation.js"; |
| 17 | + |
| 18 | +describeWithMongoDB("drop-index tool", (integration) => { |
| 19 | + let moviesCollection: Collection; |
| 20 | + let indexName: string; |
| 21 | + beforeEach(async () => { |
| 22 | + await integration.connectMcpClient(); |
| 23 | + const client = integration.mongoClient(); |
| 24 | + moviesCollection = client.db("mflix").collection("movies"); |
| 25 | + await moviesCollection.insertMany([ |
| 26 | + { |
| 27 | + name: "Movie1", |
| 28 | + year: 1994, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "Movie2", |
| 32 | + year: 2001, |
| 33 | + }, |
| 34 | + ]); |
| 35 | + indexName = await moviesCollection.createIndex({ year: 1 }); |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(async () => { |
| 39 | + await moviesCollection.drop(); |
| 40 | + }); |
| 41 | + |
| 42 | + validateToolMetadata(integration, "drop-index", "Drop an index for the provided database and collection.", [ |
| 43 | + ...databaseCollectionParameters, |
| 44 | + { |
| 45 | + name: "indexName", |
| 46 | + type: "string", |
| 47 | + description: "The name of the index to be dropped.", |
| 48 | + required: true, |
| 49 | + }, |
| 50 | + ]); |
| 51 | + |
| 52 | + validateThrowsForInvalidArguments(integration, "drop-index", [ |
| 53 | + ...databaseCollectionInvalidArgs, |
| 54 | + { database: "test", collection: "testColl", indexName: null }, |
| 55 | + { database: "test", collection: "testColl", indexName: undefined }, |
| 56 | + { database: "test", collection: "testColl", indexName: [] }, |
| 57 | + { database: "test", collection: "testColl", indexName: true }, |
| 58 | + { database: "test", collection: "testColl", indexName: false }, |
| 59 | + { database: "test", collection: "testColl", indexName: 0 }, |
| 60 | + { database: "test", collection: "testColl", indexName: 12 }, |
| 61 | + { database: "test", collection: "testColl", indexName: "" }, |
| 62 | + ]); |
| 63 | + |
| 64 | + describe.each([ |
| 65 | + { |
| 66 | + database: "mflix", |
| 67 | + collection: "non-existent", |
| 68 | + }, |
| 69 | + { |
| 70 | + database: "non-db", |
| 71 | + collection: "non-coll", |
| 72 | + }, |
| 73 | + ])( |
| 74 | + "when attempting to delete an index from non-existent namespace - $database $collection", |
| 75 | + ({ database, collection }) => { |
| 76 | + it("should fail with error", async () => { |
| 77 | + const response = await integration.mcpClient().callTool({ |
| 78 | + name: "drop-index", |
| 79 | + arguments: { database, collection, indexName: "non-existent" }, |
| 80 | + }); |
| 81 | + expect(response.isError).toBe(true); |
| 82 | + const content = getResponseContent(response.content); |
| 83 | + expect(content).toEqual(`Error running drop-index: ns not found ${database}.${collection}`); |
| 84 | + }); |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + describe("when attempting to delete an index that does not exist", () => { |
| 89 | + it("should fail with error", async () => { |
| 90 | + const response = await integration.mcpClient().callTool({ |
| 91 | + name: "drop-index", |
| 92 | + arguments: { database: "mflix", collection: "movies", indexName: "non-existent" }, |
| 93 | + }); |
| 94 | + expect(response.isError).toBe(true); |
| 95 | + const content = getResponseContent(response.content); |
| 96 | + expect(content).toEqual(`Error running drop-index: index not found with name [non-existent]`); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("when attempting to delete an index that exists", () => { |
| 101 | + it("should succeed", async () => { |
| 102 | + const response = await integration.mcpClient().callTool({ |
| 103 | + name: "drop-index", |
| 104 | + // The index is created in beforeEach |
| 105 | + arguments: { database: "mflix", collection: "movies", indexName: indexName }, |
| 106 | + }); |
| 107 | + expect(response.isError).toBe(undefined); |
| 108 | + const content = getResponseContent(response.content); |
| 109 | + expect(content).toContain(`Successfully dropped the index from the provided namespace.`); |
| 110 | + const data = getDataFromUntrustedContent(content); |
| 111 | + expect(JSON.parse(data)).toMatchObject({ indexName, namespace: "mflix.movies" }); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe("drop-index tool - when invoked via an elicitation enabled client", () => { |
| 117 | + const mockElicitInput = createMockElicitInput(); |
| 118 | + const mdbIntegration = setupMongoDBIntegrationTest(); |
| 119 | + const integration = setupIntegrationTest( |
| 120 | + () => defaultTestConfig, |
| 121 | + () => defaultDriverOptions, |
| 122 | + { elicitInput: mockElicitInput } |
| 123 | + ); |
| 124 | + let moviesCollection: Collection; |
| 125 | + let indexName: string; |
| 126 | + |
| 127 | + beforeEach(async () => { |
| 128 | + moviesCollection = mdbIntegration.mongoClient().db("mflix").collection("movies"); |
| 129 | + await moviesCollection.insertMany([ |
| 130 | + { name: "Movie1", year: 1994 }, |
| 131 | + { name: "Movie2", year: 2001 }, |
| 132 | + ]); |
| 133 | + indexName = await moviesCollection.createIndex({ year: 1 }); |
| 134 | + await integration.mcpClient().callTool({ |
| 135 | + name: "connect", |
| 136 | + arguments: { |
| 137 | + connectionString: mdbIntegration.connectionString(), |
| 138 | + }, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + afterEach(async () => { |
| 143 | + await moviesCollection.drop(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should ask for confirmation before proceeding with tool call", async () => { |
| 147 | + expect(await moviesCollection.listIndexes().toArray()).toHaveLength(2); |
| 148 | + mockElicitInput.confirmYes(); |
| 149 | + await integration.mcpClient().callTool({ |
| 150 | + name: "drop-index", |
| 151 | + arguments: { database: "mflix", collection: "movies", indexName }, |
| 152 | + }); |
| 153 | + expect(mockElicitInput.mock).toHaveBeenCalledTimes(1); |
| 154 | + expect(mockElicitInput.mock).toHaveBeenCalledWith({ |
| 155 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 156 | + message: expect.stringContaining( |
| 157 | + "You are about to drop the `year_1` index from the `mflix.movies` namespace" |
| 158 | + ), |
| 159 | + requestedSchema: Elicitation.CONFIRMATION_SCHEMA, |
| 160 | + }); |
| 161 | + expect(await moviesCollection.listIndexes().toArray()).toHaveLength(1); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should not drop the index if the confirmation was not provided", async () => { |
| 165 | + expect(await moviesCollection.listIndexes().toArray()).toHaveLength(2); |
| 166 | + mockElicitInput.confirmNo(); |
| 167 | + await integration.mcpClient().callTool({ |
| 168 | + name: "drop-index", |
| 169 | + arguments: { database: "mflix", collection: "movies", indexName }, |
| 170 | + }); |
| 171 | + expect(mockElicitInput.mock).toHaveBeenCalledTimes(1); |
| 172 | + expect(mockElicitInput.mock).toHaveBeenCalledWith({ |
| 173 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 174 | + message: expect.stringContaining( |
| 175 | + "You are about to drop the `year_1` index from the `mflix.movies` namespace" |
| 176 | + ), |
| 177 | + requestedSchema: Elicitation.CONFIRMATION_SCHEMA, |
| 178 | + }); |
| 179 | + expect(await moviesCollection.listIndexes().toArray()).toHaveLength(2); |
| 180 | + }); |
| 181 | +}); |
0 commit comments