day-script 0.7.9

Declarative app development API using native UI toolkits
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The mock harness for `#[day::test]` cases (docs/testing.md): every case the built-in
//! pieces register, driven by the real engine against the mock toolkit. What `day test` runs on
//! a toolkit, this runs under `cargo test`, so a broken binding or id fails here first.

use day_core::conformance::{Case, Drive, TestFn};
use day_mock::MockToolkit;
use day_pieces::Decorate;
use day_script::conformance::{Outcome, RunReport, ShotPolicy};
use day_script::{Reply, Step};
use day_spec::{Size, WindowOptions};

fn boot() -> day_mock::MockProbe {
    boot_with(|| {
        day_core::AnyPiece::new(day_pieces::test_host(day_pieces::conformance::test_pages))
    })
}

fn boot_with(root: impl FnOnce() -> day_core::AnyPiece + 'static) -> day_mock::MockProbe {
    day_core::uninstall_tree();
    let (mock, probe) = MockToolkit::new();
    // No test drives the mock's native side here (binding rows, reporting covers), so it does
    // that by itself.
    probe.set_native_behavior(20);
    // The mock groups windows like AppKit does (recorded, never drawn), so the document tabs'
    // window-group case runs here rather than skipping on `Cap::WindowTabbing`.
    probe.state.borrow_mut().native_window_tabs = true;
    // And realizes list items natively, so their case runs here too.
    probe.set_native_list_items(true);
    day_core::launch_with(
        mock,
        WindowOptions {
            title: "conformance".into(),
            size: Size::new(400.0, 600.0),
            ..Default::default()
        },
        root,
    );
    probe
}

/// Drive `run_tests` to its report: the first answer starts the run, and on the mock the whole
/// run completes inside it, so the next answer carries the report.
fn run(filter: &[&str]) -> RunReport {
    // No time limits: the mock's timers fire the moment they are set, so a limit would trip
    // on the first retry. `a_case_past_its_limit_fails_as_timed_out` runs with them on.
    run_with(filter, Some(0.0))
}

fn run_with(filter: &[&str], case_timeout_secs: Option<f64>) -> RunReport {
    let step = Step::RunTests {
        filter: filter
            .iter()
            .map(std::string::ToString::to_string)
            .collect(),
        shots: ShotPolicy::Never,
        timeout_secs: None,
        case_timeout_secs,
    };
    for _ in 0..100 {
        let reply: Reply = day_script::step(step.clone());
        if reply.ok {
            let data = reply.data.expect("a finished run answers its report");
            return serde_json::from_value(data).expect("the report decodes");
        }
        assert!(reply.retryable, "{:?}", reply.error);
    }
    panic!("the run never finished");
}

#[test]
fn every_built_in_case_passes_on_the_mock() {
    let _probe = boot();
    let listing = day_script::step(Step::Tests)
        .data
        .expect("tests lists the registry");
    let names: Vec<&str> = listing
        .as_array()
        .expect("a list")
        .iter()
        .filter_map(|t| t.get("name").and_then(|n| n.as_str()))
        .collect();
    assert!(names.contains(&"button-press"), "{names:?}");

    let report = run(&[]);
    assert_eq!(report.tests.len(), names.len());
    let failed: Vec<String> = report
        .tests
        .iter()
        .filter(|t| t.verdict != "pass")
        .map(|t| {
            format!(
                "{}: {} {}",
                t.name,
                t.verdict,
                t.message.clone().or(t.reason.clone()).unwrap_or_default()
            )
        })
        .collect();
    assert!(failed.is_empty(), "{failed:#?}");
}

#[test]
fn a_filter_selects_by_glob() {
    let _probe = boot();
    let report = run(&["text-field-*"]);
    let names: Vec<&str> = report.tests.iter().map(|t| t.name.as_str()).collect();
    assert_eq!(
        names,
        [
            "text-field-binding",
            "text-field-disabled",
            "text-field-max-length",
            "text-field-read-only",
            "text-field-secure",
            "text-field-submit"
        ]
    );
    assert!(
        report.tests.iter().all(|t| t.verdict == "pass"),
        "{report:?}"
    );
}

