|
| 1 | +import { ObjectId } from "bson"; |
| 2 | +import { |
| 3 | + databaseParameters, |
| 4 | + validateToolMetadata, |
| 5 | + validateThrowsForInvalidArguments, |
| 6 | + databaseInvalidArgs, |
| 7 | + getResponseElements, |
| 8 | +} from "../../../helpers.js"; |
| 9 | +import * as crypto from "crypto"; |
| 10 | +import { describeWithMongoDB, validateAutoConnectBehavior } from "../mongodbHelpers.js"; |
| 11 | + |
| 12 | +describeWithMongoDB("dbStats tool", (integration) => { |
| 13 | + validateToolMetadata( |
| 14 | + integration, |
| 15 | + "db-stats", |
| 16 | + "Returns statistics that reflect the use state of a single database", |
| 17 | + databaseParameters |
| 18 | + ); |
| 19 | + |
| 20 | + validateThrowsForInvalidArguments(integration, "db-stats", databaseInvalidArgs); |
| 21 | + |
| 22 | + describe("with non-existent database", () => { |
| 23 | + it("returns an error", async () => { |
| 24 | + await integration.connectMcpClient(); |
| 25 | + const response = await integration.mcpClient().callTool({ |
| 26 | + name: "db-stats", |
| 27 | + arguments: { database: integration.randomDbName() }, |
| 28 | + }); |
| 29 | + const elements = getResponseElements(response.content); |
| 30 | + expect(elements).toHaveLength(2); |
| 31 | + expect(elements[0].text).toBe(`Statistics for database ${integration.randomDbName()}`); |
| 32 | + |
| 33 | + const stats = JSON.parse(elements[1].text); |
| 34 | + expect(stats.db).toBe(integration.randomDbName()); |
| 35 | + expect(stats.collections).toBe(0); |
| 36 | + expect(stats.storageSize).toBe(0); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("with existing database", () => { |
| 41 | + const testCases = [ |
| 42 | + { |
| 43 | + collections: { |
| 44 | + foos: 3, |
| 45 | + }, |
| 46 | + name: "single collection", |
| 47 | + }, |
| 48 | + { |
| 49 | + collections: { |
| 50 | + foos: 2, |
| 51 | + bars: 5, |
| 52 | + }, |
| 53 | + name: "multiple collections", |
| 54 | + }, |
| 55 | + ]; |
| 56 | + for (const test of testCases) { |
| 57 | + it(`returns correct stats for ${test.name}`, async () => { |
| 58 | + for (const [name, count] of Object.entries(test.collections)) { |
| 59 | + const objects = Array(count) |
| 60 | + .fill(0) |
| 61 | + .map(() => { |
| 62 | + return { data: crypto.randomBytes(1024), _id: new ObjectId() }; |
| 63 | + }); |
| 64 | + await integration.mongoClient().db(integration.randomDbName()).collection(name).insertMany(objects); |
| 65 | + } |
| 66 | + |
| 67 | + await integration.connectMcpClient(); |
| 68 | + const response = await integration.mcpClient().callTool({ |
| 69 | + name: "db-stats", |
| 70 | + arguments: { database: integration.randomDbName() }, |
| 71 | + }); |
| 72 | + const elements = getResponseElements(response.content); |
| 73 | + expect(elements).toHaveLength(2); |
| 74 | + expect(elements[0].text).toBe(`Statistics for database ${integration.randomDbName()}`); |
| 75 | + |
| 76 | + const stats = JSON.parse(elements[1].text); |
| 77 | + expect(stats.db).toBe(integration.randomDbName()); |
| 78 | + expect(stats.collections).toBe(Object.entries(test.collections).length); |
| 79 | + expect(stats.storageSize).toBeGreaterThan(1024); |
| 80 | + expect(stats.objects).toBe(Object.values(test.collections).reduce((a, b) => a + b, 0)); |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + describe("when not connected", () => { |
| 86 | + validateAutoConnectBehavior(integration, "db-stats", () => { |
| 87 | + return { |
| 88 | + args: { |
| 89 | + database: integration.randomDbName(), |
| 90 | + collection: "foo", |
| 91 | + }, |
| 92 | + expectedResponse: `Statistics for database ${integration.randomDbName()}`, |
| 93 | + }; |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments