liveactivity 0.14.0

Framework-neutral Live Activities: one interface over ActivityKit, Android Live Updates and other backends
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Requests that arrive as JSON, at the limits of the contract: the templates' requests of contract v1, a number's
//! way through the reader, the one error of a refusal, and what a layout request says beside its layout.

use liveactivity::{
    AndroidContent, Error, LayoutStartInput, PresentationStyle, Timer, Validate, validate_request,
};
use serde_json::{Value, json};

const LARGEST_REVISION: u64 = 9_007_199_254_740_991;
const LARGEST_TIME: u64 = 8_640_000_000_000_000;

/// A start of the progress template that the contract takes.
fn progress() -> Value {
    json!({"activityKey":"job:1","schemaVersion":1,"template":"progress","fallback":"notification",
        "snapshot":{"revision":0,"title":"Job","status":"Running","progress":0.3}})
}

/// An update with a timer that the contract takes.
fn timer() -> Value {
    json!({"id":"opaque-handle-1","snapshot":{"revision":1,"title":"Timer","status":"Running",
        "timer":{"startedAt":0,"endsAt":1}}})
}

/// An end that the contract takes.
fn end() -> Value {
    json!({"id":"opaque-handle-1","policy":{"kind":"default"}})
}

/// `request` with the member at `pointer` set, whether it was there or not.
fn with(mut request: Value, pointer: &str, value: Value) -> Value {
    let (parent, member) = pointer.rsplit_once('/').unwrap();
    request.pointer_mut(parent).unwrap()[member] = value;
    request
}

/// `request` without the member at `pointer`, which was there.
fn without(mut request: Value, pointer: &str) -> Value {
    let (parent, member) = pointer.rsplit_once('/').unwrap();
    let parent = request
        .pointer_mut(parent)
        .unwrap()
        .as_object_mut()
        .unwrap();
    parent.remove(member).unwrap();
    request
}

fn refused() -> liveactivity::Result<()> {
    Err(Error::invalid())
}

#[test]
fn req_c_008_the_fixtures_of_the_templates_get_their_verdict() {
    // REQ-C-008, REQ-C-010
    let fixtures: Value = serde_json::from_str(include_str!("fixtures/requests.json")).unwrap();
    let fixtures = fixtures.as_array().unwrap();
    assert_eq!(fixtures.len(), 11);
    for fixture in fixtures {
        let request = &fixture["request"];
        let verdict = validate_request(
            request["command"].as_str().unwrap(),
            request["input"].clone(),
        );
        let expected = if fixture["valid"] == true {
            Ok(())
        } else {
            refused()
        };
        assert_eq!(verdict, expected, "{}", fixture["name"]);
    }
}

#[test]
fn req_c_008_a_template_request_is_taken_at_each_limit_and_refused_past_it() {
    // REQ-C-008
    type Request = fn() -> Value;
    let requests: [(&str, Request); 3] = [("start", progress), ("update", timer), ("end", end)];
    // Lengths count code points, whatever their bytes; a key is of one-byte characters.
    for (command, pointer, last, past) in [
        (
            0,
            "/activityKey",
            json!("a".repeat(128)),
            json!("a".repeat(129)),
        ),
        (
            0,
            "/snapshot/title",
            json!("😀".repeat(120)),
            json!("😀".repeat(121)),
        ),
        (
            0,
            "/snapshot/status",
            json!("한".repeat(240)),
            json!("한".repeat(241)),
        ),
        (0, "/snapshot/progress", json!(0), json!(-0.1)),
        (0, "/snapshot/progress", json!(1), json!(1.1)),
        (
            0,
            "/snapshot/revision",
            json!(LARGEST_REVISION),
            json!(LARGEST_REVISION + 1),
        ),
        (
            0,
            "/snapshot/staleAt",
            json!(LARGEST_TIME),
            json!(LARGEST_TIME + 1),
        ),
        (1, "/id", json!("한".repeat(256)), json!("한".repeat(257))),
        (
            1,
            "/snapshot/timer/endsAt",
            json!(LARGEST_TIME),
            json!(LARGEST_TIME + 1),
        ),
        (2, "/id", json!("한".repeat(256)), json!("한".repeat(257))),
        (
            2,
            "/policy",
            json!({"kind":"after","at":LARGEST_TIME}),
            json!({"kind":"after","at":LARGEST_TIME + 1}),
        ),
    ] {
        let (command, request) = requests[command];
        let taken = validate_request(command, with(request(), pointer, last));
        assert_eq!(taken, Ok(()), "{command} {pointer}");
        let beyond = validate_request(command, with(request(), pointer, past));
        assert_eq!(beyond, refused(), "{command} {pointer}");
    }
    // Every limit of a start at once.
    let mut input = progress();
    input["activityKey"] = json!("a".repeat(128));
    input["snapshot"] = json!({"revision":LARGEST_REVISION,"title":"😀".repeat(120),
        "status":"한".repeat(240),"progress":1,"staleAt":LARGEST_TIME});
    assert_eq!(validate_request("start", input), Ok(()));
}

