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