Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions evals/shared/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeFS from "node:fs";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import { describe, expect, it, vi } from "vite-plus/test";
import { proxyConfig } from "./proxy.ts";

const config = `model_provider = "cliproxy"

[model_providers.cliproxy]
base_url = "https://proxy.example/v1"
requires_openai_auth = true
`;

describe("proxyConfig", () => {
it.each([
["no auth.json", undefined],
["a ChatGPT-login auth.json", { OPENAI_API_KEY: null, tokens: { access_token: "t" } }],
])("fails when the provider requires OpenAI auth and %s has no key", (_, auth) => {
const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-evals-proxy-"));
using _home = {
[Symbol.dispose]: () => {
vi.unstubAllEnvs();
NodeFS.rmSync(home, { recursive: true });
},
};
NodeFS.writeFileSync(NodePath.join(home, "config.toml"), config);
if (auth) NodeFS.writeFileSync(NodePath.join(home, "auth.json"), JSON.stringify(auth));
vi.stubEnv("CODEX_HOME", home);
expect(() => proxyConfig()).toThrow(/requires_openai_auth/);
});
});
1 change: 1 addition & 0 deletions evals/shared/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ h.update({k: os.environ[v] for k,v in p.get('env_http_headers', {}).items()})
key = os.environ.get(p['env_key']) if p.get('env_key') else p.get('experimental_bearer_token')
if p.get('env_key') and not key: fail(f'[model_providers.{name}] in {path} reads its key from {p["env_key"]}, which is not set.')
if not key and p.get('requires_openai_auth') and (home / 'auth.json').is_file(): key = json.loads((home / 'auth.json').read_text()).get('OPENAI_API_KEY')
if not key and p.get('requires_openai_auth'): fail(f'[model_providers.{name}] in {path} sets requires_openai_auth, but {home / "auth.json"} has no OPENAI_API_KEY.')
if key: h['Authorization'] = 'Bearer ' + key
print(json.dumps({'url': p['base_url'].rstrip('/'), 'headers': h}))
`,
Expand Down
Loading