#[test]
fn req_c_008_a_template_request_of_a_wrong_shape_is_refused() {
    // REQ-C-008
    for (command, request) in [("start", progress()), ("update", timer()), ("end", end())] {
        assert_eq!(validate_request(command, request), Ok(()), "{command}");
    }
    // A key begins with a letter or a digit, and goes on with those and with `.`, `_`, `:` and `-`.
    for key in ["9a", "A.b_c:d-e"] {
        let start = with(progress(), "/activityKey", json!(key));
        assert_eq!(validate_request("start", start), Ok(()), "{key}");
    }
    for (pointer, value) in [
        ("/activityKey", json!("../secret")),
        ("/activityKey", json!(".hidden")),
        ("/activityKey", json!("a/b")),
        ("/activityKey", json!("key\n")),
        ("/activityKey", json!("é")),
        ("/activityKey", json!("a한")),
        ("/activityKey", json!("")),
        ("/schemaVersion", json!(2)),
        ("/fallback", json!(null)),
        // Blank is what ECMAScript's \S does not match: a byte order mark too.
        ("/snapshot/title", json!(" \n\t")),
        ("/snapshot/status", json!(" \n\t")),
        ("/snapshot/status", json!("\u{feff}")),
        ("/snapshot/progress", json!(null)),
        ("/snapshot/progress", json!("private-input")),
        ("/snapshot/revision", json!(-1)),
        ("/snapshot/revision", json!(0.5)),
        ("/snapshot/staleAt", json!(null)),
        ("/snapshot/staleAt", json!(-1)),
        ("/snapshot/staleAt", json!(0.5)),
        // No member that the contract does not name, and no `null` for one it names.
        ("/snapshot/token", json!("secret")),
        ("/unsafePath", json!("/tmp")),
        ("/snapshot", json!(null)),
    ] {
        let verdict = validate_request("start", with(progress(), pointer, value));
        assert_eq!(verdict, refused(), "{pointer}");
    }
    // A next-line character is no blank.
    let status = with(progress(), "/snapshot/status", json!("\u{0085}"));
    assert_eq!(validate_request("start", status), Ok(()));
    assert_eq!(validate_request("start", json!([])), refused());

    // In an update and an end alike.
    for (command, request) in [
        ("update", with(timer(), "/extra", json!(1))),
        ("update", with(timer(), "/snapshot/timer/extra", json!(1))),
        ("update", with(timer(), "/snapshot", json!(null))),
        ("update", with(timer(), "/id", json!(" "))),
        ("end", with(end(), "/extra", json!(1))),
        ("end", with(end(), "/policy/extra", json!(1))),
        ("end", with(end(), "/policy", json!(null))),
        ("end", with(end(), "/id", json!(" "))),
    ] {
        let verdict = validate_request(command, request.clone());
        assert_eq!(verdict, refused(), "{command} {request}");
    }
}

#[test]
fn req_c_008_a_snapshot_has_the_one_presentation_of_its_template() {
    // REQ-C-008
    let a_timer = json!({"startedAt":0,"endsAt":1});
    let timed = with(
        without(progress(), "/snapshot/progress"),
        "/snapshot/timer",
        a_timer.clone(),
    );
    let of_a_timer = with(timed.clone(), "/template", json!("timer"));
    assert_eq!(validate_request("start", of_a_timer), Ok(()));
    for (case, start) in [
        ("both", with(progress(), "/snapshot/timer", a_timer)),
        ("neither", without(progress(), "/snapshot/progress")),
        ("a timer for the progress template", timed),
        (
            "a progress for the timer template",
            with(progress(), "/template", json!("timer")),
        ),
    ] {
        assert_eq!(validate_request("start", start), refused(), "{case}");
    }
    // An update names no template, and takes either presentation, one of them.
    let both = with(timer(), "/snapshot/progress", json!(0.5));
    assert_eq!(validate_request("update", both), refused());
    let neither = without(timer(), "/snapshot/timer");
    assert_eq!(validate_request("update", neither), refused());
}

