mobile-agent-protocol 0.1.0-alpha.1

The wire protocol between the mobile-agent daemon and every device agent: the message schema, the framing and the handshake
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Task 3.3: the handshake (the agent speaks first; the highest common
//! version wins; no common version fails listing both sets), capability
//! refusal, request multiplexing and deadlines.

mod common;

use std::time::Duration;

use common::{Link, agent_config, every_capability, host_config, one};
use protocol::frame::{DEFAULT_MAX_FRAME, HEADER_LEN, MIN_MAX_FRAME, write_frame};
use protocol::message::{
    BoundsParams, Call, FramesParams, ImageFormat, InputAck, Message, Method, Ping, Reply,
    Response, Rotation, Screenshot, ScreenshotParams, SnapshotParams, TapParams, TextParams, kind,
};
use protocol::session::{
    AgentEvent, AgentSession, DisconnectReason, HandshakeError, HostConfig, HostEvent, HostSession,
    Responded, select_version,
};
use protocol::snapshot::{Snapshot, Truncation};
use protocol::{Capabilities, code};

fn empty_snapshot() -> Snapshot {
    Snapshot {
        strings: vec![],
        windows: vec![],
        node_count: 0,
        truncated: Truncation::EMPTY,
        seq: 0,
    }
}

fn screenshot(bytes: usize) -> Reply {
    Reply::Screenshot(Screenshot {
        format: ImageFormat::PNG,
        width: 10,
        height: 10,
        rotation: Rotation::R0,
        data: vec![7; bytes],
    })
}

/// The one request the agent has received since the last call.
fn only_request(link: &mut Link) -> protocol::message::Request {
    match link.agent_events().as_slice() {
        [AgentEvent::Request(r)] => r.clone(),
        other => panic!("expected one request, got {other:?}"),
    }
}

#[test]
fn the_agent_speaks_first() {
    let mut link = Link::new(host_config(&[1]), agent_config(&[1], every_capability()));
    assert!(
        link.host.poll_transmit().is_none(),
        "the host waits for the hello"
    );
    let hello = link
        .agent
        .poll_transmit()
        .expect("the agent sends its hello at once");
    assert_eq!(
        Message::decode(&hello[HEADER_LEN..]).unwrap().kind(),
        kind::HELLO
    );
    link.host.receive(&hello, link.t0);
    let welcome = link
        .host
        .poll_transmit()
        .expect("the host answers the hello");
    assert_eq!(
        Message::decode(&welcome[HEADER_LEN..]).unwrap().kind(),
        kind::WELCOME
    );
}

#[test]
fn the_highest_common_version_is_selected() {
    assert_eq!(select_version(&[1, 2, 3], &[3, 2, 1]), Some(3));
    assert_eq!(select_version(&[1, 2], &[2, 3]), Some(2));
    assert_eq!(select_version(&[3], &[1]), None);
    assert_eq!(select_version(&[], &[1]), None);
}

/// Spec, Version negotiation on connect: common version selected.
#[test]
fn both_sides_proceed_on_the_common_version() {
    let mut link = Link::new(
        host_config(&[1, 2]),
        agent_config(&[2, 3], every_capability()),
    );
    link.pump(link.t0);
    let HostEvent::Ready(host) = one(link.host_events()) else {
        panic!("host not ready")
    };
    let AgentEvent::Ready(agent) = one(link.agent_events()) else {
        panic!("agent not ready")
    };
    assert_eq!(host.version, 2);
    assert_eq!(agent.version, 2);
    assert_eq!(host.capabilities, every_capability());
    assert_eq!(host.agent, "placeholder-agent 0.0.0");
    assert_eq!(link.host.negotiated().map(|n| n.version), Some(2));
}

/// Spec, Version negotiation on connect: no common version.
#[test]
fn no_common_version_closes_the_connection_listing_both_sets() {
    let mut link = Link::new(host_config(&[3]), agent_config(&[1], every_capability()));
    link.pump(link.t0);
    let host_events = link.host_events();
    let [HostEvent::Closed(DisconnectReason::Handshake(err))] = host_events.as_slice() else {
        panic!("expected a handshake failure, got {host_events:?}")
    };
    assert_eq!(
        *err,
        HandshakeError::NoCommonVersion {
            host: vec![3],
            agent: vec![1],
        }
    );
    let text = err.to_string();
    assert!(text.contains("[3]") && text.contains("[1]"), "{text}");
    assert!(text.contains("redeploy"), "{text}");
    assert!(link.host.is_closed());
    // The agent learns why before the host closes the connection.
    let agent_events = link.agent_events();
    let [AgentEvent::Closed(DisconnectReason::Handshake(HandshakeError::Refused(refusal)))] =
        agent_events.as_slice()
    else {
        panic!("expected the agent to be refused, got {agent_events:?}")
    };
    assert_eq!(refusal.code, code::VERSION_MISMATCH);
    assert!(!refusal.retryable);
    assert!(link.agent.is_closed());
    assert!(matches!(
        link.agent.close_reason(),
        Some(DisconnectReason::Handshake(HandshakeError::Refused(_)))
    ));
    assert!(matches!(
        link.host.close_reason(),
        Some(DisconnectReason::Handshake(
            HandshakeError::NoCommonVersion { .. }
        ))
    ));
    let err = link
        .host
        .send_request(Call::Shutdown, None, link.t0)
        .unwrap_err();
    assert_eq!(err.code, code::DISCONNECTED);
}

#[test]
fn the_frame_limit_is_the_smaller_maximum() {
    let host = HostConfig {
        max_frame: 1 << 20,
        ..host_config(&[1])
    };
    let mut agent = agent_config(&[1], every_capability());
    agent.max_frame = 64 * 1024;
    let mut link = Link::new(host, agent);
    link.pump(link.t0);
    let HostEvent::Ready(host) = one(link.host_events()) else {
        panic!("host not ready")
    };
    let AgentEvent::Ready(agent) = one(link.agent_events()) else {
        panic!("agent not ready")
    };
    assert_eq!(host.max_frame, 64 * 1024);
    assert_eq!(agent.max_frame, 64 * 1024);
}

#[test]
fn a_frame_limit_below_the_minimum_is_refused() {
    let mut agent = agent_config(&[1], every_capability());
    agent.max_frame = MIN_MAX_FRAME - 1;
    let mut link = Link::new(host_config(&[1]), agent);
    link.pump(link.t0);
    let events = link.host_events();
    assert!(
        matches!(
            events.as_slice(),
            [HostEvent::Closed(DisconnectReason::Handshake(
                HandshakeError::FrameLimitTooSmall { limit }
            ))] if *limit == MIN_MAX_FRAME - 1
        ),
        "{events:?}"
    );
}

#[test]
fn a_hello_with_the_wrong_token_is_refused() {
    let host = HostConfig {
        token: Some("placeholder-token-a".to_owned()),
        ..host_config(&[1])
    };
    let mut agent = agent_config(&[1], every_capability());
    agent.token = Some("placeholder-token-b".to_owned());
    let mut link = Link::new(host, agent);
    link.pump(link.t0);
    let events = link.host_events();
    assert!(
        matches!(
            events.as_slice(),
            [HostEvent::Closed(DisconnectReason::Handshake(
                HandshakeError::TokenMismatch
            ))]
        ),
        "{events:?}"
    );
    let agent_events = link.agent_events();
    assert!(
        matches!(
            agent_events.as_slice(),
            [AgentEvent::Closed(DisconnectReason::Handshake(HandshakeError::Refused(e)))]
                if e.code == code::TOKEN_MISMATCH
        ),
        "{agent_events:?}"
    );
}

#[test]
fn a_first_message_other_than_a_hello_is_a_protocol_error() {
    let t0 = std::time::Instant::now();
    let mut host = HostSession::new(host_config(&[1]), t0);
    let mut wire = Vec::new();
    write_frame(
        &Message::Ping(Ping { nonce: 1 }),
        DEFAULT_MAX_FRAME,
        &mut wire,
    )
    .unwrap();
    host.receive(&wire, t0);
    assert!(host.is_closed());
    assert!(matches!(
        host.poll_event(),
        Some(HostEvent::Closed(DisconnectReason::Protocol(_)))
    ));
}

#[test]
fn requests_wait_for_the_handshake() {
    let t0 = std::time::Instant::now();
    let mut host = HostSession::new(host_config(&[1]), t0);
    let err = host
        .send_request(Call::Snapshot(SnapshotParams::default()), None, t0)
        .unwrap_err();
    assert_eq!(err.code, code::NOT_READY);
    assert!(err.retryable);
    assert!(host.poll_transmit().is_none());
}

/// Spec, Capability advertisement: a command that needs an unadvertised
/// capability is refused, naming it, and not forwarded.
#[test]
fn an_unadvertised_capability_is_refused_naming_it() {
    let mut link = Link::ready(
        host_config(&[1]),
        agent_config(&[1], Capabilities::SCREENSHOT),
    );
    let err = link
        .host
        .send_request(Call::FramesStart(FramesParams::default()), None, link.t0)
        .unwrap_err();
    assert_eq!(err.code, code::CAPABILITY_MISSING);
    assert!(!err.retryable);
    assert!(err.message.contains("frame streaming"), "{}", err.message);
    assert!(err.message.contains("frames"), "{}", err.message);
    assert!(
        link.host.poll_transmit().is_none(),
        "the request is not forwarded"
    );
    // What the agent advertised still goes through.
    link.host
        .send_request(Call::Screenshot(ScreenshotParams::default()), None, link.t0)
        .unwrap();
    assert!(link.host.poll_transmit().is_some());
    // Task 10.11: a bounds probe needs its own capability, which an agent
    // from before it does not advertise.
    let err = link
        .host
        .send_request(Call::Bounds(BoundsParams { nodes: vec![3] }), None, link.t0)
        .unwrap_err();
    assert_eq!(err.code, code::CAPABILITY_MISSING);
    assert!(err.message.contains("bounds probes"), "{}", err.message);
    assert!(link.host.poll_transmit().is_none());
}

/// Spec, Request multiplexing: overlapping requests are matched by id
/// whatever order their responses arrive in.
#[test]
fn overlapping_requests_are_matched_by_id() {
    let mut link = Link::ready(host_config(&[1]), agent_config(&[1], every_capability()));
    let snapshot_id = link
        .host
        .send_request(Call::Snapshot(SnapshotParams::default()), None, link.t0)
        .unwrap();
    let screenshot_id = link
        .host
        .send_request(Call::Screenshot(ScreenshotParams::default()), None, link.t0)
        .unwrap();
    assert_ne!(snapshot_id, screenshot_id);
    link.pump(link.t0);
    let requests = link.agent_events();
    assert_eq!(requests.len(), 2, "{requests:?}");
    // The screenshot finishes first.
    assert_eq!(
        link.agent.respond(screenshot_id, Ok(screenshot(100))),
        Responded::Sent
    );
    assert_eq!(
        link.agent
            .respond(snapshot_id, Ok(Reply::Snapshot(empty_snapshot()))),
        Responded::Sent
    );
    link.pump(link.t0);
    let events = link.host_events();
    assert_eq!(
        events,
        [
            HostEvent::Response {
                id: screenshot_id,
                method: Method::SCREENSHOT,
                result: Ok(screenshot(100)),
            },
            HostEvent::Response {
                id: snapshot_id,
                method: Method::SNAPSHOT,
                result: Ok(Reply::Snapshot(empty_snapshot())),
            },
        ]
    );
}

#[test]
fn request_ids_are_unique_and_never_zero() {
    let mut link = Link::ready(host_config(&[1]), agent_config(&[1], every_capability()));
    let mut ids = std::collections::HashSet::new();
    for _ in 0..1000 {
        let id = link
            .host
            .send_request(Call::Shutdown, None, link.t0)
            .unwrap();
        assert_ne!(id, 0);
        assert!(ids.insert(id), "id {id} reused while in flight");
    }
}

/// Spec, Structured errors: an unknown method is answered with a
/// non-retryable error.
#[test]
fn the_agent_answers_an_unknown_method_with_a_permanent_error() {
    let mut link = Link::ready(host_config(&[1]), agent_config(&[1], every_capability()));
    let id = link
        .host
        .send_request(Call::Unknown(Method(999)), None, link.t0)
        .unwrap();
    link.pump(link.t0);
    assert!(
        link.agent_events().is_empty(),
        "answered by the session itself"
    );
    let events = link.host_events();
    let [
        HostEvent::Response {
            id: got,
            result: Err(err),
            ..
        },
    ] = events.as_slice()
    else {
        panic!("expected an error response, got {events:?}")
    };
    assert_eq!(*got, id);
    assert_eq!(err.code, code::UNKNOWN_METHOD);
    assert!(!err.retryable);
}

/// Spec, Deadlines and cancellation: the agent answers a request whose
/// deadline passed with a deadline error and serves the next request.
#[test]
fn a_passed_deadline_is_answered_and_the_connection_stays_usable() {
    let mut link = Link::ready(host_config(&[1]), agent_config(&[1], every_capability()));
    let slow = link
        .host
        .send_request(
            Call::Snapshot(SnapshotParams::default()),
            Some(Duration::from_millis(200)),
            link.t0,
        )
        .unwrap();
    link.pump(link.t0);
    let request = only_request(&mut link);
    assert_eq!(request.deadline_ms, Some(200));
    assert_eq!(link.agent.deadline(slow), Some(link.at(200)));
    assert_eq!(link.agent.poll_timeout(), Some(link.at(200)));

    // The tree read is still running when the deadline passes.
    link.agent.handle_timeout(link.at(199));
    assert!(
        link.agent.poll_transmit().is_none(),
        "not before the deadline"
    );
    link.agent.handle_timeout(link.at(200));
    link.pump(link.at(201));
    let events = link.host_events();
    let [
        HostEvent::Response {
            id,
            method,
            result: Err(err),
        },
    ] = events.as_slice()
    else {
        panic!("expected a deadline error, got {events:?}")
    };
    assert_eq!((*id, *method), (slow, Method::SNAPSHOT));
    assert_eq!(err.code, code::DEADLINE_EXCEEDED);
    assert!(err.message.contains("200 ms"), "{}", err.message);

    // The walk finishes late: its answer is dropped, not sent.
    assert_eq!(
        link.agent
            .respond(slow, Ok(Reply::Snapshot(empty_snapshot()))),
        Responded::NotInFlight
    );
    assert!(link.agent.poll_transmit().is_none());

    // The next request is served normally.
    let tap = link
        .host
        .send_request(
            Call::Tap(TapParams {
                x: 1,
                y: 2,
                duration_ms: None,
            }),
            Some(Duration::from_millis(200)),
            link.at(300),
        )
        .unwrap();
    link.pump(link.at(300));
    let request = only_request(&mut link);
    assert_eq!(request.id, tap);
    link.agent
        .respond(tap, Ok(Reply::Input(InputAck { accepted: true })));
    link.pump(link.at(310));
    assert_eq!(
        link.host_events(),
        [HostEvent::Response {
            id: tap,
            method: Method::TAP,
            result: Ok(Reply::Input(InputAck { accepted: true })),
        }]
    );
}

#[test]
fn the_host_reports_a_deadline_itself_when_the_agent_stays_silent() {
    let host = HostConfig {
        deadline_grace: Duration::from_millis(500),
        ..host_config(&[1])
    };
    let mut link = Link::ready(host, agent_config(&[1], every_capability()));
    let id = link
        .host
        .send_request(
            Call::Snapshot(SnapshotParams::default()),
            Some(Duration::from_millis(200)),
            link.t0,
        )
        .unwrap();
    link.pump(link.t0);
    let _ = link.agent_events();
    assert_eq!(link.host.poll_timeout(), Some(link.at(700)));
    link.host.handle_timeout(link.at(699));
    assert!(link.host_events().is_empty());
    link.host.handle_timeout(link.at(700));
    let events = link.host_events();
    assert!(
        matches!(
            events.as_slice(),
            [HostEvent::Response { id: got, result: Err(e), .. }]
                if *got == id && e.code == code::DEADLINE_EXCEEDED
        ),
        "{events:?}"
    );
    // A late answer is dropped; the next request is served normally.
    link.agent
        .respond(id, Ok(Reply::Snapshot(empty_snapshot())));
    link.pump(link.at(800));
    assert!(link.host_events().is_empty());
    let next = link
        .host
        .send_request(
            Call::Snapshot(SnapshotParams::default()),
            None,
            link.at(800),
        )
        .unwrap();
    link.pump(link.at(800));
    let _ = only_request(&mut link);
    link.agent
        .respond(next, Ok(Reply::Snapshot(empty_snapshot())));
    link.pump(link.at(810));
    assert!(matches!(
        link.host_events().as_slice(),
        [HostEvent::Response { id, result: Ok(_), .. }] if *id == next
    ));
}

/// Spec, Framed self-describing messages: a response larger than the
/// negotiated maximum reaches the host as an error naming the limit and the
/// request, with no partial payload.
#[test]
fn an_oversized_response_reaches_the_host_as_an_error_naming_the_limit_and_request() {
    let mut agent = agent_config(&[1], every_capability());
    agent.max_frame = 64 * 1024;
    let mut link = Link::ready(host_config(&[1]), agent);
    let id = link
        .host
        .send_request(Call::Screenshot(ScreenshotParams::default()), None, link.t0)
        .unwrap();
    link.pump(link.t0);
    let _ = only_request(&mut link);
    assert_eq!(
        link.agent.respond(id, Ok(screenshot(100_000))),
        Responded::TooLarge
    );
    link.pump(link.t0);
    let events = link.host_events();
    let [
        HostEvent::Response {
            id: got,
            method,
            result: Err(err),
        },
    ] = events.as_slice()
    else {
        panic!("expected an error response, got {events:?}")
    };
    assert_eq!((*got, *method), (id, Method::SCREENSHOT));
    assert_eq!(err.code, code::FRAME_TOO_LARGE);
    assert!(err.message.contains("65536"), "{}", err.message);
    assert!(
        err.message.contains(&format!("request {id}")),
        "{}",
        err.message
    );
    assert!(err.message.contains("screenshot"), "{}", err.message);
}

#[test]
fn an_oversized_response_frame_is_refused_by_the_host_too() {
    let mut agent = agent_config(&[1], every_capability());
    agent.max_frame = 64 * 1024;
    let mut link = Link::ready(host_config(&[1]), agent);
    let id = link
        .host
        .send_request(Call::Screenshot(ScreenshotParams::default()), None, link.t0)
        .unwrap();
    let after = link
        .host
        .send_request(Call::Shutdown, None, link.t0)
        .unwrap();
    link.pump(link.t0);
    let _ = link.agent_events();
    // An agent that ignores the negotiated limit writes the frame anyway.
    let mut wire = Vec::new();
    let big = Message::Response(Response {
        id,
        method: Method::SCREENSHOT,
        result: Ok(screenshot(100_000)),
    });
    write_frame(&big, u32::MAX, &mut wire).unwrap();
    link.host.receive(&wire, link.t0);
    link.agent.respond(after, Ok(Reply::Empty));
    link.pump(link.t0);
    let events = link.host_events();
    assert_eq!(events.len(), 2, "{events:?}");
    let HostEvent::Response {
        id: got,
        result: Err(err),
        ..
    } = &events[0]
    else {
        panic!("expected an error for the oversized frame, got {events:?}")
    };
    assert_eq!(*got, id);
    assert_eq!(err.code, code::FRAME_TOO_LARGE);
    assert!(err.message.contains("65536"), "{}", err.message);
    assert_eq!(
        events[1],
        HostEvent::Response {
            id: after,
            method: Method::SHUTDOWN,
            result: Ok(Reply::Empty),
        }
    );
}

#[test]
fn an_oversized_request_is_refused_before_it_is_sent() {
    let mut agent = agent_config(&[1], every_capability());
    agent.max_frame = 64 * 1024;
    let mut link = Link::ready(host_config(&[1]), agent);
    let err = link
        .host
        .send_request(
            Call::TextSet(TextParams {
                text: "x".repeat(100_000),
                node: None,
                mode: None,
            }),
            None,
            link.t0,
        )
        .unwrap_err();
    assert_eq!(err.code, code::FRAME_TOO_LARGE);
    assert!(err.message.contains("65536"), "{}", err.message);
    assert!(link.host.poll_transmit().is_none());
}

#[test]
fn the_end_of_the_stream_fails_pending_requests_with_a_reconnect_hint() {
    let mut link = Link::ready(host_config(&[1]), agent_config(&[1], every_capability()));
    let id = link
        .host
        .send_request(Call::Snapshot(SnapshotParams::default()), None, link.t0)
        .unwrap();
    link.host.receive_eof(link.at(10));
    let events = link.host_events();
    assert!(
        matches!(
            events.as_slice(),
            [
                HostEvent::Response { id: got, result: Err(e), .. },
                HostEvent::Closed(DisconnectReason::Eof),
            ] if *got == id && e.code == code::DISCONNECTED
        ),
        "{events:?}"
    );
    let err = link
        .host
        .send_request(Call::Snapshot(SnapshotParams::default()), None, link.at(11))
        .unwrap_err();
    assert_eq!(err.code, code::DISCONNECTED);
    assert!(err.message.contains("reconnect"), "{}", err.message);
    assert!(link.host.poll_timeout().is_none());
}

#[test]
fn agent_session_rejects_requests_without_the_advertised_capability() {
    // A host that ignores the capability set still gets a refusal.
    let t0 = std::time::Instant::now();
    let mut agent = AgentSession::new(agent_config(&[1], Capabilities::TREE), t0);
    let mut host = HostSession::new(host_config(&[1]), t0);
    while let Some(b) = agent.poll_transmit() {
        host.receive(&b, t0);
    }
    while let Some(b) = host.poll_transmit() {
        agent.receive(&b, t0);
    }
    let _ = std::iter::from_fn(|| agent.poll_event()).count();
    let mut wire = Vec::new();
    write_frame(
        &Message::Request(protocol::message::Request {
            id: 5,
            deadline_ms: None,
            call: Call::Tap(TapParams {
                x: 0,
                y: 0,
                duration_ms: None,
            }),
        }),
        DEFAULT_MAX_FRAME,
        &mut wire,
    )
    .unwrap();
    agent.receive(&wire, t0);
    assert!(agent.poll_event().is_none());
    let out = agent.poll_transmit().expect("an error response");
    let Message::Response(resp) = Message::decode(&out[HEADER_LEN..]).unwrap() else {
        panic!("not a response")
    };
    assert_eq!(resp.id, 5);
    assert_eq!(resp.result.unwrap_err().code, code::CAPABILITY_MISSING);
}