Repository navigation
Introduce Tester and simplify TUI integration tests #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4290c21
tui/tests: extract shared helpers to tests/common.rs and de-duplicate…
panarch 87aabe1
tui/tests: add AppTestExt and input helpers (press/ctrl/key) + draw_f…
panarch e1567dc
tui/tests: remove unused helpers (send_*/ev_*/draw_once), switch to A…
panarch f1c8407
tui: drop test-utils feature; move test-only helpers to tests via App…
panarch 143bece
tests/common: inline open_instant into AppTestExt; remove free helper…
panarch 6be4880
tests/common: split into modules (app_ext, terminal_ext); add Termina…
panarch 405919e
tests/common: split into app_ext and terminal_ext; add TerminalTestEx…
panarch b57b442
tests/common: remove unnecessary String import
panarch 3c890df
tests: introduce Tester struct (App+Terminal) with convenience method…
panarch 14638c8
tests/common: collapse AppTestExt/TerminalTestExt into single Tester;…
panarch 96939fc
tests: rename common.rs to tester.rs; update imports; move snapshot h…
panarch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,5 +28,4 @@ once_cell = "1.19" | |
| [dev-dependencies] | ||
| insta = { version = "1", features = ["glob"] } | ||
|
|
||
| [features] | ||
| test-utils = [] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,144 +1,53 @@ | ||
| #![cfg(feature = "test-utils")] | ||
| mod tester; | ||
| use tester::Tester; | ||
|
|
||
| use color_eyre::Result; | ||
| use ratatui::{ | ||
| Terminal, | ||
| backend::TestBackend, | ||
| crossterm::event::{Event as Input, KeyCode, KeyEvent, KeyModifiers}, | ||
| }; | ||
|
|
||
| use glues::{App, config, logger}; | ||
|
|
||
| fn buffer_to_lines(term: &Terminal<TestBackend>) -> Vec<String> { | ||
| let buf = term.backend().buffer().clone(); | ||
| let area = buf.area(); | ||
| let mut lines = Vec::with_capacity(area.height as usize); | ||
| for y in 0..area.height { | ||
| let mut line = String::new(); | ||
| for x in 0..area.width { | ||
| line.push_str(buf[(x, y)].symbol()); | ||
| } | ||
| lines.push(line); | ||
| } | ||
| lines | ||
| } | ||
|
|
||
| async fn setup() -> Result<(App, Terminal<TestBackend>)> { | ||
| // ensure logger/config have a writable HOME directory | ||
| let cwd = std::env::current_dir()?; | ||
| unsafe { | ||
| std::env::set_var("HOME", &cwd); | ||
| } | ||
| config::init().await; | ||
| logger::init().await; | ||
|
|
||
| let backend = TestBackend::new(120, 40); | ||
| let term = Terminal::new(backend)?; | ||
| let app = App::new(); | ||
|
|
||
| Ok((app, term)) | ||
| } | ||
| use ratatui::crossterm::event::KeyCode; | ||
|
|
||
| #[tokio::test] | ||
| async fn entry_nav_enter_opens_instant_inproc() -> Result<()> { | ||
| let (mut app, mut term) = setup().await?; | ||
| let mut t = Tester::new().await?; | ||
|
|
||
| // initial draw (home) | ||
| app.draw_once_on(&mut term)?; | ||
| t.draw()?; | ||
|
|
||
| // move down then up, then Enter to open Instant via selection | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('j'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('k'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Enter, | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| t.press('j').await; | ||
| t.press('k').await; | ||
| t.key(KeyCode::Enter).await; | ||
|
|
||
| // draw and assert notebook content is visible (e.g., sample note) | ||
| app.draw_once_on(&mut term)?; | ||
| let buf = buffer_to_lines(&term).join("\n"); | ||
| assert!(buf.contains("Sample Note")); | ||
| t.draw()?; | ||
| t.assert_contains("Sample Note"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn entry_quit_with_q_inproc() -> Result<()> { | ||
| let (mut app, mut term) = setup().await?; | ||
| app.draw_once_on(&mut term)?; | ||
| let mut t = Tester::new().await?; | ||
| t.draw()?; | ||
|
|
||
| let quit = app | ||
| .handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('q'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| let quit = t.press('q').await; | ||
| assert!(quit); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn entry_keymap_toggle_inproc() -> Result<()> { | ||
| let (mut app, mut term) = setup().await?; | ||
| app.draw_once_on(&mut term)?; | ||
|
|
||
| // show keymap | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('?'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| app.draw_once_on(&mut term)?; | ||
| let buf = buffer_to_lines(&term).join("\n"); | ||
| assert!(buf.contains(" [?] Hide keymap ")); | ||
|
|
||
| // hide keymap | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('?'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| app.draw_once_on(&mut term)?; | ||
| let buf = buffer_to_lines(&term).join("\n"); | ||
| assert!(!buf.contains(" [?] Hide keymap ")); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn entry_help_overlay_open_close_inproc() -> Result<()> { | ||
| let (mut app, mut term) = setup().await?; | ||
| app.draw_once_on(&mut term)?; | ||
| let mut t = Tester::new().await?; | ||
| t.draw()?; | ||
|
|
||
| // open help (currently bound to 'a') | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('a'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| app.draw_once_on(&mut term)?; | ||
| let buf = buffer_to_lines(&term).join("\n"); | ||
| assert!(buf.contains("Press any key to close")); | ||
| t.press('a').await; | ||
| t.draw()?; | ||
| t.assert_contains("Press any key to close"); | ||
|
|
||
| // any key closes help | ||
| app.handle_input(Input::Key(KeyEvent::new( | ||
| KeyCode::Char('x'), | ||
| KeyModifiers::NONE, | ||
| ))) | ||
| .await; | ||
| app.draw_once_on(&mut term)?; | ||
| let buf = buffer_to_lines(&term).join("\n"); | ||
| assert!(!buf.contains("Press any key to close")); | ||
| t.press('x').await; | ||
| t.draw()?; | ||
| t.assert_not_contains("Press any key to close"); | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Tester harness: avoid
unsafeHOME mutation; prefer temp HOME per test.In
tester.rs,std::env::set_varisn’t unsafe—drop theunsafeblock and consider a temp dir to isolate runs.Example (tester.rs):
🤖 Prompt for AI Agents