#[test]
fn req_c_008_a_timer_ends_after_its_start_and_an_end_says_when_only_with_after() {
    // REQ-C-008
    for (started_at, ends_at, valid) in [(0, 1, true), (1, 1, false), (2, 1, false)] {
        let times = json!({"startedAt":started_at,"endsAt":ends_at});
        let verdict = validate_request("update", with(timer(), "/snapshot/timer", times));
        assert_eq!(verdict.is_ok(), valid, "{started_at} to {ends_at}");
    }
    for (policy, valid) in [
        (json!({"kind":"default"}), true),
        (json!({"kind":"immediate"}), true),
        (json!({"kind":"after","at":0}), true),
        (json!({"kind":"after","at":-1}), false),
        (json!({"kind":"after","at":0.5}), false),
        (json!({"kind":"after"}), false),
        (json!({"kind":"immediate","at":0}), false),
        (json!({"kind":"default","at":0}), false),
        (json!({"kind":"later","at":0}), false),
        (json!({}), false),
    ] {
        let verdict = validate_request("end", with(end(), "/policy", policy.clone()));
        assert_eq!(verdict.is_ok(), valid, "{policy}");
    }
}

#[test]
fn req_c_009_a_number_keeps_its_value_when_read_from_json_text() {
    // REQ-C-009
    for text in [
        "977.9461044818163",
        "0.0018531278500143397",
        "96.00000000000001",
        "0.09999999999999999",
        "3875413157790899.5",
        "8640000000000000.6",
    ] {
        let read: Value = serde_json::from_str(text).unwrap();
        assert_eq!(read.as_f64(), text.parse().ok(), "{text}");
    }
    let start = |style: &str, values: &str| {
        let text = format!(
            r#"{{"activityKey":"job:1","schemaVersion":2,"fallback":"none","layout":{{"lockScreen":
            {{"type":"vstack","children":[{{"type":"text","value":"a","style":"s"}},
            {{"type":"timerText","start":"from","end":"to"}}]}}}},"styles":{{"s":{style}}},
            "snapshot":{{"revision":0,"values":{values}}}}}"#
        );
        validate_request("start_layout", serde_json::from_str(&text).unwrap())
    };
    let times = r#"{"from":1,"to":2}"#;
    assert_eq!(start(r#"{"size":12}"#, times), Ok(()));
    assert_eq!(
        start(r#"{"size":96,"minimumScaleFactor":0.1}"#, times),
        Ok(())
    );
    // Each number is just off what its member takes: past the end of a range, or beside a whole number. A read
    // that rounds would bring it back.
    for (style, values) in [
        (r#"{"size":96.00000000000001}"#, times),
        (r#"{"minimumScaleFactor":0.09999999999999999}"#, times),
        (r#"{"size":12}"#, r#"{"from":0.9999999999999999,"to":2}"#),
        (
            r#"{"size":12}"#,
            r#"{"from":3875413157790899.5,"to":3875413157790901}"#,
        ),
        (r#"{"size":12}"#, r#"{"from":1,"to":8640000000000000.6}"#),
    ] {
        assert_eq!(start(style, values), refused(), "{style} {values}");
    }
}

#[test]
fn req_c_010_a_refusal_repeats_nothing_of_the_request() {
    // REQ-C-010
    let secret = "private-input";
    for request in [
        with(progress(), "/snapshot/progress", json!(secret)),
        with(progress(), "/activityKey", json!(format!("../{secret}"))),
        with(progress(), "/snapshot/title", json!(secret.repeat(40))),
        with(progress(), &format!("/{secret}"), json!(1)),
    ] {
        let error = validate_request("start", request).unwrap_err();
        assert_eq!(error, Error::invalid());
        assert!(!format!("{error:?} {error}").contains(secret));
    }
}

/// A start of a layout that the contract takes.
fn layout() -> Value {
    json!({"activityKey":"demo:1","schemaVersion":2,"fallback":"none",
        "layout":{"lockScreen":{"type":"text","value":"a"}},"snapshot":{"revision":0,"values":{}}})
}

#[test]
fn req_c_005_a_request_is_read_under_its_own_command_and_under_no_other_name() {
    // REQ-C-005
    assert_eq!(validate_request("start", progress()), Ok(()));
    assert_eq!(validate_request("start_layout", layout()), Ok(()));
    // The two contracts do not mix: a start of one is no start of the other.
    assert_eq!(validate_request("start", layout()), refused());
    assert_eq!(validate_request("start_layout", progress()), refused());
    // A name that is none of the six commands takes no request, not that of the command it resembles.
    for command in [
        "start ",
        "Start",
        "startLayout",
        "start_layout ",
        "list_layout",
        "endAll",
        "",
    ] {
        for request in [json!({}), progress(), layout(), end()] {
            assert_eq!(validate_request(command, request), refused(), "{command:?}");
        }
    }
}

/// The verdict on a start of a layout with one more member, on an update of one and on an end of one.
fn started(member: &str, value: Value) -> liveactivity::Result<()> {
    validate_request("start_layout", with(layout(), &format!("/{member}"), value))
}

fn updated(member: &str, value: Value) -> liveactivity::Result<()> {
    let update = json!({"id":"opaque-handle-1","snapshot":{"revision":1,"values":{}}});
    validate_request("update_layout", with(update, &format!("/{member}"), value))
}

fn ended(member: &str, value: Value) -> liveactivity::Result<()> {
    validate_request("end_layout", with(end(), &format!("/{member}"), value))
}

fn alert() -> Value {
    json!({"title":"Your ride","body":"It starts now"})
}

fn public_version() -> Value {
    json!({"title":"Delivery","status":"On the way"})
}

/// A start whose Android content has a public version, of an activity with `visibility`, or with none said.
fn with_a_public_content(visibility: Option<&str>) -> Value {
    let content = json!({"title":"On time","status":"Gate 4",
        "presentation":{"style":{"kind":"standard"},"publicVersion":public_version()}});
    let start = with(layout(), "/snapshot/android", content);
    match visibility {
        Some(visibility) => with(start, "/android", json!({"visibility":visibility})),
        None => start,
    }
}

#[test]
fn req_c_011_the_sound_of_an_alert_is_the_name_of_a_file() {
    // REQ-C-011
    assert_eq!(updated("alert", alert()), Ok(()));
    for sound in ["chime.aiff", "a", "0", "A_b-c.d", &"a".repeat(64)] {
        let alert = with(alert(), "/sound", json!(sound));
        assert_eq!(updated("alert", alert), Ok(()), "{sound}");
    }
    for sound in [
        json!(""),
        json!(".aiff"),
        json!("-a"),
        json!("a b"),
        json!("sounds/chime.aiff"),
        json!("a".repeat(65)),
        json!("é.aiff"),
        json!("chime.aiff\n"),
        json!(1),
        json!(null),
        json!(["a"]),
    ] {
        let alert = with(alert(), "/sound", sound.clone());
        assert_eq!(updated("alert", alert), refused(), "{sound}");
    }
}

#[test]
fn req_c_011_the_options_of_a_start_have_their_members_and_their_json_types() {
    // REQ-C-011
    // On iOS.
    for transient in [true, false] {
        assert_eq!(started("ios", json!({"transient":transient})), Ok(()));
    }
    for ios in [
        json!({}),
        json!({"transient":"yes"}),
        json!({"transient":1}),
        json!({"transient":null}),
        json!({"transient":true,"extra":1}),
        json!(null),
        json!([]),
        json!(true),
    ] {
        assert_eq!(started("ios", ios.clone()), refused(), "{ios}");
    }

    // A scheduled start: a time, up to the largest one, and the alert shown then.
    for at in [0, 1_900_000_000_000, LARGEST_TIME] {
        let scheduled = json!({"at":at,"alert":alert()});
        assert_eq!(started("scheduled", scheduled), Ok(()), "{at}");
    }
    let chime = with(alert(), "/sound", json!("chime.aiff"));
    assert_eq!(started("scheduled", json!({"at":1,"alert":chime})), Ok(()));
    for scheduled in [
        json!({}),
        json!({"at":1}),
        json!({"alert":alert()}),
        json!({"at":-1,"alert":alert()}),
        json!({"at":0.5,"alert":alert()}),
        json!({"at":LARGEST_TIME + 1,"alert":alert()}),
        json!({"at":"1","alert":alert()}),
        json!({"at":null,"alert":alert()}),
        json!({"at":1,"alert":null}),
        json!({"at":1,"alert":{"title":"a"}}),
        json!({"at":1,"alert":with(alert(), "/title", json!(" "))}),
        json!({"at":1,"alert":with(alert(), "/sound", json!("a b"))}),
        json!({"at":1,"alert":with(alert(), "/extra", json!(1))}),
        json!({"at":1,"alert":alert(),"extra":1}),
        json!(null),
        json!([]),
        json!(1),
    ] {
        assert_eq!(
            started("scheduled", scheduled.clone()),
            refused(),
            "{scheduled}"
        );
    }

    // On Android: a visibility, a placement, and a public version of a private activity only.
    for android in [
        json!({"visibility":"private"}),
        json!({"visibility":"private","publicVersion":public_version(),"placement":"foreground"}),
        json!({"visibility":"public","placement":"tagged"}),
        json!({"visibility":"secret","placement":"foreground"}),
        json!({"visibility":"private",
            "publicVersion":{"title":"😀".repeat(120),"status":"한".repeat(240)}}),
    ] {
        assert_eq!(started("android", android.clone()), Ok(()), "{android}");
    }
    for placement in [
        json!("Tagged"),
        json!("service"),
        json!(""),
        json!(null),
        json!(1),
        json!(["tagged"]),
    ] {
        let android = json!({"visibility":"private","placement":placement});
        assert_eq!(started("android", android), refused(), "{placement}");
    }
    for wrong in [
        json!({}),
        json!({"title":"a"}),
        json!({"status":"b"}),
        json!({"title":"","status":"b"}),
        json!({"title":"a","status":" "}),
        json!({"title":"a".repeat(121),"status":"b"}),
        json!({"title":"a","status":"b".repeat(241)}),
        with(public_version(), "/extra", json!(1)),
        json!({"title":1,"status":"b"}),
        json!(null),
        json!([]),
        json!("a"),
    ] {
        let android = json!({"visibility":"private","publicVersion":wrong});
        assert_eq!(started("android", android), refused(), "{wrong}");
    }
    for visibility in ["public", "secret"] {
        let android = json!({"visibility":visibility,"publicVersion":public_version()});
        assert_eq!(started("android", android), refused(), "{visibility}");
    }
    for android in [
        json!({"visibility":"hidden"}),
        json!({"placement":"tagged"}),
        json!({"visibility":"private","lights":true}),
    ] {
        assert_eq!(started("android", android.clone()), refused(), "{android}");
    }
}

#[test]
fn req_c_011_a_public_version_in_a_content_is_of_a_private_start() {
    // REQ-C-011
    // A start is private unless it says otherwise.
    for (visibility, verdict) in [
        (None, Ok(())),
        (Some("private"), Ok(())),
        (Some("public"), refused()),
        (Some("secret"), refused()),
    ] {
        let start = with_a_public_content(visibility);
        assert_eq!(
            validate_request("start_layout", start),
            verdict,
            "{visibility:?}"
        );
    }
    // An update does not say how its activity started: there only Android knows.
    let snapshot = with_a_public_content(None)["snapshot"].clone();
    assert_eq!(updated("snapshot", snapshot), Ok(()));
}

#[test]
fn req_c_011_each_member_belongs_to_its_command() {
    // REQ-C-011
    assert_eq!(validate_request("start_layout", layout()), Ok(()));
    assert_eq!(validate_request("end_layout", end()), Ok(()));
    let options = [
        ("ios", json!({"transient":true})),
        ("scheduled", json!({"at":1,"alert":alert()})),
        ("android", json!({"visibility":"private"})),
    ];
    for (member, value) in options {
        assert_eq!(started(member, value.clone()), Ok(()), "{member}");
        assert_eq!(updated(member, value.clone()), refused(), "{member}");
        assert_eq!(ended(member, value), refused(), "{member}");
    }
    assert_eq!(updated("alert", alert()), Ok(()));
    assert_eq!(started("alert", alert()), refused());
    assert_eq!(ended("alert", alert()), refused());
}

#[test]
fn req_c_011_a_typed_start_writes_its_members_as_it_read_them() {
    // REQ-C-011
    let full = with_a_public_content(Some("private"));
    let full = with(full, "/android/publicVersion", public_version());
    let full = with(full, "/android/placement", json!("foreground"));
    let full = with(full, "/ios", json!({"transient":true}));
    let chime = with(alert(), "/sound", json!("chime.aiff"));
    let full = with(full, "/scheduled", json!({"at":1,"alert":chime}));
    // A member that a request leaves out is not written, and one that says the default is.
    let plain = with(layout(), "/scheduled", json!({"at":1,"alert":alert()}));
    let spelled = with(layout(), "/ios", json!({"transient":false}));
    for start in [full, layout(), plain, spelled] {
        let typed: LayoutStartInput = serde_json::from_value(start.clone()).unwrap();
        assert_eq!(typed.validate(), Ok(()), "{start}");
        assert_eq!(serde_json::to_value(&typed).unwrap(), start);
    }
}

#[test]
fn req_c_006_a_timer_style_of_an_android_content_reads_from_json_and_is_checked_in_full() {
    // REQ-C-006
    let content = |started_at: u64, ends_at: u64| {
        json!({"title":"Mochi’s nap","status":"Dozing off","presentation":{"style":{"kind":"timer",
            "value":{"timer":{"startedAt":started_at,"endsAt":ends_at},"countsDown":true}}}})
    };
    // The style's timer is liveupdate's type; a caller reads its members without naming it.
    let typed: AndroidContent = serde_json::from_value(content(1_000, 2_000)).unwrap();
    let PresentationStyle::Timer(style) = &typed.presentation.style else {
        panic!("the style is a timer");
    };
    let read = (
        style.timer.started_at,
        style.timer.ends_at,
        style.counts_down,
    );
    assert_eq!(read, (1_000, 2_000, true));
    assert_eq!(serde_json::to_value(&typed).unwrap(), content(1_000, 2_000));
    // The template's `Timer` is another type and writes the same two members.
    let template = Timer {
        started_at: 1_000,
        ends_at: 2_000,
    };
    assert_eq!(
        serde_json::to_value(&template).unwrap(),
        content(1_000, 2_000)["presentation"]["style"]["value"]["timer"]
    );

    // liveupdate's rules for a timer hold before any backend: it ends after its start, and its value has its
    // members, of their types, and no other.
    let start = |android: Value| with(layout(), "/snapshot/android", android);
    assert_eq!(
        validate_request("start_layout", start(content(1_000, 2_000))),
        Ok(())
    );
    let value = "/presentation/style/value";
    for (case, broken) in [
        ("ends at its start", content(2_000, 2_000)),
        ("ends before its start", content(3_000, 2_000)),
        (
            "a null end",
            with(
                content(1_000, 2_000),
                &format!("{value}/timer/endsAt"),
                json!(null),
            ),
        ),
        (
            "a start of another type",
            with(
                content(1_000, 2_000),
                &format!("{value}/timer/startedAt"),
                json!("1000"),
            ),
        ),
        (
            "no start",
            without(content(1_000, 2_000), &format!("{value}/timer/startedAt")),
        ),
        (
            "no timer",
            without(content(1_000, 2_000), &format!("{value}/timer")),
        ),
        (
            "a member beside the timer's",
            with(
                content(1_000, 2_000),
                &format!("{value}/timer/ends_at"),
                json!(3_000),
            ),
        ),
        (
            "a member beside the style's",
            with(content(1_000, 2_000), &format!("{value}/extra"), json!(1)),
        ),
        (
            "a direction of another type",
            with(
                content(1_000, 2_000),
                &format!("{value}/countsDown"),
                json!("yes"),
            ),
        ),
        (
            "no direction",
            without(content(1_000, 2_000), &format!("{value}/countsDown")),
        ),
    ] {
        assert_eq!(
            validate_request("start_layout", start(broken)),
            refused(),
            "{case}"
        );
    }
}