use std::sync::Arc;
mod common;
use common::start_mock_server;
use kaji::client::KeycloakClient;
use kaji::models::RealmRepresentation;
use kaji::{apply, plan};
use std::fs;
use tempfile::tempdir;
use kaji::utils::ui::MockUi;
#[tokio::test]
async fn test_ultimate_flow() {
let mock_url = start_mock_server().await;
let mut client = KeycloakClient::new(mock_url);
client.set_target_realm("test-realm".to_string());
client
.login("admin-cli", Some("secret"), None, None)
.await
.expect("Login failed");
let dir = tempdir().unwrap();
let workspace_dir = dir.path().to_path_buf();
let realm_dir = workspace_dir.join("test-realm");
fs::create_dir(&realm_dir).unwrap();
let resolver = Arc::new(kaji::utils::secrets::EnvResolver::new(
std::collections::HashMap::new(),
)) as Arc<dyn kaji::utils::secrets::SecretResolver>;
let ui = Arc::new(MockUi {
inputs: std::sync::Mutex::new(Vec::new()),
confirms: std::sync::Mutex::new(Vec::new()),
selects: std::sync::Mutex::new(Vec::new()),
passwords: std::sync::Mutex::new(Vec::new()),
});
let realm = RealmRepresentation {
realm: "test-realm".to_string(),
enabled: Some(true),
display_name: Some("New Realm".to_string()), extra: std::collections::HashMap::new(),
};
fs::write(
realm_dir.join("realm.yaml"),
serde_yaml::to_string(&realm).unwrap(),
)
.unwrap();
plan::run(
&client,
workspace_dir.clone(),
false,
false,
&["test-realm".to_string()],
ui.clone(),
resolver.clone(),
None,
)
.await
.unwrap();
assert!(workspace_dir.join(".kajiplan").exists());
apply::run(
&client,
workspace_dir.clone(),
&["test-realm".to_string()],
true,
false,
ui.clone(),
resolver.clone(),
None,
)
.await
.unwrap();
assert!(!workspace_dir.join(".kajiplan").exists());
let realm_matches = RealmRepresentation {
realm: "test-realm".to_string(),
enabled: Some(true),
display_name: Some("Test Realm".to_string()), extra: std::collections::HashMap::new(),
};
fs::write(
realm_dir.join("realm.yaml"),
serde_yaml::to_string(&realm_matches).unwrap(),
)
.unwrap();
plan::run(
&client,
workspace_dir.clone(),
true, false,
&["test-realm".to_string()],
ui.clone(),
resolver.clone(),
None,
)
.await
.unwrap();
assert!(!workspace_dir.join(".kajiplan").exists());
let realm_mismatch = RealmRepresentation {
realm: "test-realm".to_string(),
enabled: Some(true),
display_name: Some("Mismatch".to_string()),
extra: std::collections::HashMap::new(),
};
fs::write(
realm_dir.join("realm.yaml"),
serde_yaml::to_string(&realm_mismatch).unwrap(),
)
.unwrap();
ui.confirms.lock().unwrap().push(false);
plan::run(
&client,
workspace_dir.clone(),
false,
true, &["test-realm".to_string()],
ui.clone(),
resolver.clone(),
None,
)
.await
.unwrap();
assert!(!workspace_dir.join(".kajiplan").exists());
}