use super::{
capability_banner, concise_help_text, is_server_mode, quickstart_text, resolve_worker_threads,
};
use serial_test::serial;
fn args_of(parts: &[&str]) -> Vec<String> {
parts.iter().map(|s| (*s).to_string()).collect()
}
#[test]
fn server_modes_keep_ignored_sigpipe() {
for mode in [
"mcp",
"daemon",
"proxy",
"serve",
"watch",
"cockpit",
"dashboard",
] {
assert!(
is_server_mode(&args_of(&["lean-ctx", mode])),
"{mode} must count as server mode"
);
}
for view in ["--web", "--live"] {
assert!(is_server_mode(&args_of(&["lean-ctx", "gain", view])));
}
assert!(is_server_mode(&args_of(&["lean-ctx"])));
}
#[test]
fn cli_modes_restore_default_sigpipe() {
for mode in ["doctor", "-c", "status", "ls", "grep", "gain", "help"] {
assert!(
!is_server_mode(&args_of(&["lean-ctx", mode])),
"{mode} must count as CLI mode (SIGPIPE default)"
);
}
}
#[test]
fn quickstart_is_short_and_points_to_setup() {
let q = quickstart_text();
assert!(q.contains("lean-ctx wrap"), "quickstart must point to wrap");
assert!(q.contains("lean-ctx help"), "quickstart must point to help");
assert!(
q.lines().count() <= 16,
"quickstart should be short; got {} lines",
q.lines().count()
);
assert!(
!q.contains("COMMANDS:"),
"quickstart must not inline the full command reference"
);
}
#[test]
fn concise_help_is_short_and_points_to_full() {
let h = concise_help_text();
assert!(h.contains("lean-ctx wrap"), "must lead with wrap");
assert!(
h.contains("lean-ctx help all"),
"must point to full reference"
);
assert!(
h.contains("lean-ctx tools"),
"must surface the tools profile command"
);
assert!(
h.lines().count() <= 40,
"concise help should stay short; got {} lines",
h.lines().count()
);
assert!(
!h.contains("SHELL HOOK PATTERNS"),
"concise help must not inline the full pattern catalog"
);
}
#[test]
fn capability_banner_tool_count_matches_registry() {
let n = lean_ctx_server::server::registry::tool_count();
let banner = capability_banner();
assert!(
banner.contains(&format!("{n} MCP tools")),
"banner must show the live registry count ({n}); got: {banner}"
);
}
#[test]
#[serial]
fn worker_threads_default_clamps_low() {
let _env_lock = lean_ctx::core::data_dir::test_env_lock();
lean_ctx::test_env::remove_var("LEAN_CTX_WORKER_THREADS");
assert_eq!(resolve_worker_threads(1), 1);
}
#[test]
#[serial]
fn worker_threads_default_clamps_high() {
let _env_lock = lean_ctx::core::data_dir::test_env_lock();
lean_ctx::test_env::remove_var("LEAN_CTX_WORKER_THREADS");
assert_eq!(resolve_worker_threads(32), 4);
}
#[test]
#[serial]
fn worker_threads_default_passthrough() {
let _env_lock = lean_ctx::core::data_dir::test_env_lock();
lean_ctx::test_env::remove_var("LEAN_CTX_WORKER_THREADS");
assert_eq!(resolve_worker_threads(3), 3);
}
#[test]
#[serial]
fn worker_threads_env_override() {
let _env_lock = lean_ctx::core::data_dir::test_env_lock();
lean_ctx::test_env::set_var("LEAN_CTX_WORKER_THREADS", "12");
assert_eq!(resolve_worker_threads(2), 12);
lean_ctx::test_env::remove_var("LEAN_CTX_WORKER_THREADS");
}
#[test]
#[serial]
fn worker_threads_env_invalid_falls_back() {
let _env_lock = lean_ctx::core::data_dir::test_env_lock();
lean_ctx::test_env::set_var("LEAN_CTX_WORKER_THREADS", "not_a_number");
assert_eq!(resolve_worker_threads(3), 3);
lean_ctx::test_env::remove_var("LEAN_CTX_WORKER_THREADS");
}
#[test]
fn passthrough_child_drops_startup_and_option_variables() {
let cmd = super::passthrough_command("true");
let envs: Vec<_> = cmd.get_envs().collect();
for name in lean_ctx::shell::platform::HIDDEN_OPTION_ENV {
assert!(
envs.iter()
.any(|(k, v)| *k == std::ffi::OsStr::new(name) && v.is_none()),
"passthrough must remove {name} from the child"
);
}
for name in ["BASH_ENV", "ENV"] {
assert!(
envs.iter().any(|(k, v)| *k == std::ffi::OsStr::new(name)
&& v.is_some_and(std::ffi::OsStr::is_empty)),
"passthrough must clear {name}"
);
}
}