1#[cfg(not(target_arch = "wasm32"))]
14use anyhow::{anyhow, Result};
15#[cfg(not(target_arch = "wasm32"))]
16use base64::Engine;
17use serde::{Deserialize, Serialize};
18
19#[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 SimulateMouseMove {
68 x: f32,
69 y: f32,
70 },
71 SimulateRightClick {
72 x: f32,
73 y: f32,
74 },
75 SimulateResize {
76 width: u32,
78 height: u32,
80 },
81}
82
83#[derive(Debug, Clone)]
92pub enum TestEvent {
93 MouseMove {
95 x: f32,
96 y: f32,
97 },
98 MouseDown {
99 x: f32,
100 y: f32,
101 button: u8,
102 }, 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 Screenshot {
131 path: String,
132 },
133 CaptureScreenshot,
134 GetText,
135 GetTree,
136 Pump,
137 Wake,
138 Quit,
139 TapText {
142 text: String,
143 },
144 Wait {
146 ms: u64,
147 },
148}
149
150#[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#[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#[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 width: u32,
190 height: u32,
192 },
193 Error {
194 message: String,
195 },
196}
197
198#[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 self.pump()?;
266 self.send(TestCommand::TapText {
267 text: text.to_string(),
268 })?;
269 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 pub fn simulate_mouse_move(&self, x: f32, y: f32) -> Result<()> {
367 self.send(TestCommand::SimulateMouseMove { x, y })?;
368 Ok(())
369 }
370
371 pub fn right_click(&self, x: f32, y: f32) -> Result<()> {
373 self.send(TestCommand::SimulateRightClick { x, y })?;
374 Ok(())
375 }
376
377 pub fn simulate_resize(&self, width: u32, height: u32) -> Result<()> {
379 self.send(TestCommand::SimulateResize { width, height })?;
380 Ok(())
381 }
382
383 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}