pub fn user_agent() -> String {
let base = format!("clickhousectl/{}", env!("CARGO_PKG_VERSION"));
match is_ai_agent::detect() {
Some(agent) => format!("{} (agent={})", base, agent.id.as_str()),
None => base,
}
}
#[cfg(test)]
fn user_agent_from(detected: Option<&str>) -> String {
let base = format!("clickhousectl/{}", env!("CARGO_PKG_VERSION"));
match detected {
Some(id) => format!("{} (agent={})", base, id),
None => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn human_invocation_is_just_name_and_version() {
assert_eq!(
user_agent_from(None),
format!("clickhousectl/{}", env!("CARGO_PKG_VERSION"))
);
}
#[test]
fn agent_invocation_appends_paren_comment() {
assert_eq!(
user_agent_from(Some("claude-code")),
format!(
"clickhousectl/{} (agent=claude-code)",
env!("CARGO_PKG_VERSION")
)
);
}
#[test]
fn live_user_agent_starts_with_canonical_prefix() {
let ua = user_agent();
let prefix = format!("clickhousectl/{}", env!("CARGO_PKG_VERSION"));
assert!(ua == prefix || ua.starts_with(&format!("{prefix} (")));
}
}