Skip to content

Commit 8a5da23

Browse files
authored
chore: add more create-index tests (#664)
1 parent f35f0e5 commit 8a5da23

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

tests/integration/tools/mongodb/create/createIndex.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
validateThrowsForInvalidArguments,
88
expectDefined,
99
defaultTestConfig,
10+
getResponseElements,
1011
} from "../../../helpers.js";
1112
import { ObjectId, type Collection, type Document, type IndexDirection } from "mongodb";
1213
import { afterEach, beforeEach, describe, expect, it } from "vitest";
@@ -502,6 +503,111 @@ describeWithMongoDB(
502503
],
503504
});
504505
});
506+
507+
it("doesn't duplicate indexes", async () => {
508+
const response = await integration.mcpClient().callTool({
509+
name: "create-index",
510+
arguments: {
511+
database: integration.randomDbName(),
512+
collection: collectionName,
513+
name: "vector_1_vector",
514+
definition: [
515+
{
516+
type: "vectorSearch",
517+
fields: [
518+
{ type: "vector", path: "vector_1", numDimensions: 4 },
519+
{ type: "filter", path: "category" },
520+
],
521+
},
522+
],
523+
},
524+
});
525+
526+
const content = getResponseContent(response.content);
527+
expect(content).toEqual(
528+
`Created the index "vector_1_vector" on collection "${collectionName}" in database "${integration.randomDbName()}". Since this is a vector search index, it may take a while for the index to build. Use the \`list-indexes\` tool to check the index status.`
529+
);
530+
531+
// Try to create another vector search index with the same name
532+
const duplicateVectorResponse = await integration.mcpClient().callTool({
533+
name: "create-index",
534+
arguments: {
535+
database: integration.randomDbName(),
536+
collection: collectionName,
537+
name: "vector_1_vector",
538+
definition: [
539+
{
540+
type: "vectorSearch",
541+
fields: [{ type: "vector", path: "vector_1", numDimensions: 4 }],
542+
},
543+
],
544+
},
545+
});
546+
547+
const duplicateVectorContent = getResponseContent(duplicateVectorResponse.content);
548+
expect(duplicateVectorResponse.isError).toBe(true);
549+
expect(duplicateVectorContent).toEqual(
550+
"Error running create-index: Index vector_1_vector already exists with a different definition. Drop it first if needed."
551+
);
552+
});
553+
554+
it("can create classic and vector search indexes with the same name", async () => {
555+
const response = await integration.mcpClient().callTool({
556+
name: "create-index",
557+
arguments: {
558+
database: integration.randomDbName(),
559+
collection: collectionName,
560+
name: "my-super-index",
561+
definition: [
562+
{
563+
type: "vectorSearch",
564+
fields: [
565+
{ type: "vector", path: "vector_1", numDimensions: 4 },
566+
{ type: "filter", path: "category" },
567+
],
568+
},
569+
],
570+
},
571+
});
572+
573+
const content = getResponseContent(response.content);
574+
expect(content).toEqual(
575+
`Created the index "my-super-index" on collection "${collectionName}" in database "${integration.randomDbName()}". Since this is a vector search index, it may take a while for the index to build. Use the \`list-indexes\` tool to check the index status.`
576+
);
577+
578+
const classicResponse = await integration.mcpClient().callTool({
579+
name: "create-index",
580+
arguments: {
581+
database: integration.randomDbName(),
582+
collection: collectionName,
583+
name: "my-super-index",
584+
definition: [{ type: "classic", keys: { field1: 1 } }],
585+
},
586+
});
587+
588+
// Create a classic index with the same name
589+
const classicContent = getResponseContent(classicResponse.content);
590+
expect(classicContent).toEqual(
591+
`Created the index "my-super-index" on collection "${collectionName}" in database "${integration.randomDbName()}".`
592+
);
593+
594+
const listIndexesResponse = await integration.mcpClient().callTool({
595+
name: "collection-indexes",
596+
arguments: {
597+
database: integration.randomDbName(),
598+
collection: collectionName,
599+
},
600+
});
601+
602+
const listIndexesElements = getResponseElements(listIndexesResponse.content);
603+
expect(listIndexesElements).toHaveLength(4); // 2 elements for classic indexes, 2 for vector search indexes
604+
605+
// Expect to find my-super-index in the classic definitions
606+
expect(listIndexesElements[1]?.text).toContain('"name":"my-super-index"');
607+
608+
// Expect to find my-super-index in the vector search definitions
609+
expect(listIndexesElements[3]?.text).toContain('"name":"my-super-index"');
610+
});
505611
});
506612
},
507613
{

0 commit comments

Comments
 (0)