Skip to content

Commit aa2162d

Browse files
chore: add tests for warnIfVectorSearchNotEnabledCorrectly
1 parent 63f7035 commit aa2162d

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/common/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,18 +372,18 @@ export function registerKnownSecretsInRootKeychain(userConfig: Partial<UserConfi
372372
maybeRegister(userConfig.username, "user");
373373
}
374374

375-
function warnIfVectorSearchNotEnabledCorrectly(config: UserConfig): void {
375+
export function warnIfVectorSearchNotEnabledCorrectly(config: UserConfig, warn: (message: string) => void): void {
376376
const vectorSearchEnabled = config.previewFeatures.includes("vectorSearch");
377377
const embeddingsProviderConfigured = !!config.voyageApiKey;
378378
if (vectorSearchEnabled && !embeddingsProviderConfigured) {
379-
console.warn(`\
379+
warn(`\
380380
Warning: Vector search is enabled but no embeddings provider is configured.
381381
- Set an embeddings provider configuration option to enable auto-embeddings during document insertion and text-based queries with $vectorSearch.\
382382
`);
383383
}
384384

385385
if (!vectorSearchEnabled && embeddingsProviderConfigured) {
386-
console.warn(`\
386+
warn(`\
387387
Warning: An embeddings provider is configured but the 'vectorSearch' preview feature is not enabled.
388388
- Enable vector search by adding 'vectorSearch' to the 'previewFeatures' configuration option, or remove the embeddings provider configuration if not needed.\
389389
`);
@@ -410,7 +410,7 @@ export function setupUserConfig({ cli, env }: { cli: string[]; env: Record<strin
410410
// We don't have as schema defined for all args-parser arguments so we need to merge the raw config with the parsed config.
411411
const userConfig = { ...rawConfig, ...parseResult.data } as UserConfig;
412412

413-
warnIfVectorSearchNotEnabledCorrectly(userConfig);
413+
warnIfVectorSearchNotEnabledCorrectly(userConfig, (message) => console.warn(message));
414414
registerKnownSecretsInRootKeychain(userConfig);
415415
return userConfig;
416416
}

tests/unit/common/config.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import {
55
registerKnownSecretsInRootKeychain,
66
warnAboutDeprecatedOrUnknownCliArgs,
77
UserConfigSchema,
8+
warnIfVectorSearchNotEnabledCorrectly,
89
} from "../../../src/common/config.js";
910
import { getLogPath, getExportsPath } from "../../../src/common/configUtils.js";
1011
import type { CliOptions } from "@mongosh/arg-parser";
1112
import { Keychain } from "../../../src/common/keychain.js";
1213
import type { Secret } from "../../../src/common/keychain.js";
14+
import { defaultTestConfig } from "../../integration/helpers.js";
1315

1416
describe("config", () => {
1517
it("should generate defaults from UserConfigSchema that match expected values", () => {
@@ -792,6 +794,51 @@ describe("CLI arguments", () => {
792794
});
793795
});
794796

797+
describe("warnIfVectorSearchNotEnabledCorrectly", () => {
798+
it("should warn if vectorSearch is enabled but embeddings provider is not configured", () => {
799+
const warnStub = vi.fn();
800+
warnIfVectorSearchNotEnabledCorrectly(
801+
{
802+
...defaultTestConfig,
803+
previewFeatures: ["vectorSearch"],
804+
},
805+
warnStub
806+
);
807+
expect(warnStub).toBeCalledWith(`\
808+
Warning: Vector search is enabled but no embeddings provider is configured.
809+
- Set an embeddings provider configuration option to enable auto-embeddings during document insertion and text-based queries with $vectorSearch.\
810+
`);
811+
});
812+
813+
it("should warn if vectorSearch is not enabled but embeddings provider is configured", () => {
814+
const warnStub = vi.fn();
815+
warnIfVectorSearchNotEnabledCorrectly(
816+
{
817+
...defaultTestConfig,
818+
voyageApiKey: "random-key",
819+
},
820+
warnStub
821+
);
822+
expect(warnStub).toBeCalledWith(`\
823+
Warning: An embeddings provider is configured but the 'vectorSearch' preview feature is not enabled.
824+
- Enable vector search by adding 'vectorSearch' to the 'previewFeatures' configuration option, or remove the embeddings provider configuration if not needed.\
825+
`);
826+
});
827+
828+
it("should not warn if vectorSearch is enabled correctly", () => {
829+
const warnStub = vi.fn();
830+
warnIfVectorSearchNotEnabledCorrectly(
831+
{
832+
...defaultTestConfig,
833+
voyageApiKey: "random-key",
834+
previewFeatures: ["vectorSearch"],
835+
},
836+
warnStub
837+
);
838+
expect(warnStub).not.toBeCalled();
839+
});
840+
});
841+
795842
describe("keychain management", () => {
796843
type TestCase = { readonly cliArg: keyof UserConfig; secretKind: Secret["kind"] };
797844
const testCases = [

0 commit comments

Comments
 (0)