Skip to content

Commit 3e57858

Browse files
chore: refactor helpers to use vi.waitFor
1 parent 122f062 commit 3e57858

File tree

6 files changed

+150
-188
lines changed

6 files changed

+150
-188
lines changed

tests/integration/helpers.ts

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Collection } from "mongodb";
21
import { CompositeLogger } from "../../src/common/logger.js";
32
import { ExportsManager } from "../../src/common/exportsManager.js";
43
import { Session } from "../../src/common/session.js";
@@ -24,9 +23,6 @@ import { Elicitation } from "../../src/elicitation.js";
2423
import type { MockClientCapabilities, createMockElicitInput } from "../utils/elicitationMocks.js";
2524
import { VectorSearchEmbeddingsManager } from "../../src/common/search/vectorSearchEmbeddingsManager.js";
2625

27-
export const DEFAULT_WAIT_TIMEOUT = 1000;
28-
export const DEFAULT_RETRY_INTERVAL = 100;
29-
3026
export const driverOptions = setupDriverConfig({
3127
config,
3228
defaults: defaultDriverOptionsFromConfig,
@@ -423,60 +419,3 @@ export function getDataFromUntrustedContent(content: string): string {
423419
export function sleep(ms: number): Promise<void> {
424420
return new Promise((resolve) => setTimeout(resolve, ms));
425421
}
426-
427-
export async function waitUntilSearchManagementServiceIsReady(
428-
collection: Collection,
429-
timeout: number = DEFAULT_WAIT_TIMEOUT,
430-
interval: number = DEFAULT_RETRY_INTERVAL
431-
): Promise<void> {
432-
await vi.waitFor(async () => await collection.listSearchIndexes({}).toArray(), { timeout, interval });
433-
}
434-
435-
async function waitUntilSearchIndexIs(
436-
collection: Collection,
437-
searchIndex: string,
438-
indexValidator: (index: { name: string; queryable: boolean }) => boolean,
439-
timeout: number,
440-
interval: number
441-
): Promise<void> {
442-
await vi.waitFor(
443-
async () => {
444-
const searchIndexes = (await collection.listSearchIndexes(searchIndex).toArray()) as {
445-
name: string;
446-
queryable: boolean;
447-
}[];
448-
449-
if (!searchIndexes.some((index) => indexValidator(index))) {
450-
throw new Error("Search index did not pass validation");
451-
}
452-
},
453-
{
454-
timeout,
455-
interval,
456-
}
457-
);
458-
}
459-
460-
export async function waitUntilSearchIndexIsListed(
461-
collection: Collection,
462-
searchIndex: string,
463-
timeout: number = DEFAULT_WAIT_TIMEOUT,
464-
interval: number = DEFAULT_RETRY_INTERVAL
465-
): Promise<void> {
466-
return waitUntilSearchIndexIs(collection, searchIndex, (index) => index.name === searchIndex, timeout, interval);
467-
}
468-
469-
export async function waitUntilSearchIndexIsQueryable(
470-
collection: Collection,
471-
searchIndex: string,
472-
timeout: number = DEFAULT_WAIT_TIMEOUT,
473-
interval: number = DEFAULT_RETRY_INTERVAL
474-
): Promise<void> {
475-
return waitUntilSearchIndexIs(
476-
collection,
477-
searchIndex,
478-
(index) => index.name === searchIndex && index.queryable,
479-
timeout,
480-
interval
481-
);
482-
}

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import {
88
expectDefined,
99
defaultTestConfig,
1010
} from "../../../helpers.js";
11-
import { ObjectId, type IndexDirection } from "mongodb";
12-
import { beforeEach, describe, expect, it } from "vitest";
13-
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
11+
import { ObjectId, type Collection, type Document, type IndexDirection } from "mongodb";
12+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
1413

1514
describeWithMongoDB("createIndex tool when search is not enabled", (integration) => {
1615
it("doesn't allow creating vector search indexes", async () => {
@@ -403,12 +402,9 @@ describeWithMongoDB(
403402
describeWithMongoDB(
404403
"createIndex tool with vector search indexes",
405404
(integration) => {
406-
let provider: NodeDriverServiceProvider;
407-
408-
beforeEach(async ({ signal }) => {
405+
beforeEach(async () => {
409406
await integration.connectMcpClient();
410-
provider = integration.mcpServer().session.serviceProvider;
411-
await waitUntilSearchIsReady(provider, signal);
407+
await waitUntilSearchIsReady(integration.mongoClient());
412408
});
413409

414410
describe("when the collection does not exist", () => {
@@ -457,14 +453,26 @@ describeWithMongoDB(
457453
});
458454

459455
describe("when the collection exists", () => {
456+
let collectionName: string;
457+
let collection: Collection;
458+
beforeEach(async () => {
459+
collectionName = new ObjectId().toString();
460+
collection = await integration
461+
.mongoClient()
462+
.db(integration.randomDbName())
463+
.createCollection(collectionName);
464+
});
465+
466+
afterEach(async () => {
467+
await collection.drop();
468+
});
469+
460470
it("creates the index", async () => {
461-
const collection = new ObjectId().toString();
462-
await provider.createCollection(integration.randomDbName(), collection);
463471
const response = await integration.mcpClient().callTool({
464472
name: "create-index",
465473
arguments: {
466474
database: integration.randomDbName(),
467-
collection,
475+
collection: collectionName,
468476
name: "vector_1_vector",
469477
definition: [
470478
{
@@ -480,10 +488,10 @@ describeWithMongoDB(
480488

481489
const content = getResponseContent(response.content);
482490
expect(content).toEqual(
483-
`Created the index "vector_1_vector" on collection "${collection}" 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.`
491+
`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.`
484492
);
485493

486-
const indexes = await provider.getSearchIndexes(integration.randomDbName(), collection);
494+
const indexes = (await collection.listSearchIndexes().toArray()) as unknown as Document[];
487495
expect(indexes).toHaveLength(1);
488496
expect(indexes[0]?.name).toEqual("vector_1_vector");
489497
expect(indexes[0]?.type).toEqual("vectorSearch");

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

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2-
createVectorSearchIndexAndWait,
32
describeWithMongoDB,
43
validateAutoConnectBehavior,
4+
createVectorSearchIndexAndWait,
55
waitUntilSearchIsReady,
66
} from "../mongodbHelpers.js";
77

@@ -14,8 +14,8 @@ import {
1414
getDataFromUntrustedContent,
1515
} from "../../../helpers.js";
1616
import { beforeEach, afterEach, expect, it } from "vitest";
17-
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
1817
import { ObjectId } from "bson";
18+
import type { Collection } from "mongodb";
1919

2020
describeWithMongoDB("insertMany tool when search is disabled", (integration) => {
2121
validateToolMetadata(integration, "insert-many", "Insert an array of documents into a MongoDB collection", [
@@ -109,35 +109,29 @@ describeWithMongoDB("insertMany tool when search is disabled", (integration) =>
109109
describeWithMongoDB(
110110
"insertMany tool when search is enabled",
111111
(integration) => {
112-
let provider: NodeDriverServiceProvider;
112+
// let provider: NodeDriverServiceProvider;
113+
let collection: Collection;
113114

114-
beforeEach(async ({ signal }) => {
115+
beforeEach(async () => {
115116
await integration.connectMcpClient();
116-
provider = integration.mcpServer().session.serviceProvider;
117-
await provider.createCollection(integration.randomDbName(), "test");
118-
await waitUntilSearchIsReady(provider, signal);
117+
collection = await integration.mongoClient().db(integration.randomDbName()).createCollection("test");
118+
await waitUntilSearchIsReady(integration.mongoClient());
119119
});
120120

121121
afterEach(async () => {
122-
await provider.dropCollection(integration.randomDbName(), "test");
122+
await collection.drop();
123123
});
124124

125-
it("inserts a document when the embedding is correct", async ({ signal }) => {
126-
await createVectorSearchIndexAndWait(
127-
provider,
128-
integration.randomDbName(),
129-
"test",
130-
[
131-
{
132-
type: "vector",
133-
path: "embedding",
134-
numDimensions: 8,
135-
similarity: "euclidean",
136-
quantization: "scalar",
137-
},
138-
],
139-
signal
140-
);
125+
it("inserts a document when the embedding is correct", async () => {
126+
await createVectorSearchIndexAndWait(integration.mongoClient(), integration.randomDbName(), "test", [
127+
{
128+
type: "vector",
129+
path: "embedding",
130+
numDimensions: 8,
131+
similarity: "euclidean",
132+
quantization: "scalar",
133+
},
134+
]);
141135

142136
const response = await integration.mcpClient().callTool({
143137
name: "insert-many",
@@ -152,26 +146,20 @@ describeWithMongoDB(
152146
const insertedIds = extractInsertedIds(content);
153147
expect(insertedIds).toHaveLength(1);
154148

155-
const docCount = await provider.countDocuments(integration.randomDbName(), "test", { _id: insertedIds[0] });
149+
const docCount = await collection.countDocuments({ _id: insertedIds[0] });
156150
expect(docCount).toBe(1);
157151
});
158152

159-
it("returns an error when there is a search index and quantisation is wrong", async ({ signal }) => {
160-
await createVectorSearchIndexAndWait(
161-
provider,
162-
integration.randomDbName(),
163-
"test",
164-
[
165-
{
166-
type: "vector",
167-
path: "embedding",
168-
numDimensions: 8,
169-
similarity: "euclidean",
170-
quantization: "scalar",
171-
},
172-
],
173-
signal
174-
);
153+
it("returns an error when there is a search index and quantisation is wrong", async () => {
154+
await createVectorSearchIndexAndWait(integration.mongoClient(), integration.randomDbName(), "test", [
155+
{
156+
type: "vector",
157+
path: "embedding",
158+
numDimensions: 8,
159+
similarity: "euclidean",
160+
quantization: "scalar",
161+
},
162+
]);
175163

176164
const response = await integration.mcpClient().callTool({
177165
name: "insert-many",
@@ -189,7 +177,7 @@ describeWithMongoDB(
189177
"- Field embedding is an embedding with 8 dimensions and scalar quantization, and the provided value is not compatible."
190178
);
191179

192-
const oopsieCount = await provider.countDocuments(integration.randomDbName(), "test", {
180+
const oopsieCount = await collection.countDocuments({
193181
embedding: "oopsie",
194182
});
195183
expect(oopsieCount).toBe(0);

tests/integration/tools/mongodb/delete/dropIndex.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import {
88
getResponseContent,
99
validateThrowsForInvalidArguments,
1010
validateToolMetadata,
11-
waitUntilSearchIndexIsListed,
12-
waitUntilSearchManagementServiceIsReady,
1311
} from "../../../helpers.js";
14-
import { describeWithMongoDB, type MongoDBIntegrationTestCase } from "../mongodbHelpers.js";
12+
import {
13+
describeWithMongoDB,
14+
waitUntilSearchIndexIsListed,
15+
waitUntilSearchIsReady,
16+
type MongoDBIntegrationTestCase,
17+
} from "../mongodbHelpers.js";
1518
import { createMockElicitInput } from "../../../../utils/elicitationMocks.js";
1619
import { Elicitation } from "../../../../../src/elicitation.js";
1720

18-
const SEARCH_TIMEOUT = 20_000;
19-
2021
function setupForClassicIndexes(integration: MongoDBIntegrationTestCase): {
2122
getMoviesCollection: () => Collection;
2223
getIndexName: () => string;
@@ -66,12 +67,12 @@ function setupForVectorSearchIndexes(integration: MongoDBIntegrationTestCase): {
6667
plot: "This is a horrible movie about a database called BongoDB and how it tried to copy the OG MangoDB.",
6768
},
6869
]);
69-
await waitUntilSearchManagementServiceIsReady(moviesCollection, SEARCH_TIMEOUT);
70+
await waitUntilSearchIsReady(mongoClient);
7071
await moviesCollection.createSearchIndex({
7172
name: indexName,
7273
definition: { mappings: { dynamic: true } },
7374
});
74-
await waitUntilSearchIndexIsListed(moviesCollection, indexName, SEARCH_TIMEOUT);
75+
await waitUntilSearchIndexIsListed(moviesCollection, indexName);
7576
});
7677

7778
afterEach(async () => {
@@ -366,7 +367,7 @@ describe.each([{ vectorSearchEnabled: false }, { vectorSearchEnabled: true }])(
366367
});
367368

368369
describe("and attempting to delete an existing index", () => {
369-
it("should succeed in deleting the index", { timeout: SEARCH_TIMEOUT }, async () => {
370+
it("should succeed in deleting the index", async () => {
370371
const response = await integration.mcpClient().callTool({
371372
name: "drop-index",
372373
arguments: {

0 commit comments

Comments
 (0)