Skip to content

Commit db2f049

Browse files
created waitUntilMcpClientIsSet and use test helper
1 parent 1fd7ae3 commit db2f049

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/helpers.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { MCPConnectionManager } from "../../src/common/connectionManager.js";
1515
import { DeviceId } from "../../src/helpers/deviceId.js";
1616
import { connectionErrorHandler } from "../../src/common/connectionErrorHandler.js";
1717
import { Keychain } from "../../src/common/keychain.js";
18+
import type { Client as AtlasLocalClient } from "@mongodb-js-preview/atlas-local";
1819

1920
interface ParameterInfo {
2021
name: string;
@@ -345,6 +346,37 @@ export function waitUntil<T extends ConnectionState>(
345346
});
346347
}
347348

349+
export function waitUntilMcpClientIsSet(
350+
mcpServer: Server,
351+
signal: AbortSignal,
352+
timeout: number = 5000
353+
): Promise<AtlasLocalClient> {
354+
let ts: NodeJS.Timeout | undefined;
355+
356+
const timeoutSignal = AbortSignal.timeout(timeout);
357+
const combinedSignal = AbortSignal.any([signal, timeoutSignal]);
358+
359+
return new Promise<AtlasLocalClient>((resolve, reject) => {
360+
ts = setInterval(() => {
361+
if (combinedSignal.aborted) {
362+
return reject(new Error(`Aborted: ${combinedSignal.reason}`));
363+
}
364+
365+
// wait until session.client != undefined
366+
// do not wait more than 1 second, should take a few milliseconds at most
367+
// try every 50ms to see if the client is set, if it's not set after 1 second, throw an error
368+
const client = mcpServer.session.atlasLocalClient;
369+
if (client) {
370+
return resolve(client);
371+
}
372+
}, 100);
373+
}).finally(() => {
374+
if (ts !== undefined) {
375+
clearInterval(ts);
376+
}
377+
});
378+
}
379+
348380
export function getDataFromUntrustedContent(content: string): string {
349381
const regex = /^[ \t]*<untrusted-user-data-[0-9a-f\\-]*>(?<data>.*)^[ \t]*<\/untrusted-user-data-[0-9a-f\\-]*>/gms;
350382
const match = regex.exec(content);

tests/integration/tools/atlas-local/listDeployments.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
expectDefined,
55
getResponseElements,
66
setupIntegrationTest,
7+
waitUntilMcpClientIsSet,
78
} from "../../helpers.js";
89
import { describe, expect, it } from "vitest";
910

@@ -17,22 +18,26 @@ describe("atlas-local-list-deployments", () => {
1718
() => defaultDriverOptions
1819
);
1920

20-
it.skipIf(isMacOSInGitHubActions)("should have the atlas-local-list-deployments tool", async () => {
21+
it.skipIf(isMacOSInGitHubActions)("should have the atlas-local-list-deployments tool", async ({ signal }) => {
22+
await waitUntilMcpClientIsSet(integration.mcpServer(), signal);
23+
2124
const { tools } = await integration.mcpClient().listTools();
2225
const listDeployments = tools.find((tool) => tool.name === "atlas-local-list-deployments");
2326
expectDefined(listDeployments);
2427
});
2528

2629
it.skipIf(!isMacOSInGitHubActions)(
2730
"[MacOS in GitHub Actions] should not have the atlas-local-list-deployments tool",
28-
async () => {
31+
async ({ signal }) => {
32+
await waitUntilMcpClientIsSet(integration.mcpServer(), signal);
2933
const { tools } = await integration.mcpClient().listTools();
3034
const listDeployments = tools.find((tool) => tool.name === "atlas-local-list-deployments");
3135
expect(listDeployments).toBeUndefined();
3236
}
3337
);
3438

35-
it.skipIf(isMacOSInGitHubActions)("should have correct metadata", async () => {
39+
it.skipIf(isMacOSInGitHubActions)("should have correct metadata", async ({ signal }) => {
40+
await waitUntilMcpClientIsSet(integration.mcpServer(), signal);
3641
const { tools } = await integration.mcpClient().listTools();
3742
const listDeployments = tools.find((tool) => tool.name === "atlas-local-list-deployments");
3843
expectDefined(listDeployments);
@@ -41,7 +46,9 @@ describe("atlas-local-list-deployments", () => {
4146
expect(listDeployments.inputSchema.properties).toEqual({});
4247
});
4348

44-
it.skipIf(isMacOSInGitHubActions)("should not crash when calling the tool", async () => {
49+
it.skipIf(isMacOSInGitHubActions)("should not crash when calling the tool", async ({ signal }) => {
50+
await waitUntilMcpClientIsSet(integration.mcpServer(), signal);
51+
4552
const response = await integration.mcpClient().callTool({
4653
name: "atlas-local-list-deployments",
4754
arguments: {},

0 commit comments

Comments
 (0)