use super::*;
use crate::proxy_setup::tests::{CODEX_AUTH_API_KEY, CODEX_AUTH_CHATGPT, pin_codex_home};
use crate::proxy_setup::util::{OPENAI_OMITTED_NOTE, is_lean_ctx_url_on};
#[test]
fn shell_export_omits_openai_for_a_chatgpt_login() {
let _lock = lean_ctx::core::data_dir::test_env_lock();
if std::env::var("OPENAI_API_KEY").is_ok_and(|v| !v.trim().is_empty()) {
return; }
let _codex = pin_codex_home(CODEX_AUTH_CHATGPT);
let home = tempfile::tempdir().unwrap();
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let other_port = (20000..u16::MAX)
.find(|p| !is_lean_ctx_url_on(&format!("http://127.0.0.1:{p}/v1"), Some(port)))
.unwrap();
let stale = [
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1\""),
format!("export OPENAI_BASE_URL='http://localhost:{port}/v1/'"),
];
let preserved = [
format!("# export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1\""),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1\"; echo user"),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1\" # user"),
"export OPENAI_BASE_URL=\"https://gateway.example/v1\"".to_string(),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{other_port}/v1\""),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1/custom\""),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1/../v1\""),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v%31\""),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1?user=1\""),
format!("export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1#user\""),
format!("export OTHER_BASE_URL=\"http://127.0.0.1:{port}/v1\""),
"# user spacing ".to_string(),
];
let shell_data = format!(
"helper() {{\n{}\n}}\ncat <<'LEANCTX_SAMPLE'\n{}\nLEANCTX_SAMPLE\nmessage='sample\n{}\nend'\nif true; then\n{}\nfi\n",
stale[0], stale[0], stale[0], stale[0]
);
let seed = format!(
"# user rc\n{}\n{}\n{shell_data}",
stale.join("\n"),
preserved.join("\n")
);
for name in [".zshrc", ".bashrc"] {
std::fs::write(home.path().join(name), &seed).unwrap();
}
install_shell_exports(home.path(), port, true, false);
let rc = std::fs::read_to_string(home.path().join(".zshrc")).unwrap();
assert!(rc.contains(&shell_data));
for old in &stale {
let count = rc.lines().filter(|line| *line == old).count();
let expected = if old == &stale[0] { 4 } else { 0 };
assert_eq!(
count,
expected + usize::from(!cfg!(feature = "tree-sitter"))
);
}
for line in preserved {
assert!(
rc.lines().any(|actual| actual == line),
"user line lost: {line}"
);
}
let managed = rc.split(PROXY_ENV_START).nth(1).unwrap();
assert!(!managed.contains("export OPENAI_BASE_URL="));
assert!(
rc.contains(OPENAI_OMITTED_NOTE),
"the omission must be explained in the RC block, got:\n{rc}"
);
assert_eq!(
std::fs::read_to_string(home.path().join(".bashrc")).unwrap(),
rc
);
install_shell_exports(home.path(), port, true, false);
for name in [".zshrc", ".bashrc"] {
assert_eq!(std::fs::read_to_string(home.path().join(name)).unwrap(), rc);
}
#[cfg(feature = "tree-sitter")]
for uncertain in [
format!("{PROXY_ENV_START}\n{}\n", stale[0]),
format!("{PROXY_ENV_START}\n{PROXY_ENV_END}\n{PROXY_ENV_START}\n{PROXY_ENV_END}\n"),
format!(
"cat <<'EXAMPLE'\n{PROXY_ENV_START}\n{}\n{PROXY_ENV_END}\nEXAMPLE\n",
stale[0]
),
format!("message='unterminated\n{}\n", stale[0]),
] {
std::fs::write(home.path().join(".zshrc"), &uncertain).unwrap();
install_shell_exports(home.path(), port, true, false);
assert_eq!(
std::fs::read_to_string(home.path().join(".zshrc")).unwrap(),
uncertain
);
}
}
#[test]
fn shell_export_keeps_openai_for_an_api_key_login() {
let _lock = lean_ctx::core::data_dir::test_env_lock();
let _codex = pin_codex_home(CODEX_AUTH_API_KEY);
let home = tempfile::tempdir().unwrap();
let legacy = "export OPENAI_BASE_URL=\"http://127.0.0.1:4444/v1\"";
std::fs::write(home.path().join(".zshrc"), format!("# user rc\n{legacy}\n")).unwrap();
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
install_shell_exports(home.path(), port, true, false);
let rc = std::fs::read_to_string(home.path().join(".zshrc")).unwrap();
assert!(rc.lines().any(|line| line == legacy));
assert!(
rc.contains(&format!(
"export OPENAI_BASE_URL=\"http://127.0.0.1:{port}/v1\""
)),
"API-key mode must route OpenAI through the proxy, got:\n{rc}"
);
}
#[test]
fn a_user_set_model_provider_survives_the_strip() {
let existing = "model = \"gpt-5.5\"\nmodel_provider = \"openai\"\n";
let cleaned = strip_codex_proxy_entries(existing);
assert!(
cleaned.contains("model_provider = \"openai\""),
"a pin lean-ctx never wrote must be left alone, got:\n{cleaned}"
);
}
#[test]
fn the_generated_model_provider_pin_is_still_stripped() {
let existing = "model_provider = \"leanctx-chatgpt\"\nmodel = \"gpt-5.5\"\n";
let cleaned = strip_codex_proxy_entries(existing);
assert!(
!cleaned.contains("leanctx-chatgpt"),
"lean-ctx's own pin must still be removed, got:\n{cleaned}"
);
assert!(cleaned.contains("model = \"gpt-5.5\""));
}
#[test]
fn a_full_install_pass_never_contradicts_the_codex_config() {
let _lock = lean_ctx::core::data_dir::test_env_lock();
if std::env::var("OPENAI_API_KEY").is_ok_and(|v| !v.trim().is_empty())
|| crate::proxy_setup::tests::claude_dir_overridden()
{
return;
}
let codex = pin_codex_home(CODEX_AUTH_CHATGPT);
std::fs::write(codex.path().join("config.toml"), "model = \"gpt-5.5\"\n").unwrap();
let home = tempfile::tempdir().unwrap();
std::fs::write(home.path().join(".zshrc"), "# user rc\n").unwrap();
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
install_proxy_env_unchecked(home.path(), port, true, false);
let rc = std::fs::read_to_string(home.path().join(".zshrc")).unwrap();
let codex_config = std::fs::read_to_string(codex.path().join("config.toml")).unwrap();
assert!(
!rc.contains("export OPENAI_BASE_URL="),
"the shell must not pin a ChatGPT login to the /v1 rail, got:\n{rc}"
);
assert!(
!codex_config.contains("127.0.0.1"),
"the Codex config must stay native for a ChatGPT login, got:\n{codex_config}"
);
assert!(
codex_config.contains("model = \"gpt-5.5\""),
"the user's own settings must survive the pass, got:\n{codex_config}"
);
}