listCollections.test.ts•3.1 kB
import { describeWithMongoDB, validateAutoConnectBehavior } from "../mongodbHelpers.js";
import {
getResponseElements,
getResponseContent,
validateToolMetadata,
validateThrowsForInvalidArguments,
databaseInvalidArgs,
databaseParameters,
} from "../../../helpers.js";
import { describe, expect, it } from "vitest";
describeWithMongoDB("listCollections tool", (integration) => {
validateToolMetadata(
integration,
"list-collections",
"List all collections for a given database",
databaseParameters
);
validateThrowsForInvalidArguments(integration, "list-collections", databaseInvalidArgs);
describe("with non-existent database", () => {
it("returns no collections", async () => {
await integration.connectMcpClient();
const response = await integration.mcpClient().callTool({
name: "list-collections",
arguments: { database: "non-existent" },
});
const content = getResponseContent(response.content);
expect(content).toEqual(
'Found 0 collections for database "non-existent". To create a collection, use the "create-collection" tool.'
);
});
});
describe("with existing database", () => {
it("returns collections", async () => {
const mongoClient = integration.mongoClient();
await mongoClient.db(integration.randomDbName()).createCollection("collection-1");
await integration.connectMcpClient();
const response = await integration.mcpClient().callTool({
name: "list-collections",
arguments: { database: integration.randomDbName() },
});
const items = getResponseElements(response.content);
expect(items).toHaveLength(2);
expect(items[0]?.text).toEqual(`Found 1 collections for database "${integration.randomDbName()}".`);
expect(items[1]?.text).toContain('"collection-1"');
await mongoClient.db(integration.randomDbName()).createCollection("collection-2");
const response2 = await integration.mcpClient().callTool({
name: "list-collections",
arguments: { database: integration.randomDbName() },
});
const items2 = getResponseElements(response2.content);
expect(items2).toHaveLength(2);
expect(items2[0]?.text).toEqual(`Found 2 collections for database "${integration.randomDbName()}".`);
expect(items2[1]?.text).toContain('"collection-1"');
expect(items2[1]?.text).toContain('"collection-2"');
});
});
validateAutoConnectBehavior(
integration,
"list-collections",
() => {
return {
args: { database: integration.randomDbName() },
expectedResponse: `Found 0 collections for database "${integration.randomDbName()}". To create a collection, use the "create-collection" tool.`,
};
}
);
});