Skip to content
Prev Previous commit
Next Next commit
chore: tests for isSearchIndexSupported
  • Loading branch information
himanshusinghs committed Oct 13, 2025
commit 6549ce15e9cb1a680004d197e9f9e472d70d9e60
9 changes: 3 additions & 6 deletions src/common/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,11 @@ export class Session extends EventEmitter<SessionEvents> {
const dummyDatabase = `db-${Date.now()}`;
const dummyCollection = `coll-${Date.now()}`;
// If a cluster supports search indexes, the call below will succeed
// with a cursor otherwise will throw a MongoServerError
// with a cursor otherwise will throw an Error
await this.serviceProvider.getSearchIndexes(dummyDatabase, dummyCollection);
return true;
} catch (error) {
if (error instanceof Error && "codeName" in error && error.codeName === "SearchNotEnabled") {
return false;
}
throw error;
} catch {
return false;
}
}
}
28 changes: 27 additions & 1 deletion tests/unit/common/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Mocked } from "vitest";
import type { Mocked, MockedFunction } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
import { Session } from "../../../src/common/session.js";
Expand Down Expand Up @@ -119,4 +119,30 @@ describe("Session", () => {
expect(connectionString).toContain("--test-device-id--unknown");
});
});

describe("isSearchIndexSupported", () => {
let getSearchIndexesMock: MockedFunction<() => unknown>;
beforeEach(() => {
getSearchIndexesMock = vi.fn();
MockNodeDriverServiceProvider.connect = vi.fn().mockResolvedValue({
getSearchIndexes: getSearchIndexesMock,
} as unknown as NodeDriverServiceProvider);
});

it("should return true if listing search indexes succeed", async () => {
getSearchIndexesMock.mockResolvedValue([]);
await session.connectToMongoDB({
connectionString: "mongodb://localhost:27017",
});
expect(await session.isSearchIndexSupported()).toEqual(true);
});

it("should return false if listing search indexes fail with search error", async () => {
getSearchIndexesMock.mockRejectedValue(new Error("SearchNotEnabled"));
await session.connectToMongoDB({
connectionString: "mongodb://localhost:27017",
});
expect(await session.isSearchIndexSupported()).toEqual(false);
});
});
});