test(tui): cover application interaction flows
This commit is contained in:
1 parent
aac3f47f6c
commit
798c323a82
9 files changed
+332
-28
No files matched your search
@@ -36,6 +36,14 @@ pub struct AccountStore {
|
||||
}
|
||||
|
||||
impl AccountStore {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn empty_for_test(path: PathBuf) -> Self {
|
||||
Self {
|
||||
accounts: Vec::new(),
|
||||
path,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load() -> Self {
|
||||
let path = account_store_path();
|
||||
let accounts = match std::fs::read_to_string(&path) {
|
||||
|
||||
+25
-18
@@ -452,24 +452,7 @@ impl App {
|
||||
.or_else(|_| std::env::var("VISUAL"))
|
||||
.unwrap_or_else(|_| default_editor.to_owned());
|
||||
|
||||
let editor_name = std::path::Path::new(&editor)
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or(&editor);
|
||||
let is_tui_editor = matches!(
|
||||
editor_name,
|
||||
"vi" | "vim"
|
||||
| "nvim"
|
||||
| "neovim"
|
||||
| "nano"
|
||||
| "micro"
|
||||
| "helix"
|
||||
| "hx"
|
||||
| "emacs"
|
||||
| "ne"
|
||||
| "joe"
|
||||
| "mcedit"
|
||||
);
|
||||
let is_tui_editor = editor_runs_in_terminal(&editor);
|
||||
|
||||
if is_tui_editor {
|
||||
let _ = stdout().execute(DisableMouseCapture);
|
||||
@@ -610,3 +593,27 @@ impl App {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn editor_runs_in_terminal(editor: &str) -> bool {
|
||||
let editor_name = std::path::Path::new(editor)
|
||||
.file_stem()
|
||||
.and_then(|name| name.to_str())
|
||||
.unwrap_or(editor);
|
||||
matches!(
|
||||
editor_name,
|
||||
"vi" | "vim"
|
||||
| "nvim"
|
||||
| "neovim"
|
||||
| "nano"
|
||||
| "micro"
|
||||
| "helix"
|
||||
| "hx"
|
||||
| "emacs"
|
||||
| "ne"
|
||||
| "joe"
|
||||
| "mcedit"
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,47 @@
|
||||
use super::*;
|
||||
use crate::tui::tests::harness::UiHarness;
|
||||
|
||||
#[test]
|
||||
fn edited_instance_config_reloads_into_the_ui() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_instance("Edited");
|
||||
let mut config = ui.app.instances_state.selected_instance().unwrap().clone();
|
||||
config.memory_max = Some("8G".to_owned());
|
||||
ui.app.instance_manager.save(&config).unwrap();
|
||||
|
||||
ui.app
|
||||
.reload_edited_config(&ui.instance_path("Edited").join("instance.json"));
|
||||
|
||||
assert_eq!(
|
||||
ui.app
|
||||
.instances_state
|
||||
.selected_instance()
|
||||
.unwrap()
|
||||
.memory_max
|
||||
.as_deref(),
|
||||
Some("8G")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completed_background_instance_is_drained_into_the_ui() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_instance("Pending");
|
||||
let config = ui.app.instances_state.instances.pop().unwrap();
|
||||
PENDING_INSTANCES.lock().unwrap().push(config);
|
||||
|
||||
ui.app.drain_pending_instances();
|
||||
|
||||
assert_eq!(
|
||||
ui.app.instances_state.selected_instance().unwrap().name,
|
||||
"Pending"
|
||||
);
|
||||
assert!(PENDING_INSTANCES.lock().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn editor_kind_is_detected_from_the_executable_name() {
|
||||
assert!(editor_runs_in_terminal("/usr/bin/nvim"));
|
||||
assert!(editor_runs_in_terminal("nano"));
|
||||
assert!(!editor_runs_in_terminal("/usr/bin/code"));
|
||||
}
|
||||
+1
-1
@@ -10,7 +10,7 @@ mod render;
|
||||
pub mod widgets;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub(crate) mod tests;
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
|
||||
+191
-1
@@ -1,8 +1,9 @@
|
||||
use crossterm::event::KeyCode;
|
||||
|
||||
use super::harness::UiHarness;
|
||||
use crate::instance::ProviderProject;
|
||||
use crate::tui::{
|
||||
app::FocusedArea,
|
||||
app::{FocusedArea, ProviderConflictState},
|
||||
widgets::{
|
||||
content::{ContentMode, ContentTab},
|
||||
popups::confirm,
|
||||
@@ -47,6 +48,7 @@ fn discovery_mode_recovers_from_a_hidden_tab_and_cycles_visible_tabs() {
|
||||
fn instance_delete_can_be_cancelled_without_touching_disk() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_instance("Test Instance");
|
||||
let instance_path = ui.instance_path("Test Instance");
|
||||
|
||||
ui.key(KeyCode::Char('d'));
|
||||
assert_eq!(ui.app.focused, FocusedArea::ConfirmDelete);
|
||||
@@ -59,4 +61,192 @@ fn instance_delete_can_be_cancelled_without_touching_disk() {
|
||||
assert_eq!(ui.app.focused, FocusedArea::Instances);
|
||||
assert!(confirm::pending_target().is_none());
|
||||
assert_eq!(ui.app.instances_state.instances.len(), 1);
|
||||
assert!(instance_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirmed_instance_delete_removes_state_and_disk() {
|
||||
let mut ui = UiHarness::new();
|
||||
let name = format!("rmcl-ui-test-{}", std::process::id());
|
||||
ui.add_instance(&name);
|
||||
let instance_path = ui.instance_path(&name);
|
||||
assert!(!crate::instance::desktop::exists(&name));
|
||||
|
||||
ui.key(KeyCode::Char('d'));
|
||||
ui.draw();
|
||||
assert!(ui.screen().contains(&format!("Delete '{name}'")));
|
||||
assert!(
|
||||
ui.screen()
|
||||
.contains("This will permanently remove the instance")
|
||||
);
|
||||
ui.key(KeyCode::Char('y'));
|
||||
|
||||
assert_eq!(ui.app.focused, FocusedArea::Instances);
|
||||
assert!(ui.app.instances_state.instances.is_empty());
|
||||
assert!(!instance_path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirmed_screenshot_delete_removes_state_and_file() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_instance("Screenshots");
|
||||
let path = ui
|
||||
.instance_path("Screenshots")
|
||||
.join(crate::storage::MINECRAFT_DIR_NAME)
|
||||
.join("screenshots")
|
||||
.join("shot.png");
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
std::fs::write(&path, b"image").unwrap();
|
||||
ui.app.screenshots_state.entries = vec![crate::instance::screenshots::ScreenshotEntry {
|
||||
name: "shot.png".to_owned(),
|
||||
path: path.clone(),
|
||||
width: 1,
|
||||
height: 1,
|
||||
}];
|
||||
ui.app.focused = FocusedArea::Content;
|
||||
ui.app.content_tab = ContentTab::Screenshots;
|
||||
|
||||
ui.key(KeyCode::Char('d'));
|
||||
ui.key(KeyCode::Enter);
|
||||
|
||||
assert_eq!(ui.app.focused, FocusedArea::Content);
|
||||
assert!(ui.app.screenshots_state.entries.is_empty());
|
||||
assert!(!path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirmed_account_delete_updates_the_account_panel() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_account("Player");
|
||||
ui.app.focused = FocusedArea::Account;
|
||||
|
||||
ui.key(KeyCode::Char('d'));
|
||||
ui.key(KeyCode::Char('y'));
|
||||
|
||||
assert_eq!(ui.app.focused, FocusedArea::Account);
|
||||
assert!(ui.app.account_state.store.accounts.is_empty());
|
||||
assert_eq!(ui.app.account_state.list_state.selected, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_profile_can_be_created_from_key_events() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.app.focused = FocusedArea::Settings;
|
||||
|
||||
ui.key(KeyCode::Char('a'));
|
||||
for character in "shared".chars() {
|
||||
ui.key(KeyCode::Char(character));
|
||||
}
|
||||
ui.key(KeyCode::Enter);
|
||||
|
||||
assert_eq!(ui.app.settings_state.profiles, ["shared"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_wizards_open_render_and_cancel_through_app_input() {
|
||||
let mut ui = UiHarness::new();
|
||||
|
||||
ui.key(KeyCode::Char('a'));
|
||||
assert_eq!(ui.app.focused, FocusedArea::Popup);
|
||||
ui.draw();
|
||||
assert!(ui.screen().contains("New Instance"));
|
||||
ui.key(KeyCode::Esc);
|
||||
assert_eq!(ui.app.focused, FocusedArea::Instances);
|
||||
|
||||
ui.key(KeyCode::Char('i'));
|
||||
assert_eq!(ui.app.focused, FocusedArea::ImportPopup);
|
||||
ui.draw();
|
||||
assert!(ui.screen().contains("Import Modpack"));
|
||||
ui.key(KeyCode::Esc);
|
||||
assert_eq!(ui.app.focused, FocusedArea::Instances);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_conflict_renders_and_can_be_deferred() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.app.provider_conflict = Some(ProviderConflictState {
|
||||
relative_path: "mods/example.jar".into(),
|
||||
candidates: vec![
|
||||
ProviderProject {
|
||||
provider: "modrinth".to_owned(),
|
||||
project_id: "first".to_owned(),
|
||||
version_id: "1".to_owned(),
|
||||
},
|
||||
ProviderProject {
|
||||
provider: "curseforge".to_owned(),
|
||||
project_id: "second".to_owned(),
|
||||
version_id: "2".to_owned(),
|
||||
},
|
||||
],
|
||||
selected: 0,
|
||||
});
|
||||
|
||||
ui.draw();
|
||||
assert!(ui.screen().contains("Choose provider for example.jar"));
|
||||
|
||||
ui.key(KeyCode::Down);
|
||||
assert_eq!(ui.app.provider_conflict.as_ref().unwrap().selected, 1);
|
||||
ui.key(KeyCode::Esc);
|
||||
|
||||
assert!(ui.app.provider_conflict.is_none());
|
||||
assert!(
|
||||
ui.app
|
||||
.dismissed_provider_conflicts
|
||||
.contains(std::path::Path::new("mods/example.jar"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_conflict_selection_is_persisted() {
|
||||
let mut ui = UiHarness::new();
|
||||
ui.add_instance("Conflict");
|
||||
let relative_path = std::path::PathBuf::from("mods/example.jar");
|
||||
let candidates = vec![
|
||||
ProviderProject {
|
||||
provider: "modrinth".to_owned(),
|
||||
project_id: "first".to_owned(),
|
||||
version_id: "1".to_owned(),
|
||||
},
|
||||
ProviderProject {
|
||||
provider: "curseforge".to_owned(),
|
||||
project_id: "second".to_owned(),
|
||||
version_id: "2".to_owned(),
|
||||
},
|
||||
];
|
||||
let manifest = crate::instance::ContentManifest {
|
||||
version: 1,
|
||||
files: vec![crate::instance::ContentFileRecord {
|
||||
relative_path: relative_path.clone(),
|
||||
kind: crate::instance::ContentKind::Mod,
|
||||
enabled: true,
|
||||
fingerprint: crate::instance::FileFingerprint {
|
||||
size: 1,
|
||||
modified_ns: 0,
|
||||
hashes: Default::default(),
|
||||
},
|
||||
resolution: crate::instance::Resolution::Ambiguous {
|
||||
candidates: candidates.clone(),
|
||||
},
|
||||
}],
|
||||
};
|
||||
let manifest_path =
|
||||
crate::storage::InstancePaths::new(ui.instance_path("Conflict")).content_manifest();
|
||||
manifest.save(&manifest_path).unwrap();
|
||||
ui.app.content_manifest = Some(("Conflict".to_owned(), manifest));
|
||||
ui.app.provider_conflict = Some(ProviderConflictState {
|
||||
relative_path: relative_path.clone(),
|
||||
candidates,
|
||||
selected: 0,
|
||||
});
|
||||
|
||||
ui.key(KeyCode::Down);
|
||||
ui.key(KeyCode::Enter);
|
||||
|
||||
assert!(ui.app.provider_conflict.is_none());
|
||||
let saved = crate::instance::ContentManifest::load(&manifest_path).unwrap();
|
||||
assert!(matches!(
|
||||
&saved.record(&relative_path).unwrap().resolution,
|
||||
crate::instance::Resolution::Resolved { project }
|
||||
if project.provider == "curseforge" && project.project_id == "second"
|
||||
));
|
||||
}
|
||||
@@ -4,15 +4,17 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use ratatui::{Terminal, backend::TestBackend};
|
||||
|
||||
use super::UI_TEST_LOCK;
|
||||
use crate::auth::{Account, AccountStore, AccountType};
|
||||
use crate::instance::{InstanceConfig, InstanceManager, ModLoader};
|
||||
use crate::tui::{
|
||||
app::{App, FocusedArea},
|
||||
widgets,
|
||||
};
|
||||
|
||||
pub(super) struct UiHarness {
|
||||
pub(in crate::tui) struct UiHarness {
|
||||
pub app: App,
|
||||
terminal: Terminal<TestBackend>,
|
||||
runtime: tokio::runtime::Runtime,
|
||||
_temp: tempfile::TempDir,
|
||||
_guard: std::sync::MutexGuard<'static, ()>,
|
||||
}
|
||||
@@ -38,9 +40,11 @@ impl UiHarness {
|
||||
let picker = ratatui_image::picker::Picker::halfblocks();
|
||||
let font_size = picker.font_size();
|
||||
let instance_manager = InstanceManager::new(instances_dir, meta_dir.clone());
|
||||
let mut account_state = widgets::account::AccountState::default();
|
||||
account_state.store.accounts.clear();
|
||||
account_state.list_state.selected = None;
|
||||
let account_state = widgets::account::AccountState {
|
||||
store: AccountStore::empty_for_test(temp.path().join("accounts.json")),
|
||||
list_state: Default::default(),
|
||||
add_mode: widgets::account::AddMode::None,
|
||||
};
|
||||
|
||||
let app = App {
|
||||
exit: false,
|
||||
@@ -89,13 +93,17 @@ impl UiHarness {
|
||||
Self {
|
||||
app,
|
||||
terminal: Terminal::new(TestBackend::new(100, 30)).expect("test terminal"),
|
||||
runtime: tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("test runtime"),
|
||||
_temp: temp,
|
||||
_guard: guard,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_instance(&mut self, name: &str) {
|
||||
self.app.instances_state.add_instance(InstanceConfig {
|
||||
let config = InstanceConfig {
|
||||
name: name.to_owned(),
|
||||
game_version: "1.21.1".to_owned(),
|
||||
loader: ModLoader::Fabric,
|
||||
@@ -108,7 +116,30 @@ impl UiHarness {
|
||||
jvm_args: Vec::new(),
|
||||
resolution: None,
|
||||
config_sync_profile: None,
|
||||
};
|
||||
std::fs::create_dir_all(self.instance_path(name)).expect("instance directory");
|
||||
self.app
|
||||
.instance_manager
|
||||
.save(&config)
|
||||
.expect("instance config");
|
||||
self.app.instances_state.add_instance(config);
|
||||
}
|
||||
|
||||
pub fn add_account(&mut self, username: &str) {
|
||||
self.app.account_state.store.accounts.push(Account {
|
||||
uuid: username.to_owned(),
|
||||
username: username.to_owned(),
|
||||
account_type: AccountType::Microsoft,
|
||||
active: true,
|
||||
refresh_token: Some("refresh".to_owned()),
|
||||
cached_mc_token: None,
|
||||
cached_mc_token_expires_at: None,
|
||||
});
|
||||
self.app.account_state.list_state.selected = Some(0);
|
||||
}
|
||||
|
||||
pub fn instance_path(&self, name: &str) -> std::path::PathBuf {
|
||||
self.app.instance_manager.instances_dir.join(name)
|
||||
}
|
||||
|
||||
pub fn key(&mut self, code: KeyCode) {
|
||||
@@ -116,12 +147,14 @@ impl UiHarness {
|
||||
}
|
||||
|
||||
pub fn key_with(&mut self, code: KeyCode, modifiers: KeyModifiers) {
|
||||
let _runtime = self.runtime.enter();
|
||||
self.app
|
||||
.handle_key_event(KeyEvent::new(code, modifiers))
|
||||
.expect("handle key");
|
||||
}
|
||||
|
||||
pub fn draw(&mut self) {
|
||||
let _runtime = self.runtime.enter();
|
||||
let app = &mut self.app;
|
||||
self.terminal
|
||||
.draw(|frame| app.render_frame(frame))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
mod flows;
|
||||
mod harness;
|
||||
pub(super) mod harness;
|
||||
mod snapshots;
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
@@ -6,3 +6,18 @@ fn empty_app_renders_the_complete_frame() {
|
||||
ui.draw();
|
||||
insta::assert_snapshot!(ui.screen());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn active_progress_is_visible_in_the_complete_frame() {
|
||||
let mut ui = UiHarness::new();
|
||||
crate::tui::progress::set_action("Downloading test data");
|
||||
crate::tui::progress::set_sub_action("one.jar");
|
||||
crate::tui::progress::set_progress(1, 2);
|
||||
|
||||
ui.draw();
|
||||
|
||||
let screen = ui.screen();
|
||||
assert!(screen.contains("Downloading test data"));
|
||||
assert!(screen.contains("one.jar"));
|
||||
crate::tui::progress::clear();
|
||||
}
|
||||
@@ -8,8 +8,12 @@ fn key(code: KeyCode) -> KeyEvent {
|
||||
|
||||
#[test]
|
||||
fn offline_account_requires_a_microsoft_account_and_can_be_dismissed() {
|
||||
let mut state = AccountState::default();
|
||||
state.store.accounts.clear();
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let mut state = AccountState {
|
||||
store: AccountStore::empty_for_test(temp.path().join("accounts.json")),
|
||||
list_state: Default::default(),
|
||||
add_mode: AddMode::None,
|
||||
};
|
||||
|
||||
assert!(handle_key(&key(KeyCode::Char('a')), &mut state));
|
||||
assert!(matches!(state.add_mode, AddMode::ChooseType));
|
||||
|
||||
Reference in new issue
Block a user