tilo-actions 0.5.2

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store.
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
use super::*;

tilo_test_env::scenario! {
    fn voice_commands_snapshots_and_rosters;

    phase join_cannot_carry_microphone_intent_or_a_caller_route;
    {
        for bytes in [
            br#"{"version":1,"intent":0,"command":{"op":"join","channel_id":"voice","muted":false}}"#.as_slice(),
            br#"{"version":1,"intent":0,"command":{"op":"join","channel_id":"voice"},"app_id":"other"}"#,
            br#"{"version":1,"intent":0,"command":{"op":"join","channel_id":"voice","call_id":"invented"}}"#,
        ] {
            assert!(Request::decode(bytes).is_err());
        }
        assert_eq!(
            Request::decode(
                br#"{"version":1,"intent":0,"command":{"op":"join","channel_id":"voice"}}"#
            )
            .unwrap()
            .command,
            Command::Join {
                channel_id: "voice".into()
            }
        );
    }

    phase closed_versioned_bounded_commands_round_trip;
    {
        for command in [
            Command::Join {
                channel_id: "voice".into(),
            },
            Command::SetMuted { muted: false },
            Command::SelectInput {
                input_id: "source-1".into(),
            },
            Command::Leave,
            Command::Status,
        ] {
            let request = Request::new(command);
            assert_eq!(
                Request::decode(&request.encode().unwrap()).unwrap(),
                request
            );
        }
        assert!(Request::decode(br#"{"version":2,"intent":0,"command":{"op":"status"}}"#).is_err());
        assert!(
            Request::decode(br#"{"version":1,"intent":0,"command":{"op":"unmute_remote"}}"#).is_err()
        );
        assert!(Request::decode(&vec![b' '; MAX_WIRE_BYTES + 1]).is_err());
        for channel_id in [String::new(), "x".repeat(257), "voice\nother".into()] {
            assert!(Request::new(Command::Join { channel_id }).encode().is_err());
        }
    }

    phase snapshots_cannot_claim_capture_while_muted_or_unavailable;
    {
        let mut snapshot = Snapshot::unavailable();
        assert_eq!(
            Snapshot::decode(&snapshot.encode().unwrap()).unwrap(),
            snapshot
        );
        snapshot.capture_active = true;
        assert!(snapshot.encode().is_err());
        snapshot.state = State::Live;
        assert!(snapshot.encode().is_err());
        snapshot.muted = false;
        snapshot.input_id = Some("source-1".into());
        assert!(snapshot.encode().is_ok());
    }

    phase snapshot_inputs_are_bounded_and_unique;
    {
        let mut snapshot = Snapshot::unavailable();
        let input = Input {
            id: "source".into(),
            label: "Microphone".into(),
        };
        snapshot.inputs = vec![input.clone(), input];
        assert!(snapshot.encode().is_err());
        snapshot.inputs = (0..=MAX_INPUTS)
            .map(|i| Input {
                id: i.to_string(),
                label: "Mic".into(),
            })
            .collect();
        assert!(snapshot.encode().is_err());
    }

    phase zero_intent_only_queries_and_max_only_tears_down;
    {
        assert!(Request::new(Command::Status).dispatch_valid());
        let mut request = Request::new(Command::SetMuted { muted: false });
        assert!(!request.dispatch_valid());
        request.intent = 1;
        assert!(request.dispatch_valid());
        request.intent = u64::MAX;
        assert!(!request.dispatch_valid());
        request.command = Command::Leave;
        assert!(request.dispatch_valid());
    }

    phase publisher_inventory_is_current_bounded_and_canonical;
    {
        let own = person(1);
        let remote = person(2);
        let mut snapshot = Snapshot::unavailable();
        snapshot.state = State::Live;
        snapshot.inventory_revision = Some(4);
        snapshot.publication = Some(own.clone());
        snapshot.publishers = vec![remote.clone()];
        assert!(snapshot.encode().is_ok());
        snapshot.publication = None; // A Reader may listen without a publication.
        assert!(snapshot.encode().is_ok());
        snapshot.inventory_revision = None;
        assert!(snapshot.encode().is_err());
        snapshot.inventory_revision = Some(4);
        for publishers in [
            vec![remote.clone(), remote.clone()],
            vec![person(0xab).to_uppercase()],
            vec!["ana".to_owned()],
        ] {
            snapshot.publication = Some(own.clone());
            snapshot.publishers = publishers;
            assert!(snapshot.encode().is_err());
        }
        // The person publishing their own picture, receiving nobody's.
        snapshot.publication = Some(own);
        snapshot.publishers.clear();
        assert!(snapshot.encode().is_ok());
        snapshot.publication = None;
        // A call has no member limit.
        snapshot.publishers = (1..=200).map(person).collect();
        assert!(snapshot.encode().is_ok());
        snapshot.publishers = vec![remote];
        for state in [
            State::Ended,
            State::Unavailable,
            State::Reconnecting,
            State::Joining,
        ] {
            snapshot.state = state;
            assert!(
                snapshot.encode().is_err(),
                "inventory only names a live call"
            );
        }
    }

    phase rosters_name_people_once_per_channel;
    {
        let person = |n: u8| hex_person(n);
        let mut snapshot = Snapshot::unavailable();
        snapshot.state = State::Idle;
        snapshot.issue = None;
        snapshot.rosters = vec![
            Roster {
                channel_id: "lounge".into(),
                people: vec![person(1), person(2)],
                ..Default::default()
            },
            Roster {
                channel_id: "voice".into(),
                people: (3..=120).map(person).collect(),
                ..Default::default()
            },
        ];
        let bytes = snapshot.encode().unwrap();
        assert_eq!(Snapshot::decode(&bytes).unwrap(), snapshot);
        for people in [
            vec![person(2), person(1)],
            vec![person(1), person(1)],
            vec![person(10).to_uppercase()],
            vec!["ana".to_owned()],
        ] {
            let mut invalid = snapshot.clone();
            invalid.rosters[0].people = people;
            assert!(invalid.encode().is_err());
        }
        let mut unordered = snapshot;
        unordered.rosters.reverse();
        assert!(unordered.encode().is_err());
    }

    phase a_watch_is_a_query_and_speaking_names_people_once_each;
    {
        let watch = Request::new(Command::Watch { revision: 7 });
        assert!(watch.dispatch_valid(), "a watch is a query: intent zero");
        let bytes = watch.encode().unwrap();
        assert_eq!(Request::decode(&bytes).unwrap(), watch);
        let mut effect = watch;
        effect.intent = 3;
        assert!(
            !effect.dispatch_valid(),
            "a watch changes nothing, so carries no intent"
        );

        let mut snapshot = Snapshot::unavailable();
        snapshot.speaking = vec![hex_person(1), hex_person(2)];
        snapshot.revision = 42;
        let bytes = snapshot.encode().unwrap();
        assert_eq!(Snapshot::decode(&bytes).unwrap(), snapshot);
        for speaking in [
            vec![hex_person(2), hex_person(1)],
            vec![hex_person(1), hex_person(1)],
            vec!["ana".to_owned()],
        ] {
            let mut invalid = snapshot.clone();
            invalid.speaking = speaking;
            assert!(invalid.encode().is_err());
        }
    }
}

fn hex_person(n: u8) -> String {
    format!("{n:02x}").repeat(32)
}

fn person(n: u8) -> String {
    format!("{n:02x}").repeat(32)
}

tilo_test_env::scenario! {
    fn video_commands_pictures_and_rosters;

    phase video_commands_round_trip_and_shown_pictures_are_bounded_sorted_and_layered;
    {
        let shown = |p: u8, source, layer| Shown {
            person: person(p),
            source,
            layer,
        };
        for command in [
            Command::SetCamera { on: true },
            Command::SetScreen { on: false },
            Command::Pictures { after: 7 },
            Command::Show {
                pictures: vec![
                    shown(1, PictureSource::Camera, PictureLayer::Mid),
                    shown(1, PictureSource::Screen, PictureLayer::High),
                    shown(2, PictureSource::Camera, PictureLayer::Low),
                ],
            },
        ] {
            let request = Request::new(command);
            assert_eq!(
                Request::decode(&request.encode().unwrap()).unwrap(),
                request
            );
        }
        // A screen has no middle layer.
        assert!(
            !Request::new(Command::Show {
                pictures: vec![shown(1, PictureSource::Screen, PictureLayer::Mid)],
            })
            .valid()
        );
        // One entry per person and source, in order.
        assert!(
            !Request::new(Command::Show {
                pictures: vec![
                    shown(2, PictureSource::Camera, PictureLayer::Low),
                    shown(1, PictureSource::Camera, PictureLayer::Low),
                ],
            })
            .valid()
        );
        assert!(
            !Request::new(Command::Show {
                pictures: vec![
                    shown(1, PictureSource::Camera, PictureLayer::Low),
                    shown(1, PictureSource::Camera, PictureLayer::High),
                ],
            })
            .valid()
        );
        assert!(
            !Request::new(Command::Show {
                pictures: (0..=MAX_SHOWN)
                    .map(|n| shown(
                        u8::try_from(n).unwrap(),
                        PictureSource::Camera,
                        PictureLayer::Low
                    ))
                    .collect(),
            })
            .valid()
        );
        // Collecting and showing pictures carry no intent; turning a camera on
        // does.
        for command in [
            Command::Pictures { after: 0 },
            Command::Show {
                pictures: Vec::new(),
            },
        ] {
            let mut request = Request::new(command);
            assert!(!request.carries_intent() && request.dispatch_valid());
            request.intent = 1;
            assert!(!request.dispatch_valid());
        }
        let mut camera = Request::new(Command::SetCamera { on: true });
        assert!(camera.carries_intent() && !camera.dispatch_valid());
        camera.intent = 1;
        assert!(camera.dispatch_valid());
    }

    phase a_pictures_answer_carries_whole_objects_in_base64_and_nothing_shows_outside_a_call;
    {
        let mut snapshot = Snapshot::unavailable();
        snapshot.state = State::Live;
        snapshot.camera = true;
        snapshot.pictures = vec![Picture {
            person: person(3),
            stream: "a".repeat(129),
            source: PictureSource::Camera,
            object: vec![1, 0, 1, 1, 0xff, 0x80],
        }];
        snapshot.pictures_cursor = 4;
        let bytes = snapshot.encode().unwrap();
        assert!(
            std::str::from_utf8(&bytes)
                .unwrap()
                .contains("\"AQABAf+A\""),
            "the object travels as base64"
        );
        assert_eq!(Snapshot::decode(&bytes).unwrap(), snapshot);

        // The largest picture fits one answer.
        snapshot.pictures[0].object = vec![7; MAX_PICTURE_BYTES];
        assert!(snapshot.encode().is_ok());
        snapshot.pictures[0].object = vec![7; MAX_PICTURE_BYTES + 1];
        assert!(snapshot.encode().is_err());
        snapshot.pictures[0].object = Vec::new();
        assert!(snapshot.encode().is_err());
        snapshot.pictures.clear();

        snapshot.state = State::Idle;
        assert!(snapshot.encode().is_err(), "a camera is on only in a call");
        snapshot.camera = false;
        assert!(snapshot.encode().is_ok());
    }

    phase a_roster_names_cameras_screens_and_the_muted_only_of_its_people;
    {
        let mut roster = Roster {
            channel_id: "voice".into(),
            people: vec![person(1), person(2)],
            cameras: vec![person(2)],
            screens: vec![person(1)],
            muted: vec![person(1), person(2)],
            ..Roster::default()
        };
        assert!(roster.valid());
        roster.cameras = vec![person(3)];
        assert!(!roster.valid(), "someone not in the call shows no camera");
        roster.cameras = vec![person(2), person(1)];
        assert!(!roster.valid(), "sorted");
        roster.cameras.clear();
        roster.muted = vec![person(3)];
        assert!(
            !roster.valid(),
            "someone not in the call is not muted in it"
        );
        roster.muted = vec![person(2), person(2)];
        assert!(!roster.valid(), "distinct");
        roster.muted.clear();
        let json = serde_json::to_string(&roster).unwrap();
        assert!(
            !json.contains("muted"),
            "written only when someone is muted"
        );
    }

    phase a_picture_names_its_coded_unit_and_refuses_one_that_does_not_hold_together;
    {
        let mut object = vec![1, 1, 1, 2];
        object.extend_from_slice(&7_u64.to_le_bytes());
        object.extend_from_slice(&1_000_u64.to_le_bytes());
        object.extend_from_slice(&640_u16.to_le_bytes());
        object.extend_from_slice(&360_u16.to_le_bytes());
        object.extend_from_slice(&3_u32.to_le_bytes());
        object.extend_from_slice(&[0; 4]);
        object.extend_from_slice(&[0, 0, 1]);
        let mut picture = Picture {
            person: person(1),
            stream: "s".into(),
            source: PictureSource::Camera,
            object,
        };
        assert_eq!(
            picture.unit(),
            Some(PictureUnit {
                keyframe: true,
                codec: PictureCodec::H264,
                width: 640,
                height: 360,
                payload: &[0, 0, 1],
            })
        );
        let unit = picture.unit().unwrap();
        assert_eq!(
            unit.to_object(PictureLayer::High, 7, 1_000),
            picture.object,
            "a unit frames back into the same object"
        );
        picture.object.push(9);
        assert_eq!(picture.unit(), None, "the length names the payload");
        picture.object.pop();
        picture.object[0] = 2;
        assert_eq!(picture.unit(), None, "another version");
    }
}

tilo_test_env::scenario! {
    fn volume_and_deafening;

    phase a_volume_is_a_local_bounded_query_for_one_member;
    {
        let request = Request::new(Command::SetVolume {
            member: person(1),
            percent: MAX_VOLUME_PERCENT,
        });
        assert!(request.dispatch_valid(), "it changes nobody else's call");
        assert!(!request.carries_intent());
        assert_eq!(
            Request::decode(&request.encode().unwrap()).unwrap(),
            request
        );
        for (member, percent) in [
            (person(1), MAX_VOLUME_PERCENT + 1),
            ("ana".to_owned(), 100),
            (String::new(), 100),
        ] {
            assert!(
                Request::new(Command::SetVolume {
                    member,
                    percent
                })
                .encode()
                .is_err()
            );
        }
    }

    phase deafened_is_muted_in_a_call_and_said_only_when_set;
    {
        let command = Request::new(Command::SetDeafened { deafened: true });
        assert_eq!(
            Request::decode(&command.encode().unwrap()).unwrap(),
            command
        );
        assert!(
            String::from_utf8(command.encode().unwrap())
                .unwrap()
                .contains(r#"{"op":"set_deafened","deafened":true}"#)
        );
        let mut snapshot = Snapshot::unavailable();
        snapshot.deafened = true;
        assert!(
            snapshot.encode().is_err(),
            "nobody is deafened outside a call"
        );
        snapshot.state = State::Live;
        snapshot.issue = None;
        assert!(snapshot.encode().is_ok());
        snapshot.muted = false;
        assert!(snapshot.encode().is_err(), "a deafened person is muted");
        snapshot.deafened = false;
        assert!(
            !String::from_utf8(snapshot.encode().unwrap())
                .unwrap()
                .contains("deafened"),
            "written only when set"
        );

        let mut roster = Roster {
            channel_id: "voice".into(),
            people: vec![person(1), person(2)],
            muted: vec![person(1)],
            deafened: vec![person(1)],
            ..Roster::default()
        };
        assert!(roster.valid());
        roster.deafened = vec![person(2)];
        assert!(!roster.valid(), "the deafened are among the muted");
        roster.deafened = vec![person(1)];
        roster.bridges = vec!["discord".into()];
        assert!(roster.valid(), "a bridged call names the service it reaches");
        roster.bridges = vec!["Discord ".into()];
        assert!(!roster.valid(), "a service is named by its id");

        // A bridged participant is a person of the call, shown by their name there.
        roster.bridges = vec!["discord".into()];
        roster.bridged = vec![BridgedPerson {
            person: person(2),
            service: "discord".into(),
            account: "7001".into(),
            name: "Dana".into(),
            avatar: Some("ab".repeat(32)),
        }];
        let json = serde_json::to_string(&roster).unwrap();
        assert!(json.contains("\"bridged\"") && json.contains("Dana"));
        assert!(roster.valid());
        roster.bridged[0].person = person(3);
        assert!(!roster.valid(), "a bridged person is in the call's people");
        roster.bridged[0].person = person(2);
        roster.bridged[0].name = "  ".into();
        assert!(!roster.valid(), "a bridged person is shown by a name");
    }
}