#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum ServerUrl {
#[default]
Prod,
Custom(String),
}
impl ServerUrl {
pub const PROD: &'static str = "https://code.aretta.ai";
pub fn as_str(&self) -> &str {
match self {
Self::Prod => Self::PROD,
Self::Custom(s) => s,
}
}
pub fn parse(raw: &str) -> Self {
let trimmed = raw.trim();
match trimmed {
"" => Self::Prod,
other if other.starts_with("http://") || other.starts_with("https://") => {
Self::Custom(other.trim_end_matches('/').to_string())
}
other => Self::Custom(format!("https://{}", other.trim_end_matches('/'))),
}
}
}
impl std::fmt::Display for ServerUrl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[aristo::intent(
"Data-plane base-URL precedence is exactly ARETTA_API_URL (env) > \
the credential's server, and no other tier. A blank/whitespace env \
override is treated as unset (matching the login resolver, \
login_server) so it falls through to the credential's server \
instead of routing to an empty base; a present env override is \
normalized via ServerUrl::parse, the same reading every server \
spec gets. Adding a tier, dropping the blank-as-unset guard, or \
reading the env override differently from the login server would \
silently misroute verify and canon-match requests to the wrong \
Aretta deployment.",
verify = "neural",
id = "data_plane_base_precedence"
)]
pub fn data_plane_base(env_override: Option<&str>, server: &ServerUrl) -> String {
match env_override.map(str::trim).filter(|s| !s.is_empty()) {
Some(v) => ServerUrl::parse(v).as_str().to_string(),
None => server.as_str().to_string(),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoginServerSource {
Flag,
Env,
}
impl LoginServerSource {
pub fn provenance(self) -> &'static str {
match self {
Self::Flag => "from --server",
Self::Env => "from ARETTA_API_URL",
}
}
}
pub fn login_server(
flag: Option<&str>,
env_override: Option<&str>,
) -> Option<(ServerUrl, LoginServerSource)> {
if let Some(f) = flag {
return Some((ServerUrl::parse(f), LoginServerSource::Flag));
}
let v = env_override.map(str::trim).filter(|s| !s.is_empty())?;
Some((ServerUrl::parse(v), LoginServerSource::Env))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prod_resolves_to_code_aretta_ai() {
assert_eq!(ServerUrl::Prod.as_str(), "https://code.aretta.ai");
}
#[test]
fn parse_has_no_alias() {
assert_eq!(ServerUrl::parse(""), ServerUrl::Prod);
assert_eq!(ServerUrl::parse(" "), ServerUrl::Prod);
assert_eq!(
ServerUrl::parse("prod"),
ServerUrl::Custom("https://prod".into())
);
assert_eq!(
ServerUrl::parse(" x.example.com "),
ServerUrl::Custom("https://x.example.com".into())
);
}
#[test]
fn former_dev_aliases_are_now_plain_custom_hosts() {
assert_eq!(
ServerUrl::parse("dev"),
ServerUrl::Custom("https://dev".into())
);
assert_eq!(
ServerUrl::parse("development"),
ServerUrl::Custom("https://development".into())
);
assert_eq!(
ServerUrl::parse("staging"),
ServerUrl::Custom("https://staging".into())
);
}
#[test]
fn parse_full_url_passes_through_as_custom() {
let s = ServerUrl::parse("https://aretta.example.com");
assert_eq!(s, ServerUrl::Custom("https://aretta.example.com".into()));
assert_eq!(s.as_str(), "https://aretta.example.com");
}
#[test]
fn parse_http_url_is_accepted_for_self_hosted() {
let s = ServerUrl::parse("http://aretta.internal");
assert_eq!(s, ServerUrl::Custom("http://aretta.internal".into()));
}
#[test]
fn parse_strips_trailing_slash_for_clean_format_strings() {
let s = ServerUrl::parse("https://example.com/");
assert_eq!(s.as_str(), "https://example.com");
}
#[test]
fn parse_bare_host_defaults_to_https() {
let s = ServerUrl::parse("aretta.example.com");
assert_eq!(s, ServerUrl::Custom("https://aretta.example.com".into()));
}
#[test]
fn parse_empty_string_falls_back_to_prod() {
assert_eq!(ServerUrl::parse(""), ServerUrl::Prod);
assert_eq!(ServerUrl::parse(" "), ServerUrl::Prod);
}
#[test]
fn default_is_prod() {
assert_eq!(ServerUrl::default(), ServerUrl::Prod);
}
#[test]
fn display_renders_full_url() {
assert_eq!(format!("{}", ServerUrl::Prod), "https://code.aretta.ai");
assert_eq!(
format!("{}", ServerUrl::Custom("https://x.example.com".into())),
"https://x.example.com"
);
}
#[test]
fn data_plane_base_env_override_wins_and_is_normalized() {
let s = data_plane_base(Some("ci.example.com/"), &ServerUrl::Prod);
assert_eq!(s, "https://ci.example.com");
}
#[test]
fn data_plane_base_blank_env_falls_through_to_server() {
let custom = ServerUrl::Custom("https://staging.example.com".into());
assert_eq!(
data_plane_base(Some(""), &custom),
"https://staging.example.com"
);
assert_eq!(
data_plane_base(Some(" "), &custom),
"https://staging.example.com"
);
}
#[test]
fn data_plane_base_falls_back_to_server() {
assert_eq!(
data_plane_base(None, &ServerUrl::Prod),
"https://code.aretta.ai"
);
}
#[test]
fn login_server_flag_beats_env() {
let (server, source) = login_server(
Some("https://flag.example.com"),
Some("https://turso.aretta.ai"),
)
.unwrap();
assert_eq!(server, ServerUrl::Custom("https://flag.example.com".into()));
assert_eq!(source, LoginServerSource::Flag);
}
#[test]
fn login_server_env_when_no_flag() {
let (server, source) = login_server(None, Some("https://turso.aretta.ai")).unwrap();
assert_eq!(server, ServerUrl::Custom("https://turso.aretta.ai".into()));
assert_eq!(source, LoginServerSource::Env);
}
#[test]
fn login_server_env_parsed_via_serverurl_parse() {
assert_eq!(
login_server(None, Some("turso.aretta.ai/")).unwrap().0,
ServerUrl::Custom("https://turso.aretta.ai".into())
);
}
#[test]
fn login_server_blank_env_is_unset() {
assert_eq!(login_server(None, Some(" ")), None);
}
#[test]
fn login_server_has_no_default() {
assert_eq!(login_server(None, None), None);
}
#[test]
fn login_server_provenance_is_always_named() {
assert_eq!(LoginServerSource::Flag.provenance(), "from --server");
assert_eq!(LoginServerSource::Env.provenance(), "from ARETTA_API_URL");
}
}