fn only(report: &RunReport, name: &str) -> Outcome {
    let found: Vec<&Outcome> = report.tests.iter().filter(|t| t.name == name).collect();
    assert_eq!(found.len(), 1, "{report:?}");
    found[0].clone()
}

// Harness-only cases, registered at run time on this thread (the registry is per thread, so
// they never reach the built-in run above).

fn panics_in_its_drive() -> Case {
    Case::new()
        .page(|| day_pieces::label("here").id(day_core::id!(pd_label)))
        .drive(|d: Drive| async move {
            d.assert_text("pd_label", "here").await?;
            panic!("the drive gave up");
        })
}

fn panics_in_its_page() -> Case {
    Case::new()
        .page(|| -> day_pieces::Label { panic!("no page today") })
        .drive(|_d: Drive| async move { Ok(()) })
}

fn panics_headless() -> Case {
    Case::headless().run(|_t: Drive| async move { panic!("headless boom") })
}

fn never_finishes() -> Case {
    Case::headless().timeout(0.05).run(|_t: Drive| async move {
        std::future::pending::<day_core::conformance::TestResult>().await
    })
}

fn shares_a_name() -> Case {
    Case::headless()
}

mod elsewhere {
    pub fn shares_a_name() -> day_core::conformance::Case {
        day_core::conformance::Case::headless()
    }
}

#[test]
fn a_panic_fails_its_case_and_the_run_goes_on() {
    let _probe = boot();
    for t in [
        TestFn::new("panics_in_its_drive", panics_in_its_drive),
        TestFn::new("panics_in_its_page", panics_in_its_page),
        TestFn::new("panics_headless", panics_headless),
    ] {
        day_core::conformance::register(t);
    }
    let report = run(&["panics-*", "button-press"]);
    let drive = only(&report, "panics-in-its-drive");
    assert_eq!(drive.verdict, "fail");
    assert!(
        drive
            .message
            .as_deref()
            .unwrap_or("")
            .contains("the drive gave up"),
        "{drive:?}"
    );
    let page = only(&report, "panics-in-its-page");
    assert_eq!(page.verdict, "fail");
    assert!(
        page.message
            .as_deref()
            .unwrap_or("")
            .contains("no page today"),
        "{page:?}"
    );
    let headless = only(&report, "panics-headless");
    assert!(
        headless
            .message
            .as_deref()
            .unwrap_or("")
            .contains("headless boom"),
        "{headless:?}"
    );
    // The cases after a panic still run, on a page built fresh for them.
    assert_eq!(only(&report, "button-press").verdict, "pass");
}

#[test]
fn a_case_past_its_limit_fails_as_timed_out() {
    let _probe = boot();
    day_core::conformance::register(TestFn::new("never_finishes", never_finishes));
    let report = run_with(&["never-finishes"], None);
    let t = only(&report, "never-finishes");
    assert_eq!(t.verdict, "fail");
    assert!(
        t.message.as_deref().unwrap_or("").contains("timed out"),
        "{t:?}"
    );
}

#[test]
fn two_tests_with_one_name_both_fail() {
    let _probe = boot();
    day_core::conformance::register(TestFn::new("shares_a_name", shares_a_name));
    day_core::conformance::register(TestFn::new(
        "elsewhere::shares_a_name",
        elsewhere::shares_a_name,
    ));
    let report = run(&["shares-a-name"]);
    assert_eq!(report.tests.len(), 2, "{report:?}");
    assert!(
        report.tests.iter().all(|t| t.verdict == "fail"),
        "{report:?}"
    );
}

#[test]
fn a_gui_case_needs_a_test_host() {
    let _probe = boot_with(|| day_core::AnyPiece::new(day_pieces::label("no host here")));
    let report = run(&["button-press"]);
    let t = only(&report, "button-press");
    assert_eq!(t.verdict, "fail");
    assert!(
        t.message.as_deref().unwrap_or("").contains("test host"),
        "{t:?}"
    );
}

#[test]
fn test_names_come_from_the_function() {
    assert_eq!(
        TestFn::new("button_press", shares_a_name).name(),
        "button-press"
    );
    assert_eq!(
        TestFn::new("a :: b :: text_field_secure", shares_a_name).name(),
        "text-field-secure"
    );
}

