Skip to main content

fission_test_driver/
lib.rs

1//! Automated UI testing client and protocol for Fission applications.
2//!
3//! This crate provides the JSON protocol types (shared between the test client
4//! and the desktop shell server) and a [`LiveTestClient`] that drives a running
5//! Fission application over HTTP.
6//!
7//! # Architecture
8//!
9//! The application must be launched with `FISSION_TEST_CONTROL_PORT=<port>`.
10//! The [`LiveTestClient`] connects to `http://127.0.0.1:<port>` and sends
11//! [`TestCommand`] JSON payloads to `/cmd`, receiving [`TestResponse`] replies.
12
13#[cfg(not(target_arch = "wasm32"))]
14use anyhow::{anyhow, Result};
15#[cfg(not(target_arch = "wasm32"))]
16use base64::Engine;
17use serde::{Deserialize, Serialize};
18
19// --- Protocol types (shared between client and server) ---
20
21/// A command sent from the test client to the running application.
22///
23/// Serialized with `#[serde(tag = "cmd")]`. See the crate-level docs for
24/// the full command reference.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(tag = "cmd")]
27pub enum TestCommand {
28    Tap {
29        x: f32,
30        y: f32,
31    },
32    Drag {
33        start_x: f32,
34        start_y: f32,
35        end_x: f32,
36        end_y: f32,
37        steps: u32,
38    },
39    TapText {
40        text: String,
41    },
42    Scroll {
43        x: f32,
44        y: f32,
45        dx: f32,
46        dy: f32,
47    },
48    TypeText {
49        text: String,
50    },
51    PressKey {
52        key: String,
53        modifiers: u8,
54    },
55    Screenshot {
56        path: String,
57    },
58    CaptureScreenshot {},
59    GetText {},
60    GetTree {},
61    Wait {
62        ms: u64,
63    },
64    Pump {},
65    Quit {},
66    // NEW: simulate real winit-level events for realistic testing
67    SimulateMouseMove {
68        x: f32,
69        y: f32,
70    },
71    SimulateRightClick {
72        x: f32,
73        y: f32,
74    },
75    SimulateResize {
76        /// Target logical viewport width in test-space pixels.
77        width: u32,
78        /// Target logical viewport height in test-space pixels.
79        height: u32,
80    },
81}
82
83/// Events injected into the winit event loop via `EventLoopProxy`.
84///
85/// Input-simulation variants (`MouseMove`, `MouseDown`, etc.) travel through
86/// the **same** `Event::UserEvent` → handler path as real `WindowEvent`s, so
87/// test code exercises identical code paths as real user interaction.
88///
89/// Query / control variants (`Screenshot`, `GetText`, etc.) also go through
90/// the proxy so the main loop can respond via a dedicated response channel.
91#[derive(Debug, Clone)]
92pub enum TestEvent {
93    // --- Input simulation (mirrors winit WindowEvents) ---
94    MouseMove {
95        x: f32,
96        y: f32,
97    },
98    MouseDown {
99        x: f32,
100        y: f32,
101        button: u8,
102    }, // 0=left, 1=right, 2=middle
103    MouseUp {
104        x: f32,
105        y: f32,
106        button: u8,
107    },
108    KeyDown {
109        key_code: String,
110        modifiers: u8,
111    },
112    KeyUp {
113        key_code: String,
114        modifiers: u8,
115    },
116    TextInput {
117        text: String,
118    },
119    Scroll {
120        x: f32,
121        y: f32,
122        dx: f32,
123        dy: f32,
124    },
125    Resize {
126        width: u32,
127        height: u32,
128    },
129    // --- Queries / control (need response channel) ---
130    Screenshot {
131        path: String,
132    },
133    CaptureScreenshot,
134    GetText,
135    GetTree,
136    Pump,
137    Wake,
138    Quit,
139    /// Internal: TapText resolves a text label to coordinates; the server
140    /// injects this so the main loop can do the lookup with access to the IR.
141    TapText {
142        text: String,
143    },
144    /// Internal: Wait is handled server-side (sleep) then responds.
145    Wait {
146        ms: u64,
147    },
148}
149
150/// A visible text element with its bounding rectangle, in logical test-space
151/// pixels, returned by [`TestCommand::GetText`].
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct TextItem {
154    pub text: String,
155    pub x: f32,
156    pub y: f32,
157    pub width: f32,
158    pub height: f32,
159}
160
161/// A node in the semantic accessibility tree, returned by [`TestCommand::GetTree`].
162/// Bounding rectangles are expressed in logical test-space pixels.
163#[derive(Debug, Clone, Serialize, Deserialize)]
164pub struct SemanticNode {
165    pub role: String,
166    pub label: Option<String>,
167    pub value: Option<String>,
168    pub focusable: bool,
169    pub x: f32,
170    pub y: f32,
171    pub width: f32,
172    pub height: f32,
173}
174
175/// The response from the application to a [`TestCommand`].
176#[derive(Debug, Clone, Serialize, Deserialize)]
177#[serde(tag = "status")]
178pub enum TestResponse {
179    Ok {},
180    Text {
181        items: Vec<TextItem>,
182    },
183    Tree {
184        nodes: Vec<SemanticNode>,
185    },
186    Screenshot {
187        png_base64: String,
188        /// PNG width in logical test-space pixels.
189        width: u32,
190        /// PNG height in logical test-space pixels.
191        height: u32,
192    },
193    Error {
194        message: String,
195    },
196}
197
198// --- Client ---
199
200/// An HTTP client that drives a running Fission application for automated UI testing.
201///
202/// Connect to a running application via [`LiveTestClient::connect(port)`]. The
203/// application must have been started with `FISSION_TEST_CONTROL_PORT=<port>`.
204///
205/// # Example
206///
207/// ```rust,ignore
208/// let client = LiveTestClient::connect(9876);
209/// client.wait_for_ready(5000).unwrap();
210/// client.tap_text("Submit").unwrap();
211/// client.assert_text_visible("Success").unwrap();
212/// client.screenshot("/tmp/result.png").unwrap();
213/// client.quit().unwrap();
214/// ```
215#[cfg(not(target_arch = "wasm32"))]
216pub struct LiveTestClient {
217    base_url: String,
218}
219
220#[cfg(not(target_arch = "wasm32"))]
221impl LiveTestClient {
222    pub fn connect(port: u16) -> Self {
223        Self {
224            base_url: format!("http://127.0.0.1:{}", port),
225        }
226    }
227
228    pub fn wait_for_ready(&self, timeout_ms: u64) -> Result<()> {
229        let start = std::time::Instant::now();
230        let timeout = std::time::Duration::from_millis(timeout_ms);
231        loop {
232            match ureq::get(&format!("{}/health", self.base_url)).call() {
233                Ok(_) => return Ok(()),
234                Err(_) => {
235                    if start.elapsed() > timeout {
236                        return Err(anyhow!("timed out waiting for test server"));
237                    }
238                    std::thread::sleep(std::time::Duration::from_millis(100));
239                }
240            }
241        }
242    }
243
244    fn send(&self, cmd: TestCommand) -> Result<TestResponse> {
245        let body = serde_json::to_string(&cmd)?;
246        let resp = ureq::post(&format!("{}/cmd", self.base_url))
247            .set("Content-Type", "application/json")
248            .send_string(&body)
249            .map_err(|e| anyhow!("request failed: {}", e))?;
250        let text = resp.into_string()?;
251        let response: TestResponse = serde_json::from_str(&text)?;
252        if let TestResponse::Error { message } = &response {
253            return Err(anyhow!("server error: {}", message));
254        }
255        Ok(response)
256    }
257
258    pub fn tap(&self, x: f32, y: f32) -> Result<()> {
259        self.send(TestCommand::Tap { x, y })?;
260        Ok(())
261    }
262
263    pub fn tap_text(&self, text: &str) -> Result<()> {
264        // Pump first to ensure layout positions are current
265        self.pump()?;
266        self.send(TestCommand::TapText {
267            text: text.to_string(),
268        })?;
269        // Pump after to render the result of the tap
270        self.pump()?;
271        Ok(())
272    }
273
274    pub fn drag(
275        &self,
276        start_x: f32,
277        start_y: f32,
278        end_x: f32,
279        end_y: f32,
280        steps: u32,
281    ) -> Result<()> {
282        self.send(TestCommand::Drag {
283            start_x,
284            start_y,
285            end_x,
286            end_y,
287            steps,
288        })?;
289        self.pump()?;
290        Ok(())
291    }
292
293    pub fn scroll(&self, x: f32, y: f32, dx: f32, dy: f32) -> Result<()> {
294        self.send(TestCommand::Scroll { x, y, dx, dy })?;
295        Ok(())
296    }
297
298    pub fn press_key(&self, key: &str, modifiers: u8) -> Result<()> {
299        self.send(TestCommand::PressKey {
300            key: key.to_string(),
301            modifiers,
302        })?;
303        self.pump()?;
304        Ok(())
305    }
306
307    pub fn type_text(&self, text: &str) -> Result<()> {
308        self.send(TestCommand::TypeText {
309            text: text.to_string(),
310        })?;
311        Ok(())
312    }
313
314    pub fn screenshot(&self, path: &str) -> Result<()> {
315        match self.send(TestCommand::CaptureScreenshot {})? {
316            TestResponse::Screenshot {
317                png_base64,
318                width: _,
319                height: _,
320            } => {
321                let bytes = base64::engine::general_purpose::STANDARD
322                    .decode(png_base64)
323                    .map_err(|e| anyhow!("invalid screenshot payload: {}", e))?;
324                std::fs::write(path, bytes)?;
325                Ok(())
326            }
327            other => Err(anyhow!(
328                "unexpected response to CaptureScreenshot: {:?}",
329                other
330            )),
331        }
332    }
333
334    pub fn get_text(&self) -> Result<Vec<TextItem>> {
335        match self.send(TestCommand::GetText {})? {
336            TestResponse::Text { items } => Ok(items),
337            other => Err(anyhow!("unexpected response: {:?}", other)),
338        }
339    }
340
341    pub fn get_tree(&self) -> Result<Vec<SemanticNode>> {
342        match self.send(TestCommand::GetTree {})? {
343            TestResponse::Tree { nodes } => Ok(nodes),
344            other => Err(anyhow!("unexpected response: {:?}", other)),
345        }
346    }
347
348    pub fn wait(&self, ms: u64) -> Result<()> {
349        self.send(TestCommand::Wait { ms })?;
350        Ok(())
351    }
352
353    pub fn pump(&self) -> Result<()> {
354        self.send(TestCommand::Pump {})?;
355        Ok(())
356    }
357
358    pub fn quit(&self) -> Result<()> {
359        let _ = self.send(TestCommand::Quit {});
360        Ok(())
361    }
362
363    // --- NEW: simulate real winit-level events ---
364
365    /// Simulate a mouse move to (x, y) — goes through the real CursorMoved path.
366    pub fn simulate_mouse_move(&self, x: f32, y: f32) -> Result<()> {
367        self.send(TestCommand::SimulateMouseMove { x, y })?;
368        Ok(())
369    }
370
371    /// Simulate a right-click at (x, y) — move + down + up with right button.
372    pub fn right_click(&self, x: f32, y: f32) -> Result<()> {
373        self.send(TestCommand::SimulateRightClick { x, y })?;
374        Ok(())
375    }
376
377    /// Simulate a window resize in logical test-space pixels.
378    pub fn simulate_resize(&self, width: u32, height: u32) -> Result<()> {
379        self.send(TestCommand::SimulateResize { width, height })?;
380        Ok(())
381    }
382
383    // --- High-level helpers ---
384
385    pub fn tap_text_and_wait(&self, text: &str, ms: u64) -> Result<()> {
386        self.tap_text(text)?;
387        self.wait(ms)?;
388        Ok(())
389    }
390
391    pub fn assert_text_visible(&self, needle: &str) -> Result<()> {
392        let items = self.get_text()?;
393        let found = items.iter().any(|t| t.text.contains(needle));
394        if !found {
395            let all: Vec<&str> = items.iter().map(|t| t.text.as_str()).collect();
396            return Err(anyhow!(
397                "expected '{}' to be visible, found: {:?}",
398                needle,
399                &all[..all.len().min(20)]
400            ));
401        }
402        Ok(())
403    }
404
405    pub fn assert_text_not_visible(&self, needle: &str) -> Result<()> {
406        let items = self.get_text()?;
407        let found = items.iter().any(|t| t.text.contains(needle));
408        if found {
409            return Err(anyhow!("expected '{}' to NOT be visible", needle));
410        }
411        Ok(())
412    }
413}