Files
OmniRoute/tests/unit/cli/launch-codex.test.ts
Diego Rodrigues de Sa e Souza 5c4b0e327d fix(cli): harden launch/launch-codex with free-claude-code patterns (#4278)
Applies proven patterns from the free-claude-code reference adapters:

launch (Claude Code):
- always set ANTHROPIC_AUTH_TOKEN — a no-auth sentinel when none is resolved —
  so newer Claude Code doesn't stop at its local login gate before contacting
  OmniRoute (an open backend ignores the value; ANTHROPIC_API_KEY stays stripped).

launch-codex:
- remote-aware: resolves the root base URL + auth from --remote/--api-key, the
  active context, then localhost (was localhost/--remote only).
- inject the `omniroute` provider via `-c` flags (model_provider + base_url +
  env_key + wire_api=responses + requires_openai_auth=false) so it works WITHOUT
  a pre-existing ~/.codex/config.toml.
- strip OPENAI_*/CODEX_* from the child env (defense-in-depth) and set
  OMNIROUTE_API_KEY to the token or a sentinel. (Honest note: this does NOT
  silence codex's refresh_token log noise — that comes from ~/.codex/auth.json,
  is cosmetic, and does not block requests.)
- replace a hard-coded Tailscale IP in --remote help with a placeholder.

Tests: buildCodexEnv (strip + sentinel + no-mutate), buildCodexProviderArgs
(inline provider def), resolveCodexTarget; updated buildClaudeEnv sentinel test.
Validated remotely vs VPS v3.8.30: `launch-codex --remote ... exec` → "OK".
19 unit tests pass; check:cli-i18n green.
2026-06-19 11:40:53 -03:00

60 lines
2.3 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
buildCodexEnv,
buildCodexProviderArgs,
resolveCodexTarget,
} from "../../../bin/cli/commands/launch-codex.mjs";
test("buildCodexEnv strips stale OpenAI/Codex creds from the child env (defense-in-depth)", () => {
const env = buildCodexEnv(
{
OPENAI_API_KEY: "leak",
OPENAI_BASE_URL: "https://api.openai.com/v1",
OPENAI_ORG_ID: "org",
CODEX_API_KEY: "leak2",
PATH: "/bin",
},
"oma_live_x"
);
assert.equal(env.OPENAI_API_KEY, undefined);
assert.equal(env.OPENAI_BASE_URL, undefined);
assert.equal(env.OPENAI_ORG_ID, undefined);
assert.equal(env.CODEX_API_KEY, undefined);
assert.equal(env.OMNIROUTE_API_KEY, "oma_live_x");
assert.equal(env.PATH, "/bin", "unrelated vars preserved");
});
test("buildCodexEnv uses a no-auth sentinel when no token is given", () => {
const env = buildCodexEnv({ PATH: "/bin" }, undefined);
assert.equal(env.OMNIROUTE_API_KEY, "omniroute-no-auth");
});
test("buildCodexEnv does not mutate the input env", () => {
const input = { OPENAI_API_KEY: "leak", PATH: "/bin" };
buildCodexEnv(input, "x");
assert.equal(input.OPENAI_API_KEY, "leak");
});
test("buildCodexProviderArgs defines the omniroute provider inline (works without config.toml)", () => {
const args = buildCodexProviderArgs("http://vps:20128");
const joined = args.join(" ");
assert.ok(joined.includes('model_provider="omniroute"'));
assert.ok(joined.includes('model_providers.omniroute.base_url="http://vps:20128/v1"'));
assert.ok(joined.includes('model_providers.omniroute.env_key="OMNIROUTE_API_KEY"'));
assert.ok(joined.includes('model_providers.omniroute.wire_api="responses"'));
assert.ok(joined.includes("model_providers.omniroute.requires_openai_auth=false"));
// each assignment is preceded by a -c flag
assert.equal(args.filter((a) => a === "-c").length, 6);
});
test("resolveCodexTarget: --remote wins and /v1 is stripped from the root", () => {
const { baseUrl } = resolveCodexTarget({ remote: "http://vps:20128/v1" });
assert.equal(baseUrl, "http://vps:20128");
});
test("resolveCodexTarget: explicit --api-key wins", () => {
const { authToken } = resolveCodexTarget({ remote: "http://x:20128", apiKey: "tok-explicit" });
assert.equal(authToken, "tok-explicit");
});