copilot-api / tests /provider-auth.test.ts
imspsycho's picture
Initial upload from Google Colab
98c9143 verified
Raw
History Blame Contribute Delete
2.45 kB
import { describe, expect, test } from "bun:test"
import {
resolveProviderAuthType,
type ResolvedProviderConfig,
} from "~/lib/config"
import { buildProviderUpstreamHeaders } from "../src/services/providers/anthropic-proxy"
function createProviderConfig(
overrides: Partial<ResolvedProviderConfig> = {},
): ResolvedProviderConfig {
return {
name: "custom",
type: "anthropic",
baseUrl: "https://example.com",
apiKey: "provider-key",
authType: "x-api-key",
...overrides,
}
}
describe("buildProviderUpstreamHeaders", () => {
test("uses x-api-key auth by default", () => {
const headers = buildProviderUpstreamHeaders(
createProviderConfig(),
new Headers({
accept: "application/json",
"anthropic-version": "2023-06-01",
}),
)
expect(headers).toEqual({
"content-type": "application/json",
accept: "application/json",
"x-api-key": "provider-key",
"anthropic-version": "2023-06-01",
})
})
test("uses Authorization bearer auth when configured", () => {
const headers = buildProviderUpstreamHeaders(
createProviderConfig({ authType: "authorization" }),
new Headers({
accept: "application/json",
"user-agent": "test-client",
}),
)
expect(headers).toEqual({
"content-type": "application/json",
accept: "application/json",
authorization: "Bearer provider-key",
"user-agent": "test-client",
})
})
test("does not forward Anthropic-only headers to OpenAI-compatible providers", () => {
const headers = buildProviderUpstreamHeaders(
createProviderConfig({
authType: "authorization",
type: "openai-compatible",
}),
new Headers({
accept: "application/json",
"anthropic-version": "2023-06-01",
}),
)
expect(headers).toEqual({
"content-type": "application/json",
accept: "application/json",
authorization: "Bearer provider-key",
})
})
})
describe("resolveProviderAuthType", () => {
test("falls back to OpenAI-compatible default for invalid authType", () => {
expect(
resolveProviderAuthType("dash", "invalid-auth-type", "openai-compatible"),
).toBe("authorization")
})
test("falls back to Anthropic default for invalid authType", () => {
expect(
resolveProviderAuthType("custom", "invalid-auth-type", "anthropic"),
).toBe("x-api-key")
})
})