Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
70
crates/luxe-providers/src/auth/service/custom.rs
Normal file
70
crates/luxe-providers/src/auth/service/custom.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
//! The custom endpoint — a wizard entry that becomes a provider once the user names it.
|
||||
|
||||
use luxe_core::auth::{ApiKind, AuthError, AuthInteraction, AuthPrompt};
|
||||
use secrecy::ExposeSecret;
|
||||
|
||||
use super::models::normalize_base_url;
|
||||
use super::{ProviderAuth, ProviderDescriptor};
|
||||
use crate::auth::catalog::provider_descriptor;
|
||||
|
||||
impl ProviderAuth {
|
||||
/// Resolve the custom-endpoint pseudo-provider into a real one the user names.
|
||||
pub(super) async fn resolve_custom(
|
||||
&self,
|
||||
interaction: &dyn AuthInteraction,
|
||||
) -> Result<(ProviderDescriptor, String), AuthError> {
|
||||
let id_answer = interaction
|
||||
.prompt(AuthPrompt::text(
|
||||
"Name for this provider (letters, digits, - and _):",
|
||||
Some("my-endpoint".to_string()),
|
||||
))
|
||||
.await?;
|
||||
let id = id_answer.expose_secret().trim().to_ascii_lowercase();
|
||||
if id.is_empty() || !id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') {
|
||||
return Err(AuthError::Input(
|
||||
"use letters, digits, dashes or underscores for the provider name".to_string(),
|
||||
));
|
||||
}
|
||||
if provider_descriptor(&id).is_some() {
|
||||
return Err(AuthError::Input(format!(
|
||||
"'{id}' is already a built-in provider — run `luxe auth login {id}` instead"
|
||||
)));
|
||||
}
|
||||
|
||||
let url_answer = interaction
|
||||
.prompt(AuthPrompt::text(
|
||||
"Base URL, including the version segment:",
|
||||
Some("https://host/v1".to_string()),
|
||||
))
|
||||
.await?;
|
||||
let base_url = normalize_base_url(url_answer.expose_secret())?;
|
||||
|
||||
let api_answer = interaction
|
||||
.prompt(AuthPrompt::select(
|
||||
"Which API does it speak?",
|
||||
vec![
|
||||
luxe_core::auth::AuthChoice {
|
||||
id: ApiKind::OpenaiCompletions.as_str().to_string(),
|
||||
label: "OpenAI /chat/completions".to_string(),
|
||||
detail: Some("vLLM, llama.cpp, Ollama, most proxies".to_string()),
|
||||
},
|
||||
luxe_core::auth::AuthChoice {
|
||||
id: ApiKind::OpenaiResponses.as_str().to_string(),
|
||||
label: "OpenAI /responses".to_string(),
|
||||
detail: Some("Azure OpenAI, newer OpenAI-compatible gateways".to_string()),
|
||||
},
|
||||
luxe_core::auth::AuthChoice {
|
||||
id: ApiKind::AnthropicMessages.as_str().to_string(),
|
||||
label: "Anthropic /messages".to_string(),
|
||||
detail: Some("An Anthropic-compatible gateway".to_string()),
|
||||
},
|
||||
],
|
||||
))
|
||||
.await?;
|
||||
let api = ApiKind::parse(api_answer.expose_secret())
|
||||
.ok_or_else(|| AuthError::Input("unknown API kind".to_string()))?;
|
||||
|
||||
let descriptor = ProviderDescriptor::custom(id, api);
|
||||
Ok((descriptor, base_url))
|
||||
}
|
||||
}
|
||||
193
crates/luxe-providers/src/auth/service/models.rs
Normal file
193
crates/luxe-providers/src/auth/service/models.rs
Normal file
@@ -0,0 +1,193 @@
|
||||
//! Model lists — verifying a credential reaches a provider, and reading what it serves.
|
||||
//!
|
||||
//! The pure half of a login: nothing here writes anything. [`verify_credential`] is the one network
|
||||
//! call, and the rest turn its answer into the confirmation a user reads.
|
||||
|
||||
use luxe_core::auth::{ApiKind, AuthError, AuthEvent, AuthInteraction};
|
||||
|
||||
use super::ProviderDescriptor;
|
||||
use crate::auth::source::RequestAuth;
|
||||
|
||||
/// How many models to name when confirming a login.
|
||||
const MODEL_PREVIEW: usize = 3;
|
||||
|
||||
/// Check that a credential reaches the provider, and learn what it can serve.
|
||||
///
|
||||
/// One cheap authenticated request — the model list — which doubles as the source of the "17 models
|
||||
/// available" confirmation and of a sensible default model. Codex has no such endpoint, and its login
|
||||
/// already proved the account, so it is exempt; so is a provider whose `lists_models` says the same
|
||||
/// (Foundry's Anthropic passthrough), which verifies through the sibling sharing its resource
|
||||
/// rather than reading a 404 as a broken key.
|
||||
///
|
||||
/// **What this does and doesn't prove.** A 401/403 is a real rejection and fails the login, which is
|
||||
/// what catches a mistyped or revoked key on the providers that authenticate this endpoint (OpenAI,
|
||||
/// Anthropic, most others). A few providers serve `/models` publicly — NVIDIA NIM does — so a wrong
|
||||
/// key there passes this check and fails on the first turn instead, with the auth error the adapter
|
||||
/// now raises. The alternative is spending tokens on a real completion at every login, which is a
|
||||
/// worse trade for a check whose main job is catching typos and unreachable endpoints.
|
||||
pub(super) async fn verify_credential(
|
||||
descriptor: &ProviderDescriptor,
|
||||
base_url: Option<&str>,
|
||||
auth: &RequestAuth,
|
||||
) -> Result<Vec<String>, AuthError> {
|
||||
if descriptor.api == ApiKind::OpenaiCodex || !descriptor.lists_models {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let Some(base) = base_url else { return Ok(Vec::new()) };
|
||||
let url = format!("{}/models", base.trim_end_matches('/'));
|
||||
|
||||
let mut request = crate::http::shared_client().get(&url).header("Accept", "application/json");
|
||||
request = auth.apply(request);
|
||||
if descriptor.api == ApiKind::AnthropicMessages {
|
||||
request = request.header("anthropic-version", "2023-06-01");
|
||||
}
|
||||
let response = within_start_timeout(&url, "an answer", request.send()).await?;
|
||||
|
||||
let status = response.status();
|
||||
let body = within_start_timeout(&url, "the model list", async {
|
||||
Ok(response.text().await.unwrap_or_default())
|
||||
})
|
||||
.await?;
|
||||
if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
|
||||
return Err(AuthError::Rejected(format!(
|
||||
"{} rejected that credential ({status})",
|
||||
descriptor.name
|
||||
)));
|
||||
}
|
||||
if !status.is_success() {
|
||||
// A provider without a `/models` endpoint is not a broken credential. Say so and move on:
|
||||
// refusing the login here would block a working local server or a minimal proxy.
|
||||
tracing::debug!(provider = %descriptor.id, %status, "provider has no usable model list");
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Ok(parse_model_ids(&body))
|
||||
}
|
||||
|
||||
/// Pull model ids out of an OpenAI- or Anthropic-shaped list response.
|
||||
pub(super) fn parse_model_ids(body: &str) -> Vec<String> {
|
||||
let Ok(value) = serde_json::from_str::<serde_json::Value>(body) else { return Vec::new() };
|
||||
let rows = value.get("data").and_then(|data| data.as_array());
|
||||
let mut ids: Vec<String> = rows
|
||||
.map(|rows| {
|
||||
rows.iter()
|
||||
.filter_map(|row| row.get("id").and_then(|id| id.as_str()))
|
||||
.filter(|id| !answers_nothing(id))
|
||||
.map(str::to_string)
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
ids
|
||||
}
|
||||
|
||||
/// Whether an id names a model that cannot hold a conversation.
|
||||
///
|
||||
/// `/v1/models` is a flat list — LM Studio, Ollama and OpenAI all return an embedding model
|
||||
/// beside the ones that answer — and nothing in the response says which is which. So the name
|
||||
/// does, which is a heuristic and is kept to the two families whose ids are a convention rather
|
||||
/// than a brand: offering one is a default that fails on the first turn, and the reader has no
|
||||
/// way to know why. A model deliberately named this way is still reachable by declaring it in
|
||||
/// `models.json`.
|
||||
pub(super) fn answers_nothing(id: &str) -> bool {
|
||||
let id = id.to_ascii_lowercase();
|
||||
["embed", "rerank"].iter().any(|kind| id.contains(kind))
|
||||
}
|
||||
|
||||
/// A default model worth pointing the user at.
|
||||
///
|
||||
/// Prefers the provider's own preference order (the catalog default, when the account can actually
|
||||
/// reach it), then the first model the endpoint reported. Never invents an id the account can't use.
|
||||
pub(super) fn pick_model(descriptor: &ProviderDescriptor, available: &[String]) -> Option<String> {
|
||||
if available.is_empty() {
|
||||
return None;
|
||||
}
|
||||
descriptor
|
||||
.preferred_models
|
||||
.iter()
|
||||
.find(|candidate| available.iter().any(|id| id == *candidate))
|
||||
.map(|candidate| (*candidate).to_string())
|
||||
.or_else(|| available.first().cloned())
|
||||
}
|
||||
|
||||
/// "17 models available" — the line that tells a user the login actually worked.
|
||||
pub(super) fn describe_models(models: &[String]) -> Option<String> {
|
||||
if models.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let preview: Vec<&str> = models.iter().take(MODEL_PREVIEW).map(String::as_str).collect();
|
||||
let suffix = if models.len() > preview.len() { ", …" } else { "" };
|
||||
Some(format!(
|
||||
"{} model{} available ({}{suffix})",
|
||||
models.len(),
|
||||
if models.len() == 1 { "" } else { "s" },
|
||||
preview.join(", ")
|
||||
))
|
||||
}
|
||||
|
||||
/// Validate and tidy a user-typed base URL.
|
||||
pub(super) fn normalize_base_url(raw: &str) -> Result<String, AuthError> {
|
||||
let trimmed = raw.trim().trim_end_matches('/');
|
||||
if trimmed.is_empty() {
|
||||
return Err(AuthError::Input("no URL entered".to_string()));
|
||||
}
|
||||
let candidate =
|
||||
if trimmed.contains("://") { trimmed.to_string() } else { format!("https://{trimmed}") };
|
||||
let url = reqwest::Url::parse(&candidate)
|
||||
.map_err(|error| AuthError::Input(format!("'{trimmed}' is not a URL ({error})")))?;
|
||||
if !matches!(url.scheme(), "http" | "https") {
|
||||
return Err(AuthError::Input("the endpoint must be an http(s) URL".to_string()));
|
||||
}
|
||||
if url.host_str().is_none() {
|
||||
return Err(AuthError::Input("the endpoint has no host".to_string()));
|
||||
}
|
||||
Ok(candidate)
|
||||
}
|
||||
|
||||
/// Say so when a key is about to travel over plain http to somewhere that is not this machine.
|
||||
///
|
||||
/// A warning, not a refusal: a lab server on the LAN is a legitimate thing to point at, and the
|
||||
/// user typed the URL. They should know the key crosses the network in the clear.
|
||||
pub(super) fn warn_if_sent_in_the_clear(url: &str, interaction: &dyn AuthInteraction) {
|
||||
let Ok(url) = reqwest::Url::parse(url) else { return };
|
||||
if url.scheme() != "http" || is_loopback(&url) {
|
||||
return;
|
||||
}
|
||||
interaction.notify(AuthEvent::Info {
|
||||
message: format!(
|
||||
"{} is plain http, so your key will cross the network unencrypted.",
|
||||
url.host_str().unwrap_or("that endpoint")
|
||||
),
|
||||
links: Vec::new(),
|
||||
});
|
||||
}
|
||||
|
||||
fn is_loopback(url: &reqwest::Url) -> bool {
|
||||
let Some(host) = url.host_str() else { return false };
|
||||
host.eq_ignore_ascii_case("localhost")
|
||||
|| host
|
||||
.trim_matches(['[', ']'])
|
||||
.parse::<std::net::IpAddr>()
|
||||
.is_ok_and(|ip| ip.is_loopback())
|
||||
}
|
||||
|
||||
/// `work` against `url`, given as long as a provider gets to start answering.
|
||||
///
|
||||
/// Both halves of a verification need it: [`crate::http::send`] stops at the headers, so a server
|
||||
/// that sends those and then goes quiet would hold the login there for good, and it speaks
|
||||
/// [`luxe_core::provider::ProviderError`], which would stack a second "network error" on this
|
||||
/// one's. `what` finishes "timed out after Ns waiting for …".
|
||||
async fn within_start_timeout<T>(
|
||||
url: &str,
|
||||
what: &str,
|
||||
work: impl std::future::Future<Output = Result<T, reqwest::Error>>,
|
||||
) -> Result<T, AuthError> {
|
||||
let unreachable = |why: String| AuthError::Network(format!("could not reach {url}: {why}"));
|
||||
let finished = match crate::http::start_timeout() {
|
||||
None => work.await,
|
||||
Some(limit) => tokio::time::timeout(limit, work).await.map_err(|_| {
|
||||
unreachable(format!("timed out after {}s waiting for {what}", limit.as_secs()))
|
||||
})?,
|
||||
};
|
||||
finished.map_err(|error| unreachable(luxe_core::provider::describe_error_chain(&error)))
|
||||
}
|
||||
176
crates/luxe-providers/src/auth/service/refresh.rs
Normal file
176
crates/luxe-providers/src/auth/service/refresh.rs
Normal file
@@ -0,0 +1,176 @@
|
||||
//! Background refreshers — re-asking local servers and Azure resources what they serve.
|
||||
//!
|
||||
//! Both run while a picker is on screen and leave the recorded list standing when the server does
|
||||
//! not answer in time.
|
||||
|
||||
use super::models::verify_credential;
|
||||
use super::{ProviderAuth, ProviderDescriptor};
|
||||
use crate::auth::catalog::provider_descriptors;
|
||||
use crate::auth::deployments;
|
||||
use crate::auth::source::{AuthSource, KeyStyle, RequestAuth, StoreAuth};
|
||||
use crate::config::ApiKey;
|
||||
|
||||
/// How long a local server gets to answer before its recorded list stands in.
|
||||
///
|
||||
/// Short on purpose: this is a loopback request made while a picker is already on screen, so the
|
||||
/// cost of waiting is a list that updates late, and the cost of not waiting is nothing.
|
||||
const LOCAL_PROBE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(3);
|
||||
|
||||
/// How long an Azure resource gets to list its deployments before the recorded list stands in.
|
||||
///
|
||||
/// Longer than a local server gets, because this one crosses the internet, and still short: it
|
||||
/// runs while a picker is on screen, and the cost of giving up is a list that updates next time.
|
||||
const REMOTE_PROBE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
|
||||
|
||||
impl ProviderAuth {
|
||||
/// Re-ask every connected local server what it is serving, answering whether anything moved.
|
||||
///
|
||||
/// A local list is not a fact about the world, it is a fact about this machine ten seconds
|
||||
/// ago: loading a model in LM Studio changes it, and a picker built from a snapshot taken at
|
||||
/// connect time would keep offering yesterday's. Only keyless providers with a block are
|
||||
/// asked — they are the ones on this machine, so the call is a loopback round trip that costs
|
||||
/// nothing and fails instantly when the server is off, and the recorded list stands in when it
|
||||
/// does.
|
||||
pub async fn refresh_local_models(&self) -> bool {
|
||||
let Ok(config) = crate::config::load_models_config(self.config.path()) else {
|
||||
return false;
|
||||
};
|
||||
let mut changed = false;
|
||||
for descriptor in provider_descriptors().iter().filter(|entry| entry.keyless) {
|
||||
let Some(block) = config.providers.get(&*descriptor.id) else { continue };
|
||||
let Some(base_url) =
|
||||
block.base_url.clone().or_else(|| descriptor.base_url.map(str::to_string))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let anonymous = RequestAuth::none();
|
||||
let probe = verify_credential(descriptor, Some(&base_url), &anonymous);
|
||||
let Ok(Ok(models)) = tokio::time::timeout(LOCAL_PROBE_TIMEOUT, probe).await else {
|
||||
tracing::debug!(provider = %descriptor.id, %base_url, "local server did not answer");
|
||||
continue;
|
||||
};
|
||||
if models.is_empty() || block.served_models.as_deref() == Some(models.as_slice()) {
|
||||
continue;
|
||||
}
|
||||
match self.config.set_served_models(&descriptor.id, &models) {
|
||||
Ok(()) => changed = true,
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, provider = %descriptor.id, "could not record what the local server serves")
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
/// Ask every Azure resource Luxe is set up for what it has deployed, answering whether any
|
||||
/// recorded list moved.
|
||||
///
|
||||
/// Deployments change in a portal Luxe never sees, so the list is re-read rather than trusted
|
||||
/// from login. One request per resource, not per provider: the three Foundry providers share
|
||||
/// one. A resource that does not answer in time, refuses the key, or no longer has the route
|
||||
/// leaves its recorded list standing, and `"discover_deployments": false` on any of its
|
||||
/// providers stops the request entirely.
|
||||
pub async fn refresh_deployments(&self) -> bool {
|
||||
let Ok(config) = crate::config::load_models_config(self.config.path()) else {
|
||||
return false;
|
||||
};
|
||||
let mut asked: Vec<&str> = Vec::new();
|
||||
let mut changed = false;
|
||||
for descriptor in provider_descriptors().iter().filter(|entry| entry.lists_deployments) {
|
||||
if asked.contains(&&*descriptor.id) {
|
||||
continue;
|
||||
}
|
||||
let group = deployments::group(descriptor);
|
||||
asked.extend(group.iter().map(|member| &*member.id));
|
||||
let reachable =
|
||||
|id: &str| config.providers.get(id).filter(|block| block.base_url.is_some());
|
||||
if group.iter().any(|member| {
|
||||
config.providers.get(&*member.id).and_then(|block| block.discover_deployments)
|
||||
== Some(false)
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
let Some((lead, block)) =
|
||||
group.iter().find_map(|member| reachable(&member.id).map(|block| (member, block)))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let Some(url) = block.base_url.as_deref().and_then(deployments::listing_url) else {
|
||||
continue;
|
||||
};
|
||||
let source = StoreAuth::new(
|
||||
self.store.clone(),
|
||||
lead.id.to_string(),
|
||||
crate::config::resolve_api(&lead.id, Some(block)),
|
||||
)
|
||||
.with_key_style(Some(crate::config::resolve_key_style(&lead.id, Some(block))))
|
||||
.with_fallback(block.api_key.clone());
|
||||
let Ok(auth) = source.request_auth().await else { continue };
|
||||
let found =
|
||||
match tokio::time::timeout(REMOTE_PROBE_TIMEOUT, deployments::list(&url, &auth))
|
||||
.await
|
||||
{
|
||||
Ok(Ok(found)) if !found.is_empty() => found,
|
||||
Ok(Ok(_)) => continue,
|
||||
Ok(Err(error)) => {
|
||||
tracing::debug!(provider = %lead.id, %error, "deployments not listed");
|
||||
continue;
|
||||
}
|
||||
Err(_) => {
|
||||
tracing::debug!(provider = %lead.id, "deployment listing timed out");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
for (id, served) in deployments::partition(&group, &found) {
|
||||
let Some(block) = reachable(id) else { continue };
|
||||
let recorded = deployments::Served {
|
||||
models: block.served_models.clone().unwrap_or_default(),
|
||||
deployments: block.deployments.clone(),
|
||||
};
|
||||
if block.served_models.is_some() && recorded == served {
|
||||
continue;
|
||||
}
|
||||
match self.config.set_deployments(id, &served) {
|
||||
Ok(()) => {
|
||||
tracing::info!(
|
||||
provider = id,
|
||||
count = served.models.len(),
|
||||
"recorded deployments"
|
||||
);
|
||||
changed = true;
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, provider = id, "could not record deployments")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
}
|
||||
|
||||
/// What a resource has deployed, split between the providers that reach it, using a key that has
|
||||
/// just been typed.
|
||||
///
|
||||
/// Sent as `api-key`, which every Azure data plane accepts for a key. `None` on any failure,
|
||||
/// including a refusal: a key that cannot list deployments may still run models (a role scoped to
|
||||
/// inference), so the ordinary verification gets the final say on whether the key is good.
|
||||
pub(super) async fn discover_with_key<'a>(
|
||||
descriptor: &'a ProviderDescriptor,
|
||||
resource_root: &str,
|
||||
key: &ApiKey,
|
||||
) -> Option<Vec<(&'a str, deployments::Served)>> {
|
||||
let url = deployments::listing_url(&descriptor.endpoint_for(resource_root))?;
|
||||
let auth = KeyStyle::AzureKey.headers(key);
|
||||
match tokio::time::timeout(REMOTE_PROBE_TIMEOUT, deployments::list(&url, &auth)).await {
|
||||
Ok(Ok(found)) if !found.is_empty() => {
|
||||
Some(deployments::partition(&deployments::group(descriptor), &found))
|
||||
}
|
||||
Ok(Ok(_)) => None,
|
||||
Ok(Err(error)) => {
|
||||
tracing::debug!(provider = %descriptor.id, %error, "deployments not listed at login");
|
||||
None
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
166
crates/luxe-providers/src/auth/service/status.rs
Normal file
166
crates/luxe-providers/src/auth/service/status.rs
Normal file
@@ -0,0 +1,166 @@
|
||||
//! Status — how each provider's credential resolves, and the wizard rows built from it.
|
||||
//!
|
||||
//! A listing asks the same question of every catalog entry, so the files that answer it are read
|
||||
//! once into a [`Declared`] and handed to each row instead of re-parsed per provider.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use luxe_core::auth::{AuthMethod, CredentialStatus, ProviderOption};
|
||||
|
||||
use super::{ProviderAuth, ProviderDescriptor};
|
||||
use crate::auth::config_file::declared_in;
|
||||
use crate::auth::credential::Credential;
|
||||
use crate::config::{models_config_from, ModelsConfig};
|
||||
|
||||
/// What the credential store and `models.json` say right now.
|
||||
///
|
||||
/// Both parse as [`ModelsConfig`], which is how the legacy nested `auth.json` shape
|
||||
/// (`providers.<id>.api_key`) keeps working.
|
||||
pub(super) struct Declared {
|
||||
credentials: BTreeMap<String, Credential>,
|
||||
store: Option<ModelsConfig>,
|
||||
config: Option<ModelsConfig>,
|
||||
config_providers: Vec<String>,
|
||||
}
|
||||
|
||||
impl ProviderAuth {
|
||||
/// Read the credential store and `models.json`, once.
|
||||
pub(super) fn declared(&self) -> Declared {
|
||||
let stored = self.store.read();
|
||||
let configured = self.config.read();
|
||||
Declared {
|
||||
credentials: stored
|
||||
.as_ref()
|
||||
.map(|root| self.store.credentials_in(root))
|
||||
.unwrap_or_default(),
|
||||
config_providers: configured.as_ref().map(declared_in).unwrap_or_default(),
|
||||
store: stored.and_then(typed),
|
||||
config: configured.and_then(typed),
|
||||
}
|
||||
}
|
||||
|
||||
/// Every provider the wizard lists: the catalog's, then whatever the user configured that it
|
||||
/// has never heard of.
|
||||
///
|
||||
/// A custom endpoint, a proxy or a hand-written provider block must show up, or a working
|
||||
/// setup would look unconfigured — including to the first-run check, which would then reopen
|
||||
/// the wizard on every launch.
|
||||
pub(super) fn options(&self) -> Vec<ProviderOption> {
|
||||
let declared = self.declared();
|
||||
let mut options: Vec<ProviderOption> = crate::auth::provider_descriptors()
|
||||
.iter()
|
||||
.map(|descriptor| self.option(&declared, descriptor))
|
||||
.collect();
|
||||
let others: BTreeSet<&String> =
|
||||
declared.credentials.keys().chain(&declared.config_providers).collect();
|
||||
for id in others {
|
||||
if options.iter().any(|option| &option.id == id) {
|
||||
continue;
|
||||
}
|
||||
let status = self.status_for(&declared, id, &[]);
|
||||
if !status.is_configured() {
|
||||
continue;
|
||||
}
|
||||
options.push(ProviderOption {
|
||||
name: id.clone(),
|
||||
id: id.clone(),
|
||||
methods: vec![AuthMethod::api_key(None)],
|
||||
status,
|
||||
note: None,
|
||||
console_url: None,
|
||||
keyless: false,
|
||||
// It was configured by hand, so its endpoint is already settled.
|
||||
needs_endpoint: false,
|
||||
});
|
||||
}
|
||||
options
|
||||
}
|
||||
|
||||
/// How a provider's credential currently resolves.
|
||||
///
|
||||
/// Same precedence the runtime applies: a stored credential, then an ambient environment
|
||||
/// variable, then a key written by hand into a provider block. The last one matters more than it
|
||||
/// looks — someone whose `models.json` already works must not be shown a first-run wizard.
|
||||
///
|
||||
/// A keyless provider has none of those and is still configured once it has been connected:
|
||||
/// its block in `models.json` is the record, which is why [`Self::login_local`] writes one
|
||||
/// even for the default endpoint.
|
||||
fn status(&self, declared: &Declared, descriptor: &ProviderDescriptor) -> CredentialStatus {
|
||||
match self.status_for(declared, &descriptor.id, descriptor.env) {
|
||||
CredentialStatus::Missing if descriptor.keyless => {
|
||||
match declared_endpoint(declared, descriptor) {
|
||||
Some(endpoint) => CredentialStatus::Local(endpoint),
|
||||
None => CredentialStatus::Missing,
|
||||
}
|
||||
}
|
||||
resolved => resolved,
|
||||
}
|
||||
}
|
||||
|
||||
/// The credential half of [`Self::status`], for a provider the catalog has never heard of.
|
||||
fn status_for(&self, declared: &Declared, id: &str, env: &[&str]) -> CredentialStatus {
|
||||
if let Some(credential) = declared.credentials.get(id) {
|
||||
return CredentialStatus::Stored(credential.info());
|
||||
}
|
||||
for name in env {
|
||||
if std::env::var(name).is_ok_and(|value| !value.trim().is_empty()) {
|
||||
return CredentialStatus::Env((*name).to_string());
|
||||
}
|
||||
}
|
||||
match self.declared_key(declared, id) {
|
||||
Some(file) => CredentialStatus::Config(file),
|
||||
None => CredentialStatus::Missing,
|
||||
}
|
||||
}
|
||||
|
||||
/// The config file declaring an API key for `id`, if one does.
|
||||
fn declared_key(&self, declared: &Declared, id: &str) -> Option<String> {
|
||||
let files = [
|
||||
(self.store.path(), declared.store.as_ref()),
|
||||
(self.config.path(), declared.config.as_ref()),
|
||||
];
|
||||
files.into_iter().find_map(|(path, config)| {
|
||||
let provider = config?.providers.get(id)?;
|
||||
let has_key = provider.api_key.as_ref().is_some_and(|key| !key.is_empty());
|
||||
has_key.then(|| {
|
||||
path.file_name()
|
||||
.map(|name| name.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| path.display().to_string())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// The wizard row for one provider.
|
||||
fn option(&self, declared: &Declared, descriptor: &ProviderDescriptor) -> ProviderOption {
|
||||
ProviderOption {
|
||||
id: descriptor.id.to_string(),
|
||||
name: descriptor.name.to_string(),
|
||||
methods: descriptor.methods(),
|
||||
status: self.status(declared, descriptor),
|
||||
note: descriptor.note.map(str::to_string),
|
||||
console_url: descriptor.console_url.map(str::to_string),
|
||||
keyless: descriptor.keyless,
|
||||
needs_endpoint: descriptor.needs_base_url,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The endpoint a connected local server was recorded at, if it was.
|
||||
///
|
||||
/// The block having been written is the fact; the URL in it is only what to show. A block that
|
||||
/// omits one is still a connection, so it falls back to the descriptor's default rather than
|
||||
/// reporting the provider as unconfigured.
|
||||
fn declared_endpoint(declared: &Declared, descriptor: &ProviderDescriptor) -> Option<String> {
|
||||
let block = declared.config.as_ref()?.providers.get(&*descriptor.id)?;
|
||||
Some(
|
||||
block
|
||||
.base_url
|
||||
.clone()
|
||||
.or_else(|| descriptor.base_url.map(str::to_string))
|
||||
.unwrap_or_else(|| "this machine".to_string()),
|
||||
)
|
||||
}
|
||||
|
||||
fn typed(root: serde_json::Map<String, serde_json::Value>) -> Option<ModelsConfig> {
|
||||
models_config_from(serde_json::Value::Object(root)).ok()
|
||||
}
|
||||
781
crates/luxe-providers/src/auth/service/tests.rs
Normal file
781
crates/luxe-providers/src/auth/service/tests.rs
Normal file
@@ -0,0 +1,781 @@
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use super::models::parse_model_ids;
|
||||
use super::*;
|
||||
use local_server::LocalServer;
|
||||
use luxe_core::auth::{AuthPromptKind, CredentialStatus};
|
||||
use secrecy::SecretString;
|
||||
use std::sync::{Arc, Mutex};
|
||||
mod cleartext;
|
||||
mod key_commands;
|
||||
mod local_server;
|
||||
mod reads;
|
||||
mod verify;
|
||||
|
||||
/// A scripted interaction: answers in order, events recorded.
|
||||
struct Scripted {
|
||||
answers: Mutex<Vec<String>>,
|
||||
events: Mutex<Vec<AuthEvent>>,
|
||||
prompts: Mutex<Vec<AuthPrompt>>,
|
||||
}
|
||||
|
||||
impl Scripted {
|
||||
fn new(answers: &[&str]) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
answers: Mutex::new(answers.iter().rev().map(|a| a.to_string()).collect()),
|
||||
events: Mutex::new(Vec::new()),
|
||||
prompts: Mutex::new(Vec::new()),
|
||||
})
|
||||
}
|
||||
|
||||
fn events(&self) -> Vec<AuthEvent> {
|
||||
self.events.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
fn prompts(&self) -> Vec<AuthPrompt> {
|
||||
self.prompts.lock().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl AuthInteraction for Scripted {
|
||||
async fn prompt(&self, prompt: AuthPrompt) -> Result<SecretString, AuthError> {
|
||||
self.prompts.lock().unwrap().push(prompt);
|
||||
match self.answers.lock().unwrap().pop() {
|
||||
Some(answer) => Ok(SecretString::from(answer)),
|
||||
None => Err(AuthError::Cancelled),
|
||||
}
|
||||
}
|
||||
|
||||
fn notify(&self, event: AuthEvent) {
|
||||
self.events.lock().unwrap().push(event);
|
||||
}
|
||||
}
|
||||
|
||||
fn service() -> (tempfile::TempDir, ProviderAuth) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let service = ProviderAuth::with_paths(
|
||||
AuthStore::new(dir.path().join("auth.json")),
|
||||
ConfigFile::new(dir.path().join("models.json")),
|
||||
)
|
||||
.without_verification();
|
||||
(dir, service)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_api_key_login_stores_the_key_and_sets_the_default_provider() {
|
||||
let (dir, service) = service();
|
||||
let interaction = Scripted::new(&["sk-ant-live"]);
|
||||
let outcome =
|
||||
service.login("anthropic", "api-key", interaction.clone()).await.expect("stores the key");
|
||||
|
||||
assert_eq!(outcome.provider_id, "anthropic");
|
||||
assert_eq!(outcome.kind, Some(CredentialKind::ApiKey));
|
||||
match service.store().credential("anthropic").expect("stored") {
|
||||
Credential::ApiKey(api) => assert_eq!(api.key.expose(), "sk-ant-live"),
|
||||
Credential::OAuth(_) => panic!("wrong kind"),
|
||||
}
|
||||
let config = std::fs::read_to_string(dir.path().join("models.json")).expect("config written");
|
||||
assert!(config.contains("\"default_provider\": \"anthropic\""), "got {config}");
|
||||
// No provider block: the catalog already knows Anthropic's endpoint.
|
||||
assert!(!config.contains("\"providers\""), "got {config}");
|
||||
|
||||
// The key prompt must be masked, and the ToS note has to reach the user.
|
||||
assert!(matches!(interaction.prompts()[0].kind, AuthPromptKind::Secret));
|
||||
assert!(interaction.events().iter().any(|event| matches!(
|
||||
event,
|
||||
AuthEvent::Info { message, .. } if message.contains("terms")
|
||||
)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_empty_or_unresolvable_key_is_refused_before_anything_is_stored() {
|
||||
let (_dir, service) = service();
|
||||
let error =
|
||||
service.login("openai", "api-key", Scripted::new(&[" "])).await.expect_err("must refuse");
|
||||
assert!(matches!(error, AuthError::Input(_)), "got {error:?}");
|
||||
assert!(service.store().credential("openai").is_none(), "nothing may be stored");
|
||||
|
||||
// A command spec that produces nothing fails at login, not silently at the first turn
|
||||
let error = service
|
||||
.login("openai", "api-key", Scripted::new(&["!printf ''"]))
|
||||
.await
|
||||
.expect_err("must refuse");
|
||||
assert!(matches!(error, AuthError::Input(_)), "got {error:?}");
|
||||
}
|
||||
|
||||
/// `echo` rather than `printf`, because login validates the spec by *running* it and the shell
|
||||
/// that runs it is whatever the platform has — `cmd` on a Windows box without Git for Windows.
|
||||
#[tokio::test]
|
||||
async fn a_key_spec_is_stored_verbatim_so_the_secret_stays_where_the_user_put_it() {
|
||||
let (_dir, service) = service();
|
||||
service
|
||||
.login("openai", "api-key", Scripted::new(&["!echo sk-from-1password"]))
|
||||
.await
|
||||
.expect("accepts a command spec");
|
||||
match service.store().credential("openai").expect("stored") {
|
||||
Credential::ApiKey(api) => assert_eq!(api.key.expose(), "!echo sk-from-1password"),
|
||||
Credential::OAuth(_) => panic!("wrong kind"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_unexpected_key_shape_warns_but_still_stores() {
|
||||
let (_dir, service) = service();
|
||||
let interaction = Scripted::new(&["not-an-anthropic-key"]);
|
||||
service.login("anthropic", "api-key", interaction.clone()).await.expect("stores anyway");
|
||||
assert!(
|
||||
interaction.events().iter().any(|event| matches!(
|
||||
event,
|
||||
AuthEvent::Info { message, .. } if message.contains("usually start with")
|
||||
)),
|
||||
"expected a shape warning: {:?}",
|
||||
interaction.events()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancelling_a_prompt_cancels_the_login() {
|
||||
let (_dir, service) = service();
|
||||
let error =
|
||||
service.login("openai", "api-key", Scripted::new(&[])).await.expect_err("cancelled");
|
||||
assert!(error.is_cancelled(), "got {error:?}");
|
||||
}
|
||||
|
||||
/// Two models, as a `/models` body.
|
||||
fn two_models() -> String {
|
||||
r#"{"data":[{"id":"qwen3.8-27b"},{"id":"gemma-4-12b"}]}"#.to_string()
|
||||
}
|
||||
|
||||
/// Connecting a local server has to leave something behind. It stores no credential, so the
|
||||
/// provider block is the whole record — and the next launch reads it to decide whether this
|
||||
/// machine can run a turn at all.
|
||||
#[tokio::test]
|
||||
async fn connecting_a_local_server_records_where_it_is() {
|
||||
let server = LocalServer::start(&two_models());
|
||||
|
||||
let (dir, service) = service();
|
||||
let outcome = service
|
||||
.login("lmstudio", "local", Scripted::new(&[&server.endpoint]))
|
||||
.await
|
||||
.expect("the server answered");
|
||||
|
||||
// Whatever the server reported, in the order the list settles into.
|
||||
assert_eq!(outcome.suggested_model.as_deref(), Some("gemma-4-12b"));
|
||||
assert!(service.store().credential("lmstudio").is_none(), "nothing to store, so nothing is");
|
||||
|
||||
let config: serde_json::Value = serde_json::from_str(
|
||||
&std::fs::read_to_string(dir.path().join("models.json")).expect("config"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(config["providers"]["lmstudio"]["base_url"], server.endpoint);
|
||||
assert_eq!(config["default_provider"], "lmstudio");
|
||||
// And what it is serving, which no catalog can answer for a machine it has never seen.
|
||||
assert_eq!(
|
||||
config["providers"]["lmstudio"]["served_models"],
|
||||
serde_json::json!(["gemma-4-12b", "qwen3.8-27b"])
|
||||
);
|
||||
// Which is what makes the machine configured from here on.
|
||||
assert!(matches!(
|
||||
service.provider("lmstudio").expect("in catalog").status,
|
||||
CredentialStatus::Local(_)
|
||||
));
|
||||
assert!(service.any_configured());
|
||||
}
|
||||
|
||||
/// A local list is a fact about this machine a moment ago, so it is re-asked rather than
|
||||
/// remembered: loading a model changes what the picker should offer, and only the endpoint
|
||||
/// knows. A server that is switched off leaves the recorded list standing rather than
|
||||
/// emptying the picker.
|
||||
#[tokio::test]
|
||||
async fn a_local_servers_list_is_re_asked_and_survives_the_server_going_away() {
|
||||
let loaded = r#"{"data":[{"id":"qwen3.8-27b"},{"id":"gemma-4-12b"},{"id":"gpt-oss-20b"}]}"#;
|
||||
let mut server = LocalServer::start(&two_models());
|
||||
let (dir, service) = service();
|
||||
let models = dir.path().join("models.json");
|
||||
std::fs::write(
|
||||
&models,
|
||||
format!(r#"{{"providers": {{"lmstudio": {{"base_url": "{}"}}}}}}"#, server.endpoint),
|
||||
)
|
||||
.expect("the config");
|
||||
|
||||
let served = |path: &std::path::Path| -> Vec<String> {
|
||||
crate::config::load_models_config(path)
|
||||
.expect("loads")
|
||||
.providers
|
||||
.get("lmstudio")
|
||||
.and_then(|block| block.served_models.clone())
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
assert!(service.refresh_local_models().await, "the first answer is news");
|
||||
assert_eq!(served(&models), vec!["gemma-4-12b".to_string(), "qwen3.8-27b".to_string()]);
|
||||
|
||||
// A model was loaded since, so the recorded list moves with it.
|
||||
server.serves(loaded);
|
||||
assert!(service.refresh_local_models().await, "a different answer is news too");
|
||||
assert_eq!(
|
||||
served(&models),
|
||||
vec!["gemma-4-12b".to_string(), "gpt-oss-20b".to_string(), "qwen3.8-27b".to_string()]
|
||||
);
|
||||
|
||||
// A server that is up with nothing loaded is not a server with no models: LM Studio
|
||||
// loads on demand, so an empty answer must not erase what this machine is known to have.
|
||||
server.serves(r#"{"data":[]}"#);
|
||||
assert!(!service.refresh_local_models().await, "an empty answer is not news");
|
||||
assert_eq!(served(&models).len(), 3, "and it does not wipe the list");
|
||||
|
||||
// The server has gone. Nothing changes, and nothing is lost.
|
||||
server.stop();
|
||||
assert!(!service.refresh_local_models().await, "an unreachable server is not news");
|
||||
assert_eq!(served(&models).len(), 3, "the recorded list stands in");
|
||||
}
|
||||
|
||||
/// A probe races `LOCAL_PROBE_TIMEOUT`, so a client that gives up leaves an accepted
|
||||
/// connection that never became a request. What the server answers must not move because of
|
||||
/// one: this failed on a loaded Windows runner and nowhere else, because the mock used to hand
|
||||
/// out one scripted reply per accept and a dead connection ate the next test's answer.
|
||||
#[tokio::test]
|
||||
async fn a_connection_that_never_asks_does_not_shift_the_answer() {
|
||||
let server = LocalServer::start(&two_models());
|
||||
let (dir, service) = service();
|
||||
let models = dir.path().join("models.json");
|
||||
std::fs::write(
|
||||
&models,
|
||||
format!(r#"{{"providers": {{"lmstudio": {{"base_url": "{}"}}}}}}"#, server.endpoint),
|
||||
)
|
||||
.expect("the config");
|
||||
|
||||
let address = server.endpoint.trim_start_matches("http://").trim_end_matches("/v1").to_string();
|
||||
for _ in 0..3 {
|
||||
drop(std::net::TcpStream::connect(&address).expect("the mock is listening"));
|
||||
}
|
||||
|
||||
assert!(service.refresh_local_models().await, "the real request still gets the answer");
|
||||
let served = crate::config::load_models_config(&models)
|
||||
.expect("loads")
|
||||
.providers
|
||||
.get("lmstudio")
|
||||
.and_then(|block| block.served_models.clone())
|
||||
.unwrap_or_default();
|
||||
assert_eq!(served, vec!["gemma-4-12b".to_string(), "qwen3.8-27b".to_string()]);
|
||||
}
|
||||
|
||||
/// A Foundry resource's deployment listing, as the resource spells it: the models it runs are
|
||||
/// not the names it runs them under, and some of them cannot hold a conversation.
|
||||
const FOUNDRY_LISTING: &str = r#"{"data": [
|
||||
{"id": "gpt-5.6-sol", "model": "gpt-5.6-sol", "status": "succeeded", "object": "deployment"},
|
||||
{"id": "gpt-realtime", "model": "gpt-realtime", "status": "succeeded", "object": "deployment"},
|
||||
{"id": "claude-opus-5", "model": "claude-opus-5", "status": "succeeded", "object": "deployment"},
|
||||
{"id": "Kimi-K2-Thinking", "model": "Kimi-K2.5", "status": "succeeded", "object": "deployment"}
|
||||
], "object": "list"}"#;
|
||||
|
||||
/// What a provider's block records after discovery.
|
||||
fn recorded(path: &std::path::Path, provider: &str) -> (Vec<String>, Vec<(String, String)>) {
|
||||
let config = crate::config::load_models_config(path).expect("loads");
|
||||
let block = config.providers.get(provider).cloned().unwrap_or_default();
|
||||
(block.served_models.unwrap_or_default(), block.deployments.into_iter().collect())
|
||||
}
|
||||
|
||||
/// The picker offers what the resource has deployed rather than what Azure sells, which was
|
||||
/// the defect: `claude-opus-5-5` offered beside a resource that only has `claude-opus-5`.
|
||||
#[tokio::test]
|
||||
async fn a_foundry_resource_is_asked_what_it_has_deployed() {
|
||||
let mut server = LocalServer::start(FOUNDRY_LISTING);
|
||||
let root = server.endpoint.trim_end_matches("/v1").to_string();
|
||||
let (dir, service) = service();
|
||||
let models = dir.path().join("models.json");
|
||||
std::fs::write(
|
||||
&models,
|
||||
serde_json::json!({ "providers": {
|
||||
"foundry-openai": { "base_url": format!("{root}/openai/v1"), "api_key": "fk" },
|
||||
"foundry-anthropic": { "base_url": format!("{root}/anthropic/v1") },
|
||||
"foundry-completions": { "base_url": format!("{root}/openai/v1") },
|
||||
}})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("the config");
|
||||
|
||||
assert!(service.refresh_deployments().await, "the first answer is news");
|
||||
assert_eq!(recorded(&models, "foundry-openai").0, ["gpt-5.6-sol"], "no realtime model");
|
||||
assert_eq!(recorded(&models, "foundry-anthropic").0, ["claude-opus-5"]);
|
||||
assert_eq!(
|
||||
recorded(&models, "foundry-completions"),
|
||||
(
|
||||
vec!["Kimi-K2-Thinking".to_string()],
|
||||
vec![("Kimi-K2-Thinking".to_string(), "Kimi-K2.5".to_string())]
|
||||
),
|
||||
"routed by the model it runs, which is also what its metadata is looked up under"
|
||||
);
|
||||
assert!(!service.refresh_deployments().await, "the same answer is not news");
|
||||
|
||||
// The route going away, or the resource going quiet, leaves the record standing
|
||||
server.serves(r#"{"error": {"code": "404"}}"#);
|
||||
assert!(!service.refresh_deployments().await);
|
||||
server.stop();
|
||||
assert!(!service.refresh_deployments().await);
|
||||
assert_eq!(recorded(&models, "foundry-anthropic").0, ["claude-opus-5"]);
|
||||
}
|
||||
|
||||
/// `discover_deployments: false` is the switch the listing request answers to, and a list
|
||||
/// written by hand under it is never overwritten.
|
||||
#[tokio::test]
|
||||
async fn a_resource_with_discovery_off_is_never_asked() {
|
||||
let server = LocalServer::start(FOUNDRY_LISTING);
|
||||
let root = server.endpoint.trim_end_matches("/v1").to_string();
|
||||
let (dir, service) = service();
|
||||
let models = dir.path().join("models.json");
|
||||
std::fs::write(
|
||||
&models,
|
||||
serde_json::json!({ "providers": {
|
||||
"foundry-openai": { "base_url": format!("{root}/openai/v1"), "api_key": "fk" },
|
||||
"foundry-anthropic": {
|
||||
"base_url": format!("{root}/anthropic/v1"),
|
||||
"discover_deployments": false,
|
||||
"served_models": ["sonnet-prod"],
|
||||
},
|
||||
}})
|
||||
.to_string(),
|
||||
)
|
||||
.expect("the config");
|
||||
|
||||
assert!(!service.refresh_deployments().await);
|
||||
assert_eq!(recorded(&models, "foundry-anthropic").0, ["sonnet-prod"]);
|
||||
assert!(recorded(&models, "foundry-openai").0.is_empty(), "one switch covers the resource");
|
||||
}
|
||||
|
||||
/// Logging in reads the deployments with the key just typed, records them for every surface
|
||||
/// and suggests one that is actually deployed rather than one Azure merely offers.
|
||||
#[tokio::test]
|
||||
async fn a_foundry_login_records_the_deployments_it_found() {
|
||||
let server = LocalServer::start(FOUNDRY_LISTING);
|
||||
let root = server.endpoint.trim_end_matches("/v1").to_string();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let service = ProviderAuth::with_paths(
|
||||
AuthStore::new(dir.path().join("auth.json")),
|
||||
ConfigFile::new(dir.path().join("models.json")),
|
||||
);
|
||||
let outcome = service
|
||||
.login("foundry-anthropic", "api-key", Scripted::new(&[&root, "fk-live"]))
|
||||
.await
|
||||
.expect("configured");
|
||||
|
||||
assert_eq!(outcome.suggested_model.as_deref(), Some("claude-opus-5"));
|
||||
assert!(
|
||||
outcome.notes.iter().any(|note| note.contains("3 chat deployments")),
|
||||
"{:?}",
|
||||
outcome.notes
|
||||
);
|
||||
let models = dir.path().join("models.json");
|
||||
assert_eq!(recorded(&models, "foundry-anthropic").0, ["claude-opus-5"]);
|
||||
assert_eq!(recorded(&models, "foundry-openai").0, ["gpt-5.6-sol"]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_custom_endpoint_asks_what_it_is_and_records_a_provider_block() {
|
||||
let (dir, service) = service();
|
||||
let interaction =
|
||||
Scripted::new(&["my-proxy", "https://proxy.internal/v1", "openai-responses", "sk-x"]);
|
||||
let outcome = service.login("custom", "api-key", interaction).await.expect("configured");
|
||||
|
||||
assert_eq!(outcome.provider_id, "my-proxy");
|
||||
assert!(service.store().credential("my-proxy").is_some());
|
||||
let config: serde_json::Value = serde_json::from_str(
|
||||
&std::fs::read_to_string(dir.path().join("models.json")).expect("config"),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(config["providers"]["my-proxy"]["base_url"], "https://proxy.internal/v1");
|
||||
assert_eq!(config["providers"]["my-proxy"]["compat"]["use_responses_api"], true);
|
||||
assert_eq!(config["default_provider"], "my-proxy");
|
||||
}
|
||||
|
||||
/// One resource, one key, three wire APIs. Whichever surface is named, all three end up
|
||||
/// reachable — each with its own path, its own API and the shared metadata catalog.
|
||||
#[tokio::test]
|
||||
async fn a_foundry_login_configures_every_surface_from_one_resource() {
|
||||
const SURFACES: [(&str, &str, &str); 3] = [
|
||||
("foundry-openai", "openai-responses", "/openai/v1"),
|
||||
("foundry-anthropic", "anthropic-messages", "/anthropic/v1"),
|
||||
("foundry-completions", "openai-completions", "/openai/v1"),
|
||||
];
|
||||
|
||||
for (named, _, _) in SURFACES {
|
||||
let (dir, service) = service();
|
||||
let outcome = service
|
||||
.login(named, "api-key", Scripted::new(&["acme-foundry", "fk-live"]))
|
||||
.await
|
||||
.expect("configures the resource");
|
||||
assert_eq!(outcome.provider_id, named);
|
||||
|
||||
let config =
|
||||
crate::config::load_models_config(&dir.path().join("models.json")).expect("loads");
|
||||
for (id, api, path) in SURFACES {
|
||||
match service.store().credential(id).expect("every surface holds the key") {
|
||||
Credential::ApiKey(key) => assert_eq!(key.key.expose(), "fk-live"),
|
||||
Credential::OAuth(_) => panic!("wrong kind"),
|
||||
}
|
||||
let block = config.providers.get(id).expect("block");
|
||||
assert_eq!(
|
||||
block.base_url.as_deref(),
|
||||
Some(format!("https://acme-foundry.services.ai.azure.com{path}").as_str()),
|
||||
"{id}"
|
||||
);
|
||||
assert_eq!(block.api.as_deref(), Some(api), "{id}");
|
||||
assert_eq!(block.models_dev.as_deref(), Some("azure"), "{id}");
|
||||
}
|
||||
|
||||
// Only the surface the user asked for becomes the default; the siblings are reported,
|
||||
// not silently substituted.
|
||||
let raw = std::fs::read_to_string(dir.path().join("models.json")).expect("config");
|
||||
let root: serde_json::Value = serde_json::from_str(&raw).unwrap();
|
||||
assert_eq!(root["default_provider"], named);
|
||||
assert_eq!(
|
||||
outcome.notes.iter().filter(|note| note.contains("same key")).count(),
|
||||
2,
|
||||
"both siblings have to be visible in the outcome: {:?}",
|
||||
outcome.notes
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Pasting the target URI off a deployment's Details tab is what people actually do, and the
|
||||
/// half they copied it from must not decide which endpoints get recorded.
|
||||
#[tokio::test]
|
||||
async fn a_pasted_foundry_target_uri_still_yields_both_endpoints() {
|
||||
let (dir, service) = service();
|
||||
service
|
||||
.login(
|
||||
"foundry-openai",
|
||||
"api-key",
|
||||
Scripted::new(&[
|
||||
"https://acme-foundry.services.ai.azure.com/anthropic/v1/messages",
|
||||
"fk-live",
|
||||
]),
|
||||
)
|
||||
.await
|
||||
.expect("configures the resource");
|
||||
|
||||
let config = crate::config::load_models_config(&dir.path().join("models.json")).expect("loads");
|
||||
assert_eq!(
|
||||
config.providers["foundry-openai"].base_url.as_deref(),
|
||||
Some("https://acme-foundry.services.ai.azure.com/openai/v1")
|
||||
);
|
||||
assert_eq!(
|
||||
config.providers["foundry-anthropic"].base_url.as_deref(),
|
||||
Some("https://acme-foundry.services.ai.azure.com/anthropic/v1")
|
||||
);
|
||||
assert_eq!(
|
||||
config.providers["foundry-completions"].base_url.as_deref(),
|
||||
Some("https://acme-foundry.services.ai.azure.com/openai/v1")
|
||||
);
|
||||
}
|
||||
|
||||
/// `/login foundry` has to reach something, and the alias table is the only place that can say
|
||||
/// which half a bare name means.
|
||||
#[test]
|
||||
fn the_foundry_aliases_land_on_a_real_half() {
|
||||
let (_dir, service) = service();
|
||||
for (typed, expected) in [
|
||||
("foundry", "foundry-openai"),
|
||||
("microsoft-foundry", "foundry-openai"),
|
||||
("azure-foundry", "foundry-openai"),
|
||||
("foundry-claude", "foundry-anthropic"),
|
||||
("foundry-partners", "foundry-completions"),
|
||||
] {
|
||||
assert_eq!(
|
||||
service.provider(typed).map(|option| option.id).as_deref(),
|
||||
Some(expected),
|
||||
"typed {typed}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_custom_name_that_collides_with_a_builtin_is_refused() {
|
||||
let (_dir, service) = service();
|
||||
let error = service
|
||||
.login("custom", "api-key", Scripted::new(&["openai"]))
|
||||
.await
|
||||
.expect_err("must refuse");
|
||||
assert!(format!("{error}").contains("already a built-in"), "got {error}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_bad_custom_name_or_url_is_refused() {
|
||||
let (_dir, service) = service();
|
||||
for answers in [vec!["has spaces"], vec!["ok-name", "not a url at all"]] {
|
||||
let error = service
|
||||
.login("custom", "api-key", Scripted::new(&answers))
|
||||
.await
|
||||
.expect_err("must refuse");
|
||||
assert!(matches!(error, AuthError::Input(_)), "got {error:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn unknown_providers_and_methods_are_named_in_the_error() {
|
||||
let (_dir, service) = service();
|
||||
let error =
|
||||
service.login("not-a-provider", "api-key", Scripted::new(&[])).await.expect_err("unknown");
|
||||
assert!(matches!(error, AuthError::UnknownProvider(id) if id == "not-a-provider"));
|
||||
|
||||
// Anthropic has no subscription method, by decision.
|
||||
let error = service.login("anthropic", "oauth", Scripted::new(&[])).await.expect_err("unknown");
|
||||
assert!(matches!(error, AuthError::UnknownMethod { .. }), "got {error:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn status_reports_stored_then_environment_then_missing() {
|
||||
let (_dir, service) = service();
|
||||
let openai = || {
|
||||
service.providers().into_iter().find(|option| option.id == "openai").expect("in catalog")
|
||||
};
|
||||
// The host may legitimately have OPENAI_API_KEY set; only assert on the stored transition.
|
||||
let before = openai().status;
|
||||
service.store().put("openai", &Credential::api_key("sk")).unwrap();
|
||||
assert!(matches!(openai().status, CredentialStatus::Stored(_)));
|
||||
service.logout("openai").unwrap();
|
||||
assert_eq!(openai().status, before, "logging out restores the ambient status");
|
||||
}
|
||||
|
||||
/// The Configuration surface offers three lists, and a row that opens an empty one is a dead
|
||||
/// end a user meets on their first launch. Each has to be non-empty against the real catalog,
|
||||
/// and single-method, so choosing a provider starts its flow rather than asking again.
|
||||
#[test]
|
||||
fn every_login_scope_offers_providers_that_start_on_one_choice() {
|
||||
use luxe_core::auth::LoginScope;
|
||||
|
||||
let (_dir, service) = service();
|
||||
let all = service.providers();
|
||||
for scope in [LoginScope::Subscription, LoginScope::ApiKey, LoginScope::LocalOrCustom] {
|
||||
let scoped = scope.filter(all.clone());
|
||||
assert!(!scoped.is_empty(), "{} offers nothing", scope.as_str());
|
||||
for option in &scoped {
|
||||
assert!(
|
||||
option.default_method().is_some(),
|
||||
"{} / {}: a scoped row must be unambiguous, got {:?}",
|
||||
scope.as_str(),
|
||||
option.id,
|
||||
option.methods
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let named = |scope: LoginScope| -> Vec<String> {
|
||||
scope.filter(all.clone()).into_iter().map(|option| option.id).collect()
|
||||
};
|
||||
assert_eq!(
|
||||
named(LoginScope::Subscription),
|
||||
vec!["openai-codex", "github-copilot", "openrouter"],
|
||||
"the three sanctioned subscription flows, and only those"
|
||||
);
|
||||
let local = named(LoginScope::LocalOrCustom);
|
||||
for id in ["lmstudio", "ollama", "custom"] {
|
||||
assert!(local.iter().any(|listed| listed == id), "{id} belongs in local-or-custom");
|
||||
}
|
||||
assert!(
|
||||
named(LoginScope::ApiKey).iter().any(|id| id == "anthropic"),
|
||||
"a key-only provider belongs in the key list"
|
||||
);
|
||||
}
|
||||
|
||||
/// The regression from the first live run: a custom provider's stored credential was invisible
|
||||
/// to `providers()`, so the first-run wizard reopened on every launch.
|
||||
#[test]
|
||||
fn a_provider_the_catalog_does_not_know_still_shows_as_configured() {
|
||||
let (_dir, service) = service();
|
||||
service.store().put("my-proxy", &Credential::api_key("sk-proxy")).unwrap();
|
||||
|
||||
let listed = service.provider("my-proxy").expect("a stored credential is a provider");
|
||||
assert!(listed.status.is_stored());
|
||||
assert!(service.any_configured(), "a configured machine must not be offered onboarding");
|
||||
assert!(service.stored().iter().any(|option| option.id == "my-proxy"));
|
||||
}
|
||||
|
||||
/// The other half of that: a key written by hand into a provider block counts as configured.
|
||||
#[test]
|
||||
fn a_hand_written_provider_block_counts_as_configured() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let models = dir.path().join("models.json");
|
||||
std::fs::write(
|
||||
&models,
|
||||
r#"{"providers": {"anthropic": {"api_key": "!printf sk-ant-from-a-command"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let service = ProviderAuth::with_paths(
|
||||
AuthStore::new(dir.path().join("auth.json")),
|
||||
ConfigFile::new(models),
|
||||
);
|
||||
|
||||
let anthropic = service.provider("anthropic").expect("in catalog");
|
||||
assert!(
|
||||
matches!(anthropic.status, CredentialStatus::Config(ref file) if file == "models.json"),
|
||||
"got {:?}",
|
||||
anthropic.status
|
||||
);
|
||||
assert!(service.any_configured());
|
||||
assert!(
|
||||
!anthropic.status.is_stored(),
|
||||
"a config key isn't ours to delete, so /logout must not offer it"
|
||||
);
|
||||
}
|
||||
|
||||
/// A local server has no credential to find, so "nothing is stored" and "nothing is set up"
|
||||
/// are the same file contents. Its recorded block is what tells them apart — without it a
|
||||
/// machine running only LM Studio is told forever that no provider is configured, and the
|
||||
/// model picker it is sent to has nothing in it.
|
||||
#[test]
|
||||
fn a_connected_local_server_counts_as_configured() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let models = dir.path().join("models.json");
|
||||
let open = |models: &std::path::Path| {
|
||||
ProviderAuth::with_paths(
|
||||
AuthStore::new(dir.path().join("auth.json")),
|
||||
ConfigFile::new(models.to_path_buf()),
|
||||
)
|
||||
};
|
||||
|
||||
// Nothing written yet: a keyless provider nobody has connected is still unconfigured.
|
||||
std::fs::write(&models, "{}").unwrap();
|
||||
let service = open(&models);
|
||||
assert_eq!(service.provider("lmstudio").expect("in catalog").status, CredentialStatus::Missing);
|
||||
assert!(!service.any_configured(), "a keyless provider is not configured by existing");
|
||||
|
||||
// Connected at a chosen endpoint: the block names where.
|
||||
std::fs::write(
|
||||
&models,
|
||||
r#"{"providers": {"lmstudio": {"base_url": "http://127.0.0.1:4891/v1"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let service = open(&models);
|
||||
let lmstudio = service.provider("lmstudio").expect("in catalog");
|
||||
assert_eq!(lmstudio.status, CredentialStatus::Local("http://127.0.0.1:4891/v1".to_string()));
|
||||
assert!(service.any_configured(), "a machine with a local model can run turns");
|
||||
assert!(!lmstudio.status.is_stored(), "there is nothing in auth.json for /logout to take");
|
||||
|
||||
// Connected at the catalog's own default: the block carries no URL, and the status falls
|
||||
// back to the one the connection used rather than reporting nothing at all.
|
||||
std::fs::write(&models, r#"{"providers": {"lmstudio": {}}}"#).unwrap();
|
||||
assert_eq!(
|
||||
open(&models).provider("lmstudio").expect("in catalog").status,
|
||||
CredentialStatus::Local("http://127.0.0.1:1234/v1".to_string())
|
||||
);
|
||||
|
||||
// And the rule is about keyless providers, not about having a block: a provider that
|
||||
// takes a key is not configured by someone writing down its endpoint.
|
||||
std::fs::write(
|
||||
&models,
|
||||
r#"{"providers": {"openai": {"base_url": "https://api.openai.com/v1"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let service = open(&models);
|
||||
if std::env::var("OPENAI_API_KEY").is_err() {
|
||||
assert_eq!(
|
||||
service.provider("openai").expect("in catalog").status,
|
||||
CredentialStatus::Missing
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The legacy nested `auth.json` shape is the same code path, and predates this phase.
|
||||
#[test]
|
||||
fn the_legacy_nested_auth_file_still_counts_as_configured() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let auth = dir.path().join("auth.json");
|
||||
std::fs::write(&auth, r#"{"providers": {"openai": {"api_key": "sk-legacy"}}}"#).unwrap();
|
||||
let service = ProviderAuth::with_paths(
|
||||
AuthStore::new(auth),
|
||||
ConfigFile::new(dir.path().join("models.json")),
|
||||
);
|
||||
let openai = service.provider("openai").expect("in catalog");
|
||||
assert!(matches!(openai.status, CredentialStatus::Config(_)), "got {:?}", openai.status);
|
||||
}
|
||||
|
||||
/// `/login copilot` has to reach GitHub Copilot; the alias table is in the catalog, so the
|
||||
/// service has to apply it on lookup or every front-end would need its own copy.
|
||||
#[test]
|
||||
fn lookup_accepts_the_aliases_people_type() {
|
||||
let (_dir, service) = service();
|
||||
for (typed, expected) in [
|
||||
("copilot", "github-copilot"),
|
||||
("codex", "openai-codex"),
|
||||
("gemini", "google"),
|
||||
("openai_compat", "openai"),
|
||||
] {
|
||||
assert_eq!(
|
||||
service.provider(typed).map(|option| option.id).as_deref(),
|
||||
Some(expected),
|
||||
"typed {typed}"
|
||||
);
|
||||
}
|
||||
assert!(service.provider("not-a-provider").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout_reports_whether_there_was_anything_to_remove() {
|
||||
let (_dir, service) = service();
|
||||
assert!(!service.logout("openai").unwrap());
|
||||
service.store().put("openai", &Credential::api_key("sk")).unwrap();
|
||||
assert!(service.logout("openai").unwrap());
|
||||
assert!(service.store().credential("openai").is_none());
|
||||
// Aliases resolve, so `/logout copilot` works.
|
||||
service.store().put("github-copilot", &Credential::api_key("t")).unwrap();
|
||||
assert!(service.logout("copilot").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stored_lists_only_providers_with_a_credential_in_the_file() {
|
||||
let (_dir, service) = service();
|
||||
assert!(service.stored().is_empty());
|
||||
service.store().put("openai", &Credential::api_key("sk")).unwrap();
|
||||
let stored: Vec<String> = service.stored().into_iter().map(|option| option.id).collect();
|
||||
assert_eq!(stored, vec!["openai".to_string()]);
|
||||
assert!(service.any_configured());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_lists_are_parsed_from_both_shapes_and_summarized() {
|
||||
let ids = parse_model_ids(
|
||||
r#"{"data":[{"id":"b"},{"id":"a"},{"id":"a"},{"no_id":1}],"object":"list"}"#,
|
||||
);
|
||||
assert_eq!(ids, vec!["a".to_string(), "b".to_string()], "sorted and deduped");
|
||||
assert!(parse_model_ids("not json").is_empty());
|
||||
assert!(parse_model_ids(r#"{"models":[]}"#).is_empty());
|
||||
|
||||
// A flat `/v1/models` list mixes in the models that answer nothing. Offering one as the
|
||||
// default — which is what a local server's first entry becomes — fails on the first turn
|
||||
// with no way for the reader to know why.
|
||||
let mixed = parse_model_ids(
|
||||
r#"{"data":[{"id":"qwen3.8-27b"},{"id":"text-embedding-nomic-embed-text-v1.5"},
|
||||
{"id":"bge-reranker-v2-m3"},{"id":"text-embedding-3-large"}]}"#,
|
||||
);
|
||||
assert_eq!(mixed, vec!["qwen3.8-27b".to_string()]);
|
||||
|
||||
assert_eq!(describe_models(&[]), None);
|
||||
let one = describe_models(&["only".to_string()]).unwrap();
|
||||
assert_eq!(one, "1 model available (only)");
|
||||
let many: Vec<String> = (0..10).map(|index| format!("m{index}")).collect();
|
||||
let summary = describe_models(&many).unwrap();
|
||||
assert!(summary.starts_with("10 models available (m0, m1, m2, …"), "got {summary}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_suggested_model_is_one_the_account_can_actually_use() {
|
||||
let openai = provider_descriptor("openai").unwrap();
|
||||
// Preference order wins when it's available.
|
||||
let available = vec!["gpt-4o".to_string(), "gpt-5.4".to_string(), "gpt-5".to_string()];
|
||||
assert_eq!(pick_model(openai, &available).as_deref(), Some("gpt-5.4"));
|
||||
// Otherwise fall back to what the endpoint offered rather than inventing an id.
|
||||
assert_eq!(pick_model(openai, &["local-thing".to_string()]).as_deref(), Some("local-thing"));
|
||||
assert_eq!(pick_model(openai, &[]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn base_urls_are_normalized_and_nonsense_is_rejected() {
|
||||
assert_eq!(normalize_base_url(" https://x.dev/v1/ ").unwrap(), "https://x.dev/v1");
|
||||
assert_eq!(normalize_base_url("x.dev/v1").unwrap(), "https://x.dev/v1");
|
||||
assert_eq!(normalize_base_url("http://127.0.0.1:1234/v1").unwrap(), "http://127.0.0.1:1234/v1");
|
||||
assert!(normalize_base_url("").is_err());
|
||||
assert!(normalize_base_url("file:///etc/passwd").is_err());
|
||||
assert!(normalize_base_url("not a url").is_err());
|
||||
}
|
||||
58
crates/luxe-providers/src/auth/service/tests/cleartext.rs
Normal file
58
crates/luxe-providers/src/auth/service/tests/cleartext.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
//! A key typed for a plain-http endpoint is something the user should be told about.
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Every message the login showed, whatever it was.
|
||||
fn said(interaction: &Scripted) -> String {
|
||||
interaction
|
||||
.events()
|
||||
.into_iter()
|
||||
.filter_map(|event| match event {
|
||||
AuthEvent::Info { message, .. } => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
async fn custom_login_at(url: &str) -> String {
|
||||
let (_dir, service) = service();
|
||||
let interaction = Scripted::new(&["my-proxy", url, "openai-completions", "sk-x"]);
|
||||
service.login("custom", "api-key", interaction.clone()).await.expect("configured");
|
||||
said(&interaction)
|
||||
}
|
||||
|
||||
/// The key goes to whatever URL was typed, and `http://` to another machine sends it readable
|
||||
/// to everyone on the path. Not refused, since a lab server on a LAN is a fair thing to point at.
|
||||
#[tokio::test]
|
||||
async fn a_key_for_a_plain_http_endpoint_elsewhere_comes_with_a_warning() {
|
||||
let said = custom_login_at("http://192.168.1.20:8080/v1").await;
|
||||
assert!(said.contains("192.168.1.20") && said.contains("unencrypted"), "got: {said}");
|
||||
}
|
||||
|
||||
/// This machine, https and the bare host (which becomes https) have nothing to warn about.
|
||||
#[tokio::test]
|
||||
async fn a_key_that_stays_on_this_machine_or_travels_over_tls_is_not_warned_about() {
|
||||
for url in [
|
||||
"http://127.0.0.1:1234/v1",
|
||||
"http://localhost:1234/v1",
|
||||
"http://[::1]:1234/v1",
|
||||
"https://example.com/v1",
|
||||
"example.com/v1",
|
||||
] {
|
||||
let said = custom_login_at(url).await;
|
||||
assert!(!said.contains("unencrypted"), "{url} was warned about: {said}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The warning is only worth anything while the user can still say no, so it comes before the
|
||||
/// key is asked for. Here nobody answers the key prompt, and the login still said it.
|
||||
#[tokio::test]
|
||||
async fn the_warning_comes_before_the_key_is_asked_for() {
|
||||
let (_dir, service) = service();
|
||||
let interaction =
|
||||
Scripted::new(&["my-proxy", "http://192.168.1.20:8080/v1", "openai-completions"]);
|
||||
let error = service.login("custom", "api-key", interaction.clone()).await.expect_err("no key");
|
||||
assert!(error.is_cancelled(), "got {error:?}");
|
||||
assert!(said(&interaction).contains("unencrypted"), "got: {}", said(&interaction));
|
||||
}
|
||||
16
crates/luxe-providers/src/auth/service/tests/key_commands.rs
Normal file
16
crates/luxe-providers/src/auth/service/tests/key_commands.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
//! Key commands at login; unix only, since the failing command is `sh` syntax.
|
||||
#![cfg(unix)]
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A key command that fails says why, from its own stderr, instead of "resolved to an empty value".
|
||||
#[tokio::test]
|
||||
async fn a_failing_key_command_says_why_at_login() {
|
||||
let (_dir, service) = service();
|
||||
let error = service
|
||||
.login("openai", "api-key", Scripted::new(&["!echo 'vault is locked' >&2; exit 3"]))
|
||||
.await
|
||||
.expect_err("must refuse");
|
||||
assert!(error.to_string().contains("vault is locked"), "got {error}");
|
||||
assert!(service.store().credential("openai").is_none(), "nothing may be stored");
|
||||
}
|
||||
86
crates/luxe-providers/src/auth/service/tests/local_server.rs
Normal file
86
crates/luxe-providers/src/auth/service/tests/local_server.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
//! A local server standing in for LM Studio.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
/// A local server standing in for LM Studio: answers every `/models` request with whatever it
|
||||
/// currently serves, until it is stopped.
|
||||
///
|
||||
/// The reply is keyed on what the server is *holding*, never on how many connections have
|
||||
/// arrived. That distinction is load-bearing. A probe races `LOCAL_PROBE_TIMEOUT`, so a client
|
||||
/// that gives up still leaves an accepted connection behind; a queue popped per accept
|
||||
/// desynchronizes the moment that happens, and every later assertion reads the answer meant
|
||||
/// for the one before it. That is a real failure on a loaded runner and invisible everywhere
|
||||
/// else, which is the worst combination a test can have.
|
||||
pub(super) struct LocalServer {
|
||||
pub(super) endpoint: String,
|
||||
body: Arc<Mutex<String>>,
|
||||
running: Arc<AtomicBool>,
|
||||
thread: Option<std::thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl LocalServer {
|
||||
/// Bind a port and start answering with `body`.
|
||||
pub(super) fn start(body: &str) -> Self {
|
||||
use std::io::{Read, Write};
|
||||
|
||||
let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a port");
|
||||
listener.set_nonblocking(true).expect("a non-blocking listener");
|
||||
let endpoint = format!("http://{}/v1", listener.local_addr().expect("an address"));
|
||||
let body = Arc::new(Mutex::new(body.to_string()));
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
|
||||
let (served, alive) = (Arc::clone(&body), Arc::clone(&running));
|
||||
let thread = std::thread::spawn(move || {
|
||||
while alive.load(Ordering::Relaxed) {
|
||||
let Ok((mut sock, _)) = listener.accept() else {
|
||||
std::thread::sleep(std::time::Duration::from_millis(5));
|
||||
continue;
|
||||
};
|
||||
sock.set_nonblocking(false).expect("a blocking connection");
|
||||
// Read the request before answering: closing a socket with unread bytes on it
|
||||
// sends a reset, and the reply goes with it.
|
||||
let mut seen = Vec::new();
|
||||
let mut chunk = [0u8; 1024];
|
||||
while !seen.windows(4).any(|window| window == b"\r\n\r\n") {
|
||||
match sock.read(&mut chunk) {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(read) => seen.extend_from_slice(&chunk[..read]),
|
||||
}
|
||||
}
|
||||
let body = served.lock().expect("the body").clone();
|
||||
// `Connection: close` keeps each request on its own socket. This loop answers
|
||||
// one request per `accept` and goes back to accepting, so a client entitled to
|
||||
// reuse the connection puts its next request into a socket nobody will read —
|
||||
// and the test that changes what the server serves then never sees the change.
|
||||
// Dropping the socket closes it, but the client only learns that on its own
|
||||
// schedule; on Windows it learned late enough to fail in CI while passing
|
||||
// everywhere else. Every other stub in the workspace says this out loud.
|
||||
let reply = crate::test_server::http_reply("200 OK", "application/json", &body);
|
||||
let _ = sock.write_all(reply.as_bytes());
|
||||
let _ = sock.flush();
|
||||
}
|
||||
});
|
||||
|
||||
Self { endpoint, body, running, thread: Some(thread) }
|
||||
}
|
||||
|
||||
/// What every request from here on answers with.
|
||||
pub(super) fn serves(&self, body: &str) {
|
||||
*self.body.lock().expect("the body") = body.to_string();
|
||||
}
|
||||
|
||||
/// Take the server away, and wait until the port is really gone.
|
||||
pub(super) fn stop(&mut self) {
|
||||
self.running.store(false, Ordering::Relaxed);
|
||||
if let Some(thread) = self.thread.take() {
|
||||
let _ = thread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LocalServer {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
38
crates/luxe-providers/src/auth/service/tests/reads.rs
Normal file
38
crates/luxe-providers/src/auth/service/tests/reads.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
//! Listing providers reads the two config files once, not once per row.
|
||||
#![cfg(target_os = "linux")]
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Bytes this process has asked the kernel to read, from `/proc/self/io`.
|
||||
fn bytes_read() -> Option<u64> {
|
||||
let io = std::fs::read_to_string("/proc/self/io").ok()?;
|
||||
io.lines().find_map(|line| line.strip_prefix("rchar: ")?.trim().parse().ok())
|
||||
}
|
||||
|
||||
/// `providers()` called `status()` per catalog entry, and each of those re-read and parsed
|
||||
/// `auth.json` and `models.json`, so a first-run check on a real config meant dozens of parses.
|
||||
///
|
||||
/// Measured in bytes rather than counted in code, so a read added back anywhere in the listing
|
||||
/// shows up whichever function does it. Both files carry a megabyte of padding to dwarf what the
|
||||
/// rest of the process reads, and each should cost that once: the counter is the whole process's,
|
||||
/// so the bound leaves room for another test's small reads under `cargo test`.
|
||||
#[test]
|
||||
fn listing_the_providers_reads_each_file_once() {
|
||||
let (dir, service) = service();
|
||||
let padding = "x".repeat(1 << 20);
|
||||
let models = serde_json::json!({
|
||||
"providers": { "mine": { "base_url": "http://127.0.0.1:1/v1", "api_key": "k" } },
|
||||
"padding": padding,
|
||||
});
|
||||
std::fs::write(dir.path().join("models.json"), models.to_string()).unwrap();
|
||||
let auth = serde_json::json!({ "padding": padding });
|
||||
std::fs::write(dir.path().join("auth.json"), auth.to_string()).unwrap();
|
||||
|
||||
let before = bytes_read().expect("/proc/self/io, which Linux counts reads in");
|
||||
let options = service.providers();
|
||||
let read = bytes_read().expect("still readable") - before;
|
||||
|
||||
assert!(options.iter().any(|option| option.id == "mine"), "the hand-written block is listed");
|
||||
// Two files, one read each; one per catalog entry would be ~50 of them.
|
||||
assert!(read < 3 << 20, "read {} MiB listing the providers", read >> 20);
|
||||
}
|
||||
86
crates/luxe-providers/src/auth/service/tests/verify.rs
Normal file
86
crates/luxe-providers/src/auth/service/tests/verify.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
//! Verification against an endpoint that stops answering.
|
||||
|
||||
use super::*;
|
||||
use std::time::Duration;
|
||||
|
||||
/// A server that accepts, reads the request and then says `reply` (possibly nothing) before going
|
||||
/// quiet for good. Answers once the request has arrived, so a paused clock can start there.
|
||||
async fn quiet_server(reply: &'static str) -> (String, tokio::sync::oneshot::Receiver<()>) {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("bind");
|
||||
let endpoint = format!("http://{}/v1", listener.local_addr().expect("address"));
|
||||
let (arrived, heard) = tokio::sync::oneshot::channel();
|
||||
tokio::spawn(async move {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
let (mut socket, _) = listener.accept().await.expect("accept");
|
||||
let mut buffer = [0u8; 1024];
|
||||
let _ = socket.read(&mut buffer).await;
|
||||
let _ = socket.write_all(reply.as_bytes()).await;
|
||||
let _ = arrived.send(());
|
||||
std::future::pending::<()>().await;
|
||||
});
|
||||
(endpoint, heard)
|
||||
}
|
||||
|
||||
/// Verify against `endpoint`, then let the clock run until something gives up.
|
||||
///
|
||||
/// The outer two minutes only exists so a verification with no bound of its own fails this test
|
||||
/// instead of hanging it; the bounded one answers long before.
|
||||
async fn verified_against(
|
||||
endpoint: String,
|
||||
heard: tokio::sync::oneshot::Receiver<()>,
|
||||
) -> Option<Result<Vec<String>, AuthError>> {
|
||||
let descriptor = provider_descriptor("openai").expect("in the catalog");
|
||||
let verifying = tokio::spawn(async move {
|
||||
verify_credential(descriptor, Some(&endpoint), &RequestAuth::none()).await
|
||||
});
|
||||
heard.await.expect("the request arrived");
|
||||
tokio::time::pause();
|
||||
tokio::time::timeout(Duration::from_secs(120), verifying).await.ok().map(|done| done.unwrap())
|
||||
}
|
||||
|
||||
/// `luxe auth login` against an endpoint that takes the connection and never answers used to
|
||||
/// wait until Ctrl-C: only the background refreshers had a timeout.
|
||||
#[tokio::test]
|
||||
async fn an_endpoint_that_never_answers_ends_the_verification() {
|
||||
let (endpoint, heard) = quiet_server("").await;
|
||||
let outcome = verified_against(endpoint, heard).await.expect("gave up on its own");
|
||||
let error = outcome.expect_err("nothing came back");
|
||||
assert!(matches!(error, AuthError::Network(_)), "got {error:?}");
|
||||
assert!(error.to_string().contains("timed out"), "got {error}");
|
||||
assert_says_it_once(&error);
|
||||
}
|
||||
|
||||
/// Headers arrive, the body never does: the first timeout is behind us and the read is not
|
||||
/// covered by it.
|
||||
#[tokio::test]
|
||||
async fn an_endpoint_that_stalls_mid_answer_ends_the_verification() {
|
||||
let reply = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 500\r\n\r\n{\"data\":";
|
||||
let (endpoint, heard) = quiet_server(reply).await;
|
||||
let outcome = verified_against(endpoint, heard).await.expect("gave up on its own");
|
||||
let error = outcome.expect_err("the list never finished");
|
||||
assert!(matches!(error, AuthError::Network(_)), "got {error:?}");
|
||||
assert!(error.to_string().contains("timed out"), "got {error}");
|
||||
assert_says_it_once(&error);
|
||||
}
|
||||
|
||||
/// Nothing listening reads as the one thing it is, not as a provider error inside a network one.
|
||||
#[tokio::test]
|
||||
async fn an_endpoint_that_refuses_the_connection_says_so_plainly() {
|
||||
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
|
||||
let descriptor = provider_descriptor("openai").expect("in the catalog");
|
||||
let endpoint = format!("http://127.0.0.1:{port}/v1");
|
||||
let error = verify_credential(descriptor, Some(&endpoint), &RequestAuth::none())
|
||||
.await
|
||||
.expect_err("nothing is listening");
|
||||
assert!(error.to_string().contains("could not reach"), "got {error}");
|
||||
// The cause, which reqwest's own text for the error leaves out
|
||||
assert!(error.to_string().to_lowercase().contains("refused"), "got {error}");
|
||||
assert_says_it_once(&error);
|
||||
}
|
||||
|
||||
/// One `network error:` prefix, and no `provider error:` from the adapters' vocabulary.
|
||||
fn assert_says_it_once(error: &AuthError) {
|
||||
let said = error.to_string();
|
||||
assert_eq!(said.matches("network error").count(), 1, "got {said}");
|
||||
assert!(!said.contains("provider error"), "got {said}");
|
||||
}
|
||||
Reference in New Issue
Block a user