add UI tests for welcome and unlock flows

This commit is contained in:
jfmonty2 committed 2026-11-18 14:31:00 -05:00
1 parent 5999aba0a5
commit b2a7e5759a
9 files changed
+1003 -59

No files matched your search

Generated
+822 -35
View File
File diff suppressed because it is too large. Load diff
+1
View File
@@ -23,4 +23,5 @@ testresult = "0.4.2"
thiserror = "2.0.21" thiserror = "2.0.21"
[dev-dependencies] [dev-dependencies]
gpui-kit = { version = "0.7.0", features = ["test-support"] }
tempfile = "3.27.0" tempfile = "3.27.0"
+13
View File
@@ -0,0 +1,13 @@
use gpui_kit::{App, InteractiveElement, IntoElement, Role, SharedString, StatefulInteractiveElement, TestSupportExt};
use gpui_omarchy::{Status, alert};
/// An error alert whose message is also its accessible label,
/// so screen readers announce it and UI tests can find it by its "error" ID.
pub fn error_alert(message: SharedString, cx: &App) -> impl IntoElement {
alert(message.clone(), Status::Error, cx)
.id("error")
.role(Role::Alert)
.aria_label(message)
.test_support()
}
+2
View File
@@ -1,3 +1,5 @@
mod error_alert;
mod passphrase_input; mod passphrase_input;
pub use error_alert::error_alert;
pub use passphrase_input::{PassphraseInput, PassphraseInputEvent}; pub use passphrase_input::{PassphraseInput, PassphraseInputEvent};
+6 -3
View File
@@ -12,6 +12,7 @@ use gpui_omarchy::{ButtonVariant, icon_button, input};
/// The `show` flag is passed in from outside so that it can be shared /// The `show` flag is passed in from outside so that it can be shared
/// (e.g. passphrase and confirm-passphrase inputs should share it). /// (e.g. passphrase and confirm-passphrase inputs should share it).
pub struct PassphraseInput { pub struct PassphraseInput {
id: SharedString,
state: Entity<InputState>, state: Entity<InputState>,
show: Entity<bool>, show: Entity<bool>,
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
@@ -29,7 +30,7 @@ pub enum PassphraseInputEvent {
impl EventEmitter<PassphraseInputEvent> for PassphraseInput {} impl EventEmitter<PassphraseInputEvent> for PassphraseInput {}
impl PassphraseInput { impl PassphraseInput {
pub fn new(show: Entity<bool>, window: &mut Window, cx: &mut Context<Self>) -> Self { pub fn new(id: impl Into<SharedString>, show: Entity<bool>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let masked = !*show.read(cx); let masked = !*show.read(cx);
let state = cx.new(|cx| { let state = cx.new(|cx| {
InputState::new(window, cx) InputState::new(window, cx)
@@ -56,6 +57,7 @@ impl PassphraseInput {
}); });
Self { Self {
id: id.into(),
state, state,
show, show,
_subscriptions: vec![sync_mask, forward], _subscriptions: vec![sync_mask, forward],
@@ -87,10 +89,11 @@ impl Render for PassphraseInput {
(IconName::Eye, "Show passphrase") (IconName::Eye, "Show passphrase")
}; };
input("passphrase", &self.state, window, cx) let toggle_id = SharedString::from(format!("{}-toggle-show", self.id));
input(self.id.clone(), &self.state, window, cx)
.w_full() .w_full()
.suffix( .suffix(
icon_button("toggle-show", icon, label, ButtonVariant::Secondary, cx) icon_button(toggle_id, icon, label, ButtonVariant::Secondary, cx)
.on_click(cx.listener(|this, _, _, cx| this.toggle_show(cx))), .on_click(cx.listener(|this, _, _, cx| this.toggle_show(cx))),
) )
} }
+2 -1
View File
@@ -13,7 +13,8 @@ impl Db {
pub fn save_private_config(&self, config: &PrivateConfig) -> Result<(), CreddyError> { pub fn save_private_config(&self, config: &PrivateConfig) -> Result<(), CreddyError> {
let json = serde_json::to_string(&config)?; let json = serde_json::to_string(&config)?;
let conn = self.connection.lock().unwrap(); let conn = self.connection.lock().unwrap();
conn.execute("INSERT INTO config VALUES (?);", [&json])?; // the migration seeds the only row, so this is always an update
conn.execute("UPDATE config SET value = ?;", [&json])?;
Ok(()) Ok(())
} }
} }
+148 -11
View File
@@ -1,5 +1,6 @@
use gpui_kit::{ use gpui_kit::{
AppContext, Context, IntoElement, ParentElement, Render, Styled, Window, WindowOptions, div, App, AppContext, Context, IntoElement, ParentElement, Render, Styled, Window, WindowHandle,
WindowOptions, div,
}; };
use gpui_omarchy::ActiveTheme; use gpui_omarchy::ActiveTheme;
@@ -46,6 +47,20 @@ impl Render for CreddyApp {
} }
} }
/// Install the globals and open the main window. Tests call this too, so they get the same wiring.
fn launch(cx: &mut App, config: AppConfig, session: AppSession) -> WindowHandle<CreddyApp> {
gpui_omarchy::init(cx);
cx.set_global(config);
cx.set_global(session);
// refresh_windows() forces everything to re-render, which is kind of a nuclear solution,
// but these values aren't expected to change very frequently, so it shouldn't be a problem
cx.observe_global::<AppConfig>(|cx| cx.refresh_windows()).detach();
cx.observe_global::<AppSession>(|cx| cx.refresh_windows()).detach();
cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| CreddyApp {}))
.expect("open window")
}
fn main() { fn main() {
// todo: replace this with proper error handling // todo: replace this with proper error handling
let app_config = AppConfig::load().expect("failed to load config"); let app_config = AppConfig::load().expect("failed to load config");
@@ -55,16 +70,138 @@ fn main() {
gpui_kit::application() gpui_kit::application()
.with_assets(gpui_kit::assets::Assets) .with_assets(gpui_kit::assets::Assets)
.run(|app| { .run(|app| {
gpui_omarchy::init(app); launch(app, app_config, app_session);
app.set_global(app_config);
app.set_global(app_session);
// refresh_windows() forces everything to re-render, which is kind of a nuclear solution,
// but these values aren't expected to change very frequently, so it shouldn't be a problem
app.observe_global::<AppConfig>(|app| app.refresh_windows()).detach();
app.observe_global::<AppSession>(|app| app.refresh_windows()).detach();
app.open_window(WindowOptions::default(), |_, cx| cx.new(|_| CreddyApp {}))
.expect("open window");
app.activate(true); app.activate(true);
}); });
} }
#[cfg(test)]
mod tests {
use gpui_kit::{AnyWindowHandle, TestAppContext};
use gpui_kit::test::TestWindowExt;
use super::*;
use crate::test_data::{TEST_PASSPHRASE, TestEnv};
/// Start the app against the test environment, as `main` would.
fn open_app(env: &TestEnv, cx: &mut TestAppContext) -> AnyWindowHandle {
let window = cx.update(|cx| launch(cx, env.config.clone(), AppSession::load(&env.config)));
// first frame, so that elements exist to be found
cx.update_window(window.into(), |_, window, cx| window.render_frame(cx)).unwrap();
window.into()
}
/// Do something in the window, then let any resulting tasks (e.g. the KDF) finish.
fn act(window: AnyWindowHandle, cx: &mut TestAppContext, f: impl FnOnce(&mut Window, &mut App)) {
cx.update_window(window, |_, window, cx| f(window, cx)).unwrap();
cx.run_until_parked();
cx.update_window(window, |_, window, cx| window.render_frame(cx)).unwrap();
}
/// Click into a field and type.
fn fill(window: AnyWindowHandle, id: &'static str, text: &str, cx: &mut TestAppContext) {
act(window, cx, |window, cx| {
window.click(id, cx);
window.input(text, cx);
});
}
fn click(window: AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) {
act(window, cx, |window, cx| window.click(id, cx));
}
/// The message in the error alert, if one is showing.
fn error_message(window: AnyWindowHandle, cx: &mut TestAppContext) -> Option<String> {
cx.update_window(window, |_, window, _| {
window.try_find("error").and_then(|e| e.label().map(String::from))
}).unwrap()
}
fn is_unlocked(cx: &TestAppContext) -> bool {
cx.read(|cx| matches!(cx.global::<AppSession>(), AppSession::Unlocked(_)))
}
#[gpui_kit::test]
fn test_welcome_success(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
fill(window, "confirm", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
assert_eq!(error_message(window, cx), None);
assert!(is_unlocked(cx));
assert!(env.config.db_path().exists());
}
#[gpui_kit::test]
fn test_welcome_mismatch(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
fill(window, "confirm", "something else", cx);
click(window, "submit", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("Passphrases do not match."));
assert!(!is_unlocked(cx));
assert!(!env.config.db_path().exists());
}
#[gpui_kit::test]
fn test_welcome_empty(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let window = open_app(&env, cx);
click(window, "submit", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty."));
assert!(!is_unlocked(cx));
assert!(!env.config.db_path().exists());
}
#[gpui_kit::test]
fn test_unlock_success(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap(); // create the db, then close it again so the app starts out locked
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
assert_eq!(error_message(window, cx), None);
assert!(is_unlocked(cx));
}
#[gpui_kit::test]
fn test_unlock_wrong_passphrase(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", "not the passphrase", cx);
click(window, "submit", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("Incorrect passphrase."));
assert!(!is_unlocked(cx));
}
#[gpui_kit::test]
fn test_unlock_restores_private_config(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let mut saved = env.config.clone();
saved.rehide_ms = 5000;
env.db().unwrap().save_private_config(&saved.private_config()).unwrap();
let window = open_app(&env, cx);
// before unlocking, only the defaults are available
assert_eq!(cx.read(|cx| cx.global::<AppConfig>().rehide_ms), env.config.rehide_ms);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
assert!(is_unlocked(cx));
assert_eq!(cx.read(|cx| cx.global::<AppConfig>().rehide_ms), 5000);
}
}
+4 -4
View File
@@ -2,9 +2,9 @@ use gpui_kit::{
AppContext, BorrowAppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString, AppContext, BorrowAppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
Styled, Subscription, Task, Window, div, rems, Styled, Subscription, Task, Window, div, rems,
}; };
use gpui_omarchy::{ButtonVariant, Status, alert, button, focus_scope}; use gpui_omarchy::{ButtonVariant, button, focus_scope};
use crate::components::{PassphraseInput, PassphraseInputEvent}; use crate::components::{PassphraseInput, PassphraseInputEvent, error_alert};
use crate::config::AppConfig; use crate::config::AppConfig;
use crate::db::Db; use crate::db::Db;
use crate::error::CreddyError; use crate::error::CreddyError;
@@ -24,7 +24,7 @@ impl UnlockScreen {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
// nothing else shares the show/hide flag, so it's private to this input // nothing else shares the show/hide flag, so it's private to this input
let show = cx.new(|_| false); let show = cx.new(|_| false);
let passphrase = cx.new(|cx| PassphraseInput::new(show, window, cx)); let passphrase = cx.new(|cx| PassphraseInput::new("passphrase", show, window, cx));
passphrase.update(cx, |input, cx| input.focus(window, cx)); passphrase.update(cx, |input, cx| input.focus(window, cx));
let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| { let submit = cx.subscribe_in(&passphrase, window, |this, _, event, window, cx| {
@@ -125,7 +125,7 @@ impl Render for UnlockScreen {
.font_weight(FontWeight::BOLD) .font_weight(FontWeight::BOLD)
.child("Please enter your passphrase"), .child("Please enter your passphrase"),
) )
.children(self.error.clone().map(|e| alert(e, Status::Error, cx))) .children(self.error.clone().map(|e| error_alert(e, cx)))
.child(self.passphrase.clone()) .child(self.passphrase.clone())
.child( .child(
button("submit", "Submit", ButtonVariant::Primary, cx) button("submit", "Submit", ButtonVariant::Primary, cx)
+5 -5
View File
@@ -2,9 +2,9 @@ use gpui_kit::{
AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString, AppContext, Context, Entity, FontWeight, IntoElement, ParentElement, Render, SharedString,
Styled, Subscription, Task,Window, div, rems, Styled, Subscription, Task,Window, div, rems,
}; };
use gpui_omarchy::{ActiveTheme, ButtonVariant, Status, alert, button, focus_scope}; use gpui_omarchy::{ActiveTheme, ButtonVariant, button, focus_scope};
use crate::components::{PassphraseInput, PassphraseInputEvent}; use crate::components::{PassphraseInput, PassphraseInputEvent, error_alert};
use crate::config::AppConfig; use crate::config::AppConfig;
use crate::db::Db; use crate::db::Db;
use crate::session::AppSession; use crate::session::AppSession;
@@ -26,8 +26,8 @@ pub struct WelcomeScreen {
impl WelcomeScreen { impl WelcomeScreen {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let show = cx.new(|_| false); let show = cx.new(|_| false);
let passphrase = cx.new(|cx| PassphraseInput::new(show.clone(), window, cx)); let passphrase = cx.new(|cx| PassphraseInput::new("passphrase", show.clone(), window, cx));
let confirm = cx.new(|cx| PassphraseInput::new(show, window, cx)); let confirm = cx.new(|cx| PassphraseInput::new("confirm", show, window, cx));
passphrase.update(cx, |input, cx| input.focus(window, cx)); passphrase.update(cx, |input, cx| input.focus(window, cx));
let mut _subscriptions = Vec::new(); let mut _subscriptions = Vec::new();
@@ -162,7 +162,7 @@ impl Render for WelcomeScreen {
.flex() .flex()
.flex_col() .flex_col()
.gap(rems(1.)) .gap(rems(1.))
.children(self.error.clone().map(|e| alert(e, Status::Error, cx))) .children(self.error.clone().map(|e| error_alert(e, cx)))
.child(field("Passphrase", &self.passphrase)) .child(field("Passphrase", &self.passphrase))
.child(field("Re-enter passphrase", &self.confirm)) .child(field("Re-enter passphrase", &self.confirm))
.child( .child(