Compare commits

...
3 Commits
8 changed files with 454 additions and 440 deletions

No files matched your search

Generated
+11
View File
@@ -1440,6 +1440,9 @@ dependencies = [
"getrandom 0.4.3", "getrandom 0.4.3",
"gpui-kit", "gpui-kit",
"gpui-omarchy", "gpui-omarchy",
"http-body-util",
"hyper",
"hyper-util",
"include_dir", "include_dir",
"jiff", "jiff",
"serde", "serde",
@@ -1447,6 +1450,7 @@ dependencies = [
"tempfile", "tempfile",
"testresult", "testresult",
"thiserror 2.0.21", "thiserror 2.0.21",
"tokio",
"uuid", "uuid",
] ]
@@ -3640,6 +3644,12 @@ version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]] [[package]]
name = "hybrid-array" name = "hybrid-array"
version = "0.4.15" version = "0.4.15"
@@ -3663,6 +3673,7 @@ dependencies = [
"http", "http",
"http-body", "http-body",
"httparse", "httparse",
"httpdate",
"itoa", "itoa",
"pin-project-lite", "pin-project-lite",
"smallvec", "smallvec",
+4
View File
@@ -16,12 +16,16 @@ duckdb = { version = "1.20000.1", features = ["bundled", "uuid"] }
getrandom = { version = "0.4.3", features = ["sys_rng"] } getrandom = { version = "0.4.3", features = ["sys_rng"] }
gpui-kit = "0.7.0" gpui-kit = "0.7.0"
gpui-omarchy = "0.2.1" gpui-omarchy = "0.2.1"
http-body-util = "0.1.5"
hyper = { version = "1.12.0", features = ["server", "http1"] }
hyper-util = { version = "0.1.21", features = ["tokio"] }
include_dir = "0.7.4" include_dir = "0.7.4"
jiff = { version = "1.0.1", features = ["serde"] } jiff = { version = "1.0.1", features = ["serde"] }
serde = "1.0.229" serde = "1.0.229"
serde_json = "1.0.154" serde_json = "1.0.154"
testresult = "0.4.2" testresult = "0.4.2"
thiserror = "2.0.21" thiserror = "2.0.21"
tokio = { version = "1.54.1", features = ["rt-multi-thread", "net"] }
uuid = { version = "1.29.3", features = ["v4", "serde"] } uuid = { version = "1.29.3", features = ["v4", "serde"] }
[dev-dependencies] [dev-dependencies]
+59
View File
@@ -0,0 +1,59 @@
use gpui_kit::{
App, AppContext, Context, IntoElement, ParentElement, Render, Styled, Window, WindowHandle,
WindowOptions, div,
};
use gpui_omarchy::ActiveTheme;
use crate::config::AppConfig;
use crate::screens::{CredentialsScreen, HomeScreen, Screen, UnlockScreen, WelcomeScreen};
use crate::session::AppSession;
pub struct CreddyApp {}
impl Render for CreddyApp {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let session = cx.global::<AppSession>();
let screen = match session {
AppSession::Empty => window
.use_keyed_state("welcome", cx, |window, cx| {
WelcomeScreen::new(window, cx)
})
.into_any_element(),
AppSession::Locked => window
.use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx))
.into_any_element(),
AppSession::Unlocked { .. } => match cx.global::<Screen>() {
Screen::Home => window
.use_keyed_state("home", cx, |_, _| HomeScreen {})
.into_any_element(),
Screen::Credentials => window
.use_keyed_state("credentials", cx, |_, cx| CredentialsScreen::new(cx))
.into_any_element(),
},
};
let theme = cx.omarchy();
div()
.size_full()
.bg(theme.background)
.text_color(theme.foreground)
.font_family(theme.font.clone())
.child(screen)
}
}
/// Install the globals and open the main window. Tests call this too, so they get the same wiring.
pub fn launch(cx: &mut App, config: AppConfig, session: AppSession) -> WindowHandle<CreddyApp> {
gpui_omarchy::init(cx);
cx.set_global(config);
cx.set_global(session);
cx.set_global(Screen::Home);
// 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.observe_global::<Screen>(|cx| cx.refresh_windows()).detach();
cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| CreddyApp {}))
.expect("open window")
}
+1 -1
View File
@@ -91,7 +91,7 @@ impl Db {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use testresult::TestResult; use testresult::TestResult;
use crate::test_data::TestEnv; use crate::tests::TestEnv;
use super::*; use super::*;
fn test_cred(name: &str, access_key_id: &str) -> AwsCredential { fn test_cred(name: &str, access_key_id: &str) -> AwsCredential {
+6 -412
View File
@@ -1,9 +1,4 @@
use gpui_kit::{ mod app;
App, AppContext, Context, IntoElement, ParentElement, Render, Styled, Window, WindowHandle,
WindowOptions, div,
};
use gpui_omarchy::ActiveTheme;
mod assets; mod assets;
mod components; mod components;
mod config; mod config;
@@ -12,63 +7,13 @@ mod db;
mod error; mod error;
mod screens; mod screens;
mod session; mod session;
mod srv;
#[cfg(test)] #[cfg(test)]
mod test_data; mod tests;
use config::AppConfig; use config::AppConfig;
use session::AppSession; use session::AppSession;
use screens::{CredentialsScreen, HomeScreen, Screen, UnlockScreen, WelcomeScreen};
struct CreddyApp {}
impl Render for CreddyApp {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let session = cx.global::<AppSession>();
let screen = match session {
AppSession::Empty => window
.use_keyed_state("welcome", cx, |window, cx| {
WelcomeScreen::new(window, cx)
})
.into_any_element(),
AppSession::Locked => window
.use_keyed_state("unlock", cx, |window, cx| UnlockScreen::new(window, cx))
.into_any_element(),
AppSession::Unlocked { .. } => match cx.global::<Screen>() {
Screen::Home => window
.use_keyed_state("home", cx, |_, _| HomeScreen {})
.into_any_element(),
Screen::Credentials => window
.use_keyed_state("credentials", cx, |_, cx| CredentialsScreen::new(cx))
.into_any_element(),
},
};
let theme = cx.omarchy();
div()
.size_full()
.bg(theme.background)
.text_color(theme.foreground)
.font_family(theme.font.clone())
.child(screen)
}
}
/// 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);
cx.set_global(Screen::Home);
// 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.observe_global::<Screen>(|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
@@ -78,359 +23,8 @@ fn main() {
// Icons are read through the asset source, so register the bundle. // Icons are read through the asset source, so register the bundle.
gpui_kit::application() gpui_kit::application()
.with_assets(assets::CreddyAssets) .with_assets(assets::CreddyAssets)
.run(|app| { .run(|cx| {
launch(app, app_config, app_session); app::launch(cx, app_config, app_session);
app.activate(true); cx.activate(true);
}); });
} }
#[cfg(test)]
mod tests {
use gpui_kit::{AnyWindowHandle, TestAppContext};
use gpui_kit::test::TestWindowExt;
use uuid::Uuid;
use super::*;
use crate::credentials::AwsCredential;
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);
}
#[gpui_kit::test]
fn test_lock(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
assert!(is_unlocked(cx));
click(window, "lock", cx);
assert!(cx.read(|cx| matches!(cx.global::<AppSession>(), AppSession::Locked)));
// The unlock screen should be a new one, not the old one with the passphrase still in it.
// The masked input doesn't expose its value, so check by submitting it as-is.
click(window, "submit", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty."));
assert!(!is_unlocked(cx));
}
fn is_visible(window: AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) -> bool {
cx.update_window(window, |_, window, _| window.try_find(id).is_some()).unwrap()
}
#[gpui_kit::test]
fn test_credentials_navigation(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
assert_eq!(cx.read(|cx| *cx.global::<Screen>()), Screen::Credentials);
assert!(is_visible(window, "back", cx));
click(window, "back", cx);
assert_eq!(cx.read(|cx| *cx.global::<Screen>()), Screen::Home);
assert!(is_visible(window, "credentials", cx));
}
#[gpui_kit::test]
fn test_credentials_tabs(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
assert!(is_visible(window, "work", cx));
act(window, cx, |window, cx| window.within("credential-tabs").click(1usize, cx));
assert!(!is_visible(window, "work", cx));
act(window, cx, |window, cx| window.within("credential-tabs").click(0usize, cx));
assert!(is_visible(window, "work", cx));
}
#[gpui_kit::test]
fn test_add_aws_credential(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "aws-add", cx);
fill(window, "aws-name", "work", cx);
fill(window, "aws-key-id", "AKIAEXAMPLE", cx);
fill(window, "aws-secret", "EXAMPLESECRET", cx);
click(window, "aws-default", cx);
click(window, "aws-save", cx);
assert_eq!(error_message(window, cx), None);
assert!(is_visible(window, "work", cx));
assert!(!is_visible(window, "aws-save", cx));
let saved = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.load_aws_credential("work").unwrap()
});
assert_eq!(saved.access_key_id, "AKIAEXAMPLE");
assert_eq!(saved.secret_access_key, "EXAMPLESECRET");
assert!(saved.is_default);
}
#[gpui_kit::test]
fn test_add_aws_credential_missing_fields(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "aws-add", cx);
fill(window, "aws-name", "work", cx);
click(window, "aws-save", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("All fields are required."));
assert!(!is_visible(window, "work", cx));
}
#[gpui_kit::test]
fn test_add_aws_credential_duplicate_name(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "aws-add", cx);
fill(window, "aws-name", "work", cx);
fill(window, "aws-key-id", "AKIAOTHER", cx);
fill(window, "aws-secret", "OTHERSECRET", cx);
click(window, "aws-save", cx);
assert_eq!(
error_message(window, cx).as_deref(),
Some("An AWS credential named \"work\" already exists."),
);
let saved = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.load_aws_credential("work").unwrap()
});
assert_eq!(saved.access_key_id, "AKIAEXAMPLE");
}
#[gpui_kit::test]
fn test_edit_aws_credential(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "edit-work", cx);
act(window, cx, |window, cx| {
window.click("aws-name", cx);
window.press("ctrl-a", cx);
window.input("personal", cx);
});
click(window, "aws-default", cx);
click(window, "aws-save", cx);
assert_eq!(error_message(window, cx), None);
assert!(is_visible(window, "personal", cx));
assert!(!is_visible(window, "work", cx));
let creds = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.list_aws_credentials().unwrap()
});
assert_eq!(creds.len(), 1);
// fields that weren't touched keep the values the form was opened with
assert_eq!(creds[0].id, cred.id);
assert_eq!(creds[0].name, "personal");
assert_eq!(creds[0].access_key_id, "AKIAEXAMPLE");
assert_eq!(creds[0].secret_access_key, "EXAMPLESECRET");
assert!(creds[0].is_default);
}
#[gpui_kit::test]
fn test_delete_aws_credential(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "delete-work", cx);
click(window, "aws-delete-cancel", cx);
assert!(is_visible(window, "work", cx));
click(window, "delete-work", cx);
click(window, "aws-delete-confirm", cx);
assert_eq!(error_message(window, cx), None);
assert!(!is_visible(window, "work", cx));
let creds = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.list_aws_credentials().unwrap()
});
assert!(creds.is_empty());
}
}
+1 -2
View File
@@ -1,6 +1,5 @@
use gpui_kit::Global; use gpui_kit::Global;
use crate::config::AppConfig; use crate::config::AppConfig;
use crate::error::CreddyError;
use crate::db::Db; use crate::db::Db;
#[derive(Debug)] #[derive(Debug)]
@@ -27,7 +26,7 @@ mod tests {
use super::*; use super::*;
use std::assert_matches; use std::assert_matches;
use testresult::TestResult; use testresult::TestResult;
use crate::test_data::TestEnv; use crate::tests::TestEnv;
#[test] #[test]
fn test_load_empty() -> TestResult { fn test_load_empty() -> TestResult {
-25
View File
@@ -1,25 +0,0 @@
use tempfile::TempDir;
use crate::config::AppConfig;
use crate::db::Db;
use crate::error::CreddyError;
pub const TEST_PASSPHRASE: &'static str = "correct horse battery staple";
pub struct TestEnv {
// we have to keep this around or the tempdir will be deleted as soon as it's dropped
_dir: TempDir,
pub config: AppConfig,
}
impl TestEnv {
pub fn new() -> Result<TestEnv, CreddyError> {
let dir = tempfile::tempdir()?;
let config = AppConfig::load_from(dir.path().to_path_buf())?;
Ok(TestEnv { _dir: dir, config })
}
pub fn db(&self) -> Result<Db, CreddyError> {
Db::connect(&self.config, TEST_PASSPHRASE)
}
}
+372
View File
@@ -0,0 +1,372 @@
use gpui_kit::{AnyWindowHandle, App, AppContext, TestAppContext, Window};
use gpui_kit::test::TestWindowExt;
use tempfile::TempDir;
use uuid::Uuid;
use crate::app::launch;
use crate::config::AppConfig;
use crate::credentials::AwsCredential;
use crate::db::Db;
use crate::error::CreddyError;
use crate::screens::Screen;
use crate::session::AppSession;
pub const TEST_PASSPHRASE: &'static str = "correct horse battery staple";
pub struct TestEnv {
// we have to keep this around or the tempdir will be deleted as soon as it's dropped
_dir: TempDir,
pub config: AppConfig,
}
impl TestEnv {
pub fn new() -> Result<TestEnv, CreddyError> {
let dir = tempfile::tempdir()?;
let config = AppConfig::load_from(dir.path().to_path_buf())?;
Ok(TestEnv { _dir: dir, config })
}
pub fn db(&self) -> Result<Db, CreddyError> {
Db::connect(&self.config, TEST_PASSPHRASE)
}
}
/// 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);
}
#[gpui_kit::test]
fn test_lock(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
assert!(is_unlocked(cx));
click(window, "lock", cx);
assert!(cx.read(|cx| matches!(cx.global::<AppSession>(), AppSession::Locked)));
// The unlock screen should be a new one, not the old one with the passphrase still in it.
// The masked input doesn't expose its value, so check by submitting it as-is.
click(window, "submit", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty."));
assert!(!is_unlocked(cx));
}
fn is_visible(window: AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) -> bool {
cx.update_window(window, |_, window, _| window.try_find(id).is_some()).unwrap()
}
#[gpui_kit::test]
fn test_credentials_navigation(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
assert_eq!(cx.read(|cx| *cx.global::<Screen>()), Screen::Credentials);
assert!(is_visible(window, "back", cx));
click(window, "back", cx);
assert_eq!(cx.read(|cx| *cx.global::<Screen>()), Screen::Home);
assert!(is_visible(window, "credentials", cx));
}
#[gpui_kit::test]
fn test_credentials_tabs(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
assert!(is_visible(window, "work", cx));
act(window, cx, |window, cx| window.within("credential-tabs").click(1usize, cx));
assert!(!is_visible(window, "work", cx));
act(window, cx, |window, cx| window.within("credential-tabs").click(0usize, cx));
assert!(is_visible(window, "work", cx));
}
#[gpui_kit::test]
fn test_add_aws_credential(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "aws-add", cx);
fill(window, "aws-name", "work", cx);
fill(window, "aws-key-id", "AKIAEXAMPLE", cx);
fill(window, "aws-secret", "EXAMPLESECRET", cx);
click(window, "aws-default", cx);
click(window, "aws-save", cx);
assert_eq!(error_message(window, cx), None);
assert!(is_visible(window, "work", cx));
assert!(!is_visible(window, "aws-save", cx));
let saved = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.load_aws_credential("work").unwrap()
});
assert_eq!(saved.access_key_id, "AKIAEXAMPLE");
assert_eq!(saved.secret_access_key, "EXAMPLESECRET");
assert!(saved.is_default);
}
#[gpui_kit::test]
fn test_add_aws_credential_missing_fields(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
env.db().unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "aws-add", cx);
fill(window, "aws-name", "work", cx);
click(window, "aws-save", cx);
assert_eq!(error_message(window, cx).as_deref(), Some("All fields are required."));
assert!(!is_visible(window, "work", cx));
}
#[gpui_kit::test]
fn test_add_aws_credential_duplicate_name(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "aws-add", cx);
fill(window, "aws-name", "work", cx);
fill(window, "aws-key-id", "AKIAOTHER", cx);
fill(window, "aws-secret", "OTHERSECRET", cx);
click(window, "aws-save", cx);
assert_eq!(
error_message(window, cx).as_deref(),
Some("An AWS credential named \"work\" already exists."),
);
let saved = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.load_aws_credential("work").unwrap()
});
assert_eq!(saved.access_key_id, "AKIAEXAMPLE");
}
#[gpui_kit::test]
fn test_edit_aws_credential(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "edit-work", cx);
act(window, cx, |window, cx| {
window.click("aws-name", cx);
window.press("ctrl-a", cx);
window.input("personal", cx);
});
click(window, "aws-default", cx);
click(window, "aws-save", cx);
assert_eq!(error_message(window, cx), None);
assert!(is_visible(window, "personal", cx));
assert!(!is_visible(window, "work", cx));
let creds = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.list_aws_credentials().unwrap()
});
assert_eq!(creds.len(), 1);
// fields that weren't touched keep the values the form was opened with
assert_eq!(creds[0].id, cred.id);
assert_eq!(creds[0].name, "personal");
assert_eq!(creds[0].access_key_id, "AKIAEXAMPLE");
assert_eq!(creds[0].secret_access_key, "EXAMPLESECRET");
assert!(creds[0].is_default);
}
#[gpui_kit::test]
fn test_delete_aws_credential(cx: &mut TestAppContext) {
let env = TestEnv::new().unwrap();
let cred = AwsCredential {
id: Uuid::new_v4(),
name: "work".into(),
access_key_id: "AKIAEXAMPLE".into(),
secret_access_key: "EXAMPLESECRET".into(),
is_default: false,
};
env.db().unwrap().save_aws_credential(&cred).unwrap();
let window = open_app(&env, cx);
fill(window, "passphrase", TEST_PASSPHRASE, cx);
click(window, "submit", cx);
click(window, "credentials", cx);
click(window, "delete-work", cx);
click(window, "aws-delete-cancel", cx);
assert!(is_visible(window, "work", cx));
click(window, "delete-work", cx);
click(window, "aws-delete-confirm", cx);
assert_eq!(error_message(window, cx), None);
assert!(!is_visible(window, "work", cx));
let creds = cx.read(|cx| {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
db.list_aws_credentials().unwrap()
});
assert!(creds.is_empty());
}