fix(tui): harden rendering and centralize tests

This commit is contained in:
objz committed 2026-07-27 01:09:33 +02:00
1 parent 9b17ec96c5
commit 64bb5e2d16
66 files changed
+271 -48

No files matched your search

+1 -1
View File
@@ -165,7 +165,7 @@ pub fn confirm_popup_area(frame_area: Rect, target: &ConfirmTarget) -> Rect {
use super::word_wrap_size;
use ratatui::layout::Constraint;
const MAX_W: usize = 48;
let title_w = target.name().len() + 12;
let title_w = Span::raw(target.name()).width() + 12;
let (body_w, _) = word_wrap_size(target.body(), MAX_W);
let inner_w = title_w.max(body_w).min(MAX_W);
let (_, lines) = word_wrap_size(target.body(), inner_w);
+2
View File
@@ -96,7 +96,9 @@ pub fn popup_area(frame_area: Rect, message: &str, base_y: u16, elapsed_ms: u128
}
#[cfg(test)]
#[path = "../../tests/widgets/popups/error/render.rs"]
mod render_tests;
#[cfg(test)]
#[path = "../../tests/widgets/popups/error/area.rs"]
mod area_tests;
@@ -1,41 +0,0 @@
use super::popup_area;
use crate::config::SETTINGS;
use ratatui::layout::Rect;
fn frame() -> Rect {
Rect::new(0, 0, 80, 24)
}
#[test]
fn returns_none_after_dismiss_timeout() {
let past_dismiss = SETTINGS.ui.error_auto_dismiss_ms as u128 + 1;
assert!(popup_area(frame(), "msg", 0, past_dismiss).is_none());
}
#[test]
fn returns_some_inside_dismiss_window() {
assert!(popup_area(frame(), "msg", 0, 0).is_some());
}
#[test]
fn returns_none_when_vertical_room_too_small() {
// base_y = 22 leaves only height 24 - 22 - 1 = 1 row of usable space,
// less than the minimum 3 needed for border + content + border.
assert!(popup_area(frame(), "msg", 22, 0).is_none());
}
#[test]
fn clamps_popup_width_to_frame() {
// a wider-than-the-frame message gets clamped so the popup fits
// inside frame.width minus the right-edge padding (saturating_sub(4)).
let huge = "x".repeat(200);
let area = popup_area(frame(), &huge, 0, 0).unwrap();
assert!(area.width <= frame().width.saturating_sub(4));
}
#[test]
fn anchors_popup_to_right_edge() {
let area = popup_area(frame(), "msg", 0, 0).unwrap();
// popup_w is added to base_x to reach frame.width - 2 (right-edge gutter)
assert_eq!(area.x + area.width + 2, frame().width);
}
@@ -1,62 +0,0 @@
use super::*;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use std::time::Instant;
use tracing::Level;
use crate::tui::error_buffer::ErrorEvent;
fn render(event: ErrorEvent, width: u16, height: u16) -> Terminal<TestBackend> {
let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| {
let popup = ErrorPopup::new(event);
f.render_widget(popup, f.area());
})
.unwrap();
terminal
}
fn event(level: Level, message: &str) -> ErrorEvent {
ErrorEvent {
id: 1,
level,
message: message.to_string(),
pushed_at: Instant::now(),
}
}
#[test]
fn warn_level_renders() {
let term = render(event(Level::WARN, "Disk space low"), 40, 5);
insta::assert_snapshot!(term.backend());
}
#[test]
fn error_level_renders() {
let term = render(event(Level::ERROR, "Connection refused"), 40, 5);
insta::assert_snapshot!(term.backend());
}
// info-level events hit the catch-all `_` arm in the label match; previously
// there was no test covering it.
#[test]
fn info_level_renders() {
let term = render(event(Level::INFO, "Reloaded config"), 40, 5);
insta::assert_snapshot!(term.backend());
}
#[test]
fn long_message_wraps() {
let msg = "The Minecraft launcher could not reach the Mojang version manifest \
after three retries. Check your network connection or proxy settings.";
let term = render(event(Level::ERROR, msg), 40, 10);
insta::assert_snapshot!(term.backend());
}
#[test]
fn narrow_frame_renders() {
let term = render(event(Level::WARN, "Short message"), 18, 5);
insta::assert_snapshot!(term.backend());
}
@@ -1,9 +0,0 @@
---
source: src/tui/widgets/popups/error/render_tests.rs
expression: term.backend()
---
"╭ ERROR ───────────────────────────────╮"
"│Connection refused │"
"│ │"
"│ │"
"╰──────────────────────────────────────╯"
@@ -1,9 +0,0 @@
---
source: src/tui/widgets/popups/error/render_tests.rs
expression: term.backend()
---
"╭ INFO ────────────────────────────────╮"
"│Reloaded config │"
"│ │"
"│ │"
"╰──────────────────────────────────────╯"
@@ -1,14 +0,0 @@
---
source: src/tui/widgets/popups/error/render_tests.rs
expression: term.backend()
---
"╭ ERROR ───────────────────────────────╮"
"│The Minecraft launcher could not reach│"
"│the Mojang version manifest after │"
"│three retries. Check your network │"
"│connection or proxy settings. │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────────╯"
@@ -1,9 +0,0 @@
---
source: src/tui/widgets/popups/error/render_tests.rs
expression: term.backend()
---
"╭ WARN ──────────╮"
"│Short message │"
"│ │"
"│ │"
"╰────────────────╯"
@@ -1,9 +0,0 @@
---
source: src/tui/widgets/popups/error/render_tests.rs
expression: term.backend()
---
"╭ WARN ────────────────────────────────╮"
"│Disk space low │"
"│ │"
"│ │"
"╰──────────────────────────────────────╯"
@@ -3,3 +3,12 @@ mod state;
pub use render::{popup_rect, render};
pub use state::{ImportResult, ImportStep, ImportWizardState, handle_key, take_result};
#[cfg(test)]
pub(crate) fn reset_for_test() {
state::IMPORT_STATE
.lock()
.expect("import wizard state")
.reset();
*state::IMPORT_RESULT.lock().expect("import wizard result") = None;
}
@@ -240,4 +240,5 @@ fn step_keybinds(state: &ImportWizardState) -> Line<'static> {
}
#[cfg(test)]
#[path = "../../../tests/widgets/popups/import_modpack/render.rs"]
mod tests;
@@ -1,18 +0,0 @@
---
source: src/tui/widgets/popups/import_modpack/render/tests.rs
expression: terminal.backend()
---
"╭Import Modpack────────────────────────────────────────────╮"
"│Name: Synthetic Pack │"
"│Pack Version: 1.0.0 │"
"│MC Version: 1.20.1 │"
"│Loader: Fabric 0.15.0 │"
"│Mods: 42 files │"
"│Overrides: 3 files │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────[h] back [Enter] import╯"
@@ -1,18 +0,0 @@
---
source: src/tui/widgets/popups/import_modpack/render/tests.rs
expression: terminal.backend()
---
"╭Import Modpack────────────────────────────────────────────╮"
"│Name: Vanilla Pack │"
"│Pack Version: 2.0 │"
"│MC Version: 1.20.1 │"
"│Loader: Vanilla │"
"│Mods: 0 files │"
"│Overrides: 12 files │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────[h] back [Enter] import╯"
@@ -1,16 +0,0 @@
---
source: src/tui/widgets/popups/import_modpack/render/tests.rs
expression: terminal.backend()
---
"╭Import Modpack────────────────────────────────────────────╮"
"│Fetching modpack info... │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────────────────[Esc] cancel╯"
@@ -1,16 +0,0 @@
---
source: src/tui/widgets/popups/import_modpack/render/tests.rs
expression: terminal.backend()
---
"╭Import Modpack────────────────────────────────────────────╮"
"│URL, slug, or pack file path...█ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰─────────────────────────────────────────────[Enter] fetch╯"
@@ -1,18 +0,0 @@
---
source: src/tui/widgets/popups/import_modpack/render/tests.rs
expression: terminal.backend()
---
"╭Import Modpack────────────────────────────────────────────╮"
"│▶ 1.0.0 1.20.1 fabric │"
"│ 0.9.0 1.20.1 fabric │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────[/] search [h] back [Enter] select╯"
@@ -1,152 +0,0 @@
use super::*;
use crate::tui::tests::UI_TEST_LOCK;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
// serialise against parallel tests of the same global IMPORT_STATE.
fn reset_import_state(step: ImportStep) {
let mut guard = IMPORT_STATE.lock().expect("IMPORT_STATE lock");
*guard = ImportWizardState::default();
guard.step = step;
}
#[test]
fn import_modpack_renders_input_step() {
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset_import_state(ImportStep::Input);
let backend = TestBackend::new(60, 12);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::ImportPopup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
#[test]
fn import_modpack_renders_fetching_step() {
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
reset_import_state(ImportStep::Fetching);
let backend = TestBackend::new(60, 12);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::ImportPopup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
// Version step: pre-populate versions as LoadState::Loaded with synthetic
// VersionInfo entries so render walks the list path without triggering
// any network helpers.
#[test]
fn import_modpack_renders_version_step() {
use crate::net::modrinth::VersionInfo;
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
{
let mut guard = IMPORT_STATE.lock().expect("IMPORT_STATE lock");
*guard = ImportWizardState::default();
guard.step = ImportStep::Version;
guard.project_title = Some("Synthetic Pack".into());
guard.versions = LoadState::Loaded(vec![
VersionInfo {
id: "v1".into(),
project_id: "project".into(),
name: "1.0.0".into(),
version_number: "1.0.0".into(),
game_versions: vec!["1.20.1".into()],
loaders: vec!["fabric".into()],
date_published: String::new(),
files: vec![],
},
VersionInfo {
id: "v2".into(),
project_id: "project".into(),
name: "0.9.0".into(),
version_number: "0.9.0".into(),
game_versions: vec!["1.20.1".into()],
loaders: vec!["fabric".into()],
date_published: String::new(),
files: vec![],
},
]);
}
let backend = TestBackend::new(60, 14);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::ImportPopup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
// Confirm step: needs a populated ImportSummary so the render path
// doesn't bail. ImportSummary is constructed manually with synthetic
// values; archive_path is a fake tempdir-ish path that never gets read.
#[test]
fn import_modpack_renders_confirm_step() {
use crate::instance::import::{ImportSummary, PackFormat};
use crate::instance::models::ModLoader;
use std::path::PathBuf;
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
{
let mut guard = IMPORT_STATE.lock().expect("IMPORT_STATE lock");
*guard = ImportWizardState::default();
guard.step = ImportStep::Confirm;
guard.summary = Some(ImportSummary {
name: "Synthetic Pack".into(),
pack_version: "1.0.0".into(),
game_version: "1.20.1".into(),
loader: ModLoader::Fabric,
loader_version: Some("0.15.0".into()),
mod_count: 42,
override_count: 3,
format: PackFormat::Mrpack,
archive_path: PathBuf::from("/tmp/synthetic.mrpack"),
});
}
let backend = TestBackend::new(60, 14);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::ImportPopup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
// Confirm step with loader_version=None: covers the branch where the
// pack didn't declare a loader version (rare upstream, but happens for
// older mmc packs). render_confirm_step has to handle the Option.
#[test]
fn import_modpack_renders_confirm_step_without_loader_version() {
use crate::instance::import::{ImportSummary, PackFormat};
use crate::instance::models::ModLoader;
use std::path::PathBuf;
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
{
let mut guard = IMPORT_STATE.lock().expect("IMPORT_STATE lock");
*guard = ImportWizardState::default();
guard.step = ImportStep::Confirm;
guard.summary = Some(ImportSummary {
name: "Vanilla Pack".into(),
pack_version: "2.0".into(),
game_version: "1.20.1".into(),
loader: ModLoader::Vanilla,
loader_version: None,
mod_count: 0,
override_count: 12,
format: PackFormat::Mmc,
archive_path: PathBuf::from("/tmp/vanilla.zip"),
});
}
let backend = TestBackend::new(60, 14);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::ImportPopup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
@@ -467,4 +467,5 @@ fn clamp_version_index(state: &mut ImportWizardState) {
}
#[cfg(test)]
#[path = "../../../tests/widgets/popups/import_modpack/state.rs"]
mod tests;
@@ -1,71 +0,0 @@
use std::path::PathBuf;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use super::*;
use crate::instance::{
ModLoader,
import::{ImportSummary, PackFormat},
};
use crate::tui::tests::UI_TEST_LOCK;
fn key(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
}
fn summary() -> ImportSummary {
ImportSummary {
name: "Test Pack".to_owned(),
pack_version: "1.0.0".to_owned(),
game_version: "1.21.1".to_owned(),
loader: ModLoader::Fabric,
loader_version: Some("0.16.14".to_owned()),
mod_count: 2,
override_count: 1,
format: PackFormat::Mrpack,
archive_path: PathBuf::from("test.mrpack"),
}
}
#[test]
fn empty_input_is_ignored_and_escape_closes_the_popup() {
let _guard = UI_TEST_LOCK
.lock()
.unwrap_or_else(|error| error.into_inner());
let mut instances = instances::State {
show_import_popup: true,
..instances::State::default()
};
*IMPORT_STATE.lock().unwrap() = ImportWizardState::default();
handle_key(&key(KeyCode::Enter), &mut instances);
assert_eq!(IMPORT_STATE.lock().unwrap().step, ImportStep::Input);
handle_key(&key(KeyCode::Esc), &mut instances);
assert!(!instances.show_import_popup);
}
#[test]
fn confirm_returns_the_import_summary() {
let _guard = UI_TEST_LOCK
.lock()
.unwrap_or_else(|error| error.into_inner());
let mut instances = instances::State {
show_import_popup: true,
..instances::State::default()
};
{
let mut state = IMPORT_STATE.lock().unwrap();
*state = ImportWizardState {
step: ImportStep::Confirm,
summary: Some(summary()),
..ImportWizardState::default()
};
}
*IMPORT_RESULT.lock().unwrap() = None;
handle_key(&key(KeyCode::Enter), &mut instances);
let result = take_result().expect("import result");
assert_eq!(result.summary.name, "Test Pack");
assert!(!instances.show_import_popup);
}
+3 -2
View File
@@ -10,7 +10,7 @@ pub mod new_instance;
use ratatui::{
Frame,
layout::{Alignment, Constraint, Layout, Rect},
text::Line,
text::{Line, Span},
widgets::Paragraph,
};
@@ -26,7 +26,7 @@ pub fn word_wrap_size(text: &str, max_inner_width: usize) -> (usize, usize) {
let mut widest_line: usize = 0;
for word in text.split_whitespace() {
let word_len = word.len().min(max_inner_width);
let word_len = Span::raw(word).width().min(max_inner_width);
if current_line_len == 0 {
current_line_len = word_len;
} else if current_line_len + 1 + word_len <= max_inner_width {
@@ -153,4 +153,5 @@ pub fn render_keybind_overflow(frame: &mut Frame, area: Rect, lines: &[Line<'sta
}
#[cfg(test)]
#[path = "../../tests/widgets/popups/mod.rs"]
mod tests;
@@ -4,3 +4,9 @@ mod state;
pub(crate) use render::render_select_list;
pub use render::{popup_rect, render};
pub use state::{LoadState, WizardParams, WizardState, WizardStep, handle_key, take_result};
#[cfg(test)]
pub(crate) fn reset_for_test() {
state::WIZARD_STATE.lock().expect("wizard state").reset();
*state::WIZARD_RESULT.lock().expect("wizard result") = None;
}
@@ -320,4 +320,5 @@ fn render_confirm_step(state: &WizardState, area: Rect, buf: &mut ratatui::buffe
}
#[cfg(test)]
#[path = "../../../tests/widgets/popups/new_instance/render.rs"]
mod tests;
@@ -1,16 +0,0 @@
---
source: src/tui/widgets/popups/new_instance/render/tests.rs
expression: terminal.backend()
---
"╭New Instance──────────────────────────────────────────────╮"
"│Name: MyPack │"
"│MC: 1.20.1 │"
"│Loader: Fabric │"
"│Loader version: 0.15.0 │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────[h] back [Enter] create╯"
@@ -1,16 +0,0 @@
---
source: src/tui/widgets/popups/new_instance/render/tests.rs
expression: terminal.backend()
---
"╭New Instance──────────────────────────────────────────────╮"
"│▶ Vanilla │"
"│ Fabric │"
"│ Forge │"
"│ NeoForge │"
"│ Quilt │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────[h] back [Enter] select╯"
@@ -1,18 +0,0 @@
---
source: src/tui/widgets/popups/new_instance/render/tests.rs
expression: terminal.backend()
---
"╭New Instance──────────────────────────────────────────────╮"
"│▶ 47.2.0 │"
"│ 47.1.0 │"
"│ 47.0.50 │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────[h] back [Enter] select╯"
@@ -1,16 +0,0 @@
---
source: src/tui/widgets/popups/new_instance/render/tests.rs
expression: terminal.backend()
---
"╭New Instance──────────────────────────────────────────────╮"
"│Instance name...█ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰──────────────────────────────────────────[Enter] continue╯"
@@ -1,18 +0,0 @@
---
source: src/tui/widgets/popups/new_instance/render/tests.rs
expression: terminal.backend()
---
"╭New Instance──────────────────────────────────────────────╮"
"│▶ 1.20.1 │"
"│ 1.19.4 │"
"│ 1.18.2 │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"│ │"
"╰────────────[/] search [s] snap [h] back [Enter] select╯"
@@ -1,140 +0,0 @@
use super::*;
use crate::tui::tests::UI_TEST_LOCK;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
// WIZARD_STATE is a process-global static; without serialisation, parallel
// tests would race when each test sets the step and then renders, since
// render re-acquires the WIZARD_STATE mutex internally. this guard mutex
// ensures only one wizard snapshot test runs at a time.
fn reset_wizard_state(step: WizardStep) {
let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock");
*guard = WizardState::default();
guard.step = step;
}
#[test]
fn new_instance_renders_name_step() {
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Name is the default step; render touches no network helpers.
reset_wizard_state(WizardStep::Name);
let backend = TestBackend::new(60, 12);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::Popup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
#[test]
fn new_instance_renders_loader_step() {
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Loader step is reached after Name; render just paints the hardcoded
// loader list, no network.
reset_wizard_state(WizardStep::Loader);
let backend = TestBackend::new(60, 12);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::Popup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
// Version step: pre-populate versions as LoadState::Loaded so
// ensure_versions_loaded short-circuits and never spawns a network task.
// the three synthetic versions are marked stable=true so they show with
// show_snapshots=false (the default).
#[test]
fn new_instance_renders_version_step() {
use crate::instance::loader::GameVersion;
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
{
let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock");
*guard = WizardState::default();
guard.step = WizardStep::Version;
guard.versions = LoadState::Loaded(vec![
GameVersion {
id: "1.20.1".into(),
stable: true,
},
GameVersion {
id: "1.19.4".into(),
stable: true,
},
GameVersion {
id: "1.18.2".into(),
stable: true,
},
]);
}
let backend = TestBackend::new(60, 14);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::Popup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
// LoaderVersion step: needs both versions and loader_versions pre-loaded.
// pick a non-Vanilla loader (loader_idx=2 = Forge) so the step doesn't
// skip itself to Confirm.
#[test]
fn new_instance_renders_loader_version_step() {
use crate::instance::loader::GameVersion;
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
{
let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock");
*guard = WizardState::default();
guard.step = WizardStep::LoaderVersion;
guard.loader_idx = 2; // Forge
guard.versions = LoadState::Loaded(vec![GameVersion {
id: "1.20.1".into(),
stable: true,
}]);
guard.loader_versions =
LoadState::Loaded(vec!["47.2.0".into(), "47.1.0".into(), "47.0.50".into()]);
}
let backend = TestBackend::new(60, 14);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::Popup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
// Confirm step: paints a summary, no network, no list. requires
// versions + loader_versions Loaded so selected_*() return Some.
#[test]
fn new_instance_renders_confirm_step() {
use crate::instance::loader::GameVersion;
use tui_prompts::TextState;
let _serial = UI_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
{
let mut guard = WIZARD_STATE.lock().expect("WIZARD_STATE lock");
*guard = WizardState::default();
guard.step = WizardStep::Confirm;
guard.loader_idx = 1; // Fabric
guard.versions = LoadState::Loaded(vec![GameVersion {
id: "1.20.1".into(),
stable: true,
}]);
guard.loader_versions = LoadState::Loaded(vec!["0.15.0".into()]);
// TextState exposes only constructors; rebuilding with the
// desired initial value is the supported path.
guard.name_state = TextState::new().with_value("MyPack");
}
let backend = TestBackend::new(60, 12);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| render(f, f.area(), FocusedArea::Popup))
.unwrap();
insta::assert_snapshot!(terminal.backend());
}
@@ -482,4 +482,5 @@ fn sort_versions_semver(versions: &mut [GameVersion]) {
}
#[cfg(test)]
#[path = "../../../tests/widgets/popups/new_instance/state.rs"]
mod tests;
@@ -1,74 +0,0 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use tui_prompts::TextState;
use super::*;
use crate::tui::tests::UI_TEST_LOCK;
fn key(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
}
#[test]
fn vanilla_wizard_reaches_confirm_and_returns_parameters() {
let _guard = UI_TEST_LOCK
.lock()
.unwrap_or_else(|error| error.into_inner());
let mut instances = instances::State {
show_popup: true,
..instances::State::default()
};
{
let mut state = WIZARD_STATE.lock().unwrap();
*state = WizardState::default();
state.name_state = TextState::new().with_value("Test Instance");
}
*WIZARD_RESULT.lock().unwrap() = None;
handle_key(&key(KeyCode::Enter), &mut instances);
assert_eq!(WIZARD_STATE.lock().unwrap().step, WizardStep::Loader);
{
let mut state = WIZARD_STATE.lock().unwrap();
state.step = WizardStep::Version;
state.versions = LoadState::Loaded(vec![GameVersion {
id: "1.21.1".to_owned(),
stable: true,
}]);
}
handle_key(&key(KeyCode::Enter), &mut instances);
assert_eq!(WIZARD_STATE.lock().unwrap().step, WizardStep::Confirm);
handle_key(&key(KeyCode::Enter), &mut instances);
let result = take_result().expect("wizard result");
assert_eq!(result.name, "Test Instance");
assert_eq!(result.game_version, "1.21.1");
assert_eq!(result.loader, ModLoader::Vanilla);
assert_eq!(result.loader_version, None);
assert!(!instances.show_popup);
}
#[test]
fn version_filter_clamps_a_stale_selection() {
let mut state = WizardState {
versions: LoadState::Loaded(vec![
GameVersion {
id: "1.21.1".to_owned(),
stable: true,
},
GameVersion {
id: "25w01a".to_owned(),
stable: false,
},
]),
version_idx: 9,
..WizardState::default()
};
clamp_version_index(&mut state);
assert_eq!(state.version_idx, 0);
assert_eq!(visible_versions(&state).len(), 1);
state.show_snapshots = true;
assert_eq!(visible_versions(&state).len(), 2);
}
-9
View File
@@ -1,9 +0,0 @@
use super::keybind_lines_wrapped;
#[test]
fn keybind_wrapping_uses_terminal_width_for_unicode_keys() {
let lines = keybind_lines_wrapped(&[("⏎", " select"), ("a", " add")], 19);
assert_eq!(lines.len(), 1);
assert_eq!(lines[0].width(), 19);
}