Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
264 changes: 203 additions & 61 deletions crates/coldvox-text-injection/src/enigo_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 35 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs
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)
Comment on lines +35 to +42
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ WARNING: Detailed error strings

New error mapping includes underlying backend error details in InjectionError::MethodFailed(format!(...{}...)), which may be user-visible depending on how InjectionError is 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.

.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();

Check warning on line 60 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs

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;

Expand All @@ -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

Check warning on line 87 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs
.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

Check warning on line 96 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs
.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)

Check warning on line 109 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs
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;

Expand Down Expand Up @@ -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() {

Check warning on line 226 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs
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());

Check warning on line 249 in crates/coldvox-text-injection/src/enigo_injector.rs

View workflow job for this annotation

GitHub Actions / Unit Tests & Golden Master (Hosted) (stable)

Diff in /home/runner/work/ColdVox/ColdVox/crates/coldvox-text-injection/src/enigo_injector.rs
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
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test compares injector.config with config, but InjectionConfig does not derive PartialEq. This comparison will fail to compile. Consider removing this assertion or deriving PartialEq for InjectionConfig.

Suggested change
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);

Copilot uses AI. Check for mistakes.
}

#[tokio::test]
async fn test_inject_text_empty() {
let config = InjectionConfig::default();
let injector = EnigoInjector::new(config);
let result = injector.inject_text("", None).await;
assert!(result.is_ok());
}
}
Loading
Loading