diff --git a/evals/shared/proxy.test.ts b/evals/shared/proxy.test.ts new file mode 100644 index 0000000000000..a38c1656510f9 --- /dev/null +++ b/evals/shared/proxy.test.ts @@ -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/); + }); +}); diff --git a/evals/shared/proxy.ts b/evals/shared/proxy.ts index 12ea4c150e80c..f088316cea97f 100644 --- a/evals/shared/proxy.ts +++ b/evals/shared/proxy.ts @@ -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})) `,