use std::time::Instant;
use pretty_assertions::assert_eq;
use rho_protocol::{CompareState, ErrorCode, SealRecipient, SecretComparison, SecretItem};
use rho_providers::{
auth::login_dispatch::ProviderAuthentication, credentials::MemoryCredentialStore,
};
use super::*;
use crate::config::ConfigDocument;
fn api_key(auth: &str) -> SecretItem {
SecretItem::api_key(auth)
}
fn mcp_file(header: &str) -> ConfigDocument {
ConfigDocument::parse(&format!(
"[mcp.servers.docs]\nurl = \"https://docs.example/mcp\"\nheaders = {{ Authorization = \"{header}\" }}\n"
))
.unwrap()
}
fn none() -> ConfigDocument {
ConfigDocument::parse("").unwrap()
}
fn store(saved: &[(&str, &str)]) -> MemoryCredentialStore {
let store = MemoryCredentialStore::default();
for (auth, key) in saved {
ProviderAuthentication::save_api_key(&store, auth, key).unwrap();
}
store
}
#[test]
fn sealed_keys_compare_with_the_targets() {
let source = store(&[
("anthropic-api-key", "sk-same"),
("openrouter-api-key", "sk-new"),
("moonshot-api-key", "sk-only-here"),
("google-api-key", " "),
]);
let target = store(&[
("anthropic-api-key", "sk-same"),
("openrouter-api-key", "sk-old"),
]);
let recipients = seal::Recipients::default();
let recipient = SealRecipient::new(SUITE, recipients.issue(Instant::now()), 0);
let header = SecretItem::mcp_header("docs", "Authorization");
let unset = SecretItem::mcp_env("docs", "TOKEN");
let saved = values::saved_values(
&source,
&mcp_file("Bearer source"),
vec![
api_key("anthropic-api-key"),
api_key("google-api-key"),
api_key("openrouter-api-key"),
api_key("moonshot-api-key"),
header.clone(),
unset.clone(),
],
)
.unwrap();
assert_eq!(saved.missing, vec![api_key("google-api-key"), unset]);
let sealed = seal_values(&recipient, saved.items, &saved.values).unwrap();
let values = open_values(&recipients, Instant::now(), &sealed).unwrap();
assert_eq!(
values::compare_values(&target, &mcp_file("Bearer target"), sealed.items, &values).unwrap(),
vec![
SecretComparison::new(api_key("anthropic-api-key"), CompareState::Same),
SecretComparison::new(api_key("openrouter-api-key"), CompareState::Differs),
SecretComparison::new(api_key("moonshot-api-key"), CompareState::Absent),
SecretComparison::new(header, CompareState::Differs),
]
);
assert_eq!(
values::compare_values(
&target,
&none(),
vec![api_key("only-on-source")],
&["sk".into()]
)
.unwrap(),
vec![SecretComparison::new(
api_key("only-on-source"),
CompareState::Absent
)]
);
}
#[test]
fn a_bundle_it_cannot_take_is_refused_without_using_the_recipient() {
let recipients = seal::Recipients::default();
let recipient = SealRecipient::new(SUITE, recipients.issue(Instant::now()), 0);
for (items, values) in [
(vec![SecretItem::Unknown], vec!["sk".to_owned()]),
(
vec![api_key("openai"), api_key("openai")],
vec!["sk".to_owned(), "sk-2".to_owned()],
),
] {
let refused = seal_values(&recipient, items.clone(), &values).unwrap();
assert_eq!(
open_values(&recipients, Instant::now(), &refused)
.err()
.and_then(|error| error.error_code()),
Some(ErrorCode::InvalidParams),
"{items:?}"
);
}
let known = seal_values(&recipient, vec![api_key("openai")], &["sk".into()]).unwrap();
assert_eq!(
open_values(&recipients, Instant::now(), &known)
.unwrap()
.to_vec(),
vec!["sk".to_owned()]
);
}
#[test]
fn only_api_keys_are_exported_of_the_credentials() {
for item in [
api_key("codex"),
api_key("no-such-auth"),
SecretItem::Unknown,
] {
assert_eq!(
values::saved_values(&store(&[]), &none(), vec![item.clone()])
.err()
.and_then(|error| error.error_code()),
Some(ErrorCode::InvalidParams),
"{item:?}"
);
}
}
#[tokio::test]
async fn issued_keys_do_not_outlive_the_sync_state() {
let sync = SecretSync::default();
sync.recipient();
let keys = Arc::downgrade(&sync.recipients);
drop(sync);
assert!(keys.upgrade().is_none());
}