use std::cell::{OnceCell, RefCell};
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use day_reactive::Signal;
use day_spec::{Event, NodeId};
use crate::Step;
pub use day_script_protocol::{steps_from_yaml, steps_to_yaml};
#[must_use]
pub fn annotate_yaml(steps: &[Step], labels: &[Option<String>]) -> String {
let yaml = steps_to_yaml(steps);
let key_of = |step: &Step| match step {
Step::Tap { .. }
| Step::Input { .. }
| Step::Select { .. }
| Step::Activate { .. }
| Step::WaitFor { .. } => Some("id:"),
Step::Navigate { .. } => Some("route:"),
_ => None,
};
let mut out = String::with_capacity(yaml.len());
let mut idx: isize = -1;
let mut done_this_step = false;
for line in yaml.lines() {
if let Some(rest) = line.strip_prefix("- ") {
let _ = rest;
idx += 1;
done_this_step = false;
}
out.push_str(line);
if !done_this_step
&& idx >= 0
&& let Some(Some(label)) = labels.get(idx as usize)
&& let Some(Some(key)) = steps.get(idx as usize).map(key_of)
&& line.trim_start().starts_with(key)
{
let clean = label
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.replace('"', "");
let _ = write!(out, " # \"{clean}\"");
done_this_step = true;
}
out.push('\n');
}
out
}
fn event_to_step(id: Option<&str>, ev: &Event) -> Option<Step> {
match ev {
Event::Pressed | Event::Tap(_) => id.map(|id| Step::Tap {
if_present: false,
at: None,
id: id.to_string(),
repeat: Some(1),
modifiers: Vec::new(),
}),
Event::TextChanged(text) => id.map(|id| Step::Input {
id: id.to_string(),
text: Some(text.clone()),
key: None,
args: None,
}),
Event::ListActivated(index) => id.map(|id| Step::Activate {
id: id.to_string(),
index: *index,
}),
Event::SelectionChanged(index) => id.map(|id| Step::Select {
id: id.to_string(),
index: *index,
if_present: false,
}),
Event::ToggleChanged(on) => id.map(|id| Step::Select {
id: id.to_string(),
index: i64::from(*on),
if_present: false,
}),
Event::ValueCommitted(value) => id.map(|id| Step::SetValue {
id: id.to_string(),
value: *value,
}),
_ => None,
}
}
#[cfg(test)]
const PLAYBACK_EMISSIONS: &[(&str, &[Event], Disposition)] = &[
("tap", &[Event::Pressed], Disposition::Records("tap")),
(
"tap",
&[Event::Tap(day_spec::Point::ZERO)],
Disposition::Records("tap"),
),
(
"activate",
&[Event::ListActivated(1)],
Disposition::Records("activate"),
),
(
"select",
&[Event::SelectionChanged(1)],
Disposition::Records("select"),
),
(
"toggle",
&[Event::ToggleChanged(true)],
Disposition::Records("select"),
),
(
"input",
&[Event::TextChanged(String::new())],
Disposition::Records("input"),
),
(
"submit",
&[Event::Submitted],
Disposition::Dropped("a submit follows the input that recorded it"),
),
(
"set_value",
&[Event::ValueChanged(0.5)],
Disposition::Dropped("mid-drag; the settled value arrives as ValueCommitted"),
),
(
"set_value",
&[Event::ValueCommitted(0.5)],
Disposition::Records("set_value"),
),
];
#[cfg(test)]
fn step_entry_op(step: &Step) -> String {
match serde_json::to_value(step) {
Ok(serde_json::Value::Object(m)) => m
.get("op")
.and_then(|v| v.as_str())
.unwrap_or("<untagged>")
.to_string(),
_ => "<unserializable>".to_string(),
}
}
#[cfg(test)]
enum Disposition {
Records(&'static str),
Dropped(&'static str),
}
fn coalesces(last: &Step, next: &Step) -> bool {
match (last, next) {
(Step::Input { id: a, .. }, Step::Input { id: b, .. })
| (Step::Select { id: a, .. }, Step::Select { id: b, .. }) => a == b,
(Step::Navigate { .. }, Step::Navigate { .. }) => true,
_ => false,
}
}
#[derive(Default)]
struct Recorder {
active: bool,
echo: bool,
steps: Vec<Step>,
into: Option<Signal<String>>,
file: Option<PathBuf>,
exclude: String,
input_gen: Option<u64>,
last_pressed: Option<String>,
labels: Vec<Option<String>>,
}
impl Recorder {
fn push(&mut self, step: Step, label: Option<String>) {
if let Some(last) = self.steps.last_mut()
&& coalesces(last, &step)
{
*last = step;
*self.labels.last_mut().expect("labels align with steps") = label;
return;
}
self.steps.push(step);
self.labels.push(label);
}
fn flush(&self) {
let script = annotate_yaml(&self.steps, &self.labels);
if let Some(sig) = self.into {
sig.set(script.clone());
}
if let Some(path) = &self.file {
let _ = std::fs::write(path, script.as_bytes());
}
}
}
day_reactive::tls_group! {
static REC: RefCell<Recorder> = RefCell::new(Recorder::default());
static FLAG: OnceCell<Signal<bool>> = const { OnceCell::new() };
static VERSION: OnceCell<Signal<u64>> = const { OnceCell::new() };
}
#[must_use]
pub fn recording_signal() -> Signal<bool> {
FLAG.with(|c| *c.get_or_init(|| Signal::global(false)))
}
#[must_use]
pub fn version() -> Signal<u64> {
VERSION.with(|c| *c.get_or_init(|| Signal::global(0)))
}
fn bump_version() {
version().update(|n| *n += 1);
}
fn is_press_twin(ev: &Event, step: &Step, last_pressed: Option<&str>) -> bool {
matches!(
(ev, step),
(Event::Tap(_), Step::Tap { id, .. }) if last_pressed == Some(id.as_str())
)
}
fn is_excluded(id: Option<&str>, prefix: &str) -> bool {
!prefix.is_empty() && id.is_some_and(|id| id.starts_with(prefix))
}
fn echo_action(step: &Step, label: Option<&str>, recording: bool) {
use std::io::Write as _;
let mut line = String::from(if recording {
"day record ▸ "
} else {
"dayscript ▸ "
});
match step {
Step::Tap { id, .. } => {
let _ = write!(line, "tap {id}");
}
Step::Input { id, text, .. } => {
let _ = write!(line, "input {id} = {:?}", text.as_deref().unwrap_or(""));
}
Step::Activate { id, index } => {
let _ = write!(line, "activate {id} = {index}");
}
Step::Select { id, index, .. } => {
let _ = write!(line, "select {id} = {index}");
}
Step::SetValue { id, value } => {
let _ = write!(line, "set_value {id} = {value}");
}
Step::Navigate { route } => {
let _ = write!(
line,
"navigate → {}",
if route.is_empty() { "/" } else { route }
);
}
Step::NavBack { native } => {
line.push_str("nav_back");
if *native {
line.push_str(": { native: true }");
}
}
_ => return,
}
if let Some(label) = label {
let _ = write!(
line,
" \"{}\"",
label.split_whitespace().collect::<Vec<_>>().join(" ")
);
}
let mut out = std::io::stdout();
let _ = writeln!(out, "{line}");
let _ = out.flush();
}
fn on_event(node: NodeId, ev: &Event) {
if !is_observing() {
return;
}
let id = day_core::id_of(node);
let Some(step) = event_to_step(id.as_deref(), ev) else {
return;
};
let label = day_core::label_of(node);
REC.with(|r| {
let mut rec = r.borrow_mut();
if is_excluded(id.as_deref(), &rec.exclude) {
return;
}
if is_press_twin(ev, &step, rec.last_pressed.as_deref()) {
return;
}
rec.last_pressed = match (ev, &step) {
(Event::Pressed, Step::Tap { id, .. }) => Some(id.clone()),
_ => None,
};
echo_action(&step, label.as_deref(), rec.active);
if !rec.active {
return;
}
let is_input = matches!(step, Step::Tap { .. } | Step::Select { .. });
rec.push(step, label);
rec.input_gen = is_input.then(day_core::pump_generation);
rec.flush();
});
bump_version();
}
fn on_nav(route: &str, label: Option<&str>) {
if !is_observing() {
return;
}
let step = Step::Navigate {
route: route.to_string(),
};
let label = label.map(str::to_string);
REC.with(|r| {
let mut rec = r.borrow_mut();
if !rec.active {
echo_action(&step, label.as_deref(), false);
return;
}
let foldable = rec
.input_gen
.is_some_and(|g| day_core::pump_generation().saturating_sub(g) <= 1)
&& matches!(
rec.steps.last(),
Some(Step::Tap { .. } | Step::Select { .. })
);
echo_action(&step, label.as_deref(), true);
if foldable {
*rec.steps.last_mut().expect("input_gen implies a last step") = step;
*rec.labels.last_mut().expect("labels align with steps") = label;
} else {
rec.push(step, label);
}
rec.input_gen = None;
rec.flush();
});
bump_version();
}
fn install_observer() {
day_core::set_event_observer(Some(Box::new(on_event)));
day_core::set_nav_observer(Some(Box::new(on_nav)));
}
fn start_common(into: Option<Signal<String>>, file: Option<PathBuf>) {
REC.with(|r| {
let mut rec = r.borrow_mut();
if !rec.active {
rec.steps.clear();
rec.labels.clear();
rec.input_gen = None;
rec.into = None;
rec.file = None;
rec.active = true;
}
if into.is_some() {
rec.into = into;
}
if file.is_some() {
rec.file = file;
}
rec.flush();
});
install_observer();
recording_signal().set_if_changed(true);
bump_version();
}
pub fn start() {
start_common(None, None);
}
pub fn start_into(sig: Signal<String>) {
start_common(Some(sig), None);
}
pub fn start_to_file(path: impl AsRef<Path>) {
start_common(None, Some(path.as_ref().to_path_buf()));
}
pub fn stop() {
REC.with(|r| r.borrow_mut().active = false);
if !is_logging_actions() {
day_core::set_event_observer(None);
day_core::set_nav_observer(None);
}
recording_signal().set_if_changed(false);
}
#[must_use]
pub fn is_recording() -> bool {
REC.with(|r| r.borrow().active)
}
fn is_observing() -> bool {
REC.with(|r| {
let rec = r.borrow();
rec.active || rec.echo
})
}
pub fn log_actions(on: bool) {
REC.with(|r| r.borrow_mut().echo = on);
if on {
install_observer();
} else if !is_recording() {
day_core::set_event_observer(None);
day_core::set_nav_observer(None);
}
}
#[must_use]
pub fn is_logging_actions() -> bool {
REC.with(|r| r.borrow().echo)
}
#[must_use]
pub fn script() -> String {
REC.with(|r| steps_to_yaml(&r.borrow().steps))
}
#[must_use]
pub fn steps() -> Vec<Step> {
REC.with(|r| r.borrow().steps.clone())
}
pub fn clear() {
REC.with(|r| {
let mut rec = r.borrow_mut();
rec.steps.clear();
rec.labels.clear();
rec.input_gen = None;
rec.flush();
});
bump_version();
}
pub fn save(path: &Path) -> std::io::Result<()> {
std::fs::write(path, script().as_bytes())
}
pub fn exclude_prefix(prefix: &str) {
REC.with(|r| r.borrow_mut().exclude = prefix.to_string());
}
#[cfg(not(target_arch = "wasm32"))]
pub fn play(yaml: &str) -> Result<(), String> {
play_with_delay(yaml, 0.0)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn play_with_delay(yaml: &str, step_delay_secs: f64) -> Result<(), String> {
if is_recording() {
return Err("cannot play while recording — stop the recording first".to_string());
}
if is_playing() {
return Err("a script is already playing".to_string());
}
let steps = steps_from_yaml(yaml)?;
if steps.is_empty() {
return Err("script has no steps to play".to_string());
}
let delay = std::time::Duration::from_secs_f64(step_delay_secs.max(0.0));
set_playing(true);
set_paused(false);
std::thread::spawn(move || {
replay_steps(
steps,
delay,
is_playing,
is_paused,
std::thread::sleep,
|step| {
let _ = crate::run_step_with_wait(&step);
},
);
set_playing(false);
set_paused(false);
});
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
fn replay_steps(
steps: Vec<Step>,
delay: std::time::Duration,
playing: impl Fn() -> bool,
paused: impl Fn() -> bool,
mut sleep: impl FnMut(std::time::Duration),
mut dispatch: impl FnMut(Step),
) {
for (i, step) in steps.into_iter().enumerate() {
if !playing() {
break;
}
if i > 0 && !delay.is_zero() {
sleep(delay);
}
while paused() && playing() {
sleep(std::time::Duration::from_millis(PAUSE_POLL_MS));
}
if !playing() {
break;
}
dispatch(step);
}
}
#[cfg(not(target_arch = "wasm32"))]
const PAUSE_POLL_MS: u64 = 50;
#[cfg(not(target_arch = "wasm32"))]
#[must_use]
pub fn playback_supported() -> bool {
true
}
#[cfg(target_arch = "wasm32")]
#[must_use]
pub fn playback_supported() -> bool {
false
}
#[cfg(not(target_arch = "wasm32"))]
static PLAYING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
#[cfg(not(target_arch = "wasm32"))]
static PAUSED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
#[cfg(not(target_arch = "wasm32"))]
thread_local! {
static PLAYING_SIG: OnceCell<Signal<bool>> = const { OnceCell::new() };
static PAUSED_SIG: OnceCell<Signal<bool>> = const { OnceCell::new() };
}
#[cfg(not(target_arch = "wasm32"))]
#[must_use]
pub fn playing_signal() -> Signal<bool> {
PLAYING_SIG.with(|c| *c.get_or_init(|| Signal::global(false)))
}
#[cfg(not(target_arch = "wasm32"))]
#[must_use]
pub fn paused_signal() -> Signal<bool> {
PAUSED_SIG.with(|c| *c.get_or_init(|| Signal::global(false)))
}
#[cfg(not(target_arch = "wasm32"))]
fn set_playing(on: bool) {
PLAYING.store(on, std::sync::atomic::Ordering::SeqCst);
day_reactive::on_main(move || playing_signal().set_if_changed(on));
}
#[cfg(not(target_arch = "wasm32"))]
fn set_paused(on: bool) {
PAUSED.store(on, std::sync::atomic::Ordering::SeqCst);
day_reactive::on_main(move || paused_signal().set_if_changed(on));
}
#[cfg(not(target_arch = "wasm32"))]
pub fn is_playing() -> bool {
PLAYING.load(std::sync::atomic::Ordering::SeqCst)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn is_paused() -> bool {
PAUSED.load(std::sync::atomic::Ordering::SeqCst)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn pause_playback() {
if is_playing() {
set_paused(true);
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn resume_playback() {
if is_playing() {
set_paused(false);
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn stop_playback() {
set_playing(false);
set_paused(false);
}
#[must_use]
pub fn is_playable(yaml: &str) -> bool {
steps_from_yaml(yaml).is_ok_and(|s| !s.is_empty())
}
#[cfg(target_arch = "wasm32")]
pub fn play(yaml: &str) -> Result<(), String> {
play_with_delay(yaml, 0.0)
}
#[cfg(target_arch = "wasm32")]
pub fn play_with_delay(_yaml: &str, _step_delay_secs: f64) -> Result<(), String> {
Err(
"day::play_script is not available on web (no background thread); drive the page over the \
dayscript WebSocket transport instead"
.to_string(),
)
}
#[cfg(target_arch = "wasm32")]
#[must_use]
pub fn playing_signal() -> Signal<bool> {
PLAYING_SIG.with(|c| *c.get_or_init(|| Signal::global(false)))
}
#[cfg(target_arch = "wasm32")]
#[must_use]
pub fn paused_signal() -> Signal<bool> {
PAUSED_SIG.with(|c| *c.get_or_init(|| Signal::global(false)))
}
#[cfg(target_arch = "wasm32")]
thread_local! {
static PLAYING_SIG: OnceCell<Signal<bool>> = const { OnceCell::new() };
static PAUSED_SIG: OnceCell<Signal<bool>> = const { OnceCell::new() };
}
#[cfg(target_arch = "wasm32")]
#[must_use]
pub fn is_playing() -> bool {
false
}
#[cfg(target_arch = "wasm32")]
#[must_use]
pub fn is_paused() -> bool {
false
}
#[cfg(target_arch = "wasm32")]
pub fn pause_playback() {}
#[cfg(target_arch = "wasm32")]
pub fn resume_playback() {}
#[cfg(target_arch = "wasm32")]
pub fn stop_playback() {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_recorder_steps() {
let steps = vec![
Step::Navigate {
route: "controls".into(),
},
Step::Tap {
if_present: false,
id: "inc".into(),
repeat: Some(1),
at: None,
modifiers: Vec::new(),
},
Step::Input {
id: "name-field".into(),
text: Some("Ada".into()),
key: None,
args: None,
},
Step::Select {
id: "size".into(),
index: 2,
if_present: false,
},
Step::NavBack { native: false },
Step::SizeClass {
width: "compact".into(),
height: Some("regular".into()),
},
];
let yaml = steps_to_yaml(&steps);
let back = steps_from_yaml(&yaml).expect("re-parse");
assert_eq!(
format!("{steps:?}"),
format!("{back:?}"),
"yaml was:\n{yaml}"
);
assert!(yaml.contains("tap:"), "yaml was:\n{yaml}");
assert!(
!yaml.contains("key:"),
"null params must be stripped:\n{yaml}"
);
}
#[test]
fn accepts_cli_demo_template() {
let demo = include_str!("../../day-cli/templates/app/dayscript/demo.yaml");
let steps = steps_from_yaml(demo).expect("demo.yaml parses");
assert!(
steps
.iter()
.any(|s| matches!(s, Step::Screenshot { name, .. } if name == "welcome")),
"demo.yaml carries `screenshot: welcome` as a scalar"
);
assert!(
steps
.iter()
.any(|s| matches!(s, Step::Navigate { route } if route == "items")),
"demo.yaml navigates to the Items smoke test"
);
let reparsed = steps_from_yaml(&steps_to_yaml(&steps)).expect("re-parse");
assert_eq!(format!("{steps:?}"), format!("{reparsed:?}"));
}
#[test]
fn event_to_step_maps_actions_and_drops_the_rest() {
use day_spec::Point;
std::assert_matches!(
event_to_step(Some("inc"), &Event::Pressed),
Some(Step::Tap { .. })
);
std::assert_matches!(
event_to_step(Some("field"), &Event::TextChanged("hi".into())),
Some(Step::Input { .. })
);
std::assert_matches!(
event_to_step(Some("t"), &Event::ToggleChanged(true)),
Some(Step::Select { index: 1, .. })
);
assert!(event_to_step(None, &Event::RouteRequested("home".into())).is_none());
std::assert_matches!(
event_to_step(Some("list-shuffle"), &Event::Tap(Point::new(1.0, 2.0))),
Some(Step::Tap { .. })
);
assert!(event_to_step(None, &Event::Tap(Point::new(1.0, 2.0))).is_none());
assert!(event_to_step(Some("s"), &Event::ValueChanged(0.5)).is_none());
assert!(event_to_step(None, &Event::Pressed).is_none());
}
#[test]
fn playback_and_recording_agree() {
for (step_op, events, disposition) in PLAYBACK_EMISSIONS {
for ev in *events {
let got = event_to_step(Some("probe"), ev);
match disposition {
Disposition::Records(expected_op) => {
let got = got.unwrap_or_else(|| {
panic!(
"playback's `{step_op}` step emits {ev:?}, but the recorder drops \
it — every control that receives only this event records as \
nothing. Map it in `event_to_step`, or declare it Dropped in \
PLAYBACK_EMISSIONS and say why."
)
});
assert_eq!(
step_entry_op(&got),
*expected_op,
"playback's `{step_op}` step emits {ev:?}; the recorder turns that \
into a `{}` step, not the `{expected_op}` the table declares",
step_entry_op(&got),
);
}
Disposition::Dropped(why) => assert!(
got.is_none(),
"PLAYBACK_EMISSIONS says {ev:?} is dropped ({why}), but the recorder now \
maps it to {got:?} — update the table if that is intended"
),
}
}
}
}
#[test]
fn every_recorded_step_round_trips_through_yaml() {
let mapped: Vec<Step> = PLAYBACK_EMISSIONS
.iter()
.filter(|(_, _, d)| matches!(d, Disposition::Records(_)))
.flat_map(|(_, events, _)| *events)
.filter_map(|ev| event_to_step(Some("probe"), ev))
.collect();
assert!(
!mapped.is_empty(),
"the table declares some recordable steps"
);
let reparsed = steps_from_yaml(&steps_to_yaml(&mapped)).expect("recorded yaml re-parses");
assert_eq!(format!("{mapped:?}"), format!("{reparsed:?}"));
}
#[test]
fn annotate_yaml_comments_the_identifying_line() {
let steps = vec![
Step::Navigate {
route: "focus".into(),
},
Step::Tap {
if_present: false,
id: "focus-next-button".into(),
repeat: Some(1),
at: None,
modifiers: Vec::new(),
},
];
let labels = vec![Some("Focus".to_string()), Some("Focus next".to_string())];
let yaml = annotate_yaml(&steps, &labels);
assert!(
yaml.contains("route: focus # \"Focus\""),
"nav label on route line:\n{yaml}"
);
assert!(
yaml.contains("id: focus-next-button # \"Focus next\""),
"tap label on id line:\n{yaml}"
);
let reparsed = steps_from_yaml(&yaml).expect("annotated yaml parses");
assert_eq!(format!("{steps:?}"), format!("{reparsed:?}"));
}
#[test]
fn is_playable_rejects_empty_and_garbage() {
assert!(!is_playable(""));
assert!(!is_playable("flow: []\n"));
assert!(!is_playable("not: a script"));
assert!(is_playable("flow:\n- tap: { id: go }\n"));
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn playback_honors_pause_and_stop_during_the_step_delay() {
use std::cell::Cell;
use std::time::Duration;
for stop in [false, true] {
let playing = Cell::new(true);
let paused = Cell::new(false);
let dispatched = Cell::new(0);
let delay = Duration::from_millis(250);
let mut waits = Vec::new();
replay_steps(
steps_from_yaml("flow:\n- tap: { id: fixture-one }\n- tap: { id: fixture-two }\n")
.unwrap(),
delay,
|| playing.get(),
|| paused.get(),
|duration| {
waits.push(duration);
assert_eq!(dispatched.get(), 1, "only the first step has run");
if duration == delay {
paused.set(true);
playing.set(!stop);
} else {
assert!(paused.replace(false), "resume a paused replay");
}
},
|_| {
assert!(playing.get());
assert!(!paused.get(), "never dispatch a new step while paused");
dispatched.set(dispatched.get() + 1);
},
);
if stop {
assert_eq!(dispatched.get(), 1);
assert_eq!(waits, [delay]);
} else {
assert_eq!(dispatched.get(), 2);
assert_eq!(waits, [delay, Duration::from_millis(PAUSE_POLL_MS)]);
}
}
}
#[test]
fn coalesces_input_select_and_navigate() {
let inp = |id: &str, t: &str| Step::Input {
id: id.into(),
text: Some(t.into()),
key: None,
args: None,
};
let mut rec = Recorder::default();
rec.push(inp("f", "A"), None);
rec.push(inp("f", "Ab"), None); rec.push(inp("g", "x"), None); rec.push(Step::Navigate { route: "a".into() }, None);
rec.push(Step::Navigate { route: "b".into() }, Some("B page".into())); assert_eq!(rec.steps.len(), 3);
std::assert_matches!(&rec.steps[0], Step::Input { text: Some(t), .. } if t == "Ab");
std::assert_matches!(&rec.steps[2], Step::Navigate { route } if route == "b");
}
#[test]
fn a_press_and_its_positional_twin_record_once() {
use day_spec::Point;
let tap = |id: &str| Step::Tap {
if_present: false,
id: id.into(),
repeat: Some(1),
at: None,
modifiers: Vec::new(),
};
let press = Event::Pressed;
let gesture = Event::Tap(Point::new(3.0, 4.0));
assert!(!is_press_twin(&press, &tap("inc"), None));
assert!(is_press_twin(&gesture, &tap("inc"), Some("inc")));
assert!(!is_press_twin(&gesture, &tap("list_shuffle"), None));
assert!(!is_press_twin(
&gesture,
&tap("list_reset"),
Some("list-shuffle")
));
assert!(!is_press_twin(&press, &tap("inc"), Some("inc")));
assert!(!is_press_twin(
&Event::SelectionChanged(1),
&Step::Select {
id: "picker".into(),
index: 1,
if_present: false,
},
Some("picker")
));
}
#[test]
fn logging_and_recording_do_not_switch_each_other_off() {
assert!(!is_logging_actions());
assert!(!is_recording());
assert!(!is_observing());
log_actions(true);
assert!(is_logging_actions());
assert!(is_observing(), "logging alone has to run the observers");
assert!(!is_recording(), "logging is not recording");
start(); assert!(is_recording());
assert!(
is_logging_actions(),
"starting a recording must not end the log"
);
stop();
assert!(!is_recording());
assert!(
is_logging_actions(),
"stopping a recording must not end the log"
);
assert!(is_observing());
log_actions(false);
assert!(!is_observing(), "nothing left to watch");
clear();
}
#[test]
fn exclude_prefix_drops_control_ids() {
assert!(is_excluded(Some("scripting-record"), "scripting-"));
assert!(!is_excluded(Some("inc"), "scripting-"));
assert!(!is_excluded(None, "scripting-")); assert!(!is_excluded(Some("scripting-record"), "")); }
}