use crate::config_edit::brazen::ProviderRow;
use crate::keymap::CenterTab;
use crate::model_pick::remedy;
const NO_CREDENTIAL: &str = "no credential for this provider: set BRAZEN_API_KEY (or the provider \
API-key env var / --api-key) or run `bz --login --provider <id>`";
fn row(name: &str, auth: &str) -> ProviderRow {
ProviderRow {
name: name.to_owned(),
auth: auth.to_owned(),
}
}
#[test]
fn a_keyed_row_with_no_key_routes_to_the_config_editor() {
let found = remedy(&row("anthropic", "api_key"), NO_CREDENTIAL)
.expect("brazen's decline names a credential, so it is auth-shaped");
assert_eq!(found.tab, CenterTab::Config);
assert_eq!(found.verb, "Config");
assert_eq!(
found.reason.as_deref(),
Some("anthropic: api-key provider — set the key in Config"),
"the sentence is `login_blocked`'s, under the row's own name — not a \
second phrasing of what a keyed row needs"
);
}
#[test]
fn a_bearer_row_takes_the_same_route_with_its_own_sentence() {
let found = remedy(&row("acme", "bearer"), NO_CREDENTIAL).expect("auth-shaped");
assert_eq!(found.tab, CenterTab::Config);
assert_eq!(
found.reason.as_deref(),
Some("acme: bearer-token provider — set the token in Config")
);
}
#[test]
fn an_oauth_row_routes_to_login_and_the_verb_names_the_row() {
let found =
remedy(&row("openai-chatgpt", "oauth2"), "401 unauthorized").expect("a 401 is auth-shaped");
assert_eq!(found.tab, CenterTab::Login);
assert_eq!(found.verb, "Login: openai-chatgpt");
assert_eq!(
found.reason, None,
"`login_blocked` has no sentence for an oauth row because the verb is \
the whole answer"
);
}
#[test]
fn keyless_and_unknown_rows_still_reach_the_file_that_declares_them() {
for (auth, reason) in [
("none", "local: keyless — nothing to log in"),
(
"sorcery",
"local: auth \"sorcery\" — bz --login signs in oauth2 rows only",
),
] {
let found = remedy(&row("local", auth), NO_CREDENTIAL).expect("auth-shaped");
assert_eq!(found.tab, CenterTab::Config);
assert_eq!(found.reason.as_deref(), Some(reason));
}
}
#[test]
fn a_failure_that_is_not_about_credentials_offers_no_route() {
for other in [
"failed to spawn `bz`: No such file or directory (os error 2)",
crate::model_pick::query::EMPTY_ROSTER,
"connection reset by peer",
"500 internal server error",
] {
assert_eq!(
remedy(&row("anthropic", "api_key"), other),
None,
"{other} is not a credential problem"
);
}
}