Files
7e4a76a6d0 fix(amazon-bedrock): ignore blank region env overrides (#110676)
* fix(amazon-bedrock): ignore blank region env overrides

* fix(amazon-bedrock): normalize region envs across runtimes

Co-authored-by: ZengWen-DT <ceng.wen@xydigit.com>

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-18 22:38:51 +01:00

189 lines
6.0 KiB
TypeScript

// Amazon Bedrock tests cover embedding provider plugin behavior.
import { afterEach, describe, expect, it, vi } from "vitest";
import { createBedrockEmbeddingProvider, hasAwsCredentials } from "./embedding-provider.js";
import { embeddingTesting as testing } from "./test-support.js";
afterEach(() => {
vi.unstubAllEnvs();
});
describe("bedrock embedding region resolution", () => {
it.each([
{
name: "secondary region when the primary env override is blank",
primary: " ",
secondary: "eu-west-1",
expected: "eu-west-1",
},
{
name: "plugin default when both env overrides are blank",
primary: "",
secondary: " ",
expected: "us-east-1",
},
{
name: "primary region when both env overrides are nonblank",
primary: "ap-southeast-2",
secondary: "eu-west-1",
expected: "ap-southeast-2",
},
])("uses $name", async ({ primary, secondary, expected }) => {
vi.stubEnv("AWS_REGION", primary);
vi.stubEnv("AWS_DEFAULT_REGION", secondary);
const { client } = await createBedrockEmbeddingProvider({
config: {},
model: "",
});
expect(client.region).toBe(expected);
});
});
describe("hasAwsCredentials", () => {
it("accepts static AWS key credentials without loading the credential chain", async () => {
const loadCredentialProvider = vi.fn();
await expect(
hasAwsCredentials(
{
AWS_ACCESS_KEY_ID: "access-key",
AWS_SECRET_ACCESS_KEY: "secret-key",
},
loadCredentialProvider,
),
).resolves.toBe(true);
expect(loadCredentialProvider).not.toHaveBeenCalled();
});
it("accepts the Bedrock bearer token without loading the credential chain", async () => {
const loadCredentialProvider = vi.fn();
await expect(
hasAwsCredentials(
{
AWS_BEARER_TOKEN_BEDROCK: "bearer-token",
},
loadCredentialProvider,
),
).resolves.toBe(true);
expect(loadCredentialProvider).not.toHaveBeenCalled();
});
it("requires AWS profile credentials to resolve through the credential chain", async () => {
const loadCredentialProvider = vi
.fn()
.mockResolvedValue(() => async () => ({ accessKeyId: "resolved-access-key" }));
await expect(hasAwsCredentials({ AWS_PROFILE: "work" }, loadCredentialProvider)).resolves.toBe(
true,
);
expect(loadCredentialProvider).toHaveBeenCalledOnce();
});
it("rejects AWS profile markers when the credential chain cannot resolve", async () => {
const loadCredentialProvider = vi.fn().mockResolvedValue(() => async () => {
throw new Error("Could not load credentials from any providers");
});
await expect(
hasAwsCredentials({ AWS_PROFILE: "missing" }, loadCredentialProvider),
).resolves.toBe(false);
});
it("returns false when the AWS credential provider package is unavailable", async () => {
const loadCredentialProvider = vi.fn().mockResolvedValue(null);
await expect(hasAwsCredentials({}, loadCredentialProvider)).resolves.toBe(false);
});
});
describe("bedrock embedding response parsers", () => {
it("wraps malformed single embedding JSON", () => {
expect(() => testing.parseSingle("titan-v2", "{not json")).toThrow(
"Amazon Bedrock embedding response returned malformed JSON",
);
});
it("wraps malformed batch embedding JSON", () => {
expect(() => testing.parseCohereBatch("cohere-v3", "{not json")).toThrow(
"Amazon Bedrock embedding response returned malformed JSON",
);
});
it("rejects non-object embedding JSON", () => {
expect(() => testing.parseSingle("titan-v2", "[]")).toThrow(
"Amazon Bedrock embedding response returned malformed JSON",
);
});
it("rejects missing single embedding vectors", () => {
expect(() => testing.parseSingle("titan-v2", "{}")).toThrow(
"Amazon Bedrock embedding response returned malformed JSON",
);
});
it("rejects wrong single embedding vector element types", () => {
expect(() => testing.parseSingle("titan-v2", '{"embedding":[1,"bad"]}')).toThrow(
"Amazon Bedrock embedding response returned malformed JSON",
);
});
it("rejects missing batch embedding vectors", () => {
expect(() => testing.parseCohereBatch("cohere-v3", "{}")).toThrow(
"Amazon Bedrock embedding response returned malformed JSON",
);
});
it("rejects wrong batch embedding vector shapes", () => {
expect(() =>
testing.parseCohereBatch("cohere-v3", '{"embeddings":[[1],{"bad":true}]}'),
).toThrow("Amazon Bedrock embedding response returned malformed JSON");
});
});
describe("stripInferenceProfilePrefix", () => {
it("strips global prefix", () => {
expect(testing.stripInferenceProfilePrefix("global.cohere.embed-v4:0")).toBe(
"cohere.embed-v4:0",
);
});
it("strips us prefix", () => {
expect(testing.stripInferenceProfilePrefix("us.cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("strips eu prefix", () => {
expect(testing.stripInferenceProfilePrefix("eu.cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("strips ap prefix", () => {
expect(testing.stripInferenceProfilePrefix("ap.cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("strips apac prefix", () => {
expect(testing.stripInferenceProfilePrefix("apac.cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("strips au prefix", () => {
expect(testing.stripInferenceProfilePrefix("au.cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("strips jp prefix", () => {
expect(testing.stripInferenceProfilePrefix("jp.cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("returns unchanged model ID without prefix", () => {
expect(testing.stripInferenceProfilePrefix("cohere.embed-v4:0")).toBe("cohere.embed-v4:0");
});
it("returns unchanged model ID for amazon.titan-embed-text-v2:0", () => {
expect(testing.stripInferenceProfilePrefix("amazon.titan-embed-text-v2:0")).toBe(
"amazon.titan-embed-text-v2:0",
);
});
});