|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import type { AuthProfileStore } from "./auth-profiles.js"; |
3 | | -import { requireApiKey, resolveAwsSdkEnvVarName, resolveModelAuthMode } from "./model-auth.js"; |
| 3 | +import { NON_ENV_SECRETREF_MARKER } from "./model-auth-markers.js"; |
| 4 | +import { |
| 5 | + hasUsableCustomProviderApiKey, |
| 6 | + requireApiKey, |
| 7 | + resolveAwsSdkEnvVarName, |
| 8 | + resolveModelAuthMode, |
| 9 | + resolveUsableCustomProviderApiKey, |
| 10 | +} from "./model-auth.js"; |
4 | 11 |
|
5 | 12 | describe("resolveAwsSdkEnvVarName", () => { |
6 | 13 | it("prefers bearer token over access keys and profile", () => { |
@@ -117,3 +124,102 @@ describe("requireApiKey", () => { |
117 | 124 | ).toThrow('No API key resolved for provider "openai"'); |
118 | 125 | }); |
119 | 126 | }); |
| 127 | + |
| 128 | +describe("resolveUsableCustomProviderApiKey", () => { |
| 129 | + it("returns literal custom provider keys", () => { |
| 130 | + const resolved = resolveUsableCustomProviderApiKey({ |
| 131 | + cfg: { |
| 132 | + models: { |
| 133 | + providers: { |
| 134 | + custom: { |
| 135 | + baseUrl: "https://example.com/v1", |
| 136 | + apiKey: "sk-custom-runtime", // pragma: allowlist secret |
| 137 | + models: [], |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + provider: "custom", |
| 143 | + }); |
| 144 | + expect(resolved).toEqual({ |
| 145 | + apiKey: "sk-custom-runtime", |
| 146 | + source: "models.json", |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it("does not treat non-env markers as usable credentials", () => { |
| 151 | + const resolved = resolveUsableCustomProviderApiKey({ |
| 152 | + cfg: { |
| 153 | + models: { |
| 154 | + providers: { |
| 155 | + custom: { |
| 156 | + baseUrl: "https://example.com/v1", |
| 157 | + apiKey: NON_ENV_SECRETREF_MARKER, |
| 158 | + models: [], |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + provider: "custom", |
| 164 | + }); |
| 165 | + expect(resolved).toBeNull(); |
| 166 | + }); |
| 167 | + |
| 168 | + it("resolves known env marker names from process env for custom providers", () => { |
| 169 | + const previous = process.env.OPENAI_API_KEY; |
| 170 | + process.env.OPENAI_API_KEY = "sk-from-env"; // pragma: allowlist secret |
| 171 | + try { |
| 172 | + const resolved = resolveUsableCustomProviderApiKey({ |
| 173 | + cfg: { |
| 174 | + models: { |
| 175 | + providers: { |
| 176 | + custom: { |
| 177 | + baseUrl: "https://example.com/v1", |
| 178 | + apiKey: "OPENAI_API_KEY", |
| 179 | + models: [], |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + provider: "custom", |
| 185 | + }); |
| 186 | + expect(resolved?.apiKey).toBe("sk-from-env"); |
| 187 | + expect(resolved?.source).toContain("OPENAI_API_KEY"); |
| 188 | + } finally { |
| 189 | + if (previous === undefined) { |
| 190 | + delete process.env.OPENAI_API_KEY; |
| 191 | + } else { |
| 192 | + process.env.OPENAI_API_KEY = previous; |
| 193 | + } |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | + it("does not treat known env marker names as usable when env value is missing", () => { |
| 198 | + const previous = process.env.OPENAI_API_KEY; |
| 199 | + delete process.env.OPENAI_API_KEY; |
| 200 | + try { |
| 201 | + expect( |
| 202 | + hasUsableCustomProviderApiKey( |
| 203 | + { |
| 204 | + models: { |
| 205 | + providers: { |
| 206 | + custom: { |
| 207 | + baseUrl: "https://example.com/v1", |
| 208 | + apiKey: "OPENAI_API_KEY", |
| 209 | + models: [], |
| 210 | + }, |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + "custom", |
| 215 | + ), |
| 216 | + ).toBe(false); |
| 217 | + } finally { |
| 218 | + if (previous === undefined) { |
| 219 | + delete process.env.OPENAI_API_KEY; |
| 220 | + } else { |
| 221 | + process.env.OPENAI_API_KEY = previous; |
| 222 | + } |
| 223 | + } |
| 224 | + }); |
| 225 | +}); |
0 commit comments