|
| 1 | +//! Test implementation of UserInterface for testing |
| 2 | +
|
| 3 | +use anyhow::Result; |
| 4 | +use git_workers::ui::UserInterface; |
| 5 | +use std::sync::{Arc, Mutex}; |
| 6 | + |
| 7 | +/// Test implementation of UserInterface that provides pre-programmed responses |
| 8 | +#[derive(Clone)] |
| 9 | +pub struct TestUI { |
| 10 | + inputs: Arc<Mutex<Vec<String>>>, |
| 11 | + selections: Arc<Mutex<Vec<usize>>>, |
| 12 | + confirmations: Arc<Mutex<Vec<bool>>>, |
| 13 | + expect_error: Arc<Mutex<bool>>, |
| 14 | +} |
| 15 | + |
| 16 | +impl TestUI { |
| 17 | + /// Create a new TestUI with no pre-programmed responses |
| 18 | + pub fn new() -> Self { |
| 19 | + Self { |
| 20 | + inputs: Arc::new(Mutex::new(Vec::new())), |
| 21 | + selections: Arc::new(Mutex::new(Vec::new())), |
| 22 | + confirmations: Arc::new(Mutex::new(Vec::new())), |
| 23 | + expect_error: Arc::new(Mutex::new(false)), |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /// Add an input response |
| 28 | + pub fn with_input(self, input: &str) -> Self { |
| 29 | + self.inputs.lock().unwrap().push(input.to_string()); |
| 30 | + self |
| 31 | + } |
| 32 | + |
| 33 | + /// Add a selection response |
| 34 | + pub fn with_selection(self, selection: usize) -> Self { |
| 35 | + self.selections.lock().unwrap().push(selection); |
| 36 | + self |
| 37 | + } |
| 38 | + |
| 39 | + /// Add a confirmation response |
| 40 | + pub fn with_confirmation(self, confirm: bool) -> Self { |
| 41 | + self.confirmations.lock().unwrap().push(confirm); |
| 42 | + self |
| 43 | + } |
| 44 | + |
| 45 | + /// Indicate that the next operation should simulate an error/cancellation |
| 46 | + pub fn with_error(self) -> Self { |
| 47 | + *self.expect_error.lock().unwrap() = true; |
| 48 | + self |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl UserInterface for TestUI { |
| 53 | + fn multiselect(&self, _prompt: &str, items: &[String]) -> Result<Vec<usize>> { |
| 54 | + // For tests, we don't support multiselect - just return empty selection |
| 55 | + let _ = items; |
| 56 | + Ok(vec![]) |
| 57 | + } |
| 58 | + fn input(&self, _prompt: &str) -> Result<String> { |
| 59 | + let mut inputs = self.inputs.lock().unwrap(); |
| 60 | + if *self.expect_error.lock().unwrap() { |
| 61 | + *self.expect_error.lock().unwrap() = false; |
| 62 | + return Err(anyhow::anyhow!("User cancelled input")); |
| 63 | + } |
| 64 | + if inputs.is_empty() { |
| 65 | + return Err(anyhow::anyhow!("No more test inputs")); |
| 66 | + } |
| 67 | + Ok(inputs.remove(0)) |
| 68 | + } |
| 69 | + |
| 70 | + fn input_with_default(&self, _prompt: &str, default: &str) -> Result<String> { |
| 71 | + let mut inputs = self.inputs.lock().unwrap(); |
| 72 | + if *self.expect_error.lock().unwrap() { |
| 73 | + *self.expect_error.lock().unwrap() = false; |
| 74 | + return Err(anyhow::anyhow!("User cancelled input")); |
| 75 | + } |
| 76 | + if inputs.is_empty() { |
| 77 | + Ok(default.to_string()) |
| 78 | + } else { |
| 79 | + Ok(inputs.remove(0)) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn select(&self, _prompt: &str, _items: &[String]) -> Result<usize> { |
| 84 | + let mut selections = self.selections.lock().unwrap(); |
| 85 | + if *self.expect_error.lock().unwrap() { |
| 86 | + *self.expect_error.lock().unwrap() = false; |
| 87 | + return Err(anyhow::anyhow!("User cancelled selection")); |
| 88 | + } |
| 89 | + if selections.is_empty() { |
| 90 | + return Err(anyhow::anyhow!("No more test selections")); |
| 91 | + } |
| 92 | + Ok(selections.remove(0)) |
| 93 | + } |
| 94 | + |
| 95 | + fn select_with_default( |
| 96 | + &self, |
| 97 | + _prompt: &str, |
| 98 | + _items: &[String], |
| 99 | + default: usize, |
| 100 | + ) -> Result<usize> { |
| 101 | + let mut selections = self.selections.lock().unwrap(); |
| 102 | + if *self.expect_error.lock().unwrap() { |
| 103 | + *self.expect_error.lock().unwrap() = false; |
| 104 | + return Err(anyhow::anyhow!("User cancelled selection")); |
| 105 | + } |
| 106 | + if selections.is_empty() { |
| 107 | + Ok(default) |
| 108 | + } else { |
| 109 | + Ok(selections.remove(0)) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + fn fuzzy_select(&self, _prompt: &str, items: &[String]) -> Result<usize> { |
| 114 | + // For tests, fuzzy select behaves like regular select |
| 115 | + self.select(_prompt, items) |
| 116 | + } |
| 117 | + |
| 118 | + fn confirm(&self, _prompt: &str) -> Result<bool> { |
| 119 | + let mut confirmations = self.confirmations.lock().unwrap(); |
| 120 | + if *self.expect_error.lock().unwrap() { |
| 121 | + *self.expect_error.lock().unwrap() = false; |
| 122 | + return Err(anyhow::anyhow!("User cancelled confirmation")); |
| 123 | + } |
| 124 | + if confirmations.is_empty() { |
| 125 | + return Err(anyhow::anyhow!("No more test confirmations")); |
| 126 | + } |
| 127 | + Ok(confirmations.remove(0)) |
| 128 | + } |
| 129 | + |
| 130 | + fn confirm_with_default(&self, _prompt: &str, default: bool) -> Result<bool> { |
| 131 | + let mut confirmations = self.confirmations.lock().unwrap(); |
| 132 | + if *self.expect_error.lock().unwrap() { |
| 133 | + *self.expect_error.lock().unwrap() = false; |
| 134 | + return Err(anyhow::anyhow!("User cancelled confirmation")); |
| 135 | + } |
| 136 | + if confirmations.is_empty() { |
| 137 | + Ok(default) |
| 138 | + } else { |
| 139 | + Ok(confirmations.remove(0)) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +impl Default for TestUI { |
| 145 | + fn default() -> Self { |
| 146 | + Self::new() |
| 147 | + } |
| 148 | +} |
0 commit comments