use std::time::Duration;
#[derive(Debug, Clone)]
pub enum Step {
WriteText(String),
PressKey(String),
Sleep(Duration),
WaitForText {
needle: String,
timeout_ms: u32,
},
WaitForStableFrame {
stable_ms: u32,
timeout_ms: u32,
},
Capture,
CaptureLabeled {
label: String,
description: String,
},
ViewingPause(Duration),
}
impl Step {
pub fn write_text(text: impl Into<String>) -> Self {
Self::WriteText(text.into())
}
pub fn press_key(key: impl Into<String>) -> Self {
Self::PressKey(key.into())
}
pub fn sleep(duration: Duration) -> Self {
Self::Sleep(duration)
}
pub fn sleep_ms(ms: u64) -> Self {
Self::Sleep(Duration::from_millis(ms))
}
pub fn wait_for_text(needle: impl Into<String>, timeout_ms: u32) -> Self {
Self::WaitForText {
needle: needle.into(),
timeout_ms,
}
}
pub fn wait_for_stable_frame(stable_ms: u32, timeout_ms: u32) -> Self {
Self::WaitForStableFrame {
stable_ms,
timeout_ms,
}
}
pub fn capture() -> Self {
Self::Capture
}
pub fn capture_labeled(label: impl Into<String>, description: impl Into<String>) -> Self {
Self::CaptureLabeled {
label: label.into(),
description: description.into(),
}
}
pub fn viewing_pause(duration: Duration) -> Self {
Self::ViewingPause(duration)
}
pub fn viewing_pause_ms(ms: u64) -> Self {
Self::ViewingPause(Duration::from_millis(ms))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write_text_stores_content() {
let step = Step::write_text("hello");
let Step::WriteText(text) = step else {
unreachable!("Expected WriteText variant");
};
assert_eq!(text, "hello");
}
#[test]
fn sleep_ms_converts_to_duration() {
let step = Step::sleep_ms(500);
let Step::Sleep(duration) = step else {
unreachable!("Expected Sleep variant");
};
assert_eq!(duration, Duration::from_millis(500));
}
#[test]
fn capture_labeled_stores_label_and_description() {
let step = Step::capture_labeled("startup", "App launched");
let Step::CaptureLabeled { label, description } = step else {
unreachable!("Expected CaptureLabeled variant");
};
assert_eq!(label, "startup");
assert_eq!(description, "App launched");
}
#[test]
fn wait_for_text_stores_needle_and_timeout() {
let step = Step::wait_for_text("Loading", 5000);
let Step::WaitForText { needle, timeout_ms } = step else {
unreachable!("Expected WaitForText variant");
};
assert_eq!(needle, "Loading");
assert_eq!(timeout_ms, 5000);
}
#[test]
fn viewing_pause_stores_duration() {
let step = Step::viewing_pause(Duration::from_secs(2));
let Step::ViewingPause(duration) = step else {
unreachable!("Expected ViewingPause variant");
};
assert_eq!(duration, Duration::from_secs(2));
}
#[test]
fn viewing_pause_ms_converts_to_duration() {
let step = Step::viewing_pause_ms(1500);
let Step::ViewingPause(duration) = step else {
unreachable!("Expected ViewingPause variant");
};
assert_eq!(duration, Duration::from_millis(1500));
}
}