-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ydotool): add comprehensive unit tests for ydotool injector #311
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
3
commits into
main
Choose a base branch
from
integrate/273
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
205 changes: 205 additions & 0 deletions
205
crates/coldvox-text-injection/src/tests/test_ydotool_injector.rs
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 |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| //! Unit tests for ydotool_injector.rs | ||
| use crate::ydotool_injector::{candidate_socket_paths, locate_existing_socket, YdotoolInjector}; | ||
| use crate::types::InjectionConfig; | ||
| use crate::TextInjector; | ||
| use anyhow::Result; | ||
| use serial_test::serial; | ||
| use std::env; | ||
| use std::fs::{self, File}; | ||
| use std::os::unix::fs::PermissionsExt; | ||
| use std::path::{Path, PathBuf}; | ||
| use tempfile::{tempdir, TempDir}; | ||
|
|
||
| /// A test harness to create a controlled environment for ydotool tests. | ||
| struct TestHarness { | ||
| _temp_dir: TempDir, | ||
| bin_dir: PathBuf, | ||
| home_dir: PathBuf, | ||
| runtime_dir: PathBuf, | ||
| original_path: String, | ||
| /// Path to a file that mock binaries can use to report arguments. | ||
| output_file: PathBuf, | ||
| } | ||
|
|
||
| impl TestHarness { | ||
| fn new() -> Result<Self> { | ||
| let temp_dir = tempdir()?; | ||
| let base_path = temp_dir.path(); | ||
|
|
||
| let bin_dir = base_path.join("bin"); | ||
| let home_dir = base_path.join("home"); | ||
| let runtime_dir = base_path.join("run"); | ||
| let uinput_path = base_path.join("uinput"); | ||
| let output_file = base_path.join("output.log"); | ||
|
|
||
| fs::create_dir_all(&bin_dir)?; | ||
| fs::create_dir_all(&home_dir)?; | ||
| fs::create_dir_all(&runtime_dir)?; | ||
| File::create(&uinput_path)?; | ||
| File::create(&output_file)?; | ||
|
|
||
| let original_path = env::var("PATH").unwrap_or_default(); | ||
| let new_path = format!("{}:{}", bin_dir.display(), original_path); | ||
| env::set_var("PATH", new_path); | ||
| env::set_var("HOME", &home_dir); | ||
| env::set_var("XDG_RUNTIME_DIR", &runtime_dir); | ||
| env::set_var("UINPUT_PATH_OVERRIDE", &uinput_path); | ||
|
|
||
| env::remove_var("YDOTOOL_SOCKET"); | ||
| env::remove_var("UID"); | ||
|
|
||
| Ok(Self { | ||
| _temp_dir: temp_dir, | ||
| bin_dir, | ||
| home_dir, | ||
| runtime_dir, | ||
| original_path, | ||
| output_file, | ||
| }) | ||
| } | ||
|
|
||
| /// Creates a mock executable file that echoes a specific path. | ||
| fn create_which_mock(&self, target_binary: &Path) -> Result<()> { | ||
| let content = format!("#!/bin/sh\necho {}", target_binary.display()); | ||
| self.create_mock_binary("which", &content, true)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn create_mock_binary(&self, name: &str, content: &str, executable: bool) -> Result<PathBuf> { | ||
| let path = self.bin_dir.join(name); | ||
| fs::write(&path, content)?; | ||
| if executable { | ||
| fs::set_permissions(&path, fs::Permissions::from_mode(0o755))?; | ||
| } | ||
| Ok(path) | ||
| } | ||
|
|
||
| fn create_mock_socket(&self, path: &Path) -> Result<()> { | ||
| if let Some(parent) = path.parent() { | ||
| fs::create_dir_all(parent)?; | ||
| } | ||
| File::create(path)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Reads the content of the argument log file. | ||
| fn read_output(&self) -> Result<String> { | ||
| Ok(fs::read_to_string(&self.output_file)?) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for TestHarness { | ||
| fn drop(&mut self) { | ||
| env::set_var("PATH", &self.original_path); | ||
| env::remove_var("HOME"); | ||
| env::remove_var("XDG_RUNTIME_DIR"); | ||
| env::remove_var("YDOTOOL_SOCKET"); | ||
| env::remove_var("UID"); | ||
| env::remove_var("UINPUT_PATH_OVERRIDE"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| fn test_candidate_socket_paths_priority() { | ||
| let _harness = TestHarness::new().unwrap(); | ||
| env::set_var("YDOTOOL_SOCKET", "/custom/socket"); | ||
| env::set_var("UID", "1001"); | ||
|
|
||
| let paths = candidate_socket_paths(); | ||
| assert_eq!(paths.len(), 4); | ||
| assert_eq!(paths[0], PathBuf::from("/custom/socket")); | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| fn test_locate_existing_socket_finds_first_available() { | ||
| let harness = TestHarness::new().unwrap(); | ||
| let _ = harness.runtime_dir.join(".ydotool_socket"); | ||
| let expected_socket = harness.home_dir.join(".ydotool").join("socket"); | ||
| harness.create_mock_socket(&expected_socket).unwrap(); | ||
|
|
||
| let located = locate_existing_socket(); | ||
| assert_eq!(located, Some(expected_socket)); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
| async fn test_check_binary_permissions_success() { | ||
| let harness = TestHarness::new().unwrap(); | ||
| let ydotool_path = harness | ||
| .create_mock_binary("ydotool", "#!/bin/sh\nexit 0", true) | ||
| .unwrap(); | ||
| harness.create_which_mock(&ydotool_path).unwrap(); | ||
|
|
||
| let result = YdotoolInjector::check_binary_permissions("ydotool"); | ||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
|
Check warning on line 140 in crates/coldvox-text-injection/src/tests/test_ydotool_injector.rs
|
||
| async fn test_check_ydotool_available_when_binary_and_socket_present() { | ||
| let harness = TestHarness::new().unwrap(); | ||
| let ydotool_path = harness | ||
| .create_mock_binary("ydotool", "", true) | ||
| .unwrap(); | ||
| harness.create_which_mock(&ydotool_path).unwrap(); | ||
| let socket_path = harness.home_dir.join(".ydotool/socket"); | ||
| harness.create_mock_socket(&socket_path).unwrap(); | ||
|
|
||
| let injector = YdotoolInjector::new(InjectionConfig::default()); | ||
| assert!(injector.is_available().await); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
|
Check warning on line 155 in crates/coldvox-text-injection/src/tests/test_ydotool_injector.rs
|
||
| async fn test_inject_text_uses_paste_by_default() { | ||
| let harness = TestHarness::new().unwrap(); | ||
| let ydotool_script = format!( | ||
| "#!/bin/sh\necho \"$@\" > {}", | ||
| harness.output_file.display() | ||
| ); | ||
| let ydotool_path = harness | ||
| .create_mock_binary("ydotool", &ydotool_script, true) | ||
| .unwrap(); | ||
| harness.create_which_mock(&ydotool_path).unwrap(); | ||
| let socket_path = harness.home_dir.join(".ydotool/socket"); | ||
| harness.create_mock_socket(&socket_path).unwrap(); | ||
|
|
||
| let injector = YdotoolInjector::new(InjectionConfig::default()); | ||
| let result = injector.inject_text("hello", None).await; | ||
|
|
||
| assert!(result.is_ok()); | ||
| let output = harness.read_output().unwrap(); | ||
| assert!(output.contains("key ctrl+v")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[serial] | ||
| async fn test_inject_text_falls_back_to_type() { | ||
| let harness = TestHarness::new().unwrap(); | ||
| // This mock fails for 'key' command, but succeeds for 'type' | ||
| let ydotool_script = format!( | ||
| r#"#!/bin/sh | ||
| if [ "$1" = "key" ]; then | ||
| exit 1 | ||
| else | ||
| echo "$@" > {} | ||
| fi | ||
| "#, | ||
| harness.output_file.display() | ||
| ); | ||
| let ydotool_path = harness | ||
| .create_mock_binary("ydotool", &ydotool_script, true) | ||
| .unwrap(); | ||
| harness.create_which_mock(&ydotool_path).unwrap(); | ||
| let socket_path = harness.home_dir.join(".ydotool/socket"); | ||
| harness.create_mock_socket(&socket_path).unwrap(); | ||
|
|
||
| let injector = YdotoolInjector::new(InjectionConfig::default()); | ||
| let result = injector.inject_text("world", None).await; | ||
|
|
||
| assert!(result.is_ok()); | ||
| let output = harness.read_output().unwrap(); | ||
| assert!(output.contains("type --delay 10 world")); | ||
| } | ||
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.
The PathBuf created here is unused. This line appears to be a leftover from development. Either remove it if it's not needed, or create the socket at this path if the intent was to test that the HOME socket takes priority over the runtime directory socket.