Skip to main content

day_script/
conformance.rs

1// Copyright © The Daybrite Project
2// SPDX-License-Identifier: MPL-2.0
3
4//! The in-app test runner (docs/testing.md): runs the `#[day::test]` cases the binary
5//! registers, each drive op as the dayscript step it names, and answers the `tests` and
6//! `run_tests` steps `day test` sends. A run is one main-loop task: between a drive's awaited
7//! ops the app's loop turns, so a native transition settles and a capture paints exactly as
8//! they do between a script's steps.
9
10use std::cell::RefCell;
11use std::future::Future;
12use std::pin::Pin;
13use std::rc::Rc;
14use std::task::{Context, Poll};
15
16use day_core::conformance::{
17    Case, Drive, DriveBackend, DriveOp, Fail, NativeExpect, OpFuture, TestKind,
18};
19use serde::{Deserialize, Serialize};
20
21use crate::{Reply, Step, b64encode, next_capture_revision};
22
23pub use day_script_protocol::ShotPolicy;
24
25/// One test's outcome, the shape `conformance.json` records (docs/testing.md).
26#[derive(Clone, Debug, Serialize, Deserialize)]
27pub struct Outcome {
28    pub name: String,
29    pub kind: String,
30    /// `pass`, `fail` or `skip`.
31    pub verdict: String,
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub message: Option<String>,
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub reason: Option<String>,
36    pub ms: u64,
37    pub proves: Vec<String>,
38    /// The captures taken, by name; the PNG bytes ride beside them in [`RunReport::shots`].
39    #[serde(default)]
40    pub shots: Vec<String>,
41    /// Native fields this case asked about that the toolkit could not read (`<id> <field>`):
42    /// the case passed without them, and the evidence says so.
43    #[serde(default, skip_serializing_if = "Vec::is_empty")]
44    pub native_unread: Vec<String>,
45}
46
47/// A capture a test took, for the runner to write beside the evidence.
48#[derive(Clone, Debug, Serialize, Deserialize)]
49pub struct Shot {
50    pub test: String,
51    pub name: String,
52    pub png_base64: String,
53}
54
55/// What a finished run answers.
56#[derive(Clone, Debug, Default, Serialize, Deserialize)]
57pub struct RunReport {
58    pub tests: Vec<Outcome>,
59    #[serde(default)]
60    pub shots: Vec<Shot>,
61    /// What the toolkit answered for every capability during this run (`N`, `E` or `-`), keyed
62    /// by name, so the evidence can be held against the declared coverage matrix.
63    #[serde(default)]
64    pub caps: std::collections::BTreeMap<String, String>,
65}
66
67struct Run {
68    report: RunReport,
69    done: bool,
70}
71
72thread_local! {
73    static RUN: RefCell<Option<Rc<RefCell<Run>>>> = const { RefCell::new(None) };
74}
75
76/// What `tests` answers: every registered test, before any runs.
77///
78/// # Errors
79///
80/// Returns an error when the conformance registry fails validation.
81pub fn listing() -> Result<serde_json::Value, String> {
82    day_core::conformance::validate_registry()?;
83    let cases = day_core::conformance::cases();
84    Ok(serde_json::Value::Array(
85        cases
86            .iter()
87            .map(|c| {
88                serde_json::json!({
89                    "name": c.name(),
90                    "kind": kind_name(c.kind()),
91                    "proves": c.proves_keys(),
92                    "requires": c.requirements().iter().map(|r| format!("{r:?}")).collect::<Vec<_>>(),
93                })
94            })
95            .collect(),
96    ))
97}
98
99/// Answer a `run_tests` step: start the run on its first call, then report "running" until
100/// the task finishes, when the report is handed over once and the run forgotten. Retryable
101/// while running, so the runner's own wait loop polls it.
102///
103/// # Errors
104///
105/// Returns a failing [`Reply`] when the registry fails validation or the report does not serialize,
106/// and a retryable one while the run is still in progress.
107pub fn run_tests(
108    filter: &[String],
109    shots: ShotPolicy,
110    case_timeout_secs: Option<f64>,
111) -> Result<Reply, Reply> {
112    day_core::conformance::validate_registry().map_err(|e| Reply::fail(e, false))?;
113    let run = RUN.with(|r| r.borrow().clone());
114    match run {
115        None => {
116            start(filter.to_vec(), shots, case_timeout_secs);
117            Err(Reply::fail("tests running", true))
118        }
119        Some(run) => {
120            if !run.borrow().done {
121                return Err(Reply::fail("tests running", true));
122            }
123            RUN.with(|r| *r.borrow_mut() = None);
124            let report = std::mem::take(&mut run.borrow_mut().report);
125            let data = serde_json::to_value(&report)
126                .map_err(|e| Reply::fail(format!("test report: {e}"), false))?;
127            Ok(Reply {
128                data: Some(data),
129                ..Reply::ok()
130            })
131        }
132    }
133}
134
135fn kind_name(kind: TestKind) -> &'static str {
136    match kind {
137        TestKind::Gui => "gui",
138        TestKind::Headless => "headless",
139    }
140}
141
142fn matches(filter: &[String], name: &str) -> bool {
143    filter.is_empty() || filter.iter().any(|f| glob(f, name))
144}
145
146/// `*` matches any run of characters; nothing else is special.
147fn glob(pattern: &str, name: &str) -> bool {
148    let mut parts = pattern.split('*');
149    let Some(first) = parts.next() else {
150        return pattern == name;
151    };
152    if !pattern.contains('*') {
153        return pattern == name;
154    }
155    if !name.starts_with(first) {
156        return false;
157    }
158    let mut rest = &name[first.len()..];
159    let parts: Vec<&str> = parts.collect();
160    for (i, part) in parts.iter().enumerate() {
161        if part.is_empty() {
162            continue;
163        }
164        let last = i + 1 == parts.len() && !pattern.ends_with('*');
165        if last {
166            return rest.ends_with(part);
167        }
168        match rest.find(part) {
169            Some(at) => rest = &rest[at + part.len()..],
170            None => return false,
171        }
172    }
173    true
174}
175
176/// A case's time limit when neither it nor the run names one.
177const DEFAULT_CASE_TIMEOUT_SECS: f64 = 30.0;
178
179fn start(filter: Vec<String>, policy: ShotPolicy, case_timeout_secs: Option<f64>) {
180    let run = Rc::new(RefCell::new(Run {
181        report: RunReport::default(),
182        done: false,
183    }));
184    run.borrow_mut().report.caps = day_spec::Cap::ALL
185        .iter()
186        .map(|cap| {
187            let answer = match day_core::capability(*cap) {
188                day_spec::Support::Native => "N",
189                day_spec::Support::Emulated => "E",
190                day_spec::Support::Unsupported => "-",
191            };
192            (format!("{cap:?}"), answer.to_owned())
193        })
194        .collect();
195    RUN.with(|r| *r.borrow_mut() = Some(run.clone()));
196    // Links open nothing during the run; they are recorded for `assert_opened_url`.
197    day_core::conformance::set_intercepting_urls(true);
198    day_core::task(async move {
199        let cases: Vec<Case> = day_core::conformance::cases()
200            .into_iter()
201            .filter(|c| matches(&filter, c.name()))
202            .collect();
203        let mut gui = false;
204        for (i, case) in cases.iter().enumerate() {
205            let mut shots = Vec::new();
206            // Two functions with one name (in two modules, or two crates) would leave the
207            // report, the evidence and every tool that reads them unable to tell them apart.
208            let twin = cases
209                .iter()
210                .enumerate()
211                .any(|(j, c)| j != i && c.name() == case.name());
212            let outcome = if twin {
213                let mut o = outcome_for(case);
214                o.verdict = "fail".into();
215                o.message = Some(format!(
216                    "two #[day::test] functions are named {}; rename one",
217                    case.name()
218                ));
219                o
220            } else {
221                gui |= case.kind() == TestKind::Gui;
222                // Named as it starts, in the app's own log: the report arrives only when the
223                // whole run ends, so a case that takes the app (or the device) down with it
224                // would otherwise leave nothing that says which one it was.
225                log::info!("conformance: {} ({}/{})", case.name(), i + 1, cases.len());
226                run_case(case, policy, case_timeout_secs, &mut shots).await
227            };
228            let mut run = run.borrow_mut();
229            run.report.shots.extend(shots);
230            run.report.tests.push(outcome);
231        }
232        // The app's own content again, once, rather than between cases.
233        if gui {
234            day_core::conformance::clear_active();
235        }
236        day_core::conformance::set_intercepting_urls(false);
237        run.borrow_mut().done = true;
238    });
239}
240
241fn outcome_for(case: &Case) -> Outcome {
242    Outcome {
243        name: case.name().to_owned(),
244        kind: kind_name(case.kind()).to_owned(),
245        verdict: "pass".into(),
246        message: None,
247        reason: None,
248        ms: 0,
249        proves: case.proves_keys().to_vec(),
250        shots: Vec::new(),
251        native_unread: Vec::new(),
252    }
253}
254
255async fn run_case(
256    case: &Case,
257    policy: ShotPolicy,
258    run_timeout_secs: Option<f64>,
259    shots: &mut Vec<Shot>,
260) -> Outcome {
261    let started = Stopwatch::start();
262    let mut outcome = outcome_for(case);
263    // A requirement the toolkit lacks is a skip with its reason, never a failure: this is the
264    // one way a case adapts to a toolkit (docs/testing.md).
265    if let Some(cap) = case
266        .requirements()
267        .iter()
268        .find(|c| day_core::capability(**c) == day_spec::Support::Unsupported)
269    {
270        outcome.verdict = "skip".into();
271        outcome.reason = Some(format!("Cap::{cap:?} is Unsupported on this toolkit"));
272        return outcome;
273    }
274    if let Some(what) = case.missing_support() {
275        outcome.verdict = "skip".into();
276        outcome.reason = Some(format!("{what} is Unsupported on this toolkit"));
277        return outcome;
278    }
279    let backend = Rc::new(Engine {
280        shots: Rc::new(RefCell::new(Vec::new())),
281        unread: Rc::new(RefCell::new(Vec::new())),
282    });
283    let drive = Drive::new(backend.clone());
284    // A run's `case_timeout_secs: 0` turns limits off (a debugger holding a breakpoint);
285    // otherwise the case's own limit, then the run's, then the default.
286    let limit = match run_timeout_secs {
287        Some(t) if t <= 0.0 => None,
288        run => Some(
289            case.timeout_secs()
290                .or(run)
291                .unwrap_or(DEFAULT_CASE_TIMEOUT_SECS),
292        ),
293    };
294    let body = {
295        let (case, drive) = (case.clone(), drive.clone());
296        async move {
297            if case.kind() == TestKind::Gui {
298                // The test host shows THIS case's page alone, built fresh, sharing its signals
299                // with the drive.
300                if !day_core::conformance::set_active(case.clone()) {
301                    return Err(Fail(
302                        "the app shows no test host: root its test build in \
303                         `day::test_host(..)` (docs/testing.md)"
304                            .into(),
305                    ));
306                }
307                day_reactive::flush_sync();
308                drive.wait_idle().await?;
309                if let Some(message) = day_core::conformance::take_page_panic() {
310                    return Err(Fail(format!("the page panicked: {message}")));
311                }
312                for name in case.shots() {
313                    drive.shot(name).await?;
314                }
315            }
316            case.run_body(drive).await
317        }
318    };
319    let result = Guarded::new(Box::pin(body), limit).await;
320    // A kind the case proves that rendered a placeholder has no renderer on this toolkit (a
321    // piece without an arm here): the case does not apply, as when a `Cap` is missing.
322    let placeholders = day_spec::placeholder::seen();
323    if let Some(kind) = case.proves_keys().iter().find_map(|k| {
324        k.strip_prefix("kind:")
325            .filter(|kind| placeholders.iter().any(|seen| seen == kind))
326    }) {
327        outcome.verdict = "skip".into();
328        outcome.reason = Some(format!("no {kind} renderer on this toolkit"));
329        outcome.ms = started.elapsed_ms();
330        return outcome;
331    }
332    if let Err(Fail(message)) = &result {
333        outcome.verdict = "fail".into();
334        outcome.message = Some(message.clone());
335        if policy != ShotPolicy::Never {
336            // What the screen showed when the assertion failed, the first thing anyone asks.
337            let _ = drive.shot("failed").await;
338        }
339    }
340    outcome.ms = started.elapsed_ms();
341    outcome.native_unread = std::mem::take(&mut *backend.unread.borrow_mut());
342    outcome.native_unread.dedup();
343    let keep = match policy {
344        ShotPolicy::Never => false,
345        ShotPolicy::OnFailure => result.is_err(),
346        ShotPolicy::Always => true,
347    };
348    if keep {
349        for (name, png) in backend.shots.borrow_mut().drain(..) {
350            outcome.shots.push(name.clone());
351            shots.push(Shot {
352                test: case.name().to_owned(),
353                name,
354                png_base64: b64encode(&png),
355            });
356        }
357    }
358    outcome
359}
360
361/// A case's run with its failures contained: a panic in the body becomes the case's failure
362/// instead of the app's crash, and a body still pending at the limit fails as timed out. The
363/// limit is a `day_core::sleep` raced against the body, so it is counted in the app's own
364/// timers and needs no clock (std's `Instant` panics on wasm). Panics are caught where the
365/// platform unwinds; a target built to abort on panic still aborts.
366struct Guarded {
367    body: Pin<Box<dyn Future<Output = Result<(), Fail>>>>,
368    limit: Option<(f64, Pin<Box<day_core::Sleep>>)>,
369}
370
371impl Guarded {
372    fn new(body: Pin<Box<dyn Future<Output = Result<(), Fail>>>>, secs: Option<f64>) -> Self {
373        Guarded {
374            body,
375            limit: secs.map(|s| (s, Box::pin(day_core::sleep((s * 1000.0) as u32)))),
376        }
377    }
378}
379
380impl Future for Guarded {
381    type Output = Result<(), Fail>;
382
383    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
384        let this = &mut *self;
385        let polled =
386            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| this.body.as_mut().poll(cx)));
387        match polled {
388            Ok(Poll::Ready(result)) => return Poll::Ready(result),
389            Err(payload) => {
390                let message = day_core::conformance::panic_message(payload.as_ref());
391                return Poll::Ready(Err(Fail(format!("panicked: {message}"))));
392            }
393            Ok(Poll::Pending) => {}
394        }
395        if let Some((secs, timer)) = &mut this.limit
396            && timer.as_mut().poll(cx).is_ready()
397        {
398            return Poll::Ready(Err(Fail(format!("timed out after {secs} s"))));
399        }
400        Poll::Pending
401    }
402}
403
404/// The engine's [`DriveBackend`]: each op is the dayscript step of the same name, executed on
405/// the main thread and retried through the step's own wait window with the main loop turning
406/// in between, so a drive sees the app settle the way a script does.
407///
408/// In a conformance run the Day-side assertions also check the native widget: `assert_text`,
409/// `assert_value` and `assert_on` are each followed by the `assert_native` of the same fact,
410/// so a pass means the platform's widget shows it too, not only Day's tree.
411struct Engine {
412    shots: Rc<RefCell<Vec<Capture>>>,
413    /// `<id> <field>` for each native field the toolkit could not read.
414    unread: Rc<RefCell<Vec<String>>>,
415}
416
417/// A capture a drive took: its name and the PNG.
418type Capture = (String, Vec<u8>);
419
420impl DriveBackend for Engine {
421    fn run(&self, op: DriveOp) -> OpFuture {
422        let (shots, unread) = (self.shots.clone(), self.unread.clone());
423        Box::pin(async move {
424            if let DriveOp::Pause(secs) = op {
425                day_core::sleep((secs * 1000.0) as u32).await;
426                return Ok(());
427            }
428            run_step(&op, &shots, &unread).await?;
429            if let Some(native) = native_follow_up(&op) {
430                run_step(&native, &shots, &unread).await?;
431            }
432            Ok(())
433        })
434    }
435}
436
437/// The native check that follows a Day-side assertion in a conformance run.
438fn native_follow_up(op: &DriveOp) -> Option<DriveOp> {
439    let (id, expect) = match op {
440        DriveOp::AssertText(id, text) => (
441            id,
442            NativeExpect {
443                text: Some(text.clone()),
444                ..Default::default()
445            },
446        ),
447        DriveOp::AssertValue(id, value) => (
448            id,
449            NativeExpect {
450                number: Some(*value),
451                ..Default::default()
452            },
453        ),
454        DriveOp::AssertOn(id, on) => (
455            id,
456            NativeExpect {
457                checked: Some(*on),
458                ..Default::default()
459            },
460        ),
461        _ => return None,
462    };
463    Some(DriveOp::AssertNative(id.clone(), expect))
464}
465
466/// Run one op as its dayscript step through the step's retry window.
467async fn run_step(
468    op: &DriveOp,
469    shots: &Rc<RefCell<Vec<Capture>>>,
470    unread: &Rc<RefCell<Vec<String>>>,
471) -> Result<(), Fail> {
472    let step = step_for(op)?;
473    // The wait is the sleeps, or the clock where there is one, whichever says more: an attempt
474    // can itself be slow (a capture), and counted in sleeps alone a failing one would retry past
475    // the case's limit and report a timeout instead of its own message. The browser has no
476    // `Instant` (std's panics on wasm), so there the sleeps are the whole count.
477    let clock = Stopwatch::start();
478    let mut waited_ms = 0u32;
479    let budget_ms = (crate::DEFAULT_TIMEOUT_SECS * 1000.0) as u32;
480    // One capture revision for the whole wait, as the socket runner keeps: a capture's
481    // checkpoint is armed under it, and a fresh one per retry would never see it land.
482    let revision = next_capture_revision();
483    loop {
484        let reply = crate::exec(step.clone(), revision);
485        if reply.ok {
486            if let (DriveOp::Shot(name), Some(png)) = (op, &reply.png_base64) {
487                shots
488                    .borrow_mut()
489                    .push((name.clone(), crate::b64decode(png)));
490            }
491            let checked = match op {
492                DriveOp::AssertNative(id, _)
493                | DriveOp::AssertFrame(id, _)
494                | DriveOp::AssertDisplayed(id, _)
495                | DriveOp::SamplePixel(id, ..) => Some(id),
496                _ => None,
497            };
498            if let (Some(id), Some(data)) = (checked, &reply.data) {
499                let fields = data.get("unread").and_then(|u| u.as_array());
500                for f in fields.into_iter().flatten().filter_map(|f| f.as_str()) {
501                    unread.borrow_mut().push(format!("{id} {f}"));
502                }
503            }
504            return Ok(());
505        }
506        let message = reply.error.unwrap_or_else(|| "failed".into());
507        let elapsed_ms = u32::try_from(clock.elapsed_ms()).unwrap_or(u32::MAX);
508        if !reply.retryable || waited_ms.max(elapsed_ms) >= budget_ms {
509            return Err(Fail(format!("{}: {message}", op_name(op))));
510        }
511        let wait = if reply.capture_pending {
512            16
513        } else {
514            crate::RETRY_MS
515        };
516        waited_ms += wait;
517        day_core::sleep(wait).await;
518    }
519}
520
521fn op_name(op: &DriveOp) -> String {
522    match op {
523        DriveOp::Tap(id) | DriveOp::TapAt(id, ..) => format!("tap {id}"),
524        DriveOp::Input(id, _) => format!("input {id}"),
525        DriveOp::Toggle(id, _) => format!("toggle {id}"),
526        DriveOp::SetValue(id, _) => format!("set_value {id}"),
527        DriveOp::Select(id, _) => format!("select {id}"),
528        DriveOp::Focus(id) => format!("focus {id}"),
529        DriveOp::Submit(id) => format!("submit {id}"),
530        DriveOp::Navigate(route) => format!("navigate {route}"),
531        DriveOp::WaitIdle => "wait_idle".into(),
532        DriveOp::Pause(_) => "pause".into(),
533        DriveOp::Shot(name) => format!("shot {name}"),
534        DriveOp::AssertText(id, _) => format!("assert_text {id}"),
535        DriveOp::AssertVisible(id) => format!("assert_visible {id}"),
536        DriveOp::AssertMissing(id) => format!("assert_missing {id}"),
537        DriveOp::AssertHidden(id) => format!("assert_hidden {id}"),
538        DriveOp::Hover(id, _) | DriveOp::HoverLeave(id) => format!("hover {id}"),
539        DriveOp::Pan(id, ..) => format!("pan {id}"),
540        DriveOp::Pinch(id, _) => format!("pinch {id}"),
541        DriveOp::Rotate(id, _) => format!("rotate {id}"),
542        DriveOp::Drag(id, ..) | DriveOp::Fling(id, ..) => format!("drag {id}"),
543        DriveOp::Key(_, key) | DriveOp::KeyPhase(_, key, _) => format!("key {key}"),
544        DriveOp::Press(id, _) => format!("press {id}"),
545        DriveOp::LongPress(id) => format!("long_press {id}"),
546        DriveOp::DoubleTap(id) => format!("double_tap {id}"),
547        DriveOp::Touches(id, ..) => format!("touches {id}"),
548        DriveOp::Pointer(kind) => format!("pointer {kind}"),
549        DriveOp::DropText(id, ..) | DriveOp::DropFiles(id, _) => format!("drop {id}"),
550        DriveOp::AssertAnnounced(text) => format!("assert_announced {text}"),
551        DriveOp::AssertValue(id, _) | DriveOp::AssertOn(id, _) => format!("assert_value {id}"),
552        DriveOp::AssertFocused(id, _) => format!("assert_focused {id}"),
553        DriveOp::AssertRoute(route) => format!("assert_route {route}"),
554        DriveOp::A11yAudit(_) => "a11y_audit".into(),
555        DriveOp::AssertNative(id, _) => format!("assert_native {id}"),
556        DriveOp::AssertFrame(id, _) => format!("assert_frame {id}"),
557        DriveOp::AssertDisplayed(id, _) => format!("assert_displayed {id}"),
558        DriveOp::SamplePixel(id, ..) => format!("sample_pixel {id}"),
559        DriveOp::AssertOpenedUrl(url) => format!("assert_opened_url {url}"),
560        DriveOp::Activate(id, _) => format!("activate {id}"),
561        DriveOp::Reorder(id, ..) => format!("reorder {id}"),
562        DriveOp::DeleteRow(id, _) => format!("delete_row {id}"),
563        DriveOp::SwipeRow(id, ..) => format!("swipe_row {id}"),
564        DriveOp::ScrollTo(id, _) => format!("scroll_to {id}"),
565        DriveOp::Expand(id, row, _) => format!("expand {id} {row}"),
566        DriveOp::NavBack => "nav_back".into(),
567        DriveOp::TreeMove(id, row, ..) => format!("tree_move {id} {row}"),
568        DriveOp::ContextMenu(id, item) => format!("context_menu {id} {item}"),
569        DriveOp::Menu(item) => format!("menu {item}"),
570        DriveOp::ToolbarPress(item) | DriveOp::ToolbarToggle(item, _) => format!("toolbar {item}"),
571        DriveOp::DialogScripted(_) => "dialog_mode".into(),
572        DriveOp::AssertPresented(_) => "assert_presented".into(),
573        DriveOp::Respond(_) | DriveOp::RespondText(_) => "respond".into(),
574    }
575}
576
577/// The dayscript step an op stands for, built through the script format so the two can
578/// never mean different things.
579fn step_for(op: &DriveOp) -> Result<Step, Fail> {
580    use serde_json::json;
581    let v = match op {
582        DriveOp::Tap(id) => json!({"op": "tap", "id": id}),
583        DriveOp::Input(id, text) => json!({"op": "input", "id": id, "text": text}),
584        DriveOp::Toggle(id, on) => json!({"op": "toggle", "id": id, "value": on}),
585        DriveOp::SetValue(id, value) => json!({"op": "set_value", "id": id, "value": value}),
586        DriveOp::Select(id, index) => json!({"op": "select", "id": id, "index": index}),
587        DriveOp::Focus(id) => json!({"op": "focus", "id": id}),
588        DriveOp::Submit(id) => json!({"op": "submit", "id": id}),
589        DriveOp::Navigate(route) => json!({"op": "navigate", "route": route}),
590        DriveOp::WaitIdle => json!({"op": "wait_idle"}),
591        DriveOp::Pause(secs) => json!({"op": "pause", "secs": secs}),
592        DriveOp::Shot(name) => json!({"op": "screenshot", "name": name, "in_process": true}),
593        DriveOp::AssertText(id, text) => json!({"op": "assert_text", "id": id, "text": text}),
594        DriveOp::AssertVisible(id) => json!({"op": "assert_visible", "id": id}),
595        DriveOp::AssertMissing(id) => json!({"op": "assert_missing", "id": id}),
596        DriveOp::AssertHidden(id) => json!({"op": "assert_hidden", "id": id}),
597        DriveOp::Hover(id, at) => json!({"op": "hover", "id": id, "at": at.map(|(x, y)| [x, y])}),
598        DriveOp::HoverLeave(id) => json!({"op": "hover", "id": id, "leave": true}),
599        DriveOp::Pan(id, dx, dy) => json!({"op": "pan", "id": id, "by": [dx, dy]}),
600        DriveOp::Pinch(id, scale) => json!({"op": "pinch", "id": id, "scale": scale}),
601        DriveOp::Rotate(id, rotation) => {
602            json!({"op": "rotate", "id": id, "rotation": rotation})
603        }
604        DriveOp::Drag(id, (fx, fy), (tx, ty)) => {
605            json!({"op": "drag", "id": id, "from": [fx, fy], "to": [tx, ty]})
606        }
607        DriveOp::TapAt(id, x, y) => json!({"op": "tap", "id": id, "at": [x, y]}),
608        DriveOp::Key(id, key) => json!({"op": "key", "id": id, "key": key}),
609        DriveOp::KeyPhase(id, key, phase) => {
610            json!({"op": "key", "id": id, "key": key, "phase": phase})
611        }
612        DriveOp::Press(id, pressed) => json!({"op": "press", "id": id, "release": !pressed}),
613        DriveOp::LongPress(id) => json!({"op": "long_press", "id": id}),
614        DriveOp::DoubleTap(id) => json!({"op": "double_tap", "id": id}),
615        DriveOp::Touches(id, points, phase) => {
616            let points: Vec<[f64; 2]> = points.iter().map(|(x, y)| [*x, *y]).collect();
617            json!({"op": "touches", "id": id, "points": points, "phase": phase})
618        }
619        DriveOp::Fling(id, (fx, fy), (tx, ty), (vx, vy)) => {
620            json!({"op": "drag", "id": id, "from": [fx, fy], "to": [tx, ty], "velocity": [vx, vy]})
621        }
622        DriveOp::Pointer(kind) => json!({"op": "pointer", "kind": kind}),
623        DriveOp::DropText(id, text, phase) => {
624            json!({"op": "drop", "id": id, "text": text, "phase": phase})
625        }
626        DriveOp::DropFiles(id, files) => json!({"op": "drop", "id": id, "files": files}),
627        DriveOp::AssertAnnounced(text) => json!({"op": "assert_announced", "text": text}),
628        DriveOp::AssertValue(id, value) => json!({"op": "assert_value", "id": id, "value": value}),
629        DriveOp::AssertOn(id, on) => json!({"op": "assert_value", "id": id, "value": on}),
630        DriveOp::AssertFocused(id, focused) => {
631            json!({"op": "assert_focused", "id": id, "focused": focused})
632        }
633        DriveOp::AssertRoute(route) => json!({"op": "assert_route", "route": route}),
634        DriveOp::A11yAudit(id) => json!({"op": "a11y_audit", "id": id}),
635        DriveOp::AssertNative(id, e) => json!({
636            "op": "assert_native", "id": id, "text": e.text, "number": e.number,
637            "checked": e.checked, "enabled": e.enabled, "visible": e.visible,
638        }),
639        DriveOp::AssertDisplayed(id, e) => json!({
640            "op": "assert_displayed", "id": id, "opacity": e.opacity,
641            "tx": e.translation.map(|t| t.x), "ty": e.translation.map(|t| t.y),
642            "width": e.width, "height": e.height,
643            "background": e.background.map(|c| {
644                let byte = |v: f64| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
645                format!("#{:02x}{:02x}{:02x}", byte(c.r), byte(c.g), byte(c.b))
646            }),
647            "tolerance": e.tolerance,
648        }),
649        DriveOp::AssertFrame(id, f) => json!({
650            "op": "assert_frame", "id": id, "width": f.width, "height": f.height,
651            "x": f.x, "y": f.y, "relative_to": f.relative_to, "tolerance": f.tolerance,
652        }),
653        DriveOp::SamplePixel(id, x, y, color) => {
654            json!({"op": "sample_pixel", "id": id, "x": x, "y": y, "color": color})
655        }
656        DriveOp::AssertOpenedUrl(url) => json!({"op": "assert_opened_url", "url": url}),
657        DriveOp::Activate(id, index) => json!({"op": "activate", "id": id, "index": index}),
658        DriveOp::Reorder(id, from, to) => {
659            json!({"op": "reorder", "id": id, "from": from, "to": to})
660        }
661        DriveOp::DeleteRow(id, row) => json!({"op": "delete_row", "id": id, "row": row}),
662        DriveOp::SwipeRow(id, row, leading, action) => json!({
663            "op": "swipe_row", "id": id, "row": row, "action": action,
664            "edge": if *leading { "leading" } else { "trailing" },
665        }),
666        // An empty edge is a reveal (`Drive::reveal`).
667        DriveOp::ScrollTo(id, edge) if edge.is_empty() => json!({"op": "scroll_to", "id": id}),
668        DriveOp::ScrollTo(id, edge) => json!({"op": "scroll_to", "id": id, "edge": edge}),
669        DriveOp::Expand(id, row, expanded) => {
670            json!({"op": "expand", "id": id, "row": row, "expanded": expanded})
671        }
672        DriveOp::NavBack => json!({"op": "nav_back"}),
673        DriveOp::TreeMove(id, row, parent, index) => json!({
674            "op": "tree_move", "id": id, "row": row, "parent": parent, "index": index,
675        }),
676        DriveOp::ContextMenu(id, item) => json!({"op": "context_menu", "id": id, "item": item}),
677        DriveOp::Menu(item) => json!({"op": "menu", "id": item}),
678        DriveOp::ToolbarPress(item) => json!({"op": "toolbar", "item": item}),
679        DriveOp::ToolbarToggle(item, on) => json!({"op": "toolbar", "item": item, "on": on}),
680        DriveOp::DialogScripted(scripted) => json!({
681            "op": "dialog_mode", "mode": if *scripted { "scripted" } else { "native" },
682        }),
683        DriveOp::AssertPresented(title) => json!({"op": "assert_presented", "title": title}),
684        DriveOp::Respond(Some(button)) => json!({"op": "respond", "button": button}),
685        DriveOp::Respond(None) => json!({"op": "respond", "dismiss": true}),
686        DriveOp::RespondText(text) => json!({"op": "respond", "text": text}),
687    };
688    serde_json::from_value(v).map_err(|e| Fail(format!("{}: {e}", op_name(op))))
689}
690
691/// A test's wall-clock span, where the platform keeps one. `std::time::Instant` panics on
692/// wasm, so the browser reports no timing rather than a fake one.
693struct Stopwatch {
694    #[cfg(not(target_arch = "wasm32"))]
695    started: std::time::Instant,
696}
697
698impl Stopwatch {
699    fn start() -> Self {
700        Stopwatch {
701            #[cfg(not(target_arch = "wasm32"))]
702            started: std::time::Instant::now(),
703        }
704    }
705    #[cfg_attr(
706        target_arch = "wasm32",
707        expect(
708            clippy::unused_self,
709            reason = "wasm has no clock; the native arm reads the start time"
710        )
711    )]
712    fn elapsed_ms(&self) -> u64 {
713        #[cfg(not(target_arch = "wasm32"))]
714        {
715            self.started.elapsed().as_millis() as u64
716        }
717        #[cfg(target_arch = "wasm32")]
718        {
719            0
720        }
721    }
722}
723
724#[cfg(test)]
725mod tests {
726    use super::glob;
727
728    #[test]
729    fn globs_match_prefixes_suffixes_and_middles() {
730        assert!(glob("button-*", "button-status"));
731        assert!(glob("*-status", "button-status"));
732        assert!(glob("button*status", "button-status"));
733        assert!(glob("*", "anything"));
734        assert!(glob("button-status", "button-status"));
735        assert!(!glob("button-*", "slider-range"));
736        assert!(!glob("button", "button-status"));
737    }
738}