Files
owlen/crates/owlen-tui/tests/guidance_persistence.rs

85 lines
2.1 KiB
Rust

mod common;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use owlen_core::config::Config;
use owlen_tui::events::Event;
use tempfile::tempdir;
use common::build_chat_app;
struct XdgConfigGuard {
previous: Option<std::ffi::OsString>,
}
impl XdgConfigGuard {
fn set(path: &std::path::Path) -> Self {
let previous = std::env::var_os("XDG_CONFIG_HOME");
unsafe {
std::env::set_var("XDG_CONFIG_HOME", path);
}
Self { previous }
}
}
impl Drop for XdgConfigGuard {
fn drop(&mut self) {
if let Some(prev) = self.previous.take() {
unsafe {
std::env::set_var("XDG_CONFIG_HOME", prev);
}
} else {
unsafe {
std::env::remove_var("XDG_CONFIG_HOME");
}
}
}
}
#[tokio::test(flavor = "multi_thread")]
async fn onboarding_completion_persists_config() {
let temp_dir = tempdir().expect("temp config dir");
let _guard = XdgConfigGuard::set(temp_dir.path());
let mut app = build_chat_app(
|cfg| {
cfg.ui.show_onboarding = true;
cfg.ui.guidance.coach_marks_complete = false;
},
|_| {},
)
.await;
for _ in 0..3 {
app.handle_event(Event::Key(KeyEvent::new(
KeyCode::Enter,
KeyModifiers::NONE,
)))
.await
.expect("advance onboarding");
}
assert!(
app.coach_marks_complete(),
"coach marks flag should be recorded in memory"
);
drop(app);
let persisted_path = temp_dir.path().join("owlen").join("config.toml");
assert!(
persisted_path.exists(),
"expected persisted config at {:?}",
persisted_path
);
let persisted = Config::load(Some(&persisted_path)).expect("load persisted config snapshot");
assert!(
!persisted.ui.show_onboarding,
"onboarding flag should be false in persisted config"
);
assert!(
persisted.ui.guidance.coach_marks_complete,
"coach marks flag should be true in persisted config"
);
}