Skip to content

Commit 5eff73b

Browse files
chore: tests for isSearchIndexSupported
1 parent 09f1262 commit 5eff73b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/common/session.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,11 @@ export class Session extends EventEmitter<SessionEvents> {
159159
const dummyDatabase = `db-${Date.now()}`;
160160
const dummyCollection = `coll-${Date.now()}`;
161161
// If a cluster supports search indexes, the call below will succeed
162-
// with a cursor otherwise will throw a MongoServerError
162+
// with a cursor otherwise will throw an Error
163163
await this.serviceProvider.getSearchIndexes(dummyDatabase, dummyCollection);
164164
return true;
165-
} catch (error) {
166-
if (error instanceof Error && "codeName" in error && error.codeName === "SearchNotEnabled") {
167-
return false;
168-
}
169-
throw error;
165+
} catch {
166+
return false;
170167
}
171168
}
172169
}

tests/unit/common/session.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Mocked } from "vitest";
1+
import type { Mocked, MockedFunction } from "vitest";
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
44
import { Session } from "../../../src/common/session.js";
@@ -119,4 +119,30 @@ describe("Session", () => {
119119
expect(connectionString).toContain("--test-device-id--unknown");
120120
});
121121
});
122+
123+
describe("isSearchIndexSupported", () => {
124+
let getSearchIndexesMock: MockedFunction<() => unknown>;
125+
beforeEach(() => {
126+
getSearchIndexesMock = vi.fn();
127+
MockNodeDriverServiceProvider.connect = vi.fn().mockResolvedValue({
128+
getSearchIndexes: getSearchIndexesMock,
129+
} as unknown as NodeDriverServiceProvider);
130+
});
131+
132+
it("should return true if listing search indexes succeed", async () => {
133+
getSearchIndexesMock.mockResolvedValue([]);
134+
await session.connectToMongoDB({
135+
connectionString: "mongodb://localhost:27017",
136+
});
137+
expect(await session.isSearchIndexSupported()).toEqual(true);
138+
});
139+
140+
it("should return false if listing search indexes fail with search error", async () => {
141+
getSearchIndexesMock.mockRejectedValue(new Error("SearchNotEnabled"));
142+
await session.connectToMongoDB({
143+
connectionString: "mongodb://localhost:27017",
144+
});
145+
expect(await session.isSearchIndexSupported()).toEqual(false);
146+
});
147+
});
122148
});

0 commit comments

Comments
 (0)