fn reads_what_the_mock_cannot() -> Case {
    Case::new()
        .page(|| day_pieces::label("here").id(day_core::id!(unread_label)))
        .drive(|d: Drive| async move {
            d.assert_native(
                "unread_label",
                day_core::conformance::NativeExpect {
                    visible: Some(true),
                    ..Default::default()
                },
            )
            .await
        })
}

#[test]
fn a_native_field_the_toolkit_cannot_read_is_recorded_not_failed() {
    let _probe = boot();
    day_core::conformance::register(TestFn::new(
        "reads_what_the_mock_cannot",
        reads_what_the_mock_cannot,
    ));
    let t = only(
        &run(&["reads-what-the-mock-cannot"]),
        "reads-what-the-mock-cannot",
    );
    assert_eq!(t.verdict, "pass", "{t:?}");
    assert_eq!(t.native_unread, ["unread_label visible"]);
    // The default native follow-up ran on every built-in case without leaving gaps on the
    // mock, whose reader answers every field the leaves assert.
    let report = run(&["button-*", "text-field-*", "toggle-*", "slider-*"]);
    for t in &report.tests {
        assert!(t.native_unread.is_empty(), "{t:?}");
    }
}

#[test]
fn picker_selection_rejects_invalid_indices_without_changing_selection() {
    let _probe = boot_with(|| {
        day_core::AnyPiece::new(
            day_pieces::picker(
                day_reactive::Signal::new(0usize),
                [(0usize, "First"), (1, "Second")],
            )
            .id(day_core::id!(picker_bounds)),
        )
    });
    for index in [-1, 2, 999] {
        let reply = day_script::step(Step::Select {
            id: "picker_bounds".into(),
            index,
            if_present: false,
        });
        assert!(!reply.ok && !reply.retryable, "{reply:?}");
        assert!(reply.error.unwrap().contains("out of range for 2 options"));
    }
    let current: Step = serde_json::from_value(serde_json::json!({
        "op": "assert_text", "id": "picker_bounds", "text": "First"
    }))
    .unwrap();
    assert!(day_script::step(current).ok);
    assert!(
        day_script::step(Step::Select {
            id: "picker_bounds".into(),
            index: 1,
            if_present: false,
        })
        .ok
    );
    let current: Step = serde_json::from_value(serde_json::json!({
        "op": "assert_text", "id": "picker_bounds", "text": "Second"
    }))
    .unwrap();
    assert!(day_script::step(current).ok);
}

/// A disabled menu item is retryable, as a disabled `tap` target is: its enabled state follows
/// app state that may still be arriving, so the bounded wait gets to see it enable.
#[test]
fn a_disabled_menu_item_is_retried_not_failed() {
    use day_pieces::Decorate;
    let enabled = day_reactive::Signal::global(false);
    let _probe = boot_with(move || {
        day_core::AnyPiece::new(
            day_pieces::label("Host")
                .context_menu(move || {
                    vec![
                        day_pieces::menu_item("Go")
                            .enabled(move || enabled.get())
                            .action(|| {})
                            .id(day_core::id!(go_item)),
                    ]
                })
                .id(day_core::id!(menu_host)),
        )
    });
    let choose: Step = serde_json::from_value(serde_json::json!({
        "op": "context_menu", "id": "menu_host", "item_id": "go_item"
    }))
    .unwrap();
    let reply = day_script::step(choose.clone());
    assert!(!reply.ok && reply.retryable, "{reply:?}");
    enabled.set(true);
    day_reactive::flush_sync();
    assert!(day_script::step(choose).ok);
}

