-
Notifications
You must be signed in to change notification settings - Fork 0
feat(text-injection): add unit tests for enigo and kdotool injectors #312
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
Open
Coldaine
wants to merge
1
commit into
main
Choose a base branch
from
integrate/268
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,37 +30,39 @@ | |||||||||||
| Enigo::new(&Settings::default()).is_ok() | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Inner logic for typing text, generic over the Keyboard trait for testing. | ||||||||||||
| fn type_text_logic<K: Keyboard>(enigo: &mut K, text: &str) -> Result<(), InjectionError> { | ||||||||||||
| // Type each character with a small delay | ||||||||||||
| for c in text.chars() { | ||||||||||||
| match c { | ||||||||||||
| ' ' => enigo | ||||||||||||
| .key(Key::Space, Direction::Click) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to type space: {}", e)))?, | ||||||||||||
| '\n' => enigo | ||||||||||||
| .key(Key::Return, Direction::Click) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to type enter: {}", e)))?, | ||||||||||||
| '\t' => enigo | ||||||||||||
| .key(Key::Tab, Direction::Click) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to type tab: {}", e)))?, | ||||||||||||
| _ => { | ||||||||||||
| // Use text method for all other characters | ||||||||||||
| enigo | ||||||||||||
| .text(&c.to_string()) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to type text: {}", e)))?; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Type text using enigo | ||||||||||||
| async fn type_text(&self, text: &str) -> Result<(), InjectionError> { | ||||||||||||
| let text_clone = text.to_string(); | ||||||||||||
|
|
||||||||||||
| let result = tokio::task::spawn_blocking(move || { | ||||||||||||
| let mut enigo = Enigo::new(&Settings::default()).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to create Enigo: {}", e)) | ||||||||||||
| })?; | ||||||||||||
|
|
||||||||||||
| // Type each character with a small delay | ||||||||||||
| for c in text_clone.chars() { | ||||||||||||
| match c { | ||||||||||||
| ' ' => enigo.key(Key::Space, Direction::Click).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to type space: {}", e)) | ||||||||||||
| })?, | ||||||||||||
| '\n' => enigo.key(Key::Return, Direction::Click).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to type enter: {}", e)) | ||||||||||||
| })?, | ||||||||||||
| '\t' => enigo.key(Key::Tab, Direction::Click).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to type tab: {}", e)) | ||||||||||||
| })?, | ||||||||||||
| _ => { | ||||||||||||
| // Use text method for all other characters | ||||||||||||
| enigo.text(&c.to_string()).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to type text: {}", e)) | ||||||||||||
| })?; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Ok(()) | ||||||||||||
| let mut enigo = Enigo::new(&Settings::default()) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to create Enigo: {}", e)))?; | ||||||||||||
| Self::type_text_logic(&mut enigo, &text_clone) | ||||||||||||
| }) | ||||||||||||
| .await; | ||||||||||||
|
|
||||||||||||
|
|
@@ -74,44 +76,42 @@ | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Inner logic for triggering paste, generic over the Keyboard trait for testing. | ||||||||||||
| fn trigger_paste_logic<K: Keyboard>(enigo: &mut K) -> Result<(), InjectionError> { | ||||||||||||
| // Press platform-appropriate paste shortcut | ||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||
| { | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Meta, Direction::Press) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to press Cmd: {}", e)))?; | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Unicode('v'), Direction::Click) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to type 'v': {}", e)))?; | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Meta, Direction::Release) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to release Cmd: {}", e)))?; | ||||||||||||
| } | ||||||||||||
| #[cfg(not(target_os = "macos"))] | ||||||||||||
| { | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Control, Direction::Press) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to press Ctrl: {}", e)))?; | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Unicode('v'), Direction::Click) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to type 'v': {}", e)))?; | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Control, Direction::Release) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to release Ctrl: {}", e)))?; | ||||||||||||
| } | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Trigger paste action using enigo (Ctrl+V) | ||||||||||||
| async fn trigger_paste(&self) -> Result<(), InjectionError> { | ||||||||||||
| let result = tokio::task::spawn_blocking(|| { | ||||||||||||
| let mut enigo = Enigo::new(&Settings::default()).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to create Enigo: {}", e)) | ||||||||||||
| })?; | ||||||||||||
|
|
||||||||||||
| // Press platform-appropriate paste shortcut | ||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||
| { | ||||||||||||
| enigo.key(Key::Meta, Direction::Press).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to press Cmd: {}", e)) | ||||||||||||
| })?; | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Unicode('v'), Direction::Click) | ||||||||||||
| .map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to type 'v': {}", e)) | ||||||||||||
| })?; | ||||||||||||
| enigo.key(Key::Meta, Direction::Release).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to release Cmd: {}", e)) | ||||||||||||
| })?; | ||||||||||||
| } | ||||||||||||
| #[cfg(not(target_os = "macos"))] | ||||||||||||
| { | ||||||||||||
| enigo.key(Key::Control, Direction::Press).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to press Ctrl: {}", e)) | ||||||||||||
| })?; | ||||||||||||
| enigo | ||||||||||||
| .key(Key::Unicode('v'), Direction::Click) | ||||||||||||
| .map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to type 'v': {}", e)) | ||||||||||||
| })?; | ||||||||||||
| enigo.key(Key::Control, Direction::Release).map_err(|e| { | ||||||||||||
| InjectionError::MethodFailed(format!("Failed to release Ctrl: {}", e)) | ||||||||||||
| })?; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Ok(()) | ||||||||||||
| let mut enigo = Enigo::new(&Settings::default()) | ||||||||||||
| .map_err(|e| InjectionError::MethodFailed(format!("Failed to create Enigo: {}", e)))?; | ||||||||||||
| Self::trigger_paste_logic(&mut enigo) | ||||||||||||
| }) | ||||||||||||
| .await; | ||||||||||||
|
|
||||||||||||
|
|
@@ -179,3 +179,145 @@ | |||||||||||
| ] | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[cfg(test)] | ||||||||||||
| mod tests { | ||||||||||||
| use super::*; | ||||||||||||
| use crate::types::InjectionConfig; | ||||||||||||
| use std::cell::RefCell; | ||||||||||||
| use std::collections::VecDeque; | ||||||||||||
|
|
||||||||||||
| // A more robust MockEnigo that can simulate failures and captures actions. | ||||||||||||
| struct MockEnigo { | ||||||||||||
| actions: RefCell<Vec<String>>, | ||||||||||||
| failures: RefCell<VecDeque<bool>>, // A queue of whether the next action should fail. | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl MockEnigo { | ||||||||||||
| fn new() -> Self { | ||||||||||||
| Self { | ||||||||||||
| actions: RefCell::new(Vec::new()), | ||||||||||||
| failures: RefCell::new(VecDeque::new()), | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn should_fail(&self) -> bool { | ||||||||||||
| self.failures.borrow_mut().pop_front().unwrap_or(false) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[allow(dead_code)] | ||||||||||||
| fn push_failure(&self, fail: bool) { | ||||||||||||
| self.failures.borrow_mut().push_back(fail); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl Keyboard for MockEnigo { | ||||||||||||
| fn key(&mut self, key: Key, direction: Direction) -> Result<(), enigo::Error> { | ||||||||||||
| if self.should_fail() { | ||||||||||||
| return Err(enigo::Error::InvalidKey); | ||||||||||||
| } | ||||||||||||
| self.actions | ||||||||||||
| .borrow_mut() | ||||||||||||
| .push(format!("key({:?},{:?})", key, direction)); | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn text(&mut self, text: &str) -> Result<(), enigo::Error> { | ||||||||||||
| if self.should_fail() { | ||||||||||||
| return Err(enigo::Error::InvalidText); | ||||||||||||
| } | ||||||||||||
| self.actions.borrow_mut().push(format!("text(\"{}\")", text)); | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| fn test_type_text_logic_simple() { | ||||||||||||
| let mut mock_enigo = MockEnigo::new(); | ||||||||||||
| let result = EnigoInjector::type_text_logic(&mut mock_enigo, "abc"); | ||||||||||||
| assert!(result.is_ok()); | ||||||||||||
| assert_eq!( | ||||||||||||
| *mock_enigo.actions.borrow(), | ||||||||||||
| vec!["text(\"a\")", "text(\"b\")", "text(\"c\")"] | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| fn test_type_text_logic_special_chars() { | ||||||||||||
| let mut mock_enigo = MockEnigo::new(); | ||||||||||||
| let result = EnigoInjector::type_text_logic(&mut mock_enigo, " \n\t"); | ||||||||||||
| assert!(result.is_ok()); | ||||||||||||
| assert_eq!( | ||||||||||||
| *mock_enigo.actions.borrow(), | ||||||||||||
| vec![ | ||||||||||||
| "key(Space,Click)", | ||||||||||||
| "key(Return,Click)", | ||||||||||||
| "key(Tab,Click)" | ||||||||||||
| ] | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| fn test_type_text_logic_failure() { | ||||||||||||
| let mut mock_enigo = MockEnigo::new(); | ||||||||||||
| mock_enigo.push_failure(true); // First action will fail | ||||||||||||
| let result = EnigoInjector::type_text_logic(&mut mock_enigo, "a"); | ||||||||||||
| assert!(result.is_err()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| #[cfg(not(target_os = "macos"))] | ||||||||||||
| fn test_trigger_paste_logic_non_macos() { | ||||||||||||
| let mut mock_enigo = MockEnigo::new(); | ||||||||||||
| let result = EnigoInjector::trigger_paste_logic(&mut mock_enigo); | ||||||||||||
| assert!(result.is_ok()); | ||||||||||||
| assert_eq!( | ||||||||||||
| *mock_enigo.actions.borrow(), | ||||||||||||
| vec![ | ||||||||||||
| "key(Control,Press)", | ||||||||||||
| "key(Unicode('v'),Click)", | ||||||||||||
| "key(Control,Release)" | ||||||||||||
| ] | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| #[cfg(target_os = "macos")] | ||||||||||||
| fn test_trigger_paste_logic_macos() { | ||||||||||||
| let mut mock_enigo = MockEnigo::new(); | ||||||||||||
| let result = EnigoInjector::trigger_paste_logic(&mut mock_enigo); | ||||||||||||
| assert!(result.is_ok()); | ||||||||||||
| assert_eq!( | ||||||||||||
| *mock_enigo.actions.borrow(), | ||||||||||||
| vec![ | ||||||||||||
| "key(Meta,Press)", | ||||||||||||
| "key(Unicode('v'),Click)", | ||||||||||||
| "key(Meta,Release)" | ||||||||||||
| ] | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| fn test_trigger_paste_logic_failure() { | ||||||||||||
| let mut mock_enigo = MockEnigo::new(); | ||||||||||||
| mock_enigo.push_failure(true); | ||||||||||||
| let result = EnigoInjector::trigger_paste_logic(&mut mock_enigo); | ||||||||||||
| assert!(result.is_err()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // The async tests can remain as integration tests, but we'll keep them simple. | ||||||||||||
| #[tokio::test] | ||||||||||||
| async fn test_enigo_injector_new() { | ||||||||||||
| let config = InjectionConfig::default(); | ||||||||||||
| let injector = EnigoInjector::new(config); | ||||||||||||
| assert_eq!(injector.config, config); | ||||||||||||
|
Comment on lines
+312
to
+313
|
||||||||||||
| let injector = EnigoInjector::new(config); | |
| assert_eq!(injector.config, config); | |
| let expected_allow_enigo = config.allow_enigo; | |
| let injector = EnigoInjector::new(config); | |
| assert_eq!(injector.config.allow_enigo, expected_allow_enigo); |
Oops, something went wrong.
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.
New error mapping includes underlying backend error details in
InjectionError::MethodFailed(format!(...{}...)), which may be user-visible depending on howInjectionErroris surfaced.Consider whether exposing detailed system error messages to end users is appropriate from a security standpoint. You might want to log the detailed error internally and return a more generic message to users.