|
| 1 | +//! Fresh isolated live editor acceptance. Real runtime/actors, VirtualDecks and |
| 2 | +//! simulated effects only. Neither HID nor native audio/media is opened here. |
| 3 | +use crate::{ |
| 4 | + device_execution::Runtime, |
| 5 | + device_profiles::{Enrollment, Store, random_device_id}, |
| 6 | + pages::Action, |
| 7 | + shared_worker::{Backend, Fence}, |
| 8 | +}; |
| 9 | +use decksmith_core::{ |
| 10 | + InputEvent, RawEvent, |
| 11 | + device_model::{DeviceDescriptor, DeviceModel}, |
| 12 | +}; |
| 13 | +use decksmith_device::{ |
| 14 | + DeckDevice, DeviceError, VirtualDeck, |
| 15 | + registry::{Candidate, Discovery}, |
| 16 | +}; |
| 17 | +use std::{ |
| 18 | + path::Path, |
| 19 | + sync::{ |
| 20 | + Arc, Mutex, |
| 21 | + atomic::{AtomicBool, Ordering}, |
| 22 | + }, |
| 23 | + time::{Duration, Instant}, |
| 24 | +}; |
| 25 | +type Result<T> = std::result::Result<T, &'static str>; |
| 26 | +struct Probe(Arc<Mutex<VirtualDeck>>); |
| 27 | +impl DeckDevice for Probe { |
| 28 | + fn descriptor(&self) -> DeviceDescriptor { |
| 29 | + self.0.lock().unwrap().descriptor() |
| 30 | + } |
| 31 | + fn set_key_image(&mut self, index: u8, rgb: &[u8]) -> std::result::Result<(), DeviceError> { |
| 32 | + self.0.lock().unwrap().set_key_image(index, rgb) |
| 33 | + } |
| 34 | + fn set_touch_image(&mut self, rgb: &[u8]) -> std::result::Result<(), DeviceError> { |
| 35 | + self.0.lock().unwrap().set_touch_image(rgb) |
| 36 | + } |
| 37 | + fn set_brightness(&mut self, value: u8) -> std::result::Result<(), DeviceError> { |
| 38 | + self.0.lock().unwrap().set_brightness(value) |
| 39 | + } |
| 40 | + fn poll_event(&mut self) -> std::result::Result<Option<InputEvent>, DeviceError> { |
| 41 | + self.0.lock().unwrap().poll_event() |
| 42 | + } |
| 43 | +} |
| 44 | +struct Effects(Arc<Mutex<u64>>); |
| 45 | +impl Backend for Effects { |
| 46 | + fn execute(&mut self, action: &Action, fence: &Fence) -> Result<()> { |
| 47 | + if !fence.accepts() { |
| 48 | + return Err("stale_action_scope"); |
| 49 | + } |
| 50 | + if !matches!( |
| 51 | + action, |
| 52 | + Action::AudioAdjust { .. } | Action::AudioMute { .. } |
| 53 | + ) { |
| 54 | + return Err("fixture_action_unsupported"); |
| 55 | + } |
| 56 | + *self.0.lock().unwrap() += 1; |
| 57 | + Ok(()) |
| 58 | + } |
| 59 | +} |
| 60 | +pub fn run(args: &[String]) -> Result<()> { |
| 61 | + let [_, destination, flag, seconds] = args else { |
| 62 | + return Err("connected_editor_lab_arguments"); |
| 63 | + }; |
| 64 | + let seconds = seconds |
| 65 | + .parse::<u64>() |
| 66 | + .map_err(|_| "connected_editor_lab_arguments")?; |
| 67 | + if flag != "--seconds" |
| 68 | + || !(1..=600).contains(&seconds) |
| 69 | + || std::env::var("DECKSMITH_ISOLATED_TEST").as_deref() != Ok("1") |
| 70 | + || !std::env::var("DBUS_SESSION_BUS_ADDRESS") |
| 71 | + .is_ok_and(|a| a.starts_with("unix:path=/tmp/dbus-")) |
| 72 | + { |
| 73 | + return Err("connected_editor_lab_isolated_only"); |
| 74 | + } |
| 75 | + let path = Path::new(destination); |
| 76 | + if !path.is_absolute() { |
| 77 | + return Err("connected_editor_lab_absolute_path_required"); |
| 78 | + } |
| 79 | + let store = Store::create(path)?; |
| 80 | + let discovery = Discovery::new(vec![ |
| 81 | + Candidate::new(0, DeviceModel::Plus, Some("editor-fixture-plus")), |
| 82 | + Candidate::new(1, DeviceModel::Mk2, Some("editor-fixture-mk2")), |
| 83 | + ])?; |
| 84 | + let plus = random_device_id()?; |
| 85 | + let mk2 = random_device_id()?; |
| 86 | + let generation = store.enroll( |
| 87 | + 0, |
| 88 | + &discovery, |
| 89 | + 0, |
| 90 | + Enrollment::new( |
| 91 | + plus, |
| 92 | + "Stream Deck + · Live test", |
| 93 | + 50, |
| 94 | + Some(include_bytes!("../../../config/audio.json")), |
| 95 | + ), |
| 96 | + )?; |
| 97 | + store.enroll( |
| 98 | + generation, |
| 99 | + &discovery, |
| 100 | + 1, |
| 101 | + Enrollment::new(mk2, "Stream Deck MK.2 · Live test", 50, None), |
| 102 | + )?; |
| 103 | + let catalog = store.load()?; |
| 104 | + let mut value: serde_json::Value = |
| 105 | + serde_json::from_slice(catalog.profile(mk2)?.document().bytes()) |
| 106 | + .map_err(|_| "invalid_config")?; |
| 107 | + for page in value["pages"].as_array_mut().unwrap() { |
| 108 | + page["keys"][0] = serde_json::json!({"label":"Peer progress","action":{"type":"audio_adjust","target":"output:editor-fixture-peer","percent":1}}); |
| 109 | + } |
| 110 | + store.save_profile( |
| 111 | + mk2, |
| 112 | + 1, |
| 113 | + &serde_json::to_vec(&value).unwrap(), |
| 114 | + 0, |
| 115 | + "Stream Deck MK.2 · Live test", |
| 116 | + 50, |
| 117 | + )?; |
| 118 | + let effects = Arc::new(Mutex::new(0)); |
| 119 | + let mut runtime = Runtime::start( |
| 120 | + store.load()?, |
| 121 | + vec![Box::new(Effects(effects.clone()))], |
| 122 | + None, |
| 123 | + )?; |
| 124 | + let revision = runtime.discover(Discovery::new(discovery.candidates().to_vec())?)?; |
| 125 | + let a = Arc::new(Mutex::new(VirtualDeck::new(DeviceModel::Plus))); |
| 126 | + let b = Arc::new(Mutex::new(VirtualDeck::new(DeviceModel::Mk2))); |
| 127 | + runtime.attach(revision, &discovery, 0, Box::new(Probe(a.clone())))?; |
| 128 | + runtime.attach(revision, &discovery, 1, Box::new(Probe(b.clone())))?; |
| 129 | + let ready = Instant::now() + Duration::from_secs(3); |
| 130 | + while Instant::now() < ready |
| 131 | + && runtime |
| 132 | + .statuses() |
| 133 | + .iter() |
| 134 | + .any(|s| !s.input_ready || !s.display_ready) |
| 135 | + { |
| 136 | + runtime.tick(); |
| 137 | + std::thread::sleep(Duration::from_millis(2)); |
| 138 | + } |
| 139 | + if runtime |
| 140 | + .statuses() |
| 141 | + .iter() |
| 142 | + .any(|s| !s.input_ready || !s.display_ready) |
| 143 | + { |
| 144 | + return Err("connected_editor_lab_not_ready"); |
| 145 | + } |
| 146 | + let mut service = crate::live_profiles::Service::start(store, &runtime, false)?; |
| 147 | + let stop = Arc::new(AtomicBool::new(false)); |
| 148 | + for signal in [signal_hook::consts::SIGINT, signal_hook::consts::SIGTERM] { |
| 149 | + signal_hook::flag::register(signal, stop.clone()) |
| 150 | + .map_err(|_| "connected_editor_lab_signal_failed")?; |
| 151 | + } |
| 152 | + let end = Instant::now() + Duration::from_secs(seconds); |
| 153 | + let mut next = Instant::now(); |
| 154 | + let mut peer_while_pending = 0; |
| 155 | + let mut time = 0; |
| 156 | + while Instant::now() < end && !stop.load(Ordering::Acquire) { |
| 157 | + service.poll(&mut runtime); |
| 158 | + let feedback = runtime.tick(); |
| 159 | + if runtime |
| 160 | + .statuses() |
| 161 | + .iter() |
| 162 | + .any(|s| s.id == plus && s.error.as_deref() == Some("profile_update_pending")) |
| 163 | + { |
| 164 | + peer_while_pending += feedback |
| 165 | + .iter() |
| 166 | + .filter(|r| r.scope.device.device == mk2 && r.result.is_ok()) |
| 167 | + .count(); |
| 168 | + } |
| 169 | + service.publish(&runtime); |
| 170 | + if Instant::now() >= next { |
| 171 | + let mut deck = b.lock().unwrap(); |
| 172 | + for pressed in [true, false] { |
| 173 | + time += 1; |
| 174 | + deck.inject(InputEvent { |
| 175 | + timestamp_ms: time, |
| 176 | + event: RawEvent::Key { index: 0, pressed }, |
| 177 | + }) |
| 178 | + .map_err(|_| "fixture_input_invalid")?; |
| 179 | + } |
| 180 | + next = Instant::now() + Duration::from_millis(50); |
| 181 | + } |
| 182 | + std::thread::sleep(Duration::from_millis(2)); |
| 183 | + } |
| 184 | + drop(service); |
| 185 | + let blank = runtime.stop(); |
| 186 | + let result = serde_json::json!({"status":if blank.iter().all(|(_,r)|r.is_ok()){"passed"}else{"failed"},"blanked":blank.len(),"simulated_effects":*effects.lock().unwrap(),"peer_effects_while_other_save_pending":peer_while_pending,"physical_hid":false,"native_audio":false}); |
| 187 | + crate::layout_store::write( |
| 188 | + &path.join("live-editor-result.json"), |
| 189 | + &serde_json::to_vec_pretty(&result).unwrap(), |
| 190 | + )?; |
| 191 | + if result["status"] != "passed" { |
| 192 | + return Err("connected_editor_lab_blanking_failed"); |
| 193 | + } |
| 194 | + Ok(()) |
| 195 | +} |
0 commit comments