add UI tests for welcome and unlock flows
This commit is contained in:
1 parent
5999aba0a5
commit
b2a7e5759a
9 files changed
+1003
-59
No files matched your search
+148
-11
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
|
||||
@@ -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() {
|
||||
// todo: replace this with proper error handling
|
||||
let app_config = AppConfig::load().expect("failed to load config");
|
||||
@@ -55,16 +70,138 @@ fn main() {
|
||||
gpui_kit::application()
|
||||
.with_assets(gpui_kit::assets::Assets)
|
||||
.run(|app| {
|
||||
gpui_omarchy::init(app);
|
||||
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");
|
||||
launch(app, app_config, app_session);
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user