|
| 1 | +use multi_launcher::actions::Action; |
| 2 | +use multi_launcher::plugin::{Plugin, PluginManager}; |
| 3 | +use multi_launcher::plugins::todo::TodoPlugin; |
| 4 | +use std::sync::{ |
| 5 | + atomic::{AtomicUsize, Ordering}, |
| 6 | + Arc, |
| 7 | +}; |
| 8 | + |
| 9 | +struct CountingPlugin { |
| 10 | + name: &'static str, |
| 11 | + prefixes: &'static [&'static str], |
| 12 | + always_search: bool, |
| 13 | + calls: Arc<AtomicUsize>, |
| 14 | +} |
| 15 | + |
| 16 | +impl CountingPlugin { |
| 17 | + fn new( |
| 18 | + name: &'static str, |
| 19 | + prefixes: &'static [&'static str], |
| 20 | + always_search: bool, |
| 21 | + calls: Arc<AtomicUsize>, |
| 22 | + ) -> Self { |
| 23 | + Self { |
| 24 | + name, |
| 25 | + prefixes, |
| 26 | + always_search, |
| 27 | + calls, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl Plugin for CountingPlugin { |
| 33 | + fn search(&self, query: &str) -> Vec<Action> { |
| 34 | + self.calls.fetch_add(1, Ordering::SeqCst); |
| 35 | + vec![Action { |
| 36 | + label: format!("{}:{query}", self.name), |
| 37 | + desc: "test".into(), |
| 38 | + action: self.name.into(), |
| 39 | + args: None, |
| 40 | + }] |
| 41 | + } |
| 42 | + |
| 43 | + fn name(&self) -> &str { |
| 44 | + self.name |
| 45 | + } |
| 46 | + |
| 47 | + fn description(&self) -> &str { |
| 48 | + "test" |
| 49 | + } |
| 50 | + |
| 51 | + fn capabilities(&self) -> &[&str] { |
| 52 | + &["search"] |
| 53 | + } |
| 54 | + |
| 55 | + fn query_prefixes(&self) -> &[&str] { |
| 56 | + self.prefixes |
| 57 | + } |
| 58 | + |
| 59 | + fn always_search(&self) -> bool { |
| 60 | + self.always_search |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn routing_selects_expected_plugins() { |
| 66 | + let todo_calls = Arc::new(AtomicUsize::new(0)); |
| 67 | + let timer_calls = Arc::new(AtomicUsize::new(0)); |
| 68 | + let global_calls = Arc::new(AtomicUsize::new(0)); |
| 69 | + |
| 70 | + let mut pm = PluginManager::new(); |
| 71 | + pm.register(Box::new(CountingPlugin::new( |
| 72 | + "todo_plugin", |
| 73 | + &["todo"], |
| 74 | + false, |
| 75 | + todo_calls.clone(), |
| 76 | + ))); |
| 77 | + pm.register(Box::new(CountingPlugin::new( |
| 78 | + "timer_plugin", |
| 79 | + &["timer"], |
| 80 | + false, |
| 81 | + timer_calls.clone(), |
| 82 | + ))); |
| 83 | + pm.register(Box::new(CountingPlugin::new( |
| 84 | + "global_plugin", |
| 85 | + &[], |
| 86 | + false, |
| 87 | + global_calls.clone(), |
| 88 | + ))); |
| 89 | + |
| 90 | + let out = pm.search_filtered("todo list", None, None); |
| 91 | + assert_eq!(todo_calls.load(Ordering::SeqCst), 1); |
| 92 | + assert_eq!(timer_calls.load(Ordering::SeqCst), 0); |
| 93 | + assert_eq!(global_calls.load(Ordering::SeqCst), 1); |
| 94 | + assert!(out.iter().any(|a| a.action == "todo_plugin")); |
| 95 | + assert!(out.iter().any(|a| a.action == "global_plugin")); |
| 96 | + assert!(!out.iter().any(|a| a.action == "timer_plugin")); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn global_plugins_and_opt_out_plugins_still_run() { |
| 101 | + let global_calls = Arc::new(AtomicUsize::new(0)); |
| 102 | + let opt_out_calls = Arc::new(AtomicUsize::new(0)); |
| 103 | + let prefixed_calls = Arc::new(AtomicUsize::new(0)); |
| 104 | + |
| 105 | + let mut pm = PluginManager::new(); |
| 106 | + pm.register(Box::new(CountingPlugin::new( |
| 107 | + "global", |
| 108 | + &[], |
| 109 | + false, |
| 110 | + global_calls.clone(), |
| 111 | + ))); |
| 112 | + pm.register(Box::new(CountingPlugin::new( |
| 113 | + "always", |
| 114 | + &["timer"], |
| 115 | + true, |
| 116 | + opt_out_calls.clone(), |
| 117 | + ))); |
| 118 | + pm.register(Box::new(CountingPlugin::new( |
| 119 | + "prefixed", |
| 120 | + &["todo"], |
| 121 | + false, |
| 122 | + prefixed_calls.clone(), |
| 123 | + ))); |
| 124 | + |
| 125 | + pm.search_filtered("plain query", None, None); |
| 126 | + assert_eq!(global_calls.load(Ordering::SeqCst), 1); |
| 127 | + assert_eq!(opt_out_calls.load(Ordering::SeqCst), 1); |
| 128 | + assert_eq!(prefixed_calls.load(Ordering::SeqCst), 0); |
| 129 | +} |
| 130 | + |
| 131 | +#[test] |
| 132 | +fn existing_prefix_commands_remain_equivalent() { |
| 133 | + let plugin = TodoPlugin::default(); |
| 134 | + let direct = plugin.search("todo list"); |
| 135 | + |
| 136 | + let mut pm = PluginManager::new(); |
| 137 | + pm.register(Box::new(TodoPlugin::default())); |
| 138 | + let routed = pm.search_filtered("todo list", None, None); |
| 139 | + |
| 140 | + let routed_view: Vec<_> = routed |
| 141 | + .iter() |
| 142 | + .map(|a| { |
| 143 | + ( |
| 144 | + a.label.as_str(), |
| 145 | + a.desc.as_str(), |
| 146 | + a.action.as_str(), |
| 147 | + a.args.as_ref(), |
| 148 | + ) |
| 149 | + }) |
| 150 | + .collect(); |
| 151 | + let direct_view: Vec<_> = direct |
| 152 | + .iter() |
| 153 | + .map(|a| { |
| 154 | + ( |
| 155 | + a.label.as_str(), |
| 156 | + a.desc.as_str(), |
| 157 | + a.action.as_str(), |
| 158 | + a.args.as_ref(), |
| 159 | + ) |
| 160 | + }) |
| 161 | + .collect(); |
| 162 | + |
| 163 | + assert_eq!(routed_view, direct_view); |
| 164 | +} |
0 commit comments