/// The animation cases catch each way an animator can fall short of the `AnimSpec` contract
/// (docs/tween.md "The animation contract"). The mock's animator is the contract itself, and
/// `AnimFlaw` breaks it one field at a time: every case passes on the faithful animator with
/// nothing left unread, and each flaw fails the cases that exist to notice it and no others.
#[test]
fn the_animation_cases_catch_a_faulty_animator() {
    use day_mock::AnimFlaw;
    const CASES: [&str; 8] = [
        "animation-autoreverse-plays-back",
        "animation-background-passes-between",
        "animation-delay-holds-the-start",
        "animation-frame-passes-between",
        "animation-repeat-plays-every-cycle",
        "animation-repeats-forever-until-set",
        "animation-retargets-from-the-value-shown",
        "animation-spring-overshoots-and-settles",
    ];
    let probe = boot();
    let verdicts = |flaw: Option<AnimFlaw>| -> Vec<(String, bool)> {
        probe.set_animation_flaw(flaw);
        let report = run(&["animation-*"]);
        let names: Vec<&str> = report.tests.iter().map(|t| t.name.as_str()).collect();
        for case in CASES {
            assert!(names.contains(&case), "{case} did not run: {names:?}");
        }
        report
            .tests
            .iter()
            .filter(|t| CASES.contains(&t.name.as_str()))
            .map(|t| {
                assert!(
                    t.native_unread.is_empty(),
                    "{}: {:?}",
                    t.name,
                    t.native_unread
                );
                (t.name.clone(), t.verdict == "pass")
            })
            .collect()
    };
    let failed = |flaw: AnimFlaw| -> Vec<String> {
        verdicts(Some(flaw))
            .into_iter()
            .filter(|(_, passed)| !passed)
            .map(|(name, _)| name)
            .collect()
    };

    let faithful = verdicts(None);
    assert!(faithful.iter().all(|(_, passed)| *passed), "{faithful:?}");

    assert_eq!(
        failed(AnimFlaw::IgnoresDelay),
        ["animation-delay-holds-the-start"]
    );
    assert_eq!(
        failed(AnimFlaw::IgnoresRepeat),
        [
            "animation-autoreverse-plays-back",
            "animation-repeat-plays-every-cycle",
            "animation-repeats-forever-until-set",
        ]
    );
    assert_eq!(
        failed(AnimFlaw::IgnoresAutoreverse),
        [
            "animation-autoreverse-plays-back",
            "animation-repeats-forever-until-set",
        ]
    );
    assert_eq!(
        failed(AnimFlaw::FlattensSprings),
        ["animation-spring-overshoots-and-settles"]
    );
    assert_eq!(
        failed(AnimFlaw::SnapsBackground),
        ["animation-background-passes-between"]
    );
    assert_eq!(failed(AnimFlaw::Instant), CASES);
    probe.set_animation_flaw(None);
}

/// The mock records each animated change it is asked for, and shows the value in between at
/// its own clock, which moves only as sleeps are served.
#[test]
fn the_mock_records_and_plays_requested_animations() {
    use day_mock::AnimProperty;
    use day_spec::AnimSpec;
    let level = day_reactive::Signal::global(1.0);
    let probe = boot_with(move || {
        day_core::AnyPiece::new(
            day_pieces::rectangle()
                .frame(40.0, 40.0)
                .id(day_core::id!(faded))
                .opacity(level),
        )
    });
    let spec = AnimSpec::linear(400).delay(200).repeat(1, true);
    day_core::with_animation(spec, || level.set(0.0));
    let layer = {
        let state = probe.state.borrow();
        let (id, widget) = state
            .widgets
            .iter()
            .find(|(_, w)| !w.animations.is_empty())
            .expect("a widget was asked to animate");
        let asked = widget.animations[0];
        assert_eq!((asked.property, asked.spec), (AnimProperty::Opacity, spec));
        assert_eq!(widget.opacity, Some(0.0), "the model value is the target");
        day_mock::MockHandle(*id)
    };
    let shown = |ms: u32| {
        probe.advance(ms);
        probe.widget(layer).shown_opacity()
    };
    assert!((shown(100) - 1.0).abs() < 1e-9, "held through the delay");
    assert!((shown(300) - 0.5).abs() < 1e-9, "half way out");
    assert!((shown(400) - 0.5).abs() < 1e-9, "half way back");
    assert!((shown(300) - 0.0).abs() < 1e-9, "at rest on the target");
    // A change with no animation cancels the flight.
    day_core::with_animation(spec, || level.set(1.0));
    level.set(0.25);
    day_reactive::flush_sync();
    assert!((shown(0) - 0.25).abs() < 1e